Go
版本,及系统版本、系统架构?go version go1.15.6 windows/amd64
GoFrame
框架版本?github.com/gogf/gf v1.14.5
文档代码:
package main
import (
"errors"
"fmt"
"github.com/gogf/gf/crypto/gcrc32"
"github.com/gogf/gf/encoding/gbinary"
"github.com/gogf/gf/util/gconv"
)
type Pkg struct {
Length uint16 // Total length.
Crc32 uint32 // CRC32.
Data []byte
}
// NewPkg creates and returns a package with given data.
func NewPkg(data []byte) *Pkg {
return &Pkg{
Length: uint16(len(data) + 6),
Crc32: gcrc32.Encrypt(data),
Data: data,
}
}
// Marshal encodes the protocol struct to bytes.
func (p *Pkg) Marshal() []byte {
b := make([]byte, 6+len(p.Data))
copy(b, gbinary.EncodeUint16(p.Length))
copy(b[2:], gbinary.EncodeUint32(p.Crc32))
copy(b[6:], p.Data)
return b
}
// UnmarshalValue decodes bytes to protocol struct.
func (p *Pkg) UnmarshalValue(v interface{}) error {
b := gconv.Bytes(v)
if len(b) < 6 {
return errors.New("invalid package length")
}
p.Length = gbinary.DecodeToUint16(b[:2])
if len(b) < int(p.Length) {
return errors.New("invalid data length")
}
p.Crc32 = gbinary.DecodeToUint32(b[2:6])
p.Data = b[6:]
if gcrc32.Encrypt(p.Data) != p.Crc32 {
return errors.New("crc32 validation failed")
}
return nil
}
func main() {
var p1, p2 *Pkg
// Create a demo pkg as p1.
p1 = NewPkg([]byte("123"))
fmt.Println(p1)
// Convert bytes from p1 to p2 using gconv.Struct.
err := gconv.Struct(p1.Marshal(), &p2)
if err != nil {
panic(err)
}
fmt.Println(p2)
}
地址:https://itician.org/pages/viewpage.action?pageId=1114226
我的测试代码
package test
import (
"errors"
"fmt"
"github.com/gogf/gf/crypto/gcrc32"
"github.com/gogf/gf/encoding/gbinary"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
"testing"
)
type Pkg struct {
Length uint16 // Total length.
Crc32 uint32 // CRC32.
Data []byte
}
// NewPkg creates and returns a package with given data.
func NewPkg(data []byte) *Pkg {
return &Pkg{
Length: uint16(len(data) + 6),
Crc32: gcrc32.Encrypt(data),
Data: data,
}
}
// Marshal encodes the protocol struct to bytes.
func (p *Pkg) Marshal() []byte {
fmt.Println(p)
b := make([]byte, 6+len(p.Data))
copy(b, gbinary.EncodeUint16(p.Length))
copy(b[2:], gbinary.EncodeUint32(p.Crc32))
copy(b[6:], p.Data)
fmt.Println(p)
return b
}
// UnmarshalValue decodes bytes to protocol struct.
func (p *Pkg) UnmarshalValue(v interface{}) error {
b := gconv.Bytes(v)
if len(b) < 6 {
return errors.New("invalid package length")
}
p.Length = gbinary.DecodeToUint16(b[:2])
if len(b) < int(p.Length) {
return errors.New("invalid data length")
}
p.Crc32 = gbinary.DecodeToUint32(b[2:6])
p.Data = b[6:]
if gcrc32.Encrypt(p.Data) != p.Crc32 {
return errors.New("crc32 validation failed")
}
return nil
}
func TestConvStruct(t *testing.T) {
var p1, p2 *Pkg
// Create a demo pkg as p1.
p1 = NewPkg([]byte("123"))
fmt.Println(p1)
// Convert bytes from p1 to p2 using gconv.Struct.
err := gconv.Struct(p1.Marshal(), &p2)
if err != nil {
panic(err)
}
fmt.Println(p2)
}
运行结果及错误提示:
=== RUN TestConvStruct
&{9 2286445522 [49 50 51]}
&{9 2286445522 [49 50 51]}
&{9 2286445522 [49 50 51]}
--- FAIL: TestConvStruct (0.00s)
panic: convert params to map failed: [9 0 210 99 72 136 49 50 51] [recovered]
panic: convert params to map failed: [9 0 210 99 72 136 49 50 51]
goroutine 34 [running]:
testing.tRunner.func1.1(0xfbc7c0, 0xc000252680)
D:/Go/src/testing/testing.go:1072 +0x310
testing.tRunner.func1(0xc000248480)
D:/Go/src/testing/testing.go:1075 +0x43a
panic(0xfbc7c0, 0xc000252680)
D:/Go/src/runtime/panic.go:969 +0x1c7
base/test.TestConvStruct(0xc000248480)
E:/docker/dnmpr/www/go/test/test/map_test.go:125 +0x268
testing.tRunner(0xc000248480, 0x1074560)
D:/Go/src/testing/testing.go:1123 +0xef
created by testing.(*T).Run
D:/Go/src/testing/testing.go:1168 +0x2b3
Process finished with exit code 1
这是文档里面的一个问题,我进行测试的时候,没测试通过。
测试通过
运行结果及错误提示:
=== RUN TestConvStruct
&{9 2286445522 [49 50 51]}
&{9 2286445522 [49 50 51]}
&{9 2286445522 [49 50 51]}
--- FAIL: TestConvStruct (0.00s)
panic: convert params to map failed: [9 0 210 99 72 136 49 50 51] [recovered]
panic: convert params to map failed: [9 0 210 99 72 136 49 50 51]
goroutine 34 [running]:
testing.tRunner.func1.1(0xfbc7c0, 0xc000252680)
D:/Go/src/testing/testing.go:1072 +0x310
testing.tRunner.func1(0xc000248480)
D:/Go/src/testing/testing.go:1075 +0x43a
panic(0xfbc7c0, 0xc000252680)
D:/Go/src/runtime/panic.go:969 +0x1c7
base/test.TestConvStruct(0xc000248480)
E:/docker/dnmpr/www/go/test/test/map_test.go:125 +0x268
testing.tRunner(0xc000248480, 0x1074560)
D:/Go/src/testing/testing.go:1123 +0xef
created by testing.(*T).Run
D:/Go/src/testing/testing.go:1168 +0x2b3
Process finished with exit code 1