Golang How to read the INI configuration file

Source: Internet
Author: User
Tags read ini file
This is a creation in Article, where the information may have evolved or changed.

For several days did not update because I am studying a new project, and then very time-consuming, I hope to get everyone's approval and help, here affirm that I hope you help, if you have a good program or code to judge, want to share to everyone, you can contribute to me, share code is to help everyone, The code must be workable, and then I'll test it and then release it! Send code snippet can be directly to my mailbox admin#widuu.com (#换成 @), also can add my qq:23169450, below is a Golang read INI file simple case!


[PHP]
Package Main

Import (
"Bufio"
"FMT"
"IO"
"OS"
"Strings"
)

Declare a map to access the configuration file
var per map[string]interface{}

Func Main () {
Instantiate this map
per = Make (map[string]interface{})
Open this INI file
F, _: = OS. Open ("Test.ini")
Read the file into buffer inside
BUF: = Bufio. Newreader (f)
for {
Read each row by line break
L, Err: = buf. ReadString (' \ n ')
Equivalent to PHP trim
Line: = Strings. Trimspace (L)
Judging exit loops
If err! = Nil {
If err! = Io. EOF {
return err
Panic (ERR)
}
If Len (line) = = 0 {
Break
}
}
Switch {
Case Len (line) = = 0:
Match [db] and store
Case Line[0] = = ' [' && Line[len (line)-1] = = '] ':
Section: = Strings. Trimspace (Line[1:len (line)-1])
Fmt. PRINTLN (section)
Default
Dnusername = Xiaowei This can be matched to the storage
I: = strings. Indexany (line, "=")
Per[strings. Trimspace (line[0:i])] = strings. Trimspace (line[i+1:])

}
}
Loop output Results
For k, V: = Range per {
Fmt. Println (k, v)
}
}
[/php]

Do not reprint any article of this website without permission: Micro network»golang method of reading INI configuration file

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.