go 继承
package main import "fmt" type Animal struct { Color string } // 继承动物结构体 type Dog struct { Name string Animal } // dog方法 类似类方法 func (d *Dog) speak() string { return "I am a dog and my name is "+ d.Name } func (a *Animal) eat() { fmt.Println("我在吃东西 我的颜色是"+a.Color) } func TestStruct() { d := Dog{Name:"killy"} d.Color = "green" st := d.speak() d.eat() fmt.Println(st) } func main() { TestStruct() }
GOROOT=/usr/local/go #gosetup
GOPATH=/www/gopath #gosetup
/usr/local/go/bin/go build -i -o /private/var/folders/fc/4txmmczj6q92p6058h3w7t_80000gn/T/___go_build_main_go__2_ /www/go/learn/main.go #gosetup
/private/var/folders/fc/4txmmczj6q92p6058h3w7t_80000gn/T/___go_build_main_go__2_ #gosetup
我在吃东西 我的颜色是green
I am a dog and my name is killy
Process finished with exit code 0