代码拉取完成,页面将自动刷新
📚 VERSION: draft-02
⛳️ STATE: WIP
Y3 is the golang implementation of Y3 Codec, which describe a fast and low CPU binding data encoder/decoder focus on edge computing and streaming processing.
See the explainer for more info, more about the ways to combine with YoMo.
See Y3 Codec SPEC
go test ./...
go get -u github.com/yomorun/y3-codec-golang
package main
import (
"fmt"
y3 "github.com/yomorun/y3-codec-golang"
)
func main() {
// if we want to repesent `var obj = &foo{ID: -1, bar: &bar{Name: "C"}}`
// in Y3-Codec:
// 0x81 -> node
var foo = y3.NewNodePacketEncoder(0x01)
// 0x02 -> foo.ID=-11
var yp1 = y3.NewPrimitivePacketEncoder(0x02)
yp1.SetInt32Value(-1)
foo.AddPrimitivePacket(yp1)
// 0x83 -> &bar{}
var bar = y3.NewNodePacketEncoder(0x03)
// 0x04 -> bar.Name="C"
var yp2 = y3.NewPrimitivePacketEncoder(0x04)
yp2.SetStringValue("C")
bar.AddPrimitivePacket(yp2)
// -> foo.bar=&bar
foo.AddNodePacket(bar)
fmt.Printf("res=%#v", foo.Encode()) // res=[]byte{0x81, 0x08, 0x02, 0x01, 0x7F, 0x83, 0x03, 0x04, 0x01, 0x43}
}
package main
import (
"fmt"
y3 "github.com/yomorun/y3-codec-golang"
)
func main() {
fmt.Println(">> Parsing [0x0A, 0x01, 0x7F], which like Key-Value format = 0x0A: 127")
buf := []byte{0x0A, 0x01, 0x7F}
res, _, err := y3.DecodePrimitivePacket(buf)
v1, err := res.ToUInt32()
if err != nil {
panic(err)
}
fmt.Printf("Tag Key=[%#X], Value=%v\n", res.SeqID(), v1)
}
package main
import (
"fmt"
y3 "github.com/yomorun/y3-codec-golang"
)
func main() {
fmt.Println(">> Parsing [0x84, 0x06, 0x0A, 0x01, 0x7F, 0x0B, 0x01, 0x43] EQUALS JSON= 0x84: { 0x0A: -1, 0x0B: 'C' }")
buf := []byte{0x84, 0x06, 0x0A, 0x01, 0x7F, 0x0B, 0x01, 0x43}
res, _, err := y3.DecodeNodePacket(buf)
v1 := res.PrimitivePackets[0]
p1, err := v1.ToInt32()
if err != nil {
panic(err)
}
fmt.Printf("Tag Key=[%#X.%#X], Value=%v\n", res.SeqID(), v1.SeqID(), p1)
v2 := res.PrimitivePackets[1]
p2, err := v2.ToUTF8String()
if err != nil {
panic(err)
}
fmt.Printf("Tag Key=[%#X.%#X], Value=%v\n", res.SeqID(), v2.SeqID(), p2)
}
More examples in /examples/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。