packagemain import( "fmt" ) typeUserstruct{ Idint Namestring } typeManagerstruct{ User Groupstring } func(u*User)Test(){ fmt.Println("UserTest:",u.Id,u.Name) } func(uUser)ToString()string{ returnfmt.Sprintf("UserToString:[%d]%s",u.Name) } func(mManager)Test(){ fmt.Println("ManagerTest:",m.Id,m.Name) } funcmain(){ m:=&Manager{User{1,"Tom"},"IT"} m.Test() fmt.Println(m.ToString()) }
输出结果:
ManagerTest:1Tom UserToString:[1]Tom