Go Conf Builder

A declarative, struct-tag based configuration loader for Go.

Features

  • Declarative Configuration: Define your config structure using struct tags (conf).
  • Multiple Sources: Load from Environment Variables, Command-line Flags, and more.
  • Type-Safe: Automatically binds values to basic types (int, bool, string, float64, etc.).
  • Pluggable Architecture: Easily add new configuration sources by implementing the Provider interface.

Installation

go get github.com/mirkobrombin/go-conf-builder/v2

Quick Start

package main

import (
    "context"
    "fmt"
    "github.com/mirkobrombin/go-conf-builder/v2/pkg/loader"
    "github.com/mirkobrombin/go-conf-builder/v2/pkg/source/env"
    "github.com/mirkobrombin/go-conf-builder/v2/pkg/source/flag"
)

type Config struct {
    AppName string `conf:"env:APP_NAME,default:MyGoApp"`
    Port    int    `conf:"env:PORT,flag:port,default:8080"`
    Debug   bool   `conf:"env:DEBUG,flag:debug,default:false"`
}

func main() {
    // Define sources
    l := loader.New(
        env.New("APP"),
        flag.New(),
    )

    // Load into struct
    cfg := &Config{}
    if err := l.Load(context.Background(), cfg); err != nil {
        panic(err)
    }

    fmt.Printf("Loaded: %+v\n", cfg)
}

Documentation

License

This project is licensed under the MIT License. See the LICENSE file for details.