| title | Go Serialization Guide |
|---|---|
| sidebar_position | 0 |
| id | index |
| license | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
Apache Fory Go is a high-performance serialization library for Go. It supports xlang mode for cross-language payloads and native mode for Go-only payloads, with automatic object graph serialization, circular references, polymorphism, and schema-aware serializers.
- High Performance: Fast serialization and optimized binary protocols
- Xlang: Seamless data exchange with Java, Python, C++, Rust, and JavaScript
- Automatic Serialization: No IDL definitions or schema compilation required
- Reference Tracking: Built-in support for circular references and shared objects
- Type Safety: Strong typing with schema-aware serializers
- Schema Evolution: Compatible mode for forward/backward compatibility
- Thread-Safe Option: Pool-based thread-safe wrapper for concurrent use
Requirements: Go 1.24 or later
go get github.com/apache/fory/go/forypackage main
import (
"fmt"
"github.com/apache/fory/go/fory"
)
type User struct {
ID int64
Name string
Age int32
}
func main() {
// Create an xlang Fory instance.
f := fory.New(fory.WithXlang(true))
// Register struct with a type ID
if err := f.RegisterStruct(User{}, 1); err != nil {
panic(err)
}
// Serialize
user := &User{ID: 1, Name: "Alice", Age: 30}
data, err := f.Serialize(user)
if err != nil {
panic(err)
}
// Deserialize
var result User
if err := f.Deserialize(data, &result); err != nil {
panic(err)
}
fmt.Printf("Deserialized: %+v\n", result)
// Output: Deserialized: {ID:1 Name:Alice Age:30}
}Use xlang mode for cross-language payloads and schemas shared with other Fory runtimes. Xlang mode is the default Go wire mode, and Go examples that use it set fory.WithXlang(true) explicitly so the mode choice is visible.
Use native mode for Go-only traffic. Native mode is selected with fory.WithXlang(false), uses schema-consistent payloads unless compatible mode is enabled, and keeps Go object serialization on the Go runtime path. It is optimized for Go structs, pointers, interfaces, and Go-specific type behavior that does not need a portable xlang mapping.
See Xlang Serialization for Go xlang registration and interoperability rules, and Native Serialization for Go-only payloads.
Fory Go uses a functional options pattern for configuration:
f := fory.New(
fory.WithXlang(true),
fory.WithTrackRef(true), // Enable reference tracking
fory.WithMaxDepth(20), // Set max nesting depth
)See Configuration for all available options.
Fory Go supports a wide range of types:
- Primitives:
bool,int8-int64,uint8-uint64,float32,float64,string - Collections: slices, maps, sets
- Time:
time.Time,time.Duration - Pointers: pointer types with automatic nil handling
- Structs: any struct with exported fields
See Supported Types for the complete type mapping.
Fory Go is fully compatible with other Fory implementations. Data serialized in Go can be deserialized in Java, Python, C++, Rust, or JavaScript:
// Go serialization
f := fory.New(fory.WithXlang(true))
f.RegisterStruct(User{}, 1)
data, _ := f.Serialize(&User{ID: 1, Name: "Alice"})
// 'data' can be deserialized by Java, Python, etc.See Xlang Serialization for type mapping and compatibility details.
| Topic | Description |
|---|---|
| Basic Serialization | Core APIs and usage patterns |
| Xlang Serialization | Multi-language serialization |
| Native Serialization | Go-only serialization |
| Configuration | Options and settings |
| Schema Metadata | Field-level configuration |
| Type Registration | Registering types for serialization |
| Supported Types | Complete type support reference |
| References | Circular references and shared objects |
| Schema Evolution | Forward/backward compatibility |
| Custom Serializers | Extend serialization behavior |
| Thread Safety | Concurrent usage patterns |
| Troubleshooting | Common issues and solutions |