Skip to content

Instantly share code, notes, and snippets.

@Mo3g4u
Created August 19, 2022 06:48
Show Gist options
  • Save Mo3g4u/db95f9e7991215c0461d9cb23752017c to your computer and use it in GitHub Desktop.
Save Mo3g4u/db95f9e7991215c0461d9cb23752017c to your computer and use it in GitHub Desktop.
Functional Optionパターンを使ったオプション引数
package main
import "fmt"
// Functional Optionパターンを使ったオプション引数
type Portion int
const (
Regular Portion = iota
Small
Large
)
type Udon struct {
men Portion
aburaage bool
ebiten uint
}
type OptFunc func(r *Udon)
func NewUdon(opts ...OptFunc) *Udon {
r := &Udon{}
for _, opt := range opts {
opt(r)
}
return r
}
func OptMen(p Portion) OptFunc {
return func(r *Udon) { r.men = p }
}
func OptAburaage() OptFunc {
return func(r *Udon) { r.aburaage = true }
}
func OptEbiten(n uint) OptFunc {
return func(r *Udon) { r.ebiten = n }
}
func main() {
tokuseiUdon := NewUdon(OptAburaage(), OptEbiten(3))
fmt.Printf("%#v\n", tokuseiUdon)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment