Created
January 4, 2024 17:35
-
-
Save eraserhd/d789e5d291cbcc38cd037abcceb134f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Author
eraserhd
commented
Jan 4, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment