Closed decenfrontier closed 2 years ago
I found that empty slices became null after httpx called json.Marshal.
Give a simple enough and runnable code to reproduce it?
This does not seem to be the bug of gozero. Json.Marshal automatically changes slices of unallocated memory into null.
Please forgive me for being a beginner in go and I don't quite understand the details of the go language.
package main
import (
"encoding/json"
"fmt"
)
type Body struct {
Code uint32 `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func main() {
var data []string
fmt.Println(data == nil) // true
var body = Body{
Code: 0,
Msg: "获取消息成功",
Data: data,
}
bytes, err := json.Marshal(body)
if err != nil {
panic(err)
}
print(string(bytes)) // {"code":0,"msg":"获取消息成功","data":null}
}
Describe the bug The code here is the API side, you can see, If empty slice returns json, it will become null.
you can see, the pbPullRepsonse.List is also an empty slice, and after copier.Copy, it still empty slice, but when it is serialized to json, the empty slice becomes null
To Reproduce PS: If you want to see the complete code, it is open source at https://github.com/CPALyth/ws_chat
Expected behavior If empty slice returns json, it will become [].
Environments