Skip to content

Instantly share code, notes, and snippets.

@PK85
Created October 20, 2015 21:54
Show Gist options
  • Save PK85/810250b9f38b33a03626 to your computer and use it in GitHub Desktop.
Save PK85/810250b9f38b33a03626 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type Config map[string]interface{}
func (c Config) Name() string {
return c["name"].(string)
}
func (c Config) SecondNestedValue() bool {
nested := c["nested"].([]interface{})
second := nested[1].(map[string]interface{})
value := second["value"].(bool)
return value
}
func LoadConfig(path string) (Config, error) {
var m Config
data, err := ioutil.ReadFile(path)
if err != nil {
return m, err
}
err = json.Unmarshal(data, &m)
return m, err
}
func main() {
config, err := LoadConfig("config.json")
if err != nil {
panic(err)
}
fmt.Println("Config: ", config)
fmt.Println("Name: ", config.Name())
fmt.Println("SecondNestedValue ", config.SecondNestedValue())
}
{
"name": "Jurek",
"surname": "Ogórek",
"nested": [{
"value": true,
"value2": 11
},
{
"value": false ,
"value2": 1
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment