package main
import (
"fmt"
)
type User struct {
name string
age int
}
var USER = User{name: "", age: 0}
type option func(user *User)
func WithName(name string) option {
return func(user *User) {
user.name = name
}
}
func WithAge(age int) option {
return func(user *User) {
user.age = age
}
}
func NewUser(opts ...option) {
user := USER
for _, v := range opts {
v(&user)
}
fmt.Println(user)
}
func main() {
NewUser(WithName("golang"), WithAge(10))
}
GO option选项模式
最新推荐文章于 2024-04-11 15:10:16 发布