dushaoshuai / dushaoshuai.github.io

https://www.shuai.host
0 stars 0 forks source link

Go: type T value calls methods with receiver *T #40

Open dushaoshuai opened 1 year ago

dushaoshuai commented 1 year ago

type T value calls methods with receiver *T

type T struct {
    v int
}

func (t *T) setValue(v int) {
    t.v = v
}

func (t *T) getValue() int {
    return t.v
}

func Example_type_T_value_calls_methods_with_receiver_a_pointer_to_T() {
    var t T
    t.setValue(10)
    fmt.Println(t.getValue())

    // Output:
    // 10
}

按照 https://go.dev/ref/spec#Method_sets 的说法,类型 T 是没有方法的,类型 T 有两个方法 setValue 和 getValue,这里 t 的类型为 T,但却可以调用 receiver 为 T 的两个方法 setValue 和 getValue,应该是编译器做了工作,调用方法时取了 t 的指针。

See also

Method sets Method sets.. Some confusion around type T values calling methods with receivers of type *T