golang 配置文件处理
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"os"
// "strings"
//"unicode/utf8"
)
var ProfileItems map[string]interface{} //obviously,this var will be used in many files
func main() {
var profile string
flag.StringVar(&profile, "profile", os.Getenv("GOETC")+"/profile.json", "Full path of the profile.")
flag.Parse()
profileFD, err := os.Open(profile)
if err != nil {
panic(err)
}
defer profileFD.Close()
buffer := bufio.NewReader(profileFD)
var profileLines string
for {
line, err := buffer.ReadString('\n')
if err == io.EOF {
break
} else if line[0] == '#' || line[0] == ';' {
continue
} else if err != nil {
panic(err)
}
profileLines += line
}
jsonLines := []byte(profileLines)
if err := json.Unmarshal(jsonLines, &ProfileItems); err != nil {
panic(err)
}
}