How to handle configuration in Go -
i'm new @ go programming, , i'm wondering: preferred way handle configuration parameters go program (the kind of stuff 1 might use properties files or ini files for, in other contexts)?
the json format worked me quite well. standard library offers methods write data structure indented, quite readable.
the benefits of json simple parse , human readable/editable while offering semantics lists , mappings (which can become quite handy), not case many ini-type config parsers.
example usage:
conf.json:
{ "users": ["usera","userb"], "groups": ["groupa"] }
program read configuration
import ( "encoding/json" "os" "fmt" ) type configuration struct { users []string groups []string } file, _ := os.open("conf.json") decoder := json.newdecoder(file) configuration := configuration{} err := decoder.decode(&configuration) if err != nil { fmt.println("error:", err) } fmt.println(configuration.users) // output: [usera, userb]
Comments
Post a Comment