Last active
February 26, 2020 16:36
-
-
Save weibeld/9c94c51c7ffec78f23a071a6d6427927 to your computer and use it in GitHub Desktop.
Encoding Go values to JSON
This file contains hidden or 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
// Create JSON from a string | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
) | |
func main() { | |
b, err := json.Marshal("foo") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Prints: "foo" | |
fmt.Printf("%s\n", b) | |
} |
This file contains hidden or 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
// Create JSON from an array | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
) | |
func main() { | |
b, err := json.Marshal([]string{"foo", "bar", "baz"}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Prints: ["foo","bar","baz"] | |
fmt.Printf("%s\n", b) | |
} |
This file contains hidden or 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
// Create JSON from a map | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
) | |
func main() { | |
m := map[string]int{"foo": 1, "bar": 2, "baz": 3} | |
b, err := json.Marshal(m) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Prints: {"bar":2,"baz":3,"foo":1} | |
fmt.Printf("%s\n", b) | |
} |
This file contains hidden or 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
// Create JSON from a map | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
) | |
func main() { | |
m := map[string]interface{}{ | |
"name": "Alice", | |
"address": map[string]string{ | |
"street": "Wonder Lane 42", | |
"city": "Wonderland", | |
}, | |
"interests": []string{"K8s", "Go", "JSON"}, | |
} | |
b, err := json.Marshal(m) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Prints: {"address":{"city":"Wonderland","street":"Wonder Lane 42"},"interests":["K8s","Go","JSON"],"name":"Alice"} | |
fmt.Printf("%s\n", b) | |
} |
This file contains hidden or 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
// Create JSON from a struct | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
) | |
type Number struct { | |
Round int | |
Exact float64 | |
hidden string // Not exported (lower case) => not included in JSON | |
} | |
func main() { | |
m := Number{3, 3.14, "hidden"} | |
b, err := json.Marshal(m) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Prints: {"Round":3,"Exact":3.14} | |
fmt.Printf("%s\n", b) | |
} |
This file contains hidden or 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
// Create JSON from a struct | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
) | |
type Message struct { | |
Name string | |
Address Address | |
Interests []string | |
hidden string // Not exported (lower case) => not included in JSON | |
} | |
type Address struct { | |
Street string | |
City string | |
} | |
func main() { | |
m := Message{"Alice", Address{"Wonder Lane 42", "Wonderland"}, []string{"K8s", "Go", "JSON"}, "hidden"} | |
b, err := json.Marshal(m) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Prints: {"Name":"Alice","Address":{"Street":"Wonder Lane 42","City":"Wonderland"},"Interests":["K8s","Go","JSON"]} | |
fmt.Printf("%s\n", b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment