mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-26 21:56:02 +03:00
66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package option
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
//Options is key struct,关键数据结构,聚合所有外部可传入的参数
|
|
type Options struct {
|
|
UID int
|
|
GID int
|
|
Flags int
|
|
Company string
|
|
Gender bool //is male
|
|
}
|
|
|
|
//Option func is key func
|
|
type Option func(*Options)
|
|
|
|
//UID set User ID
|
|
func UID(userID int) Option {
|
|
return func(args *Options) {
|
|
args.UID = userID
|
|
}
|
|
}
|
|
|
|
//GID set User Group ID
|
|
func GID(groupID int) Option {
|
|
return func(args *Options) {
|
|
args.GID = groupID
|
|
}
|
|
}
|
|
|
|
//Company set Company Name
|
|
func Company(cname string) Option {
|
|
return func(args *Options) {
|
|
args.Company = cname
|
|
}
|
|
}
|
|
|
|
//Gender for male or female
|
|
func Gender(gender bool) Option {
|
|
return func(args *Options) {
|
|
args.Gender = gender
|
|
}
|
|
}
|
|
|
|
//Introduce someone
|
|
func Introduce(name string, setters ...Option /*传入闭包设置函数*/) {
|
|
// Default Options
|
|
args := &Options{
|
|
UID: 0,
|
|
GID: 0,
|
|
Company: "",
|
|
Gender: true,
|
|
}
|
|
for _, setter := range setters {
|
|
setter(args)
|
|
}
|
|
gender := "famale"
|
|
if args.Gender {
|
|
gender = "male"
|
|
}
|
|
fmt.Println("----------------------")
|
|
fmt.Println("im am: ", name, "\nfrom: ", args.Company, "\ngender: ", gender, "\nUID: ", args.UID)
|
|
}
|