我正在尝试在线了解一些 Go 代码。
var botInterface func(*Server) eintefaces.BotInterface
func SetBotInterface(b func(*Server) einterface.BotInterface){
botInterface = b
}
Run Code Online (Sandbox Code Playgroud)
有谁知道代码在说什么?没有无名函数的实现。据我所知,botInterface 被定义为一个返回接口的函数。目前 botInterface 为零,这是唯一的相关代码。
func(*Server) eintefaces.BotInterface不是一个无名的函数,它是一个函数类型。它是一个函数的类型,它接受*Server它的参数并返回一个 type 的值eintefaces.BotInterface。
botInterface是一个未导出的变量。它的类型是上面描述的函数类型。
该SetBotInterface()导出的函数给出的可能性组(分配)的值到未导出botInterface变量。您必须传递相同类型的函数值,它只是将其分配给未导出的变量(否则您无法访问未导出的变量)。
请参阅类似的用法示例:
var f func(string) io.Writer
func SetF(v func(string) io.Writer) {
f = v
}
func exampleF(s string) io.Writer {
fmt.Println("Received:", s)
return os.Stdout
}
func main() {
SetF(exampleF)
w := f("foo")
w.Write([]byte("bar"))
}
Run Code Online (Sandbox Code Playgroud)
哪些输出(在Go Playground上试试):
Received: foo
bar
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
212 次 |
| 最近记录: |