This is a creation in Article, where the information may have evolved or changed.
Tried the next flatbuffers, some of the East record.
Flatbuffers Official website: http://google.github.io/flatbuffers/md__go_usage.html
It supports the generation of Go language related things, the total use of the following steps:
1. Define the IDL interface definition file, usually named. FBS
Reference: Writing a schema
Http://google.github.io/flatbuffers/md__schemas.html
2. Parsing the generated language files using FLATC
Flatc
Reference: Using the schema compiler
Http://google.github.io/flatbuffers/md__compiler.html
To generate the parameters for go:
-g:generate Go classes. Skipped for data.
3. Install the Flatbuffers go support package in the Go Project and you can use it directly in the code.
Let's talk about some features of flatbuffers. First one. Examples of FBS:
Example IDL filenamespace mygame;attribute "priority"; enum Color:byte {Red = 1, Green, Blue}///union any {Monster, Weapon, Pickup}//union any {monster}union any {Monster, weapon}struct Vec3 { x:float; Y:float; Z:float;} Note Table Monster { pos:vec3; Mana:short =. Hp:short = +; name:string; Friendly:bool = False (deprecated, priority:1); Inventory:[ubyte]; Color:color = Blue; Test:any;} Table Weapon { pos:vec3; Mana:short = 150;} Root_type Monster;root_type Weapon;
Let's illustrate the above IDL as an example.
IDL generated results:
Will generate Any.go,color.go,monster.go,vec3.go,weapon.go under Mygame.
5 Go Files
Protobuf not, Protobuf will all be generated in a. pb.go file. Flatbuffers is more flexible than that.
Format Analysis:
"namespace Mygame;":
The corresponding Mygame directory is generated
"Mana:short =":
You can specify the data type and default values directly, and are very intuitive.
"Table Monster {pos:vec3;":
Table can nest structs directly
"Friendly:bool = False (deprecated, priority:1);":
By specifying deprecated, you can remove this field.
attribute "priority":
The priority keyword is defined and used on friendly, but from the resulting go file, there is no point in seeing it.
Feeling can be ignored.
"union any {Monster, weapon}":
Union-related things are used on "test:any;"
Generate a Any.go file with the contents:
Const (Anynone = 0AnyMonster = 1AnyWeapon = 2)
You can see the equivalent of an enum type.
"Root_type monster;root_type weapon;"
The role of Root_type:
In the go file generated by the table, the fields in the table are generated in addition to the related functions,
the Getrootasmonster () function is generated in addition for easy invocation
Func getrootasmonster (buf []byte, offset flatbuffers. Uoffsett) *monster {
If not, it will not have much effect.
format Other:
NOTES:
In Flatbuffers "
///"To express. And this note is brought into the generated source file.
Efficacy:
1. When an undefined type or definition is used, an error is made
2. According to the order of analysis, such as the last Root_type Monster, referring to table Monster front, will also error
required/optional:
Each field of a struct is required
Each field of the table is optional by default, but can be specified as required
Specify the order in which the table field functions are generated:
Add ID Specifies the order of generation table Monster { pos:vec3 (id:0); Mana:short = (id:3); Hp:short = (id:1); Name:string (id:2); Friendly:bool = False (deprecated, priority:1); Friendly:bool = False (Deprecated,id:4); Friendly:bool = False (priority:1); Inventory:[ubyte] (id:6); Color:color = Blue (id:5); Test:any (Id:8);}
In this case, the correlation function is generated in the order specified by ID.
Attention:
If an ID is added, all fields in the table must be ID to be passed
For any, you can see that ID is 8 instead of 7 in order. It turns out that it will generate:
Func (Rcv *monster) testtype () byte {}
Func (Rcv *monster) Test (obj *flatbuffers. Table) bool {}
That is, a field that implies a type. So add 1. That is , the union type, whose ID is {} total number plus 1.
support for nesting of. FBS files:
can use include "Include_test1.fbs"; form, nesting other. FBS files in.
How to use:
Value:
BUF, err: = Ioutil. ReadFile ("Monster.dat") offset: = 0 Monster: = example. Getrootasmonster (buf, offset) got: = Monster. Hp ()
Direct access.
Assignment Value:
Refer to this code:
Builder: = Flatbuffers. Newbuilder (0) 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, example). Monsteraddname (builder, str) example. Monsteraddinventory (builder, INV) example. Monsteraddtesttype (builder, 1)//example. Monsteraddtest (builder, Mon2)//example. MonsterAddTest4 (Builder, test4s) _ = example. Monsterend (builder)
That's it, the speed of what did not do contrast. The general feeling is that flatbuffers is simple and intuitive to define.
Very good for go support, especially to generate a corresponding. Go file is quite cool. others to see.
Mail:xcl_168@aliyun.com
blog:http://blog.csdn.net/xcl168