func main(){
facestruct := new(FaceStruct)
facestruct.face = "testface"
var face interface{} = facestruct
if a, ok := face.(Face2); ok {
fmt.Printf("animal0 implement Animal interface!\n")
fmt.Println(a)
} else {
fmt.Printf("animal0 not implement Animal interface!\n")
}
}
type Face interface {
Method1() string
}
type Face2 interface {
Face
Method2() string
}
type FaceStruct struct {
face string
}
func (f *FaceStruct) Method1() string {
fmt.Println("Method1")
return "Method1"
}
func (f *FaceStruct) Method2() string {
return "Method2"
}
注意:
var face interface{} = facestruct 只能是interface{}
实例是否实现了某个接口interface
最新推荐文章于 2025-12-05 20:49:06 发布
本文通过一个Go语言示例展示了如何定义和使用接口,并检查一个结构体是否实现了特定的接口。通过具体代码解释了interface{}
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
AutoGPT
AI应用
AutoGPT于2023年3月30日由游戏公司Significant Gravitas Ltd.的创始人Toran Bruce Richards发布,AutoGPT是一个AI agent(智能体),也是开源的应用程序,结合了GPT-4和GPT-3.5技术,给定自然语言的目标,它将尝试通过将其分解成子任务,并在自动循环中使用互联网和其他工具来实现这一目标
1763

被折叠的 条评论
为什么被折叠?



