我是 golang 的初学者
我的golang版本是1.13
我尝试使用binary如下包
package main
import (
"bytes"
"encoding/binary"
"fmt"
)
type Coordinate struct {
x float64
y float64
z float64
}
func (self Coordinate) String() string {
return fmt.Sprintf("(%f,%f,%f)", self.x, self.y, self.z)
}
//not work
func test() {
var point Coordinate = Coordinate{1, 2, 3}
buf := bytes.Buffer{}
binary.Write(&buf, binary.LittleEndian, &point)
fmt.Println(point)
fmt.Println(buf)
p := new(Coordinate)
//panic: reflect: reflect.flag.mustBeAssignable using value obtained using unexported field
binary.Read(&buf, binary.LittleEndian, p)
fmt.Println(p)
}
func main() {
test()
println("over")
}
我收到这样的错误消息:
panic: reflect: reflect.flag.mustBeAssignable using value obtained using unexported field
我的代码有什么问题吗?
胡说叔叔
相关分类