package main import "fmt" /** * 構造函數 * 上班類 */ type Work struct { opts options } type options struct { startTime string lunchTime string endTime string } var ( lunTimeDefault = "中午十二點" ) type Option func(o *options) func StartTime(s string) Option { return func(opts *options) { opts.startTime = s } } func EndTime(s string) Option { return func(opts *options) { opts.endTime = s } } func NewWork(opts ...Option) *Work { workOptions := options{} for _, opt := range opts { opt(&workOptions) } work := new(Work) work.opts = workOptions if work.opts.lunchTime == "" { work.opts.lunchTime = lunTimeDefault } return work } func main() { commonOpts := []Option{ StartTime("九點半"), EndTime("二十點"), } work := NewWork(commonOpts...) fmt.Printf("%+v", work.opts) }
參考golang
https://blog.keyboardman.me/2018/01/03/grpc-functional-options-patter/