func serializeByFlatBuffers(builder *flatbuffers.Builder, cg *ColorGroup) []byte { builder.Reset() //prepare data name := builder.CreateString(cg.Name) //prepare colors array colorsLen := len(cg.Colors) offsets := make([]flatbuffers.UOffsetT, colorsLen) for i := colorsLen - 1; i >= 0; i-- { offsets[i] = builder.CreateString(cg.Colors[i]) } FlatBufferColorGroupStartColorsVector(builder, colorsLen) for i := colorsLen - 1; i >= 0; i-- { builder.PrependUOffsetT(offsets[i]) } offset := builder.EndVector(colorsLen) FlatBufferColorGroupStart(builder) FlatBufferColorGroupAddCgId(builder, int32(cg.Id)) FlatBufferColorGroupAddName(builder, name) FlatBufferColorGroupAddColors(builder, offset) builder.Finish(FlatBufferColorGroupEnd(builder)) return builder.Bytes[builder.Head():] }
func MakeMonster(builder *flatbuffers.Builder) []byte { builder.Reset() name := []byte("One Sample Monster") name_position := builder.CreateByteString(name) example.MonsterStart(builder) example.MonsterAddPos(builder, example.CreateVec3(builder, 1.0, 2.0, 3.0)) example.MonsterAddHp(builder, 80) example.MonsterAddName(builder, name_position) monster_position := example.MonsterEnd(builder) builder.Finish(monster_position) return builder.Bytes[builder.Head():] }
// main.go part 2 of 4 func MakeUser(b *flatbuffers.Builder, name []byte, id uint64) []byte { // re-use the already-allocated Builder: b.Reset() // create the name object and get its offset: name_position := b.CreateByteString(name) // write the User object: users.UserStart(b) users.UserAddName(b, name_position) users.UserAddId(b, id) user_position := users.UserEnd(b) // finish the write operations by our User the root object: b.Finish(user_position) // return the byte slice containing encoded data: return b.Bytes[b.Head():] }
func serializeUsingFlatBuffers(builder *flatbuffers.Builder, a *A) []byte { builder.Reset() name := builder.CreateString(a.Name) phone := builder.CreateString(a.Phone) FlatBufferAStart(builder) FlatBufferAAddName(builder, name) FlatBufferAAddPhone(builder, phone) FlatBufferAAddBirthDay(builder, a.BirthDay.Unix()) FlatBufferAAddSiblings(builder, int32(a.Siblings)) var spouse byte if a.Spouse { spouse = byte(1) } FlatBufferAAddSpouse(builder, spouse) FlatBufferAAddMoney(builder, a.Money) builder.Finish(FlatBufferAEnd(builder)) return builder.Bytes[builder.Head():] }