-
I am learning. My environment: Windows 11 x64, Visual Studio Code, GoLand, Go lang 1.20.2 . I read ByteDance Sonic File module github.com/donhuvy/vy_learn_sonic
go 1.20
require github.com/bytedance/sonic v1.8.6
require (
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/sys v0.6.0 // indirect
) File package main
import "github.com/bytedance/sonic"
var data YourSchema
// Marshal
output, err := sonic.Marshal(&data)
// Unmarshal
err := sonic.Unmarshal(output, &data) Second error: Just copy and paste, but error How to fix these errors? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
It's a code snippet, you can take a look at this example for a complete one - basically just like // You can edit this code!
// Click here and start typing.
package main
import (
"fmt"
"github.com/bytedance/sonic"
)
// Your schema example
type MySchema struct {
Name string `json:"name"`
Age int `json:"age,string"`
}
func main() {
data := []*MySchema{
{"Foo", 20},
{"Bar", 21},
{"Baz", 22},
}
jsonData, err := sonic.Marshal(&data)
if err != nil {
// handle error, but in this case we are just going to throw it with panic
panic(err)
}
fmt.Println(string(jsonData))
} |
Beta Was this translation helpful? Give feedback.
-
Help me fix this error with unmarshall (I am newbie) package main
import (
"fmt"
"github.com/bytedance/sonic"
)
// Friend schema example.
type Friend struct {
Name string `json:"name"`
Age int `json:"age,string"`
}
func main() {
vyFriends := []*Friend{
{"Nguyen Bich Van", 36},
{"Nguyen Tien Nam", 36},
{"Tran Phuong Ly", 29},
}
jsonData, err := sonic.Marshal(&vyFriends)
if err != nil {
// Handle error, but in this case we are just going to throw it with panic.
panic(err)
}
fmt.Println(string(jsonData))
err2 := sonic.Unmarshal(jsonData, &Friend)
fmt.Println(err2)
} |
Beta Was this translation helpful? Give feedback.
It's a code snippet, you can take a look at this example for a complete one - basically just like
encoding/json
: