Skip to content

Instantly share code, notes, and snippets.

@alextanhongpin
Last active September 9, 2017 01:22
Show Gist options
  • Save alextanhongpin/12ca8509a51e9d11b26460aa2daa0b2c to your computer and use it in GitHub Desktop.
Save alextanhongpin/12ca8509a51e9d11b26460aa2daa0b2c to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"io/ioutil"
"log"
)
func loadJSON(file string, obj interface{}) error {
body, err := ioutil.ReadFile(file)
if err != nil {
return err
}
return json.Unmarshal(body, &obj)
}
// Point represents the schema of the json we want to load
type Point struct {
X int `json:"x"`
Y int `json:"y"`
}
func main() {
var points []Point
if err := loadJSON("out.json", &points); err != nil {
log.Printf("error loading json: %v", err)
}
log.Printf("load json: %#v\n", points)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment