Skip to content

kataras/go-options

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Build Status

Awesome GoLang

Report A+

License

Releases

Read me docs

Chat



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

Quick view

package main

import (
	"fmt"
	"github.com/kataras/go-options"
)

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() {
	passDynamicOptions()
	passDynamicOptionsAlternative()
	passFuncsOptions()
}

Installation

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

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

FAQ

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

Versioning

Current: v0.0.1

Read more about Semantic Versioning 2.0.0

People

The author of go-options is @kataras.

Contributing

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

License

This project is licensed under the MIT License.

License can be found here.

About

📦 Clean APIs for your Go Applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages