Last active
August 29, 2015 14:27
-
-
Save EtienneR/5c4344043abe1bb87f91 to your computer and use it in GitHub Desktop.
JSON export file with Go (via io/ioutil)
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":"this is the name value","description":"this is the description value","email":"this is the email value"} |
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" | |
"io/ioutil" | |
) | |
// JSON struct | |
type Conf struct { | |
Name string `json:"name"` | |
Description string `json:"description"` | |
Email string `json:"email"` | |
} | |
func main() { | |
// Values data | |
data := Conf{"this is the name value", "this is the description value", "this is the email value"} | |
// Transform to JSON (bytes format) | |
content, err := json.Marshal(data) | |
if err != nil { | |
panic(err) | |
} | |
// Write in a file (or existing ecrase) | |
err = ioutil.WriteFile("foo.json", content, 0644) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment