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{}
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Qwen-Image-Edit-2509
图片编辑
Qwen
Qwen-Image-Edit-2509 是阿里巴巴通义千问团队于2025年9月发布的最新图像编辑AI模型,主要支持多图编辑,包括“人物+人物”、“人物+商品”等组合玩法
1763

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



