package main import "fmt" type Dog struct { Name string } func (d *Dog) speak() string { return "I am a dog and my name is "+ d.Name } func TestStruct() { d := Dog{Name:"killy"} st := d.speak() 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
i am a dog my name is killy
Process finished with exit code 0
原文:https://www.cnblogs.com/php-linux/p/13057551.html