Skip to content

Instantly share code, notes, and snippets.

@andreagrandi
Created August 21, 2014 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreagrandi/614bb621d87bdc2a6869 to your computer and use it in GitHub Desktop.
Save andreagrandi/614bb621d87bdc2a6869 to your computer and use it in GitHub Desktop.
Read values from a JSON configuration file
// 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