Golang Read INI configuration file

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.