Skip to content

Simple persistent storage for application configuration

License

Notifications You must be signed in to change notification settings

downace/go-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Config

Simple persistent storage for application configuration

Example

type AppConfig struct {
    Host string `yaml:"host"`
    Port uint16 `yaml:"port"`
}

// NewConfigMinimal uses YAML format
// and stores data in config.yaml file inside current working directory
conf := config.NewConfigMinimal(AppConfig{
    Host: "192.168.10.1",
    Port: 8888,
})

// Load config from file
conf.Load()
// Use config
fmt.Printf("Address is %s:%d\n", conf.Data.Host, conf.Data.Port)
// Change config
conf.Data.Port = 8080
// Save config to file
conf.Save()

You can customize serializer and storage:

type AppConfig struct {
    AuthToken string `json:"authToken"`
}

conf := config.NewConfig(
    AppConfig{},
    config.FileStorage{Filepath: "my-config.json"},
    config.JsonSerializer[AppConfig]{},
)

// ...

Using Transaction method

type AppConfig struct {
    Host string `yaml:"host"`
    Port uint16 `yaml:"port"`
}

conf := NewConfigMinimal(AppConfig{})

err := conf.Transaction(func(data *AppConfig) error {
    data.Host = "192.168.10.1"
    data.Port = 8888
    // Use panic or return non-nil error to rollback changes
    return nil
})

if err != nil {
    // ...
}

// Changes are saved automatically if there were no error nor panic

About

Simple persistent storage for application configuration

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages