简单的 go struct 中 field 属性为 interface

package main

import "fmt"

type TestInter interface {
    Get() int
}

type TestStruct struct {
    get TestInter
}

type Ok struct {}

func(o *Ok)Get() int {
    return  1
}

func main() {
    t := TestStruct{}
    o := Ok{}
    t.get = &o
    fmt.Println(t.get.Get())
}

总结:
1、实现了 某一接口的 结构体。 拥有可以调用此接口中所有方法
2、实现接口的 结构体 可以用接口的 类型表示 此结构体的 类型

 

posted @ 2021-06-10 16:16  Black_Climber  阅读(224)  评论(0编辑  收藏  举报