-
-
Save alextanhongpin/752e524cf7dd853cd737c28e71c11fe5 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
// This program demonstrates how to write a struct to json file | |
package main | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
"log" | |
) | |
// Point represents the schema of our json output | |
type Point struct { | |
X int `json:"x"` | |
Y int `json:"y"` | |
} | |
func writeJSON(file string, obj interface{}, pretty bool) (err error) { | |
var bytes []byte | |
if pretty { | |
bytes, err = json.MarshalIndent(obj, "", " ") | |
} else { | |
bytes, err = json.Marshal(obj) | |
} | |
if err != nil { | |
return err | |
} | |
return ioutil.WriteFile(file, bytes, 0644) | |
} | |
func main() { | |
points := []Point{Point{0, 0}, Point{1, 1}} | |
err := writeJSON("out.json", points, true) | |
if err != nil { | |
log.Fatalf("error writing to json: %v\n", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment