Go 设置
Fory Go 提供 xlang 和 native 对象序列化、生成的模型以及 Fory gRPC。它以 github.com/apache/fory/go/fory 发布,需要 Go 1.25 或更高版本。
验证工具链
go version
go env GOPROXY
对象序列化
创建 module 并安装已发布的 Fory module:
mkdir fory-example
cd fory-example
go mod init example.com/fory-example
go get github.com/apache/fory/go/fory@v1.5.0
如果 Go proxy 尚未收录新的 submodule tag,请稍后重试,或者暂时使用 GOPROXY=direct。
main.go
package main
import (
"fmt"
"github.com/apache/fory/go/fory"
)
type User struct {
ID int64
Name string
}
func main() {
f := fory.New(fory.WithXlang(true))
if err := f.RegisterStruct(User{}, 1); err != nil {
panic(err)
}
bytes, err := f.Serialize(&User{ID: 1, Name: "Alice"})
if err != nil {
panic(err)
}
var decoded User
if err := f.Deserialize(bytes, &decoded); err != nil {
panic(err)
}
fmt.Println(decoded.Name)
}
go run .
跨语言数据请使用 xlang 模式,仅供 Go 使用的数据请使用 native 模式。接下来可阅读 Go 对象序列化、配置和 Schema 演进。