Configuration
Fory Go uses a functional options pattern for configuration. This allows you to customize serialization behavior while maintaining sensible defaults.
Creating a Fory Instance
Default Configuration
import "github.com/apache/fory/go/fory"
f := fory.New()
Default settings:
| Option | Default | Description |
|---|---|---|
| TrackRef | false | Reference tracking disabled |
| MaxDepth | 20 | Maximum nesting depth |
| IsXlang | false | Cross-language mode disabled |
| Compatible | false | Schema evolution mode disabled |
With Options
f := fory.New(
fory.WithTrackRef(true),
fory.WithCompatible(true),
fory.WithMaxDepth(10),
)
Configuration Options
WithTrackRef
Enable reference tracking to handle circular references and shared objects:
f := fory.New(fory.WithTrackRef(true))
When enabled:
- Objects appearing multiple times are serialized once
- Circular references are handled correctly
- Per-field
fory:"ref"tags take effect - Adds overhead for tracking object identity
When disabled (default):
- Each object occurrence is serialized independently
- Circular references cause stack overflow or max depth error
- Per-field
fory:"ref"tags are ignored - Better performance for simple data structures
Use reference tracking when:
- Data contains circular references
- Same object is referenced multiple times
- Serializing graph structures (trees with parent pointers, linked lists with cycles)
See References for details.
WithCompatible
Enable compatible mode for schema evolution:
f := fory.New(fory.WithCompatible(true))
When enabled:
- Type metadata is written to serialized data
- Supports adding/removing fields between versions
- Field names or ids are used for matching (order-independent)
- Larger serialized output due to metadata
When disabled (default):
- Compact serialization without field metadata
- Faster serialization and smaller output
- Fields matched by sorted order
- Requires consistent struct definitions across all services
See Schema Evolution for details.
WithMaxDepth
Set the maximum nesting depth to prevent stack overflow:
f := fory.New(fory.WithMaxDepth(30))
- Default: 20
- Protects against deeply nested, recursive structures or malicious data
- Serialization fails with error when exceeded
WithXlang
Enable cross-language serialization mode:
f := fory.New(fory.WithXlang(true))
When enabled:
- Uses cross-language type system
- Compatible with Java, Python, C++, Rust, JavaScript
- Type IDs follow xlang specification
When disabled (default):
- Go-native serialization mode
- Support more Go-native types
- Not compatible with other language implementations
Thread Safety
The default Fory instance is NOT thread-safe. For concurrent use, use the thread-safe wrapper:
import "github.com/apache/fory/go/fory/threadsafe"
// Create thread-safe Fory with same options
f := threadsafe.New(
fory.WithTrackRef(true),
fory.WithCompatible(true),
)
// Safe for concurrent use from multiple goroutines
go func() {
data, _ := f.Serialize(value1)
// data is already copied, safe to use after return
}()
go func() {
data, _ := f.Serialize(value2)
}()
The thread-safe wrapper:
- Uses
sync.Poolinternally for efficient instance reuse - Automatically copies serialized data before returning
- Accepts the same configuration options as
fory.New()
Global Thread-Safe Instance
For convenience, the threadsafe package provides global functions:
import "github.com/apache/fory/go/fory/threadsafe"
// Uses a global thread-safe instance with default configuration
data, err := threadsafe.Marshal(&myValue)
err = threadsafe.Unmarshal(data, &result)