Created
October 20, 2015 21:54
-
-
Save PK85/810250b9f38b33a03626 to your computer and use it in GitHub Desktop.
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
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()) | |
} |
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
{ | |
"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