go-结构体中嵌入接口

【结构体中嵌入接口】

实例

type Context interface {
    Deadline() (deadline time.Time, ok bool)
    Done() <-chan struct{}
    Err() error
    Value(key interface{}) interface{}
}

type cancelCtx struct {
    Context

    mu       sync.Mutex            // protects following fields
    done     chan struct{}         // created lazily, closed by first cancel call
    children map[canceler]struct{} // set to nil by the first cancel call
    err      error                 // set to non-nil by the first cancel call
}
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) 

传入 父ctx ,创建一个 子ctx 并给到 子协程

子ctx 的通用做法:

select {
// 等待 ctx 的退出信号
case <-ctx.Done():
	... ...
} 

此时 子ctx 就是 Context 类型 (interface),而不是 cancelCtx 类型 (struct)
只要任意结构是这个 IF 的实现,都可以当作这个 struct 的元素

func (c *cancelCtx) Done() <-chan struct{} {
	... ...
}

func (c *cancelCtx) Err() error {
	... ...
}

cancelCtx 还实现了 canceler 接口

type canceler interface {
    cancel(removeFromParent bool, err error)
    Done() <-chan struct{}
}

func (c *cancelCtx) cancel(removeFromParent bool, err error) {
	... ...
}

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

结构体没有实现里面嵌入的接口的方法

package main

import (
    "fmt"
)

type infoIF interface {
    getID() int
    getName() string
}

type employee struct {
    infoIF
    name    string
    id      int
}

type persion struct {
    name    string
    addr    string
    id      int
}

func (p persion) getName() string {
    return p.name
}

func (p persion) getAddr() string {
    return p.addr
}

func (p persion) getID() int {
    return p.id
}

func main() {
    persion1 :=  persion {
        name:"wangsan",
        addr:"beidajie",
        id:213,
    }

    employee1 := employee {}
    employee1.infoIF = persion1
    fmt.Println(employee1.getID())      // 213
    fmt.Println(employee1.getName())    // wangsan

    employee2 := employee {
        name:"lisi",
        id:624,
    }
    employee2.infoIF = persion1
    fmt.Println(employee2.getID())      // 213
    fmt.Println(employee2.getName())    // wangsan

    // type employee has no field or method getAddr
    // fmt.Println(employee1.getAddr())    // 异常
}

结构体中套接口,确保接口的赋值对象实现了该接口的所有方法,
且该结构体对象只能使用它的接口中规定的方法

结构体实现里面嵌入的接口的方法

package main

import (
    "fmt"
)

type infoIF interface {
    getID() int
    getName() string
}

type employee struct {
    infoIF
    name    string
    id      int
}

func (e employee) getName() string {
    return e.name
}

func (e employee) getID() int {
    return e.id
}

type persion struct {
    name    string
    addr    string
    id      int
}

func (p persion) getName() string {
    return p.name
}

func (p persion) getAddr() string {
    return p.addr
}

func (p persion) getID() int {
    return p.id
}

func main() {
    persion1 :=  persion {
        name:"wangsan",
        addr:"beidajie",
        id:213,
    }

    employee1 := employee {
        name:"laoliu",
        id:620,   
    }
    employee1.infoIF = persion1
    fmt.Println(employee1.getID())      // 620
    fmt.Println(employee1.getName())    // laoliu

    employee2 := employee {
        name:"lisi",
        id:624,
    }
    employee2.infoIF = persion1
    fmt.Println(employee2.getID())      // 624
    fmt.Println(employee2.getName())    // lisan
}

有点像C++的继承

猜你喜欢

转载自blog.csdn.net/wangkai6666/article/details/121192519