Documentation
¶
Overview ¶
Example ¶
Example demonstrates the complete lifecycle: call InitEnv once at application startup with a TOML config file, then use Env to read typed values anywhere in the application.
package main
import (
"fmt"
"log"
"github.com/phcp-tech/common-library-golang/env"
)
func main() {
// Initialise once – typically in main() before starting any services.
if err := env.InitEnv("/etc/myapp/config.toml"); err != nil {
log.Fatal(err)
}
// Env() returns the loaded configuration; use koanf's typed accessors.
host := env.Env().String("server.host")
port := env.Env().Int("server.port")
fmt.Printf("server: %s:%d\n", host, port)
}
Output:
Index ¶
Examples ¶
Constants ¶
const Placeholder = "TOBE_REPLACED"
Placeholder is the value config/app.toml files use for settings whose real value is set at deployment, usually with an environment variable. InitEnv fails while any setting still has this value. For an optional setting, or one used only by some configurations, use "" in app.toml instead and check it where it is used, for example with RequireStrings.
Variables ¶
This section is empty.
Functions ¶
func Env ¶
Env returns the global koanf configuration instance loaded by InitEnv. It returns nil if InitEnv has not been called or if initialization failed.
func InitEnv ¶
InitEnv initializes the global environment configuration by loading the specified TOML config file and then merging any matching OS environment variables (using the prefix defined by app.env.prefix in the config file). If configFS is provided and non-nil, the file is read from the embedded filesystem instead of the real filesystem. Only the first call takes effect; subsequent calls return the result of the first initialization without reloading.
Example (EmbeddedFS) ¶
ExampleInitEnv_embeddedFS shows how to bundle the config file inside the binary at compile time using //go:embed, so no external file is needed at runtime. Pass the embedded.FS as the second argument to InitEnv.
package main
import (
"embed"
"fmt"
"log"
"github.com/phcp-tech/common-library-golang/env"
)
// testdata/config.toml is embedded to demonstrate the embedded-FS code path.
//
//go:embed testdata/config.toml
var exampleFS embed.FS
func main() {
if err := env.InitEnv("testdata/config.toml", &exampleFS); err != nil {
log.Fatal(err)
}
fmt.Println(env.Env().String("server.host.name"))
fmt.Println(env.Env().String("server.host.ip"))
fmt.Println(env.Env().Int("server.port"))
}
Output:
Example (MissingFile) ¶
ExampleInitEnv_missingFile shows that InitEnv returns a non-nil error when the config file does not exist. The application should treat this as fatal.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/env"
)
func main() {
err := env.InitEnv("/nonexistent/config.toml")
fmt.Println(err != nil)
}
Output: true
func RequireStrings ¶ added in v0.2.6
RequireStrings checks that every key has a non-empty value that is not Placeholder. It reports all missing keys at once, each with the environment variable that sets it. Use it in a component's Init for the settings that component needs; InitEnv already rejects placeholders, but not empty values.
Types ¶
This section is empty.