Created
December 7, 2020 06:25
-
-
Save kubosuke/2f9e63946ac43eaf6e4eea358ea1fcb5 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" | |
"time" | |
) | |
func main() { | |
var ( | |
o []Option | |
id string = "idだよ" | |
email string = "emailだよ" | |
) | |
if id != "" { | |
o = append(o, WithUserId(id)) | |
} | |
if email != "" { | |
o = append(o, WithEmail(email)) | |
} | |
s := NewSearchQuery(o...) | |
fmt.Println(s) | |
} | |
type SearchQuery struct { | |
userId string | |
email string | |
kind []int | |
from time.Time | |
to time.Time | |
} | |
func GenerateSearchQuery() *SearchQuery { | |
return &SearchQuery{} | |
} | |
func NewSearchQuery(options ...Option) *SearchQuery { | |
s := GenerateSearchQuery() | |
for _, f := range options { | |
f(s) | |
} | |
return s | |
} | |
type Option func(s *SearchQuery) | |
func WithUserId(u string) Option { | |
return func(s *SearchQuery){ | |
s.userId = u | |
} | |
} | |
func WithEmail(e string) Option { | |
return func(s *SearchQuery){ | |
s.email = e | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment