Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hashimthearab/gocon
The simplest settings and config library for your Go app.
https://github.com/hashimthearab/gocon
config go golang settings
Last synced: 15 days ago
JSON representation
The simplest settings and config library for your Go app.
- Host: GitHub
- URL: https://github.com/hashimthearab/gocon
- Owner: HashimTheArab
- License: gpl-3.0
- Created: 2023-12-06T06:31:36.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2023-12-06T17:49:37.000Z (12 months ago)
- Last Synced: 2024-10-10T23:34:15.673Z (about 1 month ago)
- Topics: config, go, golang, settings
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GoCon
GoCon is the simplest way to store and load a config in your Go program.## Installation
To use GoCon in your Go project, you can install it by using `go get`:
```bash
go get github.com/HashimTheArab/gocon
```## Example
```go
type Settings struct {
Theme string
DarkMode bool
}func (Settings) Path() string {
return "settings.json"
}func main(){
// Load the settings
settings, err := gocon.Load[Settings]()
if err != nil {
// If the file fails to load, or it was not found, an error will be returned.
panic(err)
}// Save settings
gocon.Save(Settings{Theme: "blue", DarkMode: true})
}
```## Explanation
Define a config struct, you can define multiple and each one will be saved to a separate file.
1. **Define Your Settings Struct:**
In your settings.go file, define a struct representing your application settings. Implement the Path method to specify the path where the configuration file will be stored.
```go
type Settings struct {
Theme string
DarkMode bool
}func (Settings) Path() string {
return "settings.json"
}
```2. **Load and save settings:**
After creating your settings struct, you can simply use it.You can save a config:
```go
gocon.Save(Settings{Theme: "blue", DarkMode: true})
```And then you can load it in the future:
```go
settings, _ := gocon.Load[Settings]()
```You can have multiple configs / settings, each struct will be saved to its own path.