Created
August 21, 2014 16:26
-
-
Save andreagrandi/614bb621d87bdc2a6869 to your computer and use it in GitHub Desktop.
Read values from a JSON configuration file
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
// Parse a conf.json file with this content: | |
// | |
// { | |
// "username": "YourUser", | |
// "password": "YourPassword" | |
// } | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"os" | |
) | |
type Configuration struct { | |
Username string | |
Password string | |
} | |
func main() { | |
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.Username) | |
fmt.Println(configuration.Password) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment