小编Bog*_*dan的帖子

Function assignments of an interface of a structure pointer with a structure show different values

I'm a bit confused with the next part of the code:

type Tiger struct {
    weight int
}

func (t Tiger) Weight() int {
    return t.weight
}

type AsianTiger struct {
    Tiger
}

type Tigers interface {
    Weight() int
}

func main() {
    t := &AsianTiger{Tiger{3}}

    var i Tigers = t

    f := t.Weight
    g := i.Weight

    t.weight = 7

    fmt.Println(f(), g()) // result is: 3 7 | expected 7 7

    z := t
    x := i

    t.weight = 8 …
Run Code Online (Sandbox Code Playgroud)

methods struct interface go

2
推荐指数
1
解决办法
86
查看次数

标签 统计

go ×1

interface ×1

methods ×1

struct ×1