Skip to content

Instantly share code, notes, and snippets.

@ericelsken
Created October 6, 2019 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericelsken/bda914c06cfda9e90b0af8bea0a6e84a to your computer and use it in GitHub Desktop.
Save ericelsken/bda914c06cfda9e90b0af8bea0a6e84a to your computer and use it in GitHub Desktop.
Function Option TL;DR
package entity
type Entity struct {
fieldOne string
}
type Option func(e *Entity)
func FieldOne(fieldOne string) Option {
return func(e *Entity) {
e.fieldOne = fieldOne
}
}
func (e *Entity) WithOptions(options ...Option) {
for _, option := range options {
option(e)
}
}
//Usage:
//
//e := &Entity{}
//e.WithOptions(
// FieldOne("field one's value"),
//)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment