go
go copied to clipboard
*int decode as string has error
type P struct {
ID *uint64 `json:"id,string"`
}
func main() {
var p = P {
ID: nil,
}
js, err := json.Marshal(p)
if err!=nil {
fmt.Println(err.Error())
}
fmt.Printf("p = %s\n", js)
}
output as follow:
p = {"id":"null"}
how decode nil pointer to null?
I'm not sure if I misunderstood your issue, but it seems like it does exactly what it's supposed to do.
You pass in a nil ptr, which evaluates to nil anyway. The string representation of nil is "null", so that's what gets returned. Hence, ID is "null".
So, what's the problem here?