gpt4 book ai didi

go - 使用数组符号在 GoLang 中声明接口(interface)

转载 作者:IT王子 更新时间:2023-10-29 01:43:50 26 4
gpt4 key购买 nike

我见过这样定义的接口(interface):

type MyInterface interface{
Method()
}

type MyInterface []interface{
Method()
}

这两个定义有什么区别?

最佳答案

第一个是用 Method() 方法定义一个 interface,第二个是定义 slice界面。该语句使用type literal 形式,可以理解为:

type MyInterface [](interface{
Method()
})

这里的 interface{...} 是一个类型文字。与

的效果相同
type I = interface{
Method()
}
type MyInterface []I

查看有关文字类型的更多信息:https://golang.org/ref/spec#Types

注意:MyInterface 在第二个中会是一个非常糟糕的名字。

关于go - 使用数组符号在 GoLang 中声明接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54839384/

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