package main
import "fmt"
type TestFunc func(*Bar, string)
type Bar struct {
Name string
Alias string
}
func (t *Bar) SetName(name string) {
t.Name = name
fmt.Println("Name:", t.Name)
}
func (t *Bar) SetAlias(alias string) {
t.Alias = alias
fmt.Println("Alias:", t.Alias)
}
type Foo struct {
f TestFunc
}
func main() {
foo := new(Foo)
bar := new(Bar)
foo.f = (*Bar).SetName
foo.f(bar, "LeBron Raymone James")
foo.f = (*Bar).SetAlias
foo.f(bar, "King")
}
go语言 记录奇怪的写法--慢慢品味
本文通过一个Go语言示例介绍了如何定义结构体及其实现的方法,并展示了如何使用结构体的方法来设置属性。


被折叠的 条评论
为什么被折叠?



