gpt4 book ai didi

go - 如果不是隐式调用接口(interface),如何访问函数?

转载 作者:数据小太阳 更新时间:2023-10-29 03:39:45 27 4
gpt4 key购买 nike

下面是一个结构Config,它包含一个匿名函数ReturnNewAddress,它返回一个net.Conn接口(interface)。 ReturnNewAddress 然后用于返回“地址”。

type struct Config { 

ReturnNewAddress func(net.Conn, error)

}

在下面调用匿名函数 ReturnnewAddress 的地方,请注意 cfgConfig 的一个实例。

addr, err := cfg.ReturnNewAddress()

所以我的问题来了 - 考虑到接口(interface)拥有许多不同的功能,接口(interface) net.Conn 如何知道要使用什么功能?让我感到困惑的是 LocalAddr()RemoteAddr() 没有被隐式调用。如何明确使用这些方法之一。如果我需要显式使用 LocalAddr 而不是 RemoteAddr 怎么办?

下面是net.Conn的go doc:

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 - 所以它是实现接口(interface) net.Conn 的值。因此,您可以按如下方式显式调用所需的函数:

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

关于go - 如果不是隐式调用接口(interface),如何访问函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49926228/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com