mholt/json-to-go

cannot unmarshal number <very long number> into Go struct field ... of type int64

zen37 opened this issue · 7 comments

zen37 commented

https://ifconfig.co/json returns "ip_decimal":20 digits number
It is translated into int64, it should have been float64 to avoid the error.

Some edits to make it clearer.

mholt commented

Why? It's an integer...

zen37 commented

there was no error when changing the data type of the Go struct field from int64 to float64

mholt commented

That doesn't mean it is right? Anyway, closing... nothing to do here. You're free to change it yourself in your program.

zen37 commented

GO int64 is 19 digits.
If JSON number is > 19 digits convert to float64, if not leave it as it is now, int64.

mholt commented

The number is only 10 digits long.

zen37 commented

The number retrieved in https://ifconfig.co/json is 20 digits long.

mholt commented

It's 10 for me, but I see you just edited your original post to specify 20 digits. Maybe you are using IPv6.

A 64 bit integer has more unique values than a 64 bit float.

Using floats for integer values will be wrong for most users. If you require floats, just change it in your code.