In the project, some configuration information is often placed in the configuration file, so that in different running environments, only the configuration file can be modified.
Here are two ways to get a configuration file through a third-party package
A by gopkg.in/ini.v1
Go get gopkg.in/ini.v1
1. configuration file test.conf
; [Mqtt]
Mqtt_hostname = 127.0.0.1
mqtt_port = 8083
mqtt_user = admin
mqtt_pass = 123456
mqtt_keepalive =
Mqtt_timeout = 3
2. Get the configuration file and turn it into a struct
package main import ("Log" "Gopkg.in/ini.v1") var filepath = "D:/goproject/src/test/test.conf" Type Config struct {//config file to specify the name in the configuration file via tag mqtthostname string ' ini: ' mqtt_hostname ' ' mqttport string ' ini: ' Mqtt _port "' Mqttuser string ' ini:" Mqtt_user "' Mqttpass string ' ini:" mqtt_pass "' mqttkeepalive int ' ini:" Mqtt_keepal Ive "' mqtttimeout int ' ini:" Mqtt_timeout "'} Func Main () {config,err: = Readconfig (filepath)///can also be via Os.arg or F LAG specifies the configuration file path from the command line if err! = Nil {log. Fatal (Err)} log. Println (config)}//reads the configuration file and goes to struct func readconfig (path string) (config, error) {var config config conf, err: = INI.L Oad (path)//load config file if err! = Nil {log.
PRINTLN ("load config file fail!") return config, err} conf. Blockmode = False err = Conf. Mapto (&config)//parse into struct if err! = Nil {log.
Println ("mapto config file fail!") return config, err} return config, nil} run Result:
{127.0.0.1 8083 admin 123456 60 3}
Second, github.com/larspensjo/config read INI configuration file
1. Obtain a third-party package:
Go get github.com/larspensjo/config
2. configuration file Config.ini
[Mqtt] #一级选项section
#下面为option
mqtt_hostname = 127.0.0.1
mqtt_port = 8083
mqtt_user = admin
mqtt_pass = 123456
mqtt_keepalive =
Mqtt_timeout = 3
3. Get the configuration file and turn it into a map
Package main
import (
"flag"
"runtime"
"Log"
"Github.com/larspensjo/config"
)
var (
//https://studygolang.com/articles/686
//Support command line input format is-configfile=name, default is Config.ini
//profile is generally acquired to be type
configfile = flag. String ("ConfigFile", "Config.ini", "General configuration File")
TOPIC = Make (map[string]string)
)
Func Main () {
runtime. Gomaxprocs (runtime. NUMCPU ())
flag. Parse ()
cfg,err: = config. Readdefault (*configfile) //Read the configuration file and return its config
if err! = Nil {
log. Fatalf ("Fail to find%v,%v", *configfile,err)
}
if cfg. Hassection ("Mqtt") { //determines if there is a section (first label) in the configuration file
options,err: = cfg. Sectionoptions ("Mqtt") //Get all sub-tabs of the first-level label options (only the label has no value)
if Err = = Nil {for
_,v: = Range options{
Optionvalue,err: = cfg. String ("Mqtt", V) //To get the corresponding value according to the first label section and option
If Err = nil {
topic[v] =optionvalue
}
}
}
}
Log. Println (TOPIC)
}
4. Operation and Results:
Because you need to specify a configuration file, you need to run it on the command line