其他分享
首页 > 其他分享> > Go 语言Interface

Go 语言Interface

作者:互联网

go interface

类型1:eFace = {类型,地址}

类型2:iFace={{t *iTab},地址}

// ==============

type Animal interface {

    Speak() string

}

// ==============

type Dog struct {

}

func (d Dog) Speak() string {

    return "Woof!"

}

// ==============

type Cat struct {

}

func (c Cat) Speak() string {

    return "Meow!"

}

// ==============

type JavaProgrammer struct {

}

func (j JavaProgrammer) Speak() string {

    return "Design patterns!"

}

// +++++++++++++++++++++++++++++++++

func main() {

    animals := []Animal{Dog{}, Cat{}, Llama{}, JavaProgrammer{}}

    for _, animal := range animals {

        fmt.Println(animal.Speak())

    }

}

标签:return,语言,struct,Interface,func,Go,Speak,type,string
来源: https://blog.csdn.net/gumingyaotangwei/article/details/121911946