跳到主要内容
版本:dev

Supported Types

Fory Go supports a wide range of Go types for serialization. This guide covers all supported types and their cross-language mappings.

Primitive Types

Go TypeFory TypeIdEncodingNotes
boolBOOL (1)1 byte
int8INT8 (2)1 byte, signed
int16INT16 (3)2 bytes, signedLittle-endian
int32INT32 (4)VarintVariable-length encoding
int64INT64 (6)VarintVariable-length encoding
intINT32/INT64VarintPlatform-dependent (32 or 64 bit)
uint8 / byteUINT8 (9)1 byte, unsigned
uint16UINT16 (10)2 bytes, unsignedLittle-endian
uint32UINT32 (11)VaruintVariable-length encoding
uint64UINT64 (13)VaruintVariable-length encoding
float32FLOAT32 (17)4 bytesIEEE 754
float64FLOAT64 (18)8 bytesIEEE 754
stringSTRING (19)Length-prefixed UTF-8

Integer Encoding

Fory uses variable-length integer encoding (varint) for better compression:

  • Small values use fewer bytes
  • Negative values use ZigZag encoding
  • Platform int maps to int32 on 32-bit, int64 on 64-bit systems
f := fory.New()

// All integer types supported
var i8 int8 = 127
var i16 int16 = 32767
var i32 int32 = 2147483647
var i64 int64 = 9223372036854775807

data, _ := f.Serialize(i64) // Uses varint encoding

Collection Types

Slices

Go TypeFory TypeIdNotes
[]boolBOOL_ARRAYOptimized encoding
[]int8INT8_ARRAYOptimized encoding
[]int16INT16_ARRAYOptimized encoding
[]int32INT32_ARRAYOptimized encoding
[]int64INT64_ARRAYOptimized encoding
[]float32FLOAT32_ARRAYOptimized encoding
[]float64FLOAT64_ARRAYOptimized encoding
[]stringLISTGeneric list encoding
[]T (any)LIST (20)Any serializable type
[]I (any/any)LISTAny interface type
f := fory.New()

// Primitive slices (optimized)
ints := []int32{1, 2, 3, 4, 5}
data, _ := f.Serialize(ints)

// String slices
strs := []string{"a", "b", "c"}
data, _ = f.Serialize(strs)

// Struct slices
users := []User{{ID: 1}, {ID: 2}}
data, _ = f.Serialize(users)

// Dynamic slices
dynamic := []any{1, "hello", true}
data, _ = f.Serialize(dynamic)

Maps

Go TypeFory TypeIdNotes
map[string]stringMAP (22)Optimized
map[string]int64MAPOptimized
map[string]int32MAPOptimized
map[string]intMAPOptimized
map[string]float64MAPOptimized
map[string]boolMAPOptimized
map[int32]int32MAPOptimized
map[int64]int64MAPOptimized
map[int]intMAPOptimized
map[string]anyMAPDynamic values
map[any]anyMAPDynamic keys and values
f := fory.New()

// String key maps
m1 := map[string]string{"key": "value"}
m2 := map[string]int64{"count": 42}

// Integer key maps
m3 := map[int32]int32{1: 100, 2: 200}

// Dynamic maps
m4 := map[string]any{
"name": "Alice",
"age": int64(30),
}

Sets

Fory provides a generic Set[T] type (uses map[T]struct{} for zero memory overhead):

// Create a set of strings
s := fory.NewSet[string]()
s.Add("a", "b", "c")

// Check membership
if s.Contains("a") {
fmt.Println("found")
}

// Serialize
data, _ := f.Serialize(s)

Time Types

Go TypeFory TypeIdNotes
time.TimeTIMESTAMP (34)Nanosecond precision
time.DurationDURATION (33)Nanosecond precision
import "time"

f := fory.New()

// Timestamp
t := time.Now()
data, _ := f.Serialize(t)

// Duration
d := 5 * time.Second
data, _ = f.Serialize(d)

Struct Types

CategoryFory TypeIdNotes
StructSTRUCT (25)Registered by ID, no evolution
Compatible StructCOMPATIBLE_STRUCT (26)With schema evolution
Named StructNAMED_STRUCT (27)Registered by name, no evolution
Named Compatible StructNAMED_COMPATIBLE_STRUCT (28)Named with schema evolution

Struct Requirements

  1. Exported fields only: Fields starting with uppercase are serialized
  2. Supported field types: All types listed in this document
  3. Registration: Structs should be registered for cross-language use
type User struct {
ID int64 // Serialized
Name string // Serialized
Age int32 // Serialized
password string // NOT serialized (unexported)
}

f := fory.New()
f.RegisterStruct(User{}, 1)

user := &User{ID: 1, Name: "Alice", Age: 30, password: "secret"}
data, _ := f.Serialize(user)

Nested Structs

type Address struct {
Street string
City string
Country string
}

type Company struct {
Name string
Address Address
Founded int32
}

f := fory.New()
f.RegisterStruct(Address{}, 1)
f.RegisterStruct(Company{}, 2)

Pointer Types

Go TypeBehavior
*TNil-able, reference tracked (if enabled)
**TNested pointers supported
f := fory.New(fory.WithTrackRef(true))

type Node struct {
Value int32
Left *Node
Right *Node
}

f.RegisterStruct(Node{}, 1)

root := &Node{
Value: 1,
Left: &Node{Value: 2},
Right: &Node{Value: 3},
}

data, _ := f.Serialize(root)

Nil Handling

var ptr *User = nil
data, _ := f.Serialize(ptr)

var result *User
f.Deserialize(data, &result)
// result == nil

Interface Types

Go TypeFory TypeIdNotes
anyUNION (31)Polymorphic values
f := fory.New()

// Serialize any
var value any = "hello"
data, _ := f.Serialize(value)

var result any
f.Deserialize(data, &result)
// result = "hello" (string)

For struct interfaces, register all possible concrete types:

type Shape interface {
Area() float64
}

type Circle struct {
Radius float64
}

func (c Circle) Area() float64 {
return 3.14159 * c.Radius * c.Radius
}

f := fory.New()
f.RegisterStruct(Circle{}, 1)

var shape Shape = Circle{Radius: 5.0}
data, _ := f.Serialize(shape)

Binary Data

Go TypeFory TypeIdNotes
[]byteBINARY (37)Variable-length bytes
f := fory.New()

data := []byte{0x01, 0x02, 0x03, 0x04}
serialized, _ := f.Serialize(data)

var result []byte
f.Deserialize(serialized, &result)

Enum Types

Go uses integer types for enums:

type Status int32

const (
StatusPending Status = 0
StatusActive Status = 1
StatusComplete Status = 2
)

f := fory.New()
f.RegisterEnum(Status(0), 1)

status := StatusActive
data, _ := f.Serialize(status)

Cross-Language Type Mapping

Go TypeJavaPythonC++Rust
boolbooleanboolboolbool
int8byteintint8_ti8
int16shortintint16_ti16
int32intintint32_ti32
int64longintint64_ti64
float32floatfloatfloatf32
float64doublefloatdoublef64
stringStringstrstd::stringString
[]TList<T>liststd::vector<T>Vec<T>
map[K]VMap<K,V>dictstd::unordered_mapHashMap<K,V>
time.TimeInstantdatetime--
time.DurationDurationtimedelta--

See Cross-Language Serialization for detailed mapping.

Unsupported Types

The following Go types are not supported:

  • Channels (chan T)
  • Functions (func())
  • Complex numbers (complex64, complex128)
  • Unsafe pointers (unsafe.Pointer)

Attempting to serialize these types will result in an error.