config

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 9 Imported by: 4 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package config provides typed, lazily evaluated accessors for configuration values backed by github.com/spf13/viper.

A global github.com/spf13/viper.Viper instance, returned by Config, reads environment variables automatically (with "." in keys replaced by "_") and treats config sources as YAML. Accessors take the *viper.Viper to read from; Get, MustGet and GetStruct accept nil to select the global instance.

Getters

Get* functions register a key with a default and return a getter that reads the current value on each call. MustGet* functions register a required key and panic if it is not set:

addr := config.Get(nil, "service.addr", ":8080")
token := config.MustGet[string](nil, "service.token")
fmt.Println(addr(), token())

Registered keys, defaults and types can be inspected with RequiredKeys, Defaults, Types and TypeOf.

Watching

Watch and WatchChan (and their typed variants) poll a getter every second and report changes, which is useful together with remote providers added through WithRemoteConfig.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Config

func Config() *viper.Viper

Config returns the global viper.Viper instance.

func Defaults added in v0.17.0

func Defaults() map[string]any

Defaults returns the registered default values by key.

func Get added in v0.24.0

func Get[T Supported](c *viper.Viper, key string, fallback T) func() T

Get registers key with fallback as its default on c and returns a getter that reads the current value on each call. A nil c selects the global instance.

func GetBool

func GetBool(c *viper.Viper, key string, fallback bool) func() bool

GetBool registers key with fallback as its default on c and returns a getter that reads the current bool value on each call.

func GetDuration added in v0.2.7

func GetDuration(c *viper.Viper, key string, fallback time.Duration) func() time.Duration

GetDuration registers key with fallback as its default on c and returns a getter that reads the current time.Duration value on each call.

func GetFloat64 added in v0.2.7

func GetFloat64(c *viper.Viper, key string, fallback float64) func() float64

GetFloat64 registers key with fallback as its default on c and returns a getter that reads the current float64 value on each call.

func GetInt

func GetInt(c *viper.Viper, key string, fallback int) func() int

GetInt registers key with fallback as its default on c and returns a getter that reads the current int value on each call.

func GetInt32 added in v0.2.7

func GetInt32(c *viper.Viper, key string, fallback int32) func() int32

GetInt32 registers key with fallback as its default on c and returns a getter that reads the current int32 value on each call.

func GetInt64 added in v0.2.7

func GetInt64(c *viper.Viper, key string, fallback int64) func() int64

GetInt64 registers key with fallback as its default on c and returns a getter that reads the current int64 value on each call.

func GetIntSlice added in v0.2.7

func GetIntSlice(c *viper.Viper, key string, fallback []int) func() []int

GetIntSlice registers key with fallback as its default on c and returns a getter that reads the current []int value on each call.

func GetString

func GetString(c *viper.Viper, key, fallback string) func() string

GetString registers key with fallback as its default on c and returns a getter that reads the current string value on each call.

func GetStringMap added in v0.2.7

func GetStringMap(c *viper.Viper, key string, fallback map[string]any) func() map[string]any

GetStringMap registers key with fallback as its default on c and returns a getter that reads the current map[string]any value on each call.

func GetStringMapString added in v0.2.7

func GetStringMapString(c *viper.Viper, key string, fallback map[string]string) func() map[string]string

GetStringMapString registers key with fallback as its default on c and returns a getter that reads the current map[string]string value on each call.

func GetStringMapStringSlice added in v0.2.7

func GetStringMapStringSlice(c *viper.Viper, key string, fallback map[string][]string) func() map[string][]string

GetStringMapStringSlice registers key with fallback as its default on c and returns a getter that reads the current map[string][]string value on each call.

func GetStringSlice

func GetStringSlice(c *viper.Viper, key string, fallback []string) func() []string

GetStringSlice registers key with fallback as its default on c and returns a getter that reads the current []string value on each call.

func GetStruct added in v0.2.7

func GetStruct(c *viper.Viper, key string, fallback any) (func(v any) error, error)

GetStruct decodes fallback (using "yaml" struct tags) into defaults below key, merges them into c and returns a function that decodes the current value at key into v. A nil c selects the global instance. It returns an error if fallback cannot be decoded or merged.

func GetTime added in v0.2.7

func GetTime(c *viper.Viper, key string, fallback time.Time) func() time.Time

GetTime registers key with fallback as its default on c and returns a getter that reads the current time.Time value on each call.

func GetUint added in v0.2.7

func GetUint(c *viper.Viper, key string, fallback uint) func() uint

GetUint registers key with fallback as its default on c and returns a getter that reads the current uint value on each call.

func GetUint32 added in v0.2.7

func GetUint32(c *viper.Viper, key string, fallback uint32) func() uint32

GetUint32 registers key with fallback as its default on c and returns a getter that reads the current uint32 value on each call.

func GetUint64 added in v0.2.7

func GetUint64(c *viper.Viper, key string, fallback uint64) func() uint64

GetUint64 registers key with fallback as its default on c and returns a getter that reads the current uint64 value on each call.

func MustGet added in v0.24.0

func MustGet[T Supported](c *viper.Viper, key string) func() T

MustGet registers key as required on c and returns a getter that reads the current value on each call. A nil c selects the global instance. It panics if key is not set.

func MustGetBool

func MustGetBool(c *viper.Viper, key string) func() bool

MustGetBool registers key as required on c and returns a getter that reads the current bool value on each call. It panics if key is not set.

func MustGetDuration added in v0.2.7

func MustGetDuration(c *viper.Viper, key string) func() time.Duration

MustGetDuration registers key as required on c and returns a getter that reads the current time.Duration value on each call. It panics if key is not set.

func MustGetFloat64 added in v0.2.7

func MustGetFloat64(c *viper.Viper, key string) func() float64

MustGetFloat64 registers key as required on c and returns a getter that reads the current float64 value on each call. It panics if key is not set.

func MustGetInt

func MustGetInt(c *viper.Viper, key string) func() int

MustGetInt registers key as required on c and returns a getter that reads the current int value on each call. It panics if key is not set.

func MustGetInt32 added in v0.2.7

func MustGetInt32(c *viper.Viper, key string) func() int32

MustGetInt32 registers key as required on c and returns a getter that reads the current int32 value on each call. It panics if key is not set.

func MustGetInt64 added in v0.2.7

func MustGetInt64(c *viper.Viper, key string) func() int64

MustGetInt64 registers key as required on c and returns a getter that reads the current int64 value on each call. It panics if key is not set.

func MustGetIntSlice added in v0.2.7

func MustGetIntSlice(c *viper.Viper, key string) func() []int

MustGetIntSlice registers key as required on c and returns a getter that reads the current []int value on each call. It panics if key is not set.

func MustGetString

func MustGetString(c *viper.Viper, key string) func() string

MustGetString registers key as required on c and returns a getter that reads the current string value on each call. It panics if key is not set.

func MustGetStringMap added in v0.2.7

func MustGetStringMap(c *viper.Viper, key string) func() map[string]any

MustGetStringMap registers key as required on c and returns a getter that reads the current map[string]any value on each call. It panics if key is not set.

func MustGetStringMapString added in v0.2.7

func MustGetStringMapString(c *viper.Viper, key string) func() map[string]string

MustGetStringMapString registers key as required on c and returns a getter that reads the current map[string]string value on each call. It panics if key is not set.

func MustGetStringMapStringSlice added in v0.2.7

func MustGetStringMapStringSlice(c *viper.Viper, key string) func() map[string][]string

MustGetStringMapStringSlice registers key as required on c and returns a getter that reads the current map[string][]string value on each call. It panics if key is not set.

func MustGetStringSlice added in v0.2.7

func MustGetStringSlice(c *viper.Viper, key string) func() []string

MustGetStringSlice registers key as required on c and returns a getter that reads the current []string value on each call. It panics if key is not set.

func MustGetTime added in v0.2.7

func MustGetTime(c *viper.Viper, key string) func() time.Time

MustGetTime registers key as required on c and returns a getter that reads the current time.Time value on each call. It panics if key is not set.

func MustGetUint added in v0.2.7

func MustGetUint(c *viper.Viper, key string) func() uint

MustGetUint registers key as required on c and returns a getter that reads the current uint value on each call. It panics if key is not set.

func MustGetUint32 added in v0.2.7

func MustGetUint32(c *viper.Viper, key string) func() uint32

MustGetUint32 registers key as required on c and returns a getter that reads the current uint32 value on each call. It panics if key is not set.

func MustGetUint64 added in v0.2.7

func MustGetUint64(c *viper.Viper, key string) func() uint64

MustGetUint64 registers key as required on c and returns a getter that reads the current uint64 value on each call. It panics if key is not set.

func Readme deprecated added in v0.17.0

func Readme() string

Readme returns a markdown table of all registered config keys and remote providers.

Deprecated: Readme will be removed in a future release.

func RequiredKeys added in v0.17.0

func RequiredKeys() []string

RequiredKeys returns all keys registered as required.

func TypeOf added in v0.17.0

func TypeOf(key string) string

TypeOf returns the registered type name of key, or "" if key is unknown.

func Types added in v0.17.0

func Types() map[string]string

Types returns the registered type names by key.

func Watch added in v0.24.0

func Watch[T comparable](ctx context.Context, fn func() T, callback func(T))

Watch starts a goroutine that polls fn every second until ctx is done and calls callback from that goroutine whenever the value changes. It returns immediately.

func WatchBool added in v0.10.12

func WatchBool(ctx context.Context, fn func() bool, callback func(bool))

WatchBool is Watch for bool values.

func WatchBoolChan added in v0.10.13

func WatchBoolChan(ctx context.Context, fn func() bool, ch chan bool)

WatchBoolChan is WatchChan for bool values.

func WatchChan added in v0.24.0

func WatchChan[T comparable](ctx context.Context, fn func() T, ch chan T)

WatchChan starts a goroutine that polls fn every second until ctx is done and sends the value on ch whenever it changes. Sends block, so ch must be drained. It returns immediately.

func WatchDuration added in v0.10.13

func WatchDuration(ctx context.Context, fn func() time.Duration, callback func(time.Duration))

WatchDuration is Watch for time.Duration values.

func WatchDurationChan added in v0.10.13

func WatchDurationChan(ctx context.Context, fn func() time.Duration, ch chan time.Duration)

WatchDurationChan is WatchChan for time.Duration values.

func WatchFloat64 added in v0.10.13

func WatchFloat64(ctx context.Context, fn func() float64, callback func(float64))

WatchFloat64 is Watch for float64 values.

func WatchFloat64Chan added in v0.10.13

func WatchFloat64Chan(ctx context.Context, fn func() float64, ch chan float64)

WatchFloat64Chan is WatchChan for float64 values.

func WatchInt added in v0.10.13

func WatchInt(ctx context.Context, fn func() int, callback func(int))

WatchInt is Watch for int values.

func WatchInt32 added in v0.10.13

func WatchInt32(ctx context.Context, fn func() int32, callback func(int32))

WatchInt32 is Watch for int32 values.

func WatchInt32Chan added in v0.10.13

func WatchInt32Chan(ctx context.Context, fn func() int32, ch chan int32)

WatchInt32Chan is WatchChan for int32 values.

func WatchInt64 added in v0.10.13

func WatchInt64(ctx context.Context, fn func() int64, callback func(int64))

WatchInt64 is Watch for int64 values.

func WatchInt64Chan added in v0.10.13

func WatchInt64Chan(ctx context.Context, fn func() int64, ch chan int64)

WatchInt64Chan is WatchChan for int64 values.

func WatchIntChan added in v0.10.13

func WatchIntChan(ctx context.Context, fn func() int, ch chan int)

WatchIntChan is WatchChan for int values.

func WatchString added in v0.10.12

func WatchString(ctx context.Context, fn func() string, callback func(string))

WatchString is Watch for string values.

func WatchStringChan added in v0.10.13

func WatchStringChan(ctx context.Context, fn func() string, ch chan string)

WatchStringChan is WatchChan for string values.

func WatchTime added in v0.10.13

func WatchTime(ctx context.Context, fn func() time.Time, callback func(time.Time))

WatchTime is Watch for time.Time values.

func WatchTimeChan added in v0.10.13

func WatchTimeChan(ctx context.Context, fn func() time.Time, ch chan time.Time)

WatchTimeChan is WatchChan for time.Time values.

func WithRemoteConfig added in v0.10.0

func WithRemoteConfig(c *viper.Viper, provider, endpoint string, path string) error

WithRemoteConfig adds a remote provider (e.g. "etcd3", "consul") at endpoint and path to c, reads it and starts watching it for changes. It returns an error if any of these steps fail.

Types

type Supported added in v0.24.0

type Supported interface {
	bool | int | int32 | int64 | uint | uint32 | uint64 |
		float64 | string | time.Time | time.Duration |
		[]int | []string |
		map[string]any | map[string]string | map[string][]string
}

Supported is a type constraint for all config value types supported by viper.

Jump to

Keyboard shortcuts

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