Created
March 19, 2021 08:51
-
-
Save podanypepa/c9c7531a818ce197830f690070e6dd3f 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
// Functional Options Pattern | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
ct := NewCompany( | |
WitICO("11"), | |
WithName("pepa"), | |
) | |
fmt.Println(ct) | |
} | |
type Company struct { | |
ID string | |
Name string | |
ICO string | |
} | |
func NewCompany(opt ...Options) *Company { | |
nc := &Company{} | |
for _, o := range opt { | |
o(nc) | |
} | |
return nc | |
} | |
type Options func(*Company) | |
func WithID(id string) Options { | |
return func(c *Company) { | |
c.ID = id | |
} | |
} | |
func WithName(name string) Options { | |
return func(c *Company) { | |
c.Name = name | |
} | |
} | |
func WitICO(ico string) Options { | |
return func(c *Company) { | |
c.ICO = ico | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment