config

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2018 License: MIT Imports: 8 Imported by: 0

README

go-config

Build Status Report Card GoCover

Utility package to read the configuration.

Installation

go get -u github.com/Alma-media/go-config

How to use?

  1. Import package github.com/Alma-media/go-config
  2. Create a config struct
  3. Set default values (if needed) for struct fields
  4. Pass the pointer to a struct to Init() func
  5. Check error and enjoy

Supported data types

  • bool
  • int, []int
  • uint, []uint
  • int64, []int64
  • uint64, []uint64
  • float64, []float64
  • time.Duration, []time.Duration
  • string, []string

Priorities

  1. flags - hi
  2. env vars - mid
  3. defaults - low

Examples

package main

import (
	"fmt"

	"github.com/Alma-media/go-config"
)

type Config struct {
	Boolean bool `default:"true"`
	Nested  struct {
		Integer int     `default:"42"`
		Float64 float64 `default:"3.14"`
		String  string  `default:"text"`
	}
}

func main() {
	// create an instance
	conf := &Config{}
	// pass to Init() func and check the error
	if err := config.Init(conf, "MYAPP"); err != nil {
		fmt.Println(err)
	}
	// use your config
	fmt.Println(conf)
}

Documentation

Overview

Package config provides flexible access to config variables by priority: flags - HI, environment variables - MID, default values defined with a struct field tags - LOW

Index

Examples

Constants

This section is empty.

Variables

View Source
var EnvPrefix string

EnvPrefix is a prefix in the beginning of environment variable name (used to easily differentiate variables of your application).

Functions

func Init

func Init(c interface{}, prefix string) error

Init config values.

Example
package main

import (
	"fmt"

	"github.com/Alma-media/go-config"
)

// TestConfig struct
type TestConfig struct {
	String   string  `default:"strvalue"`
	Int      int     `default:"42"`
	Bool     bool    `default:"false"`
	ArrInt64 []int64 `default:"-1,2,-3,4,-5"`
	Nested   Nested
}

// Nested config struct
type Nested struct {
	String string `default:"nestedstrvalue"`
}

func main() {
	var conf TestConfig
	fmt.Println(config.Init(&conf, "myapp"), conf)
}
Output:

<nil> {strvalue 42 false [-1 2 -3 4 -5] {nestedstrvalue}}

Types

type FlagSet

type FlagSet struct {
	*flag.FlagSet
}

FlagSet represents extended flag.FlagSet.

func NewFlagSet

func NewFlagSet(name string, errorHandling flag.ErrorHandling) *FlagSet

NewFlagSet returns a new, empty flag set with the specified name and error handling property.

func (*FlagSet) ArrayDurationVar

func (f *FlagSet) ArrayDurationVar(p *[]time.Duration, name string, value []time.Duration, usage string)

ArrayDurationVar defines an []time.Duration flag with specified name, default value, and usage string. The argument p points to an []time.Duration variable in which to store the value of the flag.

func (*FlagSet) ArrayFloat64Var

func (f *FlagSet) ArrayFloat64Var(p *[]float64, name string, value []float64, usage string)

ArrayFloat64Var defines an []float64 flag with specified name, default value, and usage string. The argument p points to an []float64 variable in which to store the value of the flag.

func (*FlagSet) ArrayInt64Var

func (f *FlagSet) ArrayInt64Var(p *[]int64, name string, value []int64, usage string)

ArrayInt64Var defines an []int64 flag with specified name, default value, and usage string. The argument p points to an []int64 variable in which to store the value of the flag.

func (*FlagSet) ArrayIntVar

func (f *FlagSet) ArrayIntVar(p *[]int, name string, value []int, usage string)

ArrayIntVar defines an []int flag with specified name, default value, and usage string. The argument p points to an []int variable in which to store the value of the flag.

func (*FlagSet) ArrayStringVar

func (f *FlagSet) ArrayStringVar(p *[]string, name string, value []string, usage string)

ArrayStringVar defines an []string flag with specified name, default value, and usage string. The argument p points to an []string variable in which to store the value of the flag.

func (*FlagSet) ArrayUint64Var

func (f *FlagSet) ArrayUint64Var(p *[]uint64, name string, value []uint64, usage string)

ArrayUint64Var defines an []uint64 flag with specified name, default value, and usage string. The argument p points to an []uint64 variable in which to store the value of the flag.

func (*FlagSet) ArrayUintVar

func (f *FlagSet) ArrayUintVar(p *[]uint, name string, value []uint, usage string)

ArrayUintVar defines an []uint flag with specified name, default value, and usage string. The argument p points to an []uint variable in which to store the value of the flag.

Jump to

Keyboard shortcuts

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