The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Ritter

请教各位大佬, Go 里面接口实现函数签名必须要一样吗?

  •  
  •   Ritter · Apr 20, 2022 · 1846 views
    This topic created in 1571 days ago, the information mentioned may be changed or developed.
    notify.go
    https://imgur.com/17DVllo
    ewx_notify.go
    https://imgur.com/MgtnFDV

    这样算实现了 Send 这个接口吗?
    但是当我定义这个 SERVICES 时报错了
    https://imgur.com/8Y6Pwnw

    我的想法是定义一个 base 的接口可以接收部分相同的参数,实现的接口可以传递其他额外的参数,不知道这个用 Go 怎么实现,请教各位大佬,谢谢
    4 replies    2022-04-21 10:46:20 +08:00
    Ritter
        1
    Ritter  
    OP
       Apr 20, 2022
    Ritter
        2
    Ritter  
    OP
       Apr 20, 2022
    第三张图 SERVICES 的定义是
    ```go
    package main

    import (
    "message/service"

    "gopkg.in/ini.v1"
    )

    var SERVICES map[string]func(*ini.Section) service.Notify

    func init() {
    SERVICES = map[string]func(*ini.Section) service.Notify{
    "ewx_notify": service.GetEWXNotify,
    }
    }
    ```
    yulon
        3
    yulon  
       Apr 20, 2022
    如果接口 A 方法的返回类型是接口 B ,那么结构 A 方法的返回类型如果实现了接口 B ,结构 A 也算实现了接口 A 。

    你这里返回的都是结构指针,Go 里面用 type 声明的都是新类型而不是别名,自然不算实现,你应该定义一个接口作为返回类型。
    Ritter
        4
    Ritter  
    OP
       Apr 21, 2022
    @yulon 可是我返回的结构体没有什么共同的方法😂 这样只能用 interface{}代替了吗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2873 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:00 · PVG 17:00 · LAX 02:00 · JFK 05:00
    ♥ Do have faith in what you're doing.