package module
Version: v0.0.1 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2016 License: MIT Imports: 2 Imported by: 5


Build Status

Awesome GoLang



Read me docs


Clean APIs for your Go Applications. Inspired by functional options pattern.

Quick view

package main

import (

type appOptions struct {
	Name        string
	MaxRequests int
	DevMode     bool
	EnableLogs  bool

type app struct {
	options appOptions

func newApp(setters ...options.OptionSetter) *app {
	opts := appOptions{Name: "My App's Default Name"} // any default options here
	options.Struct(&opts, setters...)                 // convert any dynamic options to the appOptions struct, fills non-default options from the setters

	app := &app{options: opts}

	return app

// Name sets the appOptions.Name field
// pass an (optionall) option via static func
func Name(val string) options.OptionSetter {
	return options.Option("Name", val)

// Dev sets the appOptions.DevMode & appOptions.EnableLogs field
// pass an (optionall) option via static func
func Dev(val bool) options.OptionSetter {
	return options.Options{"DevMode": val, "EnableLogs": val}

// and so on...

func passDynamicOptions() {
	myApp := newApp(options.Options{"MaxRequests": 17, "DevMode": true})

	fmt.Printf("passDynamicOptions: %#v\n", myApp.options)

func passDynamicOptionsAlternative() {
	myApp := newApp(options.Option("MaxRequests", 17), options.Option("DevMode", true))

	fmt.Printf("passDynamicOptionsAlternative: %#v\n", myApp.options)

func passFuncsOptions() {
	myApp := newApp(Name("My name"), Dev(true))

	fmt.Printf("passFuncsOptions: %#v\n", myApp.options)

// go run $GOPATH/github.com/kataras/go-options/example/main.go
func main() {


The only requirement is the Go Programming Language, at least v1.7.

$ go get -u github.com/kataras/go-options


If you'd like to discuss this package, or ask questions about it, feel free to


Current: v0.0.1

Read more about Semantic Versioning 2.0.0


The author of go-options is @kataras.


If you are interested in contributing to the go-options project, please make a PR.


This project is licensed under the MIT License.

License can be found here.



Package options implements the Functional Options pattern which provides clean APIs in Go. Options implemented as a function set the state of that option. Inspired by https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md



View Source
const (
	// Version current version number
	Version = "0.0.1"


This section is empty.


func Struct

func Struct(defaultOptions interface{}, opt ...OptionSetter) error

Struct receives default options (pointer to struct) and any option setters fills the static pointer to struct (defaultOptions) returns an error if somethinf bad happen, for example if wrong type kind of value is setted


type OptionSetter

type OptionSetter interface {
	// Set receives an Options, which itself implements the OptionSetter, and sets any key-based pair values

OptionSetter contains the Set function Is the receiver to set any options

func Option

func Option(key string, val interface{}) OptionSetter

Option sets a single option's value, implements the OptionSetter, so it can be passed to the .New,.Default & .Struct also useful when you 'like' to pass .Default(Option("key","value")) instead of .Default(Options{Key:value}), both do the same thing

type Options

type Options map[string]interface{}

Options is just a map[string]interface{} which implements the OptionSetter too this map can be converted to Struct if call 'options.Struct'

func Default

func Default(opt ...OptionSetter) Options

Default accepts option setters and returns the new filled Options, you can pass multi Options as OptionSetter too

func New

func New(defOptions Options, opt ...OptionSetter) (Options, func(interface{}) error)

New receives default options, which can be empty and any option setters returns two values returns the new, filled from the setters, Options and a function which is optionally called by the caller, which receives an interface and returns this interface(pointer to struct) filled by the Setters(same as .Struct)

func (Options) Set

func (temp Options) Set(main Options)

Set implements the OptionSetter makes the Options an OptionSetter also

Source Files


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL