本文整理匯總了Golang中github.com/google/flatbuffers/go.Builder.Head方法的典型用法代碼示例。如果您正苦於以下問題:Golang Builder.Head方法的具體用法?Golang Builder.Head怎麽用?Golang Builder.Head使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/google/flatbuffers/go.Builder
的用法示例。
在下文中一共展示了Builder.Head方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: serializeByFlatBuffers
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():]
}
示例2: MakeMonster
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():]
}
示例3: MakeUser
// 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():]
}
示例4: serializeUsingFlatBuffers
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():]
}
示例5: CheckByteLayout
// CheckByteLayout verifies the bytes of a Builder in various scenarios.
func CheckByteLayout(fail func(string, ...interface{})) {
var b *flatbuffers.Builder
var i int
check := func(want []byte) {
i++
got := b.Bytes[b.Head():]
if !bytes.Equal(want, got) {
fail("case %d: want\n%v\nbut got\n%v\n", i, want, got)
}
}
// test 1: numbers
b = flatbuffers.NewBuilder(0)
check([]byte{})
b.PrependBool(true)
check([]byte{1})
b.PrependInt8(-127)
check([]byte{129, 1})
b.PrependUint8(255)
check([]byte{255, 129, 1})
b.PrependInt16(-32222)
check([]byte{0x22, 0x82, 0, 255, 129, 1}) // first pad
b.PrependUint16(0xFEEE)
check([]byte{0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1}) // no pad this time
b.PrependInt32(-53687092)
check([]byte{204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1})
b.PrependUint32(0x98765432)
check([]byte{0x32, 0x54, 0x76, 0x98, 204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1})
// test 1b: numbers 2
b = flatbuffers.NewBuilder(0)
b.PrependUint64(0x1122334455667788)
check([]byte{0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11})
// test 2: 1xbyte vector
b = flatbuffers.NewBuilder(0)
check([]byte{})
b.StartVector(flatbuffers.SizeByte, 1, 1)
check([]byte{0, 0, 0}) // align to 4bytes
b.PrependByte(1)
check([]byte{1, 0, 0, 0})
b.EndVector(1)
check([]byte{1, 0, 0, 0, 1, 0, 0, 0}) // padding
// test 3: 2xbyte vector
b = flatbuffers.NewBuilder(0)
b.StartVector(flatbuffers.SizeByte, 2, 1)
check([]byte{0, 0}) // align to 4bytes
b.PrependByte(1)
check([]byte{1, 0, 0})
b.PrependByte(2)
check([]byte{2, 1, 0, 0})
b.EndVector(2)
check([]byte{2, 0, 0, 0, 2, 1, 0, 0}) // padding
// test 3b: 11xbyte vector matches builder size
b = flatbuffers.NewBuilder(12)
b.StartVector(flatbuffers.SizeByte, 8, 1)
start := []byte{}
check(start)
for i := 1; i < 12; i++ {
b.PrependByte(byte(i))
start = append([]byte{byte(i)}, start...)
check(start)
}
b.EndVector(8)
check(append([]byte{8, 0, 0, 0}, start...))
// test 4: 1xuint16 vector
b = flatbuffers.NewBuilder(0)
b.StartVector(flatbuffers.SizeUint16, 1, 1)
check([]byte{0, 0}) // align to 4bytes
b.PrependUint16(1)
check([]byte{1, 0, 0, 0})
b.EndVector(1)
check([]byte{1, 0, 0, 0, 1, 0, 0, 0}) // padding
// test 5: 2xuint16 vector
b = flatbuffers.NewBuilder(0)
b.StartVector(flatbuffers.SizeUint16, 2, 1)
check([]byte{}) // align to 4bytes
b.PrependUint16(0xABCD)
check([]byte{0xCD, 0xAB})
b.PrependUint16(0xDCBA)
check([]byte{0xBA, 0xDC, 0xCD, 0xAB})
b.EndVector(2)
check([]byte{2, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB})
// test 6: CreateString
b = flatbuffers.NewBuilder(0)
//.........這裏部分代碼省略.........