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