Skip to content

Instantly share code, notes, and snippets.

@kubosuke
Created December 7, 2020 06:25
Show Gist options
  • Save kubosuke/2f9e63946ac43eaf6e4eea358ea1fcb5 to your computer and use it in GitHub Desktop.
Save kubosuke/2f9e63946ac43eaf6e4eea358ea1fcb5 to your computer and use it in GitHub Desktop.
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