Go 如果函数不是隐式的,那么对接口的调用如何访问函数?

Go 如果函数不是隐式的,那么对接口的调用如何访问函数?,go,interface,Go,Interface,下面是一个结构Config,其中包含一个匿名函数ReturnNewAddress,该函数返回一个net.Conn接口返回新地址然后用于返回“addr” type struct Config { ReturnNewAddress func(net.Conn, error) } 下面调用了匿名函数ReturnnewAddress,请注意cfg是Config的一个实例 addr, err := cfg.ReturnNewAddress() 所以我的问题来了-考虑到接口包含许多不同的

下面是一个结构
Config
,其中包含一个匿名函数
ReturnNewAddress
,该函数返回一个
net.Conn
接口<代码>返回新地址然后用于返回“addr”

type struct Config { 

    ReturnNewAddress func(net.Conn, error) 

}
下面调用了匿名函数
ReturnnewAddress
,请注意
cfg
Config
的一个实例

addr, err := cfg.ReturnNewAddress()
所以我的问题来了-考虑到接口包含许多不同的功能,接口
net.Conn
如何知道使用什么功能?让我感到困惑的是,
LocalAddr()
RemoteAddr()
不是隐式调用的。如何知道显式使用这些方法之一。如果我需要在
RemoteAddr
上显式使用
LocalAddr
,该怎么办

下面是net.Conn的go文档:

type Conn interface {
    // Read reads data from the connection.
    // Read can be made to time out and return an Error with Timeout() == true
    // after a fixed time limit; see SetDeadline and SetReadDeadline.
    Read(b []byte) (n int, err error)

    // Write writes data to the connection.
    // Write can be made to time out and return an Error with Timeout() == true
    // after a fixed time limit; see SetDeadline and SetWriteDeadline.
    Write(b []byte) (n int, err error)

    // Close closes the connection.
    // Any blocked Read or Write operations will be unblocked and return errors.
    Close() error

    // LocalAddr returns the local network address.
    LocalAddr() Addr

    // RemoteAddr returns the remote network address.
    RemoteAddr() Addr

    // SetDeadline sets the read and write deadlines associated
    // with the connection. It is equivalent to calling both
    // SetReadDeadline and SetWriteDeadline.
    //
    // A deadline is an absolute time after which I/O operations
    // fail with a timeout (see type Error) instead of
    // blocking. The deadline applies to all future and pending
    // I/O, not just the immediately following call to Read or
    // Write. After a deadline has been exceeded, the connection
    // can be refreshed by setting a deadline in the future.
    //
    // An idle timeout can be implemented by repeatedly extending
    // the deadline after successful Read or Write calls.
    //
    // A zero value for t means I/O operations will not time out.
    SetDeadline(t time.Time) error

    // SetReadDeadline sets the deadline for future Read calls
    // and any currently-blocked Read call.
    // A zero value for t means Read will not time out.
    SetReadDeadline(t time.Time) error

    // SetWriteDeadline sets the deadline for future Write calls
    // and any currently-blocked Write call.
    // Even if write times out, it may return n > 0, indicating that
    // some of the data was successfully written.
    // A zero value for t means Write will not time out.
    SetWriteDeadline(t time.Time) error
}
    Conn is a generic stream-oriented network connection.

这个问题还不完全清楚,但是

配置类型的代码似乎有一个输入错误

   ReturnNewAddress func(net.Conn, error) 
没有与后面的代码兼容的函数类型

addr, err := cfg.ReturnNewAddress()
我认为配置结构类型应该定义为

type struct Config { 
    ReturnNewAddress func()(net.Conn, error) 
}
基于此,调用
cfg.ReturnNewAddress()
返回的变量
addr
的类型为
net.Conn
,因此它是实现接口
net.Conn
的值。因此,您可以按如下方式显式调用所需的函数:

localAddr  :=addr.LocalAddr();
remoteAddr := addr.RemoteAddr();

我不确定我是否理解这个问题-它知道根据调用什么函数和底层类型使用什么函数。您的变量引用可能是接口类型,但是调用只能发生在值的具体类型上。这是函数
ReturnNewAddress
的实现,或者它出现在go src中,因为我可以看到
conn
接口及其函数实现,但看不到包含匿名函数的
ReturnNewAddress
,返回net.Conn接口的ReturnNewAddress
也许您的代码弄混了,但正如所写的,您的
ReturnNewAddress
是一个函数字段,它接受net.Conn和一个错误,但不返回任何内容。@Kaedys这就是为什么我问OP他的实现是否实际工作的原因。