Skip to main content

cfg package

With the cfg package, you can configure and use configurations in your gosoline project.

Functions

WithConfigFile()

Usage

options := []cfg.Option{
cfg.WithConfigFile("config.dist.yml", "yml"),
}

Description

Load configurations from a file.

info

YAML is currently the only supported configuration filetype.

Methods

GetInt()

Usage

With default:

config := cfg.New()
config.GetInt("num", 1)

Without default:

config := cfg.New()

options := []cfg.Option{
cfg.WithConfigSetting("num", 1),
}

if err := config.Option(options...); err != nil {
panic(err)
}

config.GetInt("num")

Description

Gets an integer from the configuration struct.

caution

Environment variables overwrite configuration values:

config := cfg.New()

options := []cfg.Option{
cfg.WithConfigSetting("port", "8080"),
}

if err := config.Option(options...); err != nil {
panic(err)
}

// Prints 8080
fmt.Println(config.GetString("port"))

os.Setenv("PORT", "8081")

// Prints 8081
fmt.Println(config.GetString("port"))
GetString()
config := cfg.New()
config.GetString("env", "dev")
GetBool()
config := cfg.New()
config.GetBool("enabled", true)

Variables

DefaultEnvKeyReplacer

Usage

options := []cfg.Option{
cfg.WithEnvKeyReplacer(cfg.DefaultEnvKeyReplacer),
}

Description

Replaces "." and "-" with "_" for env key loading.