/
main.go
50 lines (40 loc) · 1.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// main.go part 1 of 4
package main
import (
"fmt"
"./users"
flatbuffers "github.com/google/flatbuffers/go"
)
// 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():]
}
// main.go part 3 of 4
func ReadUser(buf []byte) (name []byte, id uint64) {
// initialize a User reader from the given buffer:
user := users.GetRootAsUser(buf, 0)
// point the name variable to the bytes containing the encoded name:
name = user.Name()
// point the id variable to the bytes containing the encoded id:
id = user.Id()
return
}
// main.go part 4 of 4
func main() {
b := flatbuffers.NewBuilder(0)
buf := MakeUser(b, []byte("Arthur Dent"), 42)
name, id := ReadUser(buf)
fmt.Printf("%s has id %d. The encoded data is %d bytes long.\n", name, id, len(buf))
}