0
点赞
收藏
分享

微信扫一扫

实例是否实现了某个接口interface

Sophia的玲珑阁 2022-01-25 阅读 74
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{}
举报

相关推荐

0 条评论