Hi,

Both t2.f() and t2.T1.f() call the same method.

t2.f() calls t2.T1.f() (struct embedding). Inside T1.f(), value of a is 3.

t2.T1.f() calls directly.

Yes, this is reasonable.

If you want absolutely clear to thing to happen, then try this

package main

import "fmt"

func main() {
    var t2 T2
    t2.a = 5
    t2.T1.a = 3
    t2.f()
    t2.T1.f()
}

type T1 struct {
    a int
}

func (t T1) f() {
    fmt.Println("T1: ", t.a)
}

type T2 struct {
    T1
    a int
}

func (t T2) f() {
    fmt.Println("T2: ", t.a)
}


On 9/7/2016 16:56, T L wrote:

package main

import "fmt"

func main() {
    var t2 T2
    t2.a = 5
    t2.T1.a = 3
    t2.f() // 3
    t2.T1.f() // 3
}

type T1 struct {
    a int
}

func (t T1) f() {
    fmt.Println(t.a)
}

type T2 struct {
    T1
    a int
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com <mailto:golang-nuts+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

--
Warm Regards,
Asit Dhal
http://asit-dhal.github.io/

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to