This is a creation in Article, where the information may have evolved or changed.
Golang Read INI configuration file
First, install config configuration explanation package:
Go get github.com/larspensjo/config
Ii. loading its package and code settings
package mainimport ("Flag" "FMT" "Github.com/larspensjo/config" "Log" "Runtime") var (configfile = flag. String ("ConfigFile", "Config.ini", "General configuration file"))//topic listvar topic = make (map[string]string) Func main () {runtime. Gomaxprocs (runtime. NUMCPU ()) flag. Parse ()//set config file stdcfg, err := config. Readdefault (*configfile) if err != nil {log. Fatalf ("Fail to find", *configfile, err)}//set config file std end// Initialized topic from the configurationif cfg. Hassection ("Topicarr") {section, err := cfg. Sectionoptions ("Topicarr") if err == nil {for _, v := range Section {options, err := cfg. String ("Topicarr", v) if err == nil {topic[v] = options}}}}//initialized topic From the configuration endfmt.println (TOPIC) fmt. Println (topic["Debug"])}
Iii. Configuration Files
File name: config.ini[topicarr]addr = 192.168.1.100debug = TrueLogin = Loginrequest[other]t1 = 0000337t2 = Admin
Iv. Introduction:
4.1 First pass CONFIG. Readdefault (*configfile) Open configuration file
4.2 Then determine if the first-level label name in the configuration file exists if CFG. Hassection ("Topicarr") {}
4.2 Reads all child tags in the first-level label CFG. Sectionoptions ()
4.3 loop tab, record the values in a sub-label in a map (topic as a global variable) for later use
For _, V: = Range section {options, err: = cfg. String ("Topicarr", v) if Err = = Nil {topic[v] = options}}
Match it to your liking