装饰器模式
type DUser struct {
Id int
Name string
}
func GetInfo(id int) *DUser { //1
return &DUser{Id: id, Name: "thaiq"}
}
type UserInfoFunc func(id int) *DUser //和一一样
func GetInfoWithRole(fn UserInfoFunc) UserInfoFunc {
return func(id int) *DUser {
u := fn(id)
u.Name = "guest" + u.Name
return u
}
}
func main() {
fmt.Println(GetInfoWithRole(GetInfo)(12).Name)
}最后更新于