env

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 11 Imported by: 1 module, 0 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

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)
}

Index

Examples

Constants

View Source
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

func Env() *koanf.Koanf

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

func InitEnv(configFile string, configFS ...*embed.FS) error

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"))
}
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

func RequireStrings(keys ...string) error

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.

func VarName added in v0.2.6

func VarName(key string) string

VarName returns the environment variable that sets key: app.env.prefix plus the key in upper case with "." replaced by "_", e.g. email.host -> TM_EMAIL_HOST.

Types

This section is empty.

Directories

Path Synopsis
Package component provides env lifecycle integration for bootstrap.
Package component provides env lifecycle integration for bootstrap.

Jump to

Keyboard shortcuts

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