Golang function options helper

import "gopkg.in/option.v0"

Go Import Go Reference

Example

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}
}

GitHub

View Github