package main
import (
“encoding/json”
“log”
)
type C struct {
}
type S struct {
A []string
B []*C
}
func main() {
data := &S{}
data.B=[]C{}
data2 := &S{A: []string{},B :[]C{}}
buf, err := json.Marshal(&data)
log.Println(string(buf), err)
buf2, err2 := json.Marshal(&data2)
log.Println(string(buf2), err2)
}
变量值为nil时,json编码后就会输出null,可以赋值一个空的slice,什么类型都可以,编码后就会生成[]
https://www.calhoun.io/how-to-determine-if-a-json-key-has-been-set-to-null-or-not-provided/
https://ethancai.github.io/2016/06/23/bad-parts-about-json-serialization-in-Golang/
https://studygolang.com/articles/21872