config

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Purpose: This file implements the GoCon environment configuration system. It parses a `.env` file and exposes typed getters used throughout the application.

Philosophy: Configuration should be environment-driven and never hardcoded. By reading from a `.env` file at startup, developers can safely switch between local, staging, and production environments without touching source code. We use zero external dependencies — pure stdlib.

Architecture: A single `Manager` struct holds a parsed key→value map loaded once at boot via `Load()`. The global `gostack.Config` facade exposes it to the entire application.

Choice: We chose a simple line-by-line `.env` parser over external libraries (like godotenv) to maintain GoStack's zero-dependency philosophy. The parser handles comments, blank lines, quoted values, and inline comments.

Implementation: - Manager: holds the parsed env map. - Load(path): reads and parses the .env file into the manager. - Get(key): returns raw string value. - GetOrDefault(key, fallback): returns value or fallback if missing. - GetInt(key, fallback): returns int-parsed value or fallback. - GetBool(key, fallback): returns bool-parsed value or fallback.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager holds the parsed environment configuration map.

func New

func New() *Manager

New builds a fresh, empty Manager instance.

func (*Manager) Get

func (m *Manager) Get(key string) string

Get returns the raw string value for key, or an empty string if not found.

func (*Manager) GetBool

func (m *Manager) GetBool(key string, fallback bool) bool

GetBool returns the boolean value for key. Accepts "true", "1", "yes" (case-insensitive) as truthy. Returns fallback if the key is absent or unrecognized.

func (*Manager) GetInt

func (m *Manager) GetInt(key string, fallback int) int

GetInt returns the integer value for key, or fallback if the key is absent or cannot be parsed as an integer.

func (*Manager) GetOrDefault

func (m *Manager) GetOrDefault(key, fallback string) string

GetOrDefault returns the value for key, falling back to the provided default if the key is absent or empty.

func (*Manager) Load

func (m *Manager) Load(path string) error

Load reads and parses a .env file into the Manager. Lines starting with '#' are treated as comments and skipped. Blank lines are skipped. Values may optionally be quoted with " or '.

Example .env:

APP_NAME=GoStack
DB_DSN="user:pass@/mydb"   # inline comment

func (*Manager) Set

func (m *Manager) Set(key, value string)

Set manually assigns a key/value pair. Useful for testing or runtime overrides.

Jump to

Keyboard shortcuts

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