Last active
April 25, 2016 13:13
-
-
Save robskillington/7dbab427867d9929191d6fa6fa378569 to your computer and use it in GitHub Desktop.
options.go
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" | |
) | |
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://play.golang.org/p/fYHLLvL-jd