config

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package config provides utility functions for accessing environment variables in a structured and consistent manner across the API application. This uses the `github.com/joho/godotenv` package to load environment variables from a `.env` file

This package acts as a centralized interface for reading from the runtime environment (e.g., from `.env` files, containerized environments, or CI/CD systems), and ensures defaults and key presence are handled gracefully

Core Functions:

  • GetEnv() map[string]string Returns all available environment variables as a map for inspection or iteration

  • GetEnvValue(key string) string Retrieves the value of a specific environment variable. Returns an empty string if not set

  • GetEnvWithDefault(key, defaultValue string) string Returns the value of the specified environment variable, or a provided default if the variable is not set

  • KeyExists(key string) bool Checks whether a specific environment variable is present in the runtime environment

Usage Example:

dbHost := config.GetEnvWithDefault("DB_HOST", "localhost")

if !config.KeyExists("JWT_SECRET") {
    log.Fatal("JWT_SECRET is required but not set")
}

Design Intent:

  • Keep all environment access logic isolated to a single, testable package
  • Reduce boilerplate in service-level configuration code
  • Provide safe access patterns that avoid panics or misconfiguration

This package is meant to be minimal and dependency-free, giving API templates a predictable and consistent way to interact with system-level configuration at runtime

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEnv

func GetEnv(key string) (string, bool)

GetEnv returns the value and existence status of the environment variable with the given key

func GetEnvValue

func GetEnvValue(key string) string

GetEnvValue returns the value of the environment variable with the given key. If no such key exists, an empty string is returned

func GetEnvWithDefault

func GetEnvWithDefault(key, defaultValue string) string

GetEnvWithDefault returns the value of the environment variable with the given key. If no such key exists, the default value is returned

func KeyExists

func KeyExists(key string) bool

KeyExists returns true if the environment variable with the given key exists, false otherwise

Types

This section is empty.

Jump to

Keyboard shortcuts

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