Created
August 19, 2022 05:14
-
-
Save Mo3g4u/7ba18892271824a8d735f11c73cda911 to your computer and use it in GitHub Desktop.
ビルダーを利用したオプション引数
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" | |
type Portion int | |
const ( | |
Regular Portion = iota | |
Small | |
Large | |
) | |
type Udon struct { | |
men Portion | |
aburaage bool | |
ebiten uint | |
} | |
type fluentOpt struct { | |
men Portion | |
aburaage bool | |
ebiten uint | |
} | |
func (o *fluentOpt) Aburaage() *fluentOpt { | |
o.aburaage = true | |
return o | |
} | |
func (o *fluentOpt) Ebiten(n uint) *fluentOpt { | |
o.ebiten = n | |
return o | |
} | |
func (o *fluentOpt) Order() *Udon { | |
return &Udon{ | |
men: o.men, | |
aburaage: o.aburaage, | |
ebiten: o.ebiten, | |
} | |
} | |
func NewUdon(p Portion) *fluentOpt { | |
return &fluentOpt{ | |
men: p, | |
aburaage: false, | |
ebiten: 1, | |
} | |
} | |
func main() { | |
oomoriKItsune := NewUdon(Large).Aburaage().Order() | |
fmt.Printf("%#v\n", oomoriKItsune) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment