TyConf is Tianyi Network's custom configuration library for Go.
Featured in parsing configuration from environment variables and command-line arguments, TyConf is designed to be simple and easy to use.
Please note that only following types are supported: string, bool, int, int64, uint, uint64, float64.
See go.dev for detailed documentation.
Run the following command to install the package:
go get github.com/luotianyi-dev/go-tyconfHere is an example of how to use TyConf:
import (
"fmt"
"github.com/luotianyi-dev/go-tyconf"
)
type Config struct {
EnableTCP bool `env:"ENABLE_TCP" cli:"tcp" description:"Enable TCP server"`
ServerName string `env:"SERVER_NAME" cli:"sname" description:"Name of the server"`
UpdateIntervalSec int `env:"UPDATE_INTERVAL_SEC" cli:"interval" description:"Update interval in seconds"`
}
func main() {
defaultConfig := Config{
EnableTCP: false,
ServerName: "default",
UpdateIntervalSec: 30,
}
config := tyconf.Parse(defaultConfig).(Config)
fmt.Printf("Config: %+v\n", config)
}Run go run main.go to test the code. TyConf use flag package as underlying implementation, so you can pass -h or --help to see the help message:
go run main.go --helpYou can set environment variables or pass command-line arguments to override the default values:
ENABLE_TCP=true SERVER_NAME=example go run main.go --interval 10CLI is always prioritized over environment variables. For example, if you run the following command:
ENABLE_TCP=false SERVER_NAME=example go run main.go --tcpThe EnableTCP field will be set to true because the --tcp flag is passed.
This project is licensed under the MIT License.