How to encode and receive struct? #160
-
I have the following code in the sender app: type Data struct {
a string `y3:"0x11"`
b string `y3:"0x12"`
c string `y3:"0x13"`
d string `y3:"0x14"`
e string `y3:"0x15"`
}
codec := y3.NewCodec(0x10)
func generateAndSendData(stream quic.Stream) {
test := Data{
a: "a",
b: "bc",
c: "def",
d: "ghi",
e: "jklmn",
}
for i := 0; i < 10; i++ {
time.Sleep(100 * time.Millisecond)
sendingBuf, _ := codec.Marshal(test)
_, err := stream.Write(sendingBuf)
if err != nil {
log.Printf("Couldn't send buffer with i=%v", i)
} else {
fmt.Print(".")
}
}
} And the following receiving-side code: package main
import (
"context"
"fmt"
y3 "github.com/yomorun/y3-codec-golang"
"github.com/yomorun/yomo/pkg/rx"
)
type Data struct {
a string `y3:"0x11"`
b string `y3:"0x12"`
c string `y3:"0x13"`
d string `y3:"0x14"`
e string `y3:"0x15"`
}
var convert = func(v []byte) (interface{}, error) {
fmt.Printf("Received: %v\n", v)
var mold Data
err := y3.ToObject(v, &mold)
if err != nil {
fmt.Printf("Error: %v\n", err)
return nil, err
}
return mold, err
}
func Handler(rxstream rx.RxStream) rx.RxStream {
return rxstream.
Subscribe(0x10).
OnObserve(convert).
StdOut().
Encode(0x11)
} It prints this and fails:
I tried int64 instead of string, but it returns
What am I doing wrong? :( |
Beta Was this translation helpful? Give feedback.
Answered by
chenjunbiao
Apr 26, 2021
Replies: 1 comment 1 reply
-
Please set member variables to start with an uppercase letter, like this: type Data struct {
A string `y3:"0x11"`
B string `y3:"0x12"`
C string `y3:"0x13"`
D string `y3:"0x14"`
E string `y3:"0x15"`
} then you can receive the data:
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fanweixiao
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please set member variables to start with an uppercase letter, like this:
then you can receive the data: