Skip to content

Instantly share code, notes, and snippets.

@eraserhd
Created January 4, 2024 17:35
Show Gist options
  • Save eraserhd/d789e5d291cbcc38cd037abcceb134f4 to your computer and use it in GitHub Desktop.
Save eraserhd/d789e5d291cbcc38cd037abcceb134f4 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"math/rand"
)
type T struct {
Value int64
}
func main() {
var foo T
foo.Value = rand.Int63()
data, err := json.Marshal(foo)
if err != nil {
fmt.Printf("err1:%v\n", err)
return
}
var bar map[string]any
if err := json.Unmarshal(data, &bar); err != nil {
fmt.Printf("err2:%v\n", err)
return
}
data2, err := json.Marshal(bar)
if err != nil {
fmt.Printf("err3:%v\n", err)
return
}
var baz T
if err := json.Unmarshal(data2, &baz); err != nil {
fmt.Printf("err4:%v\n", err)
return
}
fmt.Printf("foo.Value = %d, bar.Value = %v, baz.Value = %d, %v\n", foo.Value, bar["Value"], baz.Value, foo.Value == baz.Value)
}
@eraserhd
Copy link
Author

eraserhd commented Jan 4, 2024

12:33:26 0 jfelice@C02FV0KUQ05Q nexus (jf-in-545)
ᐅ go run /tmp/foo.go
foo.Value = 403136368954160975, bar.Value = 4.0313636895416096e+17, baz.Value = 403136368954160960, false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment