使用字符串时很容易得到一个空列表[]string{}:
import (
"encoding/json"
"fmt"
)
func main() {
slice1 := []string{} // non-nil but zero-length
json1, _ := json.Marshal(slice1)
fmt.Printf("%s\n", json1) // []
}
上面代码的输出是[],但是当我[]byte使用[]byte{}returns 时""。我应该如何获得像我进入的那样的空列表[]string{}?
import (
"encoding/json"
"fmt"
)
func main() {
slice2 := []byte{}
json2, _ := json.Marshal(slice2)
fmt.Printf("%s\n", json2) // ""
}
子衿沉夜
相关分类