Official document → https://gopkg.in/go-ini/ini.v1
go get gopkg.in/go-ini/ini.v1
.
├── config
│ └── config.go
├── config.ini
└── main.go
config.ini
[api]
api_key = aaaaaa
api_secret = bbbbbbb
[db]
password = ccccc
etc...
It is easy to manage if you divide it into sections like [api] [db] above. There is no need to enclose it in quotation marks.
config/config.go
package config
import "gopkg.in/ini.v1"
type ConfigList struct {
APIKey string
APISecret string
Password string
}
var Config ConfigList
func init() {
cfg, err := ini.Load("config.ini")
if err != nil {
//Error handling
}
Config = ConfigList{
APIKey: cfg.Section("api").Key("api_key").String(),
APISecret: cfg.Section("api").Key("api_secret").String(),
Password: cfg.Section("db").Key("password").String(),
}
}
main.go
package main
import "./config"
func main() {
Println(config.Config.APIKey)
Println(config.Config.APISecret)
Println(config.Config.Password)
}
You can get the value of config like this.
This time I created an ini file, but there is also a method using TOML. I recommend this person's article because it was very easy to understand. →https://qiita.com/futoase/items/fd697a708fcbcee104de
Recommended Posts