This is a creation in Article, where the information may have evolved or changed.
package mainimport ("bytes" "Encoding/json" "FMT" "Log" "OS" "RegExp" "Time") const configfilesizelimit = 10 << 20var defaultconfig = &struct { nettimeout int64filedeadtime string}{nettimeout: 15,filedeadtime: "24h",}//with the ' json:network ' comment, after JSON parsing can plug the corresponding data into the corresponding structure to type config struct {network networkconfig ' json:network ' files []fileconfig ' json:files '}type networkconfig struct {servers []string ' JSON: Servers ' sslcertificate string ' JSON: "Ssl certificate" ' sslkey string ' JSON: "Ssl key" ' sslca string ' JSON: "Ssl ca" ' timeout int64 ' Json:timeout ' timeout time. duration}type fileconfig struct {paths []string ' json:paths ' fields map[string]string ' json:fields ' deadtime string ' JSON: ' dead Time "' Deadtime time. Duration}func main () {loadconfig ("e:/golang/src/github.com/test/logstast_forwarder.conf")}func Loadconfig (path string) (Config config, err error) {config_file, err : = os. Open (path) if err != nil {emit ("failed to open config file '%s '): %s\n ", path, err) return}fi, _ := config_file. Stat () If size := fi. Size (); size > (configfilesizelimit) {emit ("config file (%q) size exceeds reasonable limit (%d) - aborting ", path, size) return // revu: Shouldn ' T this return an error, then?} If fi. Size () == 0 {emit ("config file (%q) is empty, skipping", path) Return}buffer := make ([]byte, fi. Size ()) _, err = config_file. Read (buffer) emit ("\n %s\n", buffer) buffer, err = stripcomments (buffer) //remove Comment if err != nil {emit ("failed to strip comments from json: %s\n" , err) return}buffer = []byte (OS. Expandenv (string (buffer))) //special Err = json. Unmarshal (buffer, &config) //parse JSON format data if err != nil {emit ("Failed Unmarshalling json: %s\n ", err) return} fmt. Printf ("111111111 %s \n", config.network.servers) For k, _&nbsP;:= range config. Files {if config. FILES[K]. deadtime == "" {config. FILES[K]. Deadtime = defaultconfig.filedeadtime}config. Files[k].deadtime, err = time. Parseduration (config. FILES[K]. Deadtime) If err != nil {emit ("Failed to parse dead time duration '%s '. error was: %s\n ", config. FILES[K]. Deadtime, err) return}}return}func stripcomments (data []byte) ([]byte, error) { Data = bytes. Replace (Data, []byte ("\ R"), []byte (""), 0) // windowslines := bytes. Split (Data, []byte ("\ n")) //split to muli linesfiltered := make ([][]byte, 0) For _, line := range lines {match, err := regexp. Match (' ^\s*# ', line) if err != Nil {return nil, err}if !match {filtered = append (Filtered, line)}} Return bytes. Join (Filtered, []byte ("\ n")), nil}func emit (msgfmt string, args ...interface{}) {log. Printf (Msgfmt, args ...)}
JSON configuration file:
# # JSON format file{"network": {"Servers": ["localhost:5043"]}, "Files": [{"Paths": [""]}]}
Output Result:
111111111 [localhost:5043] 2015/10/05 16:45:21 # # JSON format file{"network": {"Servers": ["localhost:5043"]}, "Files": [{"Paths": [""]}]}
Visible, Golang parsing of JSON files is very simple and comfortable