Created
August 19, 2022 06:48
-
-
Save Mo3g4u/db95f9e7991215c0461d9cb23752017c to your computer and use it in GitHub Desktop.
Functional Optionパターンを使ったオプション引数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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