import "gopkg.in/option.v0"
// Define an opaque option type like this
type CreationOption func(*creationOptions)
// Define the concrete option properties as a struct type
type creationOptions struct {
conn *net.Conn
limit int
}
// Define functions that generate closures that sets the
// option properties on the struct type
func CreateWithConn(conn *net.Conn) CreationOption {
return func(o *creationOptions) {
o.conn = conn
}
}
func CreateWithLimit(limit int) CreationOption {
return func(o *creationOptions) {
o.limit = limit
}
}
// Now to use it in actual API functions, add it as the last
// variadic parameter.
func Create(options ...CreationOption) *Client {
// Use the helper function from this module
opts := option.New(options)
// Use the returned options struct value with all user
// provided values
return &Client{opts.conn}
}
// And you can use defaults.
func CreateWithDefaults(options ...CreationOption) *Client {
defaultLimit := CreateWithLimit(10)
opts := option.New(options, defaultLimit)
return &Client{opts.conn, opts.limit}
}
Golang function options helper
License
go-option/option
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
Golang function options helper
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published