Skip to content

Instantly share code, notes, and snippets.

@robskillington
Last active April 25, 2016 13:13
Show Gist options
  • Save robskillington/7dbab427867d9929191d6fa6fa378569 to your computer and use it in GitHub Desktop.
Save robskillington/7dbab427867d9929191d6fa6fa378569 to your computer and use it in GitHub Desktop.
options.go
package main
import (
"fmt"
)
type Options struct {
foo int
bar string
}
func DefaultOptions() *Options {
return &Options{foo: 1, bar: "baz"}
}
func (o *Options) Foo(foo int) *Options {
o.foo = foo
return o
}
func (o *Options) Bar(bar string) *Options {
o.bar = bar
return o
}
func (o *Options) Value() Options {
return *o
}
type Thing struct {
Options
// Can embed or directly set fields as necessary
}
func New(options Options) *Thing {
// Use or embed options...
return &Thing{Options: options}
}
func main() {
t := New(DefaultOptions().Foo(2).Bar("qux").Value())
fmt.Printf("Options %v", t.Options)
}
@robskillington
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment