summaryrefslogtreecommitdiff
path: root/examples/go-echo
diff options
context:
space:
mode:
authorMichael Le <michael.le647@gmail.com>2022-11-28 18:29:48 -0800
committerGitHub <noreply@github.com>2022-11-29 02:29:48 +0000
commitc3a01c7228b491ef391a56aabeac85b15826a459 (patch)
treeeefd2210285dd8415a7d94907226a6570b13be84 /examples/go-echo
parent533f75d91b36e9948c2ea0bcff93732b51868291 (diff)
downloadflatbuffers-c3a01c7228b491ef391a56aabeac85b15826a459.tar.gz
flatbuffers-c3a01c7228b491ef391a56aabeac85b15826a459.tar.bz2
flatbuffers-c3a01c7228b491ef391a56aabeac85b15826a459.zip
Use FinshedBytes() in go-echo example instead of manually encoding offset (#7660)
Co-authored-by: Derek Bailey <derekbailey@google.com>
Diffstat (limited to 'examples/go-echo')
-rw-r--r--examples/go-echo/client/client.go15
-rw-r--r--examples/go-echo/server/server.go8
2 files changed, 3 insertions, 20 deletions
diff --git a/examples/go-echo/client/client.go b/examples/go-echo/client/client.go
index 5d2c1308..be8fd7e8 100644
--- a/examples/go-echo/client/client.go
+++ b/examples/go-echo/client/client.go
@@ -16,13 +16,7 @@ func RequestBody() *bytes.Reader {
b := flatbuffers.NewBuilder(0)
r := net.RequestT{Player: &hero.WarriorT{Name: "Krull", Hp: 100}}
b.Finish(r.Pack(b))
-
- // Encode builder head in last 4 bytes of request body
- buf := make([]byte, 4)
- flatbuffers.WriteUOffsetT(buf, b.Head())
- buf = append(b.Bytes, buf...)
-
- return bytes.NewReader(buf)
+ return bytes.NewReader(b.FinishedBytes())
}
func ReadResponse(r *http.Response) {
@@ -32,18 +26,13 @@ func ReadResponse(r *http.Response) {
return
}
- // Last 4 bytes is offset.
- off := flatbuffers.GetUOffsetT(body[len(body)-4:])
- buf := body[:len(body) - 4]
-
- res := net.GetRootAsResponse(buf, off)
+ res := net.GetRootAsResponse(body, 0)
player := res.Player(nil)
fmt.Printf("Got response (name: %v, hp: %v)\n", string(player.Name()), player.Hp())
}
func main() {
-
body := RequestBody()
req, err := http.NewRequest("POST", "http://localhost:8080/echo", body)
if err != nil {
diff --git a/examples/go-echo/server/server.go b/examples/go-echo/server/server.go
index 46ff9e10..c9885b89 100644
--- a/examples/go-echo/server/server.go
+++ b/examples/go-echo/server/server.go
@@ -5,8 +5,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
-
- flatbuffers "github.com/google/flatbuffers/go"
)
func echo(w http.ResponseWriter, r *http.Request) {
@@ -16,11 +14,7 @@ func echo(w http.ResponseWriter, r *http.Request) {
return
}
- // Last 4 bytes is offset. See client.go.
- off := flatbuffers.GetUOffsetT(body[len(body)-4:])
- buf := body[:len(body) - 4]
-
- req := net.GetRootAsRequest(buf, off)
+ req := net.GetRootAsRequest(body, 0)
player := req.Player(nil)
fmt.Printf("Got request (name: %v, hp: %v)\n", string(player.Name()), player.Hp())