Go代码片段

初始值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
type Na struct {
name string
}

type Ia interface {
}

func main() {
var e error
var na Na
var ia Ia
fmt.Println(na, e, ia)

n := new(Na)
i := new(Ia)
fmt.Println(n, i)

c := make([]byte, 2, 5)
fmt.Println(c)
}
/* output:
{} <nil> <nil>
&{} 0xc042008270
[0 0]
*/

make 和 new

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var v = make([]byte, 10, 20)
v[0] = 0
v[1] = 1
v[2] = 2
v[9] = 32
v = v[:cap(v)] // We can grow s to its capacity by slicing it again https://blog.golang.org/go-slices-usage-and-internals
v[10] = 33
fmt.Println(v)

var n = new([]byte)
n = &v
fmt.Println(*n)
/* output:
[0 1 2 0 0 0 0 0 0 32 33 0 0 0 0]
[0 1 2 0 0 0 0 0 0 32 33 0 0 0 0]
*/

How to check if a map contains a key in go?

1
2
3
if val, ok := dict["foo"]; ok {
//do something here
}

Removing item(s) from a slice, while iterating in Go

1
2
3
4
5
6
7
8
9
10
11
12
13
package main

import "fmt"

func main() {
arr := []int64{1, 2, 3, 4, 6, 8, 9, 10, 11, 23}
for i := len(arr) - 1; i >= 0; i-- {
if arr[i]%2 == 0 {
arr = append(arr[:i], arr[i+1:]...)
}
}
fmt.Println(arr)
}

过滤selected为false的商品规格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 方案1 
specProps := product.SpecProps
spec := models.Spec{}
err := json.Unmarshal([]byte(specProps), &spec)
if err == nil {
sItem := make([]models.SpecChildren, 0)
for _, v := range spec.Spec {
scItem := make([]models.SpecChildrenItem, 0)
for _, vv := range v.Children {
if vv.Selected {
vv.Selected = false
scItem = append(scItem, vv)
}
}
v.Children = scItem
sItem = append(sItem, v)
}
spec.Spec = sItem

data, err := json.Marshal(spec)
if err == nil {
product.SpecProps = string(data)
}
}

// 方案2
specProps := product.SpecProps
spec := models.Spec{}
err := json.Unmarshal([]byte(specProps), &spec)
if err == nil {
for j := 0; j < len(spec.Spec); j++ {
v := &spec.Spec[j] // Note: need pointer
for i := len(v.Children) - 1; i >= 0; i-- {
if !v.Children[i].Selected {
// https://vbauerster.github.io/2017/04/removing-items-from-a-slice-while-iterating-in-go/
v.Children = append(v.Children[:i], v.Children[i+1:]...)
}
}
}

specData, err := json.Marshal(spec)
if err == nil {
product.SpecProps = string(specData)
}
}

Converting Go struct to JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
"fmt"
"encoding/json"
)

type User struct {
name string
}

func main() {
user := &User{name:"Frank"}
b, err := json.Marshal(user)
if err != nil {
fmt.Printf("Error: %s", err)
return;
}
fmt.Println(string(b))
}

Go json.Unmarshal array

  • Decode top level JSON array into a slice of structs in golang
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    package main

    import "fmt"
    import "encoding/json"

    type PublicKey struct {
    Id int
    Key string
    }

    type KeysResponse struct {
    Collection []PublicKey
    }

    func main() {
    keysBody := []byte(`[{"id": 1,"key": "-"},{"id": 2,"key": "-"},{"id": 3,"key": "-"}]`)
    keys := make([]PublicKey,0)
    json.Unmarshal(keysBody, &keys)
    fmt.Printf("%#v", keys)
    }