This document covers setting and retrieving configuration values, including defaults, overrides, and type-safe getters.
func SetDefault(key string, value any)
func (*Viper) SetDefault(key string, value any)Sets the default value for a key. Default is only used when no value is provided by the user via flag, config file, environment variable, or key/value store. SetDefault is case-insensitive for a key.
Example:
viper.SetDefault("port", 8080)
viper.SetDefault("host", "localhost")
viper.SetDefault("database", map[string]any{
"host": "localhost",
"port": 5432,
})func Set(key string, value any)
func (*Viper) Set(key string, value any)Sets the value for the key in the override register. Set is case-insensitive for a key. Will be used instead of values obtained via flags, config file, ENV, default, or key/value store.
Example:
viper.Set("app_name", "myapp")
viper.Set("version", "1.0.0")
viper.Set("database.host", "db.example.com")func Get(key string) any
func (*Viper) Get(key string) anyRetrieves any value given the key to use. Get is case-insensitive for a key. Get checks in the following order: override, flag, env, config file, key/value store, default. Returns an interface{} value.
func GetString(key string) string
func (*Viper) GetString(key string) stringReturns the value associated with the key as a string.
func GetBool(key string) bool
func (*Viper) GetBool(key string) boolReturns the value associated with the key as a boolean.
func GetInt(key string) int
func (*Viper) GetInt(key string) intReturns the value associated with the key as an integer.
func GetInt32(key string) int32
func (*Viper) GetInt32(key string) int32Returns the value associated with the key as int32.
func GetInt64(key string) int64
func (*Viper) GetInt64(key string) int64Returns the value associated with the key as int64.
func GetUint(key string) uint
func (*Viper) GetUint(key string) uintReturns the value associated with the key as an unsigned integer.
func GetUint8(key string) uint8
func (*Viper) GetUint8(key string) uint8Returns the value associated with the key as uint8.
func GetUint16(key string) uint16
func (*Viper) GetUint16(key string) uint16Returns the value associated with the key as uint16.
func GetUint32(key string) uint32
func (*Viper) GetUint32(key string) uint32Returns the value associated with the key as uint32.
func GetUint64(key string) uint64
func (*Viper) GetUint64(key string) uint64Returns the value associated with the key as uint64.
func GetFloat64(key string) float64
func (*Viper) GetFloat64(key string) float64Returns the value associated with the key as a float64.
func GetDuration(key string) time.Duration
func (*Viper) GetDuration(key string) time.DurationReturns the value associated with the key as a time.Duration.
func GetTime(key string) time.Time
func (*Viper) GetTime(key string) time.TimeReturns the value associated with the key as time.Time.
func GetIntSlice(key string) []int
func (*Viper) GetIntSlice(key string) []intReturns the value associated with the key as a slice of int values.
func GetStringSlice(key string) []string
func (*Viper) GetStringSlice(key string) []stringReturns the value associated with the key as a slice of strings.
func GetStringMap(key string) map[string]any
func (*Viper) GetStringMap(key string) map[string]anyReturns the value associated with the key as a map of interfaces.
func GetStringMapString(key string) map[string]string
func (*Viper) GetStringMapString(key string) map[string]stringReturns the value associated with the key as a map of strings.
func GetStringMapStringSlice(key string) map[string][]string
func (*Viper) GetStringMapStringSlice(key string) map[string][]stringReturns the value associated with the key as a map to a slice of strings.
func GetSizeInBytes(key string) uint
func (*Viper) GetSizeInBytes(key string) uintReturns the size of the value associated with the given key in bytes.
func AllKeys() []string
func (*Viper) AllKeys() []stringReturns all keys holding a value, regardless of where they are set. Nested keys are returned with a key delimiter separator (default ".").
func AllSettings() map[string]any
func (*Viper) AllSettings() map[string]anyMerges all settings and returns them as a map[string]any.
Example:
// Get all keys
keys := viper.AllKeys()
fmt.Println("All configuration keys:", keys)
// Get all settings as a map
settings := viper.AllSettings()
for key, value := range settings {
fmt.Printf("%s: %v\n", key, value)
}func IsSet(key string) bool
func (*Viper) IsSet(key string) boolChecks to see if the key has been set in any of the data locations (override, flag, env, config file, key/value store, or default). IsSet is case-insensitive for a key.
func InConfig(key string) bool
func (*Viper) InConfig(key string) boolChecks to see if the given key (or an alias) is in the config file.
Example:
if viper.IsSet("database.host") {
host := viper.GetString("database.host")
fmt.Println("Database host:", host)
}
if viper.InConfig("secret_key") {
// Key is explicitly in config file
}func RegisterAlias(alias, key string)
func (*Viper) RegisterAlias(alias, key string)Creates an alias that provides another accessor for the same key. This enables one to change a name without breaking the application.
Example:
viper.RegisterAlias("verbose", "log_level")
viper.Set("log_level", "debug")
// Both return the same value
level1 := viper.GetString("log_level") // "debug"
level2 := viper.GetString("verbose") // "debug"func SetTypeByDefaultValue(enable bool)
func (*Viper) SetTypeByDefaultValue(enable bool)Enables or disables the inference of a key value's type when the Get function is used based upon a key's default value as opposed to the value returned based on the normal fetch logic.
When enabled, if a key has a default value of []string{} and the same key is set via an environment variable to "a b c", a call to Get() would return []string{"a", "b", "c"} instead of the string "a b c".
Example:
viper.SetTypeByDefaultValue(true)
viper.SetDefault("tags", []string{})
os.Setenv("TAGS", "foo bar baz")
tags := viper.Get("tags")
// With SetTypeByDefaultValue(true): []string{"foo", "bar", "baz"}
// With SetTypeByDefaultValue(false): "foo bar baz"Viper supports nested keys using a delimiter (default is "."):
viper.Set("database.host", "localhost")
viper.Set("database.port", 5432)
viper.Set("database.credentials.username", "admin")
host := viper.GetString("database.host")
port := viper.GetInt("database.port")
username := viper.GetString("database.credentials.username")func Sub(key string) *Viper
func (*Viper) Sub(key string) *ViperReturns a new Viper instance representing a sub tree of the current instance. Sub is case-insensitive for a key.
Example:
viper.Set("database.host", "localhost")
viper.Set("database.port", 5432)
viper.Set("cache.host", "localhost")
viper.Set("cache.port", 6379)
// Get database config as a sub-tree
dbConfig := viper.Sub("database")
if dbConfig != nil {
host := dbConfig.GetString("host") // "localhost"
port := dbConfig.GetInt("port") // 5432
}// Set defaults
viper.SetDefault("app_name", "myapp")
viper.SetDefault("port", 8080)
// Read config file
viper.ReadInConfig()
// Override specific value
viper.Set("port", 9090)
// Get values with type safety
appName := viper.GetString("app_name")
port := viper.GetInt("port")
debug := viper.GetBool("debug")// Set complex default
viper.SetDefault("server", map[string]any{
"host": "localhost",
"port": 8080,
"tls": map[string]any{
"enabled": false,
"cert": "",
"key": "",
},
})
// Get nested values
host := viper.GetString("server.host")
tlsEnabled := viper.GetBool("server.tls.enabled")
// Get entire sub-section as map
serverConfig := viper.GetStringMap("server")if viper.IsSet("optional_feature") {
enabled := viper.GetBool("optional_feature")
if enabled {
// Enable optional feature
}
}
// Check if key is in config file (vs env, flag, etc)
if viper.InConfig("production_mode") {
production := viper.GetBool("production_mode")
}