Skip to content

Instantly share code, notes, and snippets.

@SolemnJoker
Created May 21, 2021 15:12
Show Gist options
  • Save SolemnJoker/d7a9b94f614300dbe4559b7a390b96c5 to your computer and use it in GitHub Desktop.
Save SolemnJoker/d7a9b94f614300dbe4559b7a390b96c5 to your computer and use it in GitHub Desktop.
go read ini file
// Package config_reader provides ...
package config_reader
import (
"gopkg.in/ini.v1"
)
type AppConfig struct {
DbUser string
DbPasswd string
DbUrl string
DbPort int
DbName string
UdpPort int
UdpIP string
}
func ReadConfigFile(fileName string) (*AppConfig, error) {
cfg, err := ini.Load(fileName)
if err != nil {
return nil, err
}
var config AppConfig
config.DbUser = cfg.Section("mysql").Key("user").MustString("dev")
config.DbPasswd = cfg.Section("mysql").Key("passwd").MustString("123456")
config.DbUrl = cfg.Section("mysql").Key("addr").MustString("127.0.0.1")
config.DbPort = cfg.Section("mysql").Key("port").MustInt(3307)
config.DbName = cfg.Section("mysql").Key("db").MustString("dev")
config.UdpPort = cfg.Section("udp").Key("port").MustInt(9000)
config.UdpIP = cfg.Section("udp").Key("ip").MustString("224.0.0.1")
return &config, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment