• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang go.NewBuilder函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/google/flatbuffers/go.NewBuilder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewBuilder函数的具体用法?Golang NewBuilder怎么用?Golang NewBuilder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewBuilder函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: makeNoticeMsg

func makeNoticeMsg(str string, p uint16) []byte {
	t := transport.TcpMessage{}

	builder := flatbuffers.NewBuilder(0)

	ct := builder.CreateString(str)
	proto.NoticeStart(builder)
	proto.NoticeAddContent(builder, ct)
	payload := proto.NoticeEnd(builder)

	builder.Finish(payload)

	t.Payload = builder.Bytes[builder.Head():]

	// 填充协议头信息
	t.Header.Proto = p
	t.Header.Flag = 0xdcba
	t.Header.Size = uint16(len(t.Payload))

	ret, err := t.Pack()
	if err != nil {
		log.Fatal(err.Error())
		return nil
	}
	return ret
}
开发者ID:pengqiuyuan,项目名称:gomiddle,代码行数:26,代码来源:placard_controller.go


示例2: ToAndFromFlat

func ToAndFromFlat(uids []uint64) (error, int) {
	b := flatbuffers.NewBuilder(0)
	fuids.UidListStartUidsVector(b, len(uids))
	for i := len(uids) - 1; i >= 0; i-- {
		b.PrependUint64(uids[i])
	}
	ve := b.EndVector(len(uids))
	fuids.UidListStart(b)
	fuids.UidListAddUids(b, ve)
	ue := fuids.UidListEnd(b)
	b.Finish(ue)
	data := b.FinishedBytes()

	nl := fuids.GetRootAsUidList(data, 0)
	if nl.UidsLength() != len(uids) {
		return fmt.Errorf("Length doesn't match"), 0
	}
	for i := 0; i < len(uids); i++ {
		if nl.Uids(i) != uids[i] {
			return fmt.Errorf("ID doesn't match at index: %v Expected: %v. Got: %v",
				i, uids[i], nl.Uids(i)), 0
		}
	}

	return nil, len(data)
}
开发者ID:dgraph-io,项目名称:experiments,代码行数:26,代码来源:flats.go


示例3: BenchmarkVtableDeduplication

// BenchmarkVtableDeduplication measures the speed of vtable deduplication
// by creating prePop vtables, then populating b.N objects with a
// different single vtable.
//
// When b.N is large (as in long benchmarks), memory usage may be high.
func BenchmarkVtableDeduplication(b *testing.B) {
	prePop := 10
	builder := flatbuffers.NewBuilder(0)

	// pre-populate some vtables:
	for i := 0; i < prePop; i++ {
		builder.StartObject(i)
		for j := 0; j < i; j++ {
			builder.PrependInt16Slot(j, int16(j), 0)
		}
		builder.EndObject()
	}

	// benchmark deduplication of a new vtable:
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		lim := prePop

		builder.StartObject(lim)
		for j := 0; j < lim; j++ {
			builder.PrependInt16Slot(j, int16(j), 0)
		}
		builder.EndObject()
	}
}
开发者ID:bunnyblue,项目名称:flatbuffers,代码行数:30,代码来源:go_test.go


示例4: BenchmarkBuildGold

// BenchmarkBuildGold uses generated code to build the example Monster.
func BenchmarkBuildGold(b *testing.B) {
	buf, offset := CheckGeneratedBuild(b.Fatalf)
	bytes_length := int64(len(buf[offset:]))

	reuse_str := "MyMonster"
	reuse_test1 := "test1"
	reuse_test2 := "test2"
	reuse_fred := "Fred"

	b.SetBytes(bytes_length)
	bldr := flatbuffers.NewBuilder(0)
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		bldr.Reset()

		str := bldr.CreateString(reuse_str)
		test1 := bldr.CreateString(reuse_test1)
		test2 := bldr.CreateString(reuse_test2)
		fred := bldr.CreateString(reuse_fred)

		example.MonsterStartInventoryVector(bldr, 5)
		bldr.PrependByte(4)
		bldr.PrependByte(3)
		bldr.PrependByte(2)
		bldr.PrependByte(1)
		bldr.PrependByte(0)
		inv := bldr.EndVector(5)

		example.MonsterStart(bldr)
		example.MonsterAddName(bldr, fred)
		mon2 := example.MonsterEnd(bldr)

		example.MonsterStartTest4Vector(bldr, 2)
		example.CreateTest(bldr, 10, 20)
		example.CreateTest(bldr, 30, 40)
		test4 := bldr.EndVector(2)

		example.MonsterStartTestarrayofstringVector(bldr, 2)
		bldr.PrependUOffsetT(test2)
		bldr.PrependUOffsetT(test1)
		testArrayOfString := bldr.EndVector(2)

		example.MonsterStart(bldr)

		pos := example.CreateVec3(bldr, 1.0, 2.0, 3.0, 3.0, 2, 5, 6)
		example.MonsterAddPos(bldr, pos)

		example.MonsterAddHp(bldr, 80)
		example.MonsterAddName(bldr, str)
		example.MonsterAddInventory(bldr, inv)
		example.MonsterAddTestType(bldr, 1)
		example.MonsterAddTest(bldr, mon2)
		example.MonsterAddTest4(bldr, test4)
		example.MonsterAddTestarrayofstring(bldr, testArrayOfString)
		mon := example.MonsterEnd(bldr)

		bldr.Finish(mon)
	}
}
开发者ID:bunnyblue,项目名称:flatbuffers,代码行数:61,代码来源:go_test.go


示例5: BenchmarkFlatBuffersUnmarshal

func BenchmarkFlatBuffersUnmarshal(b *testing.B) {
	b.StopTimer()
	builder := flatbuffers.NewBuilder(0)
	data := generate()
	ser := make([][]byte, len(data))
	for i, d := range data {
		ser[i] = serializeUsingFlatBuffers(builder, d)
	}

	b.ReportAllocs()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		n := rand.Intn(len(ser))
		o := FlatBufferA{}
		sData := ser[n]
		o.Init(sData, flatbuffers.GetUOffsetT(sData))
		if validate != "" {
			i := data[n]
			spouseVal := o.Spouse() == byte(1)

			correct := o.Name() == i.Name &&
				o.Phone() == i.Phone &&
				int(o.Siblings()) == i.Siblings &&
				spouseVal == i.Spouse &&
				o.Money() == i.Money &&
				o.BirthDay() == i.BirthDay.Unix()
			if !correct {
				b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o)
			}
		}
	}
}
开发者ID:junchaowu,项目名称:go_serialization_benchmarks,代码行数:32,代码来源:serialization_benchmarks_test.go


示例6: CheckDocExample

// CheckDocExample checks that the code given in FlatBuffers documentation
// is syntactically correct.
func CheckDocExample(buf []byte, off flatbuffers.UOffsetT, fail func(string, ...interface{})) {
	monster := example.GetRootAsMonster(buf, off)
	_ = monster.Hp()
	_ = monster.Pos(nil)
	for i := 0; i < monster.InventoryLength(); i++ {
		_ = monster.Inventory(i) // do something here
	}

	builder := flatbuffers.NewBuilder(0)

	example.MonsterStartInventoryVector(builder, 5)
	for i := 4; i >= 0; i-- {
		builder.PrependByte(byte(i))
	}
	inv := builder.EndVector(5)

	str := builder.CreateString("MyMonster")
	example.MonsterStart(builder)
	example.MonsterAddPos(builder, example.CreateVec3(builder, 1.0, 2.0, 3.0, 3.0, 4, 5, 6))
	example.MonsterAddHp(builder, 80)
	example.MonsterAddName(builder, str)
	example.MonsterAddInventory(builder, inv)
	example.MonsterAddTestType(builder, 1)
	// example.MonsterAddTest(builder, mon2)
	// example.MonsterAddTest4(builder, test4s)
	_ = example.MonsterEnd(builder)
}
开发者ID:bunnyblue,项目名称:flatbuffers,代码行数:29,代码来源:go_test.go


示例7: BenchmarkMarshalByFlatBuffers

func BenchmarkMarshalByFlatBuffers(b *testing.B) {
	builder := flatbuffers.NewBuilder(0)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		serializeByFlatBuffers(builder, &group)
	}
}
开发者ID:jameswei,项目名称:gosercomp,代码行数:7,代码来源:gosercomp_test.go


示例8: BenchmarkUnmarshalByFlatBuffers

func BenchmarkUnmarshalByFlatBuffers(b *testing.B) {
	builder := flatbuffers.NewBuilder(0)
	bytes := serializeByFlatBuffers(builder, &group)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = GetRootAsFlatBufferColorGroup(bytes, 0)
	}
}
开发者ID:jameswei,项目名称:gosercomp,代码行数:8,代码来源:gosercomp_test.go


示例9: main

func main() {
	builder := flatbuffers.NewBuilder(0)
	buf := MakeMonster(builder)

	monster := example.GetRootAsMonster(buf, 0)
	monster_name := monster.Name()

	fmt.Printf("Monster named '%s' encoded in %d bytes.\n", monster_name, len(buf))
}
开发者ID:jforns,项目名称:flatbuffers-monster-sample,代码行数:9,代码来源:monster.go


示例10: CheckCreateByteVector

func CheckCreateByteVector(fail func(string, ...interface{})) {
	raw := [30]byte{}
	for i := 0; i < len(raw); i++ {
		raw[i] = byte(i)
	}

	for size := 0; size < len(raw); size++ {
		b1 := flatbuffers.NewBuilder(0)
		b2 := flatbuffers.NewBuilder(0)
		b1.StartVector(1, size, 1)
		for i := size - 1; i >= 0; i-- {
			b1.PrependByte(raw[i])
		}
		b1.EndVector(size)
		b2.CreateByteVector(raw[:size])
		CheckByteEquality(b1.Bytes, b2.Bytes, fail)
	}
}
开发者ID:bunnyblue,项目名称:flatbuffers,代码行数:18,代码来源:go_test.go


示例11: BenchmarkWrite

func BenchmarkWrite(b *testing.B) {
	builder := flatbuffers.NewBuilder(0)
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		builder.Reset()
		buf := MakeUser(builder, []byte("Arthur Dent"), 42)
		if i == 0 {
			b.SetBytes(int64(len(buf)))
		}
	}
}
开发者ID:rw,项目名称:go-flatbuffers-example,代码行数:11,代码来源:main_test.go


示例12: CheckStructIsNotInlineError

// CheckStructIsNotInlineError verifies that writing a struct in a location
// away from where it is used will cause a panic.
func CheckStructIsNotInlineError(fail func(string, ...interface{})) {
	b := flatbuffers.NewBuilder(0)
	b.StartObject(0)
	defer func() {
		r := recover()
		if r == nil {
			fail("expected panic in CheckStructIsNotInlineError")
		}
	}()
	b.PrependStructSlot(0, 1, 0)
}
开发者ID:bunnyblue,项目名称:flatbuffers,代码行数:13,代码来源:go_test.go


示例13: CheckFinishedBytesError

// CheckFinishedBytesError verifies that `FinishedBytes` panics if the table
// is not finished.
func CheckFinishedBytesError(fail func(string, ...interface{})) {
	b := flatbuffers.NewBuilder(0)

	defer func() {
		r := recover()
		if r == nil {
			fail("expected panic in CheckFinishedBytesError")
		}
	}()
	b.FinishedBytes()
}
开发者ID:google,项目名称:flatbuffers,代码行数:13,代码来源:go_test.go


示例14: BenchmarkFlatbuffersMarshal

func BenchmarkFlatbuffersMarshal(b *testing.B) {
	b.StopTimer()
	data := generate()
	builder := flatbuffers.NewBuilder(0)
	b.ReportAllocs()
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		serializeUsingFlatBuffers(builder, data[rand.Intn(len(data))])
	}
}
开发者ID:junchaowu,项目名称:go_serialization_benchmarks,代码行数:11,代码来源:serialization_benchmarks_test.go


示例15: CheckByteStringIsNestedError

// CheckByteStringIsNestedError verifies that a bytestring can not be created
// inside another object.
func CheckByteStringIsNestedError(fail func(string, ...interface{})) {
	b := flatbuffers.NewBuilder(0)
	b.StartObject(0)
	defer func() {
		r := recover()
		if r == nil {
			fail("expected panic in CheckByteStringIsNestedError")
		}
	}()
	b.CreateByteString([]byte("foo"))
}
开发者ID:bunnyblue,项目名称:flatbuffers,代码行数:13,代码来源:go_test.go


示例16: CheckObjectIsNestedError

// CheckObjectIsNestedError verifies that an object can not be created inside
// another object.
func CheckObjectIsNestedError(fail func(string, ...interface{})) {
	b := flatbuffers.NewBuilder(0)
	b.StartObject(0)
	defer func() {
		r := recover()
		if r == nil {
			fail("expected panic in CheckObjectIsNestedError")
		}
	}()
	b.StartObject(0)
}
开发者ID:bunnyblue,项目名称:flatbuffers,代码行数:13,代码来源:go_test.go


示例17: CheckNotInObjectError

// CheckNotInObjectError verifies that `EndObject` fails if not inside an
// object.
func CheckNotInObjectError(fail func(string, ...interface{})) {
	b := flatbuffers.NewBuilder(0)

	defer func() {
		r := recover()
		if r == nil {
			fail("expected panic in CheckNotInObjectError")
		}
	}()
	b.EndObject()
}
开发者ID:bunnyblue,项目名称:flatbuffers,代码行数:13,代码来源:go_test.go


示例18: BenchmarkRead

func BenchmarkRead(b *testing.B) {
	builder := flatbuffers.NewBuilder(0)
	name := []byte("Arthur Dent")
	buf := MakeUser(builder, name, 42)
	b.SetBytes(int64(len(buf)))
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		got_name, _ := ReadUser(buf)
		// do some work to prevent cheating the benchmark:
		bytes.Equal(got_name, name)
	}
}
开发者ID:rw,项目名称:go-flatbuffers-example,代码行数:12,代码来源:main_test.go


示例19: BenchmarkRoundtrip

func BenchmarkRoundtrip(b *testing.B) {
	builder := flatbuffers.NewBuilder(0)
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		builder.Reset()
		buf := MakeUser(builder, []byte("Arthur Dent"), 42)
		got_name, _ := ReadUser(buf)
		if i == 0 {
			b.SetBytes(int64(len(buf)))
		}
		// do some work to prevent cheating the benchmark:
		bytes.Equal(got_name, []byte("Arthur Dent"))
	}
}
开发者ID:rw,项目名称:go-flatbuffers-example,代码行数:14,代码来源:main_test.go


示例20: BenchmarkUnmarshalByFlatBuffers_withFields

func BenchmarkUnmarshalByFlatBuffers_withFields(b *testing.B) {
	builder := flatbuffers.NewBuilder(0)
	bytes := serializeByFlatBuffers(builder, &group)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		result := GetRootAsFlatBufferColorGroup(bytes, 0)
		result.CgId()
		result.Name()
		colorsLen := result.ColorsLength()
		for j := 0; j < colorsLen; j++ {
			result.Colors(j)
		}
	}
}
开发者ID:jameswei,项目名称:gosercomp,代码行数:14,代码来源:gosercomp_test.go



注:本文中的github.com/google/flatbuffers/go.NewBuilder函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang go.UOffsetT函数代码示例发布时间:2022-05-23
下一篇:
Golang go.GetUOffsetT函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap