JSON
Go offers built-in support for JSON encoding and decoding.
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
)
// Only exported fields(with capital letter) will be encoded/decoded in JSON
type response1 struct {
Page int
Fruits []string
}
type response2 struct {
Page int `json:"page"` // We can use tags on fields declaration to customize JSON key names.
Fruits []string `json:"fruits"`
}
type response3 struct {
omitted int
Page int `json:"page"` // We can use tags on fields declaration to customize JSON key names.
Fruits []string `json:"fruits"`
}
func main() {
/* Encode */
// We can encode Go's data types to JSON string with `Marshal`
// Basic data
bolB, _ := json.Marshal(true)
fmt.Println(string(bolB))
intB, _ := json.Marshal(1)
fmt.Println(string(intB))
fltB, _ := json.Marshal(2.34)
fmt.Println(string(fltB))
strB, _ := json.Marshal("gopher")
fmt.Println(string(strB))
// Maps and slices
slcD := []string{"apple", "peach", "pear"}
slcB, _ := json.Marshal(slcD)
fmt.Println(string(slcB))
mapD := map[string]int{"apple": 5, "lettuce": 7}
mapB, _ := json.Marshal(mapD)
fmt.Println(string(mapB))
// Custom data type(only exported fields included and used as keys)
res1D := &response1{
Page: 1,
Fruits: []string{"apple", "peach", "pear"},
}
res1B, _ := json.Marshal(res1D)
fmt.Println(string(res1B))
res2D := &response2{
Page: 1,
Fruits: []string{"apple", "peach", "pear"},
}
res2B, _ := json.Marshal(res2D)
fmt.Println(string(res2B))
res3D := &response3{
omitted: 3, // Will not included in JSON
Page: 1,
Fruits: []string{"apple", "peach", "pear"},
}
res3B, _ := json.Marshal(res3D)
fmt.Println(string(res3B))
/* Decode */
byt := []byte(`{"num":6.13,"strs":["a","b"]}`)
// We need to provide a variable where the JSON package can put the decoded data.
// This map[string]interface{} will hold a map of strings to arbitrary data types.
var dat map[string]interface{}
// We can decode json data with `Unmarshal`
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fmt.Println(dat)
// We’ll need to convert them to their appropriate type.
num := dat["num"].(float64)
fmt.Println(num)
// Accessing nested data requires a series of conversions.
strs := dat["strs"].([]interface{})
str1 := strs[0].(string)
fmt.Println(str1)
// We can also decode JSON into custom data types,
// which has better type-safety
str2 := `{"page": 1, "fruits": ["apple", "peach"]}`
res2 := response2{}
json.Unmarshal([]byte(str2), &res2)
fmt.Println(res2)
fmt.Println(res2.Fruits[0])
str3 := `{"omitted":3,"page": 1, "fruits": ["apple", "peach"]}`
res3 := response3{}
json.Unmarshal([]byte(str3), &res3)
fmt.Println(res3)
fmt.Println(res3.omitted) // 0
// We can also stream JSON encodings directly to os.Writers like os.Stdout or even HTTP response bodies.
enc := json.NewEncoder(os.Stdout)
d := map[string]int{"apple": 5, "lettuce": 7}
enc.Encode(d)
// So does decoding, we can streaming reads from os.Readers like os.Stdin or HTTP request bodies.
dec := json.NewDecoder(strings.NewReader(str2))
res1 := response2{}
dec.Decode(&res1)
fmt.Println(res1)
}
/*
true
1
2.34
"gopher"
["apple","peach","pear"]
{"apple":5,"lettuce":7}
{"Page":1,"Fruits":["apple","peach","pear"]}
{"page":1,"fruits":["apple","peach","pear"]}
{"page":1,"fruits":["apple","peach","pear"]}
map[num:6.13 strs:[a b]]
6.13
a
{1 [apple peach]}
apple
{0 1 [apple peach]}
0
{"apple":5,"lettuce":7}
{1 [apple peach]}
*/URL
package main
import (
"fmt"
"net"
"net/url"
)
func main() {
s := "postgres://user:pass@host.com:5432/path?k=v#f"
// Parsing URL with `url.Parse` and ensures no error occurred
u, err := url.Parse(s)
if err != nil {
panic(err)
}
// Accessing different part of URL
fmt.Println(u.Scheme)
fmt.Println(u.User) // User contains all authentication info
fmt.Println(u.User.Username())
p, _ := u.User.Password()
fmt.Println(p)
fmt.Println(u.Host) // The Host contains both the hostname and the port
host, port, _ := net.SplitHostPort(u.Host)
fmt.Println(host)
fmt.Println(port)
fmt.Println(u.Path)
fmt.Println(u.Fragment) // The part after "#"
fmt.Println(u.RawQuery) // Get query params in a string
m, _ := url.ParseQuery(u.RawQuery) // Parse query params into a map
// The parsed query param maps are from **strings to slices of strings**,
// so index into [0] if you only want the first value.
fmt.Println(m)
fmt.Println(m["k"][0])
}
/*
postgres
user:pass
user
pass
host.com:5432
host.com
5432
/path
f
k=v
map[k:[v]]
v
*/