Last active
January 15, 2016 08:15
-
-
Save yangsibai/c6a95f355e3ce101d4c2 to your computer and use it in GitHub Desktop.
go lang configuration, from <http://stackoverflow.com/questions/16465705/how-to-handle-configuration-in-go>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Users": ["UserA","UserB"], | |
"Groups": ["GroupA"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment