Created
October 29, 2019 05:56
-
-
Save lotusirous/85cb4350cc1acc051fca616114eec64a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 User struct { | |
| Name string | |
| UID int | |
| Menus []string | |
| } | |
| type UserOption func(*User) | |
| func DefaultMenu(m ...string) UserOption { | |
| return func(s *User) { s.Menus = m } | |
| } | |
| func UID(id int) UserOption { | |
| return func(s *User) { s.UID = id } | |
| } | |
| func New(options ...UserOption) *User { | |
| s := &User{Name: "hello"} | |
| for _, option := range options { | |
| option(s) | |
| } | |
| return s | |
| } | |
| func main() { | |
| u := New( | |
| DefaultMenu("a", "b"), | |
| UID(10), | |
| ) | |
| fmt.Println(u) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment