config

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2025 License: MIT Imports: 5 Imported by: 0

README

Config

This package provides environment-based configuration with automatic struct field mapping.

Usage

The GetConfig function takes 2 arguments:

  • filename - path to the .env file (optional, can be empty)
  • config - pointer to a struct that will be populated
type Config struct {
    Port string                    // Required by default
    DatabaseHost string            // Maps to DATABASE_HOST
    OptionalSetting string `required:"false"` // Optional field
}

var config Config
err := GetConfig(".env", &config)
if err != nil {
    log.Fatal(err)
}

Field Mapping

Struct field names are automatically converted to SNAKE_CASE environment variables:

  • ServerPortSERVER_PORT
  • DatabaseHostDATABASE_HOST

Configuration

  • All fields must be strings
  • Fields are required by default
  • Use required:"false" tag for optional fields
  • Missing required environment variables return an error

Environment File

Example .env file:

PORT=8080
DATABASE_HOST=localhost

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConfig

func GetConfig(filename string, config any) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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