go icon indicating copy to clipboard operation
go copied to clipboard

*int decode as string has error

Open gatspy opened this issue 4 years ago • 1 comments

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?

gatspy avatar Nov 13 '21 13:11 gatspy

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?

Sculas avatar Dec 19 '21 00:12 Sculas