我试图学习Go,我找到了一个很好的资源
here。
下面给出了方法重载的例子:
package main import "fmt" type Human struct { name string age int phone string } type Employee struct { Human company string } func (h *Human) SayHi() { fmt.Printf("Hi,I am %s you can call me on %s\n",h.name,h.phone) } func (e *Employee) SayHi() { fmt.Printf("Hi,I am %s,I work at %s. Call me on %s\n",e.name,e.company,e.phone) //Yes you can split into 2 lines here. } func main() { sam := Employee{Human{"Sam",45,"111-888-XXXX"},"Golang Inc"} sam.SayHi() }
可以调用“base”结构体(Human)的方法,例如。 sam.Human.SayHi()下转不工作(因为没有类型层次结构?)