Go 如何将Uint64的片转换为字节片
我目前有一个protobuf结构,如下所示:Go 如何将Uint64的片转换为字节片,go,Go,我目前有一个protobuf结构,如下所示: type RequestEnvelop_MessageQuad struct { F1 [][]byte `protobuf:"bytes,1,rep,name=f1,proto3" json:"f1,omitempty"` F2 []byte `protobuf:"bytes,2,opt,name=f2,proto3" json:"f2,omitempty"` Lat float64 `protobuf:"f
type RequestEnvelop_MessageQuad struct {
F1 [][]byte `protobuf:"bytes,1,rep,name=f1,proto3" json:"f1,omitempty"`
F2 []byte `protobuf:"bytes,2,opt,name=f2,proto3" json:"f2,omitempty"`
Lat float64 `protobuf:"fixed64,3,opt,name=lat" json:"lat,omitempty"`
Long float64 `protobuf:"fixed64,4,opt,name=long" json:"long,omitempty"`
}
F1获取我生成的一些S2几何体数据,如下所示:
ll := s2.LatLngFromDegrees(location.Latitude, location.Longitude)
cid := s2.CellIDFromLatLng(ll).Parent(15)
walkData := []uint64{cid.Pos()}
next := cid.Next()
prev := cid.Prev()
// 10 Before, 10 After
for i := 0; i < 10; i++ {
walkData = append(walkData, next.Pos())
walkData = append(walkData, prev.Pos())
next = next.Next()
prev = prev.Prev()
}
log.Println(walkData)
ll:=s2.LatLngFromDegrees(位置.纬度,位置.经度)
cid:=s2.CellIDFromLatLng(ll).Parent(15)
walkData:=[]uint64{cid.Pos()}
next:=cid.next()
prev:=cid.prev()
//10前10后
对于i:=0;i<10;i++{
walkData=append(walkData,next.Pos())
walkData=append(walkData,prev.Pos())
next=next.next()
prev=prev.prev()
}
log.Println(walkData)
唯一的问题是,protobuf结构需要一种类型的
[]字节
我只是不确定如何将uint64
数据转换成字节。谢谢。整数值可以用标准库中的包编码到字节数组中
例如,要将uint64
编码到字节缓冲区中,我们可以使用以下函数:
big := uint64(257)
buf := make([]byte, 2)
n := binary.PutUvarint(buf, big)
fmt.Printf("Wrote %d bytes into buffer: [% x]\n", n, buf)
buf := new(bytes.Buffer)
var pi float64 = math.Pi
err := binary.Write(buf, binary.LittleEndian, pi)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
fmt.Printf("% x", buf.Bytes())
将打印:
Wrote 2 bytes into buffer: [81 02]
我们还可以使用以下函数将通用流写入缓冲区:
big := uint64(257)
buf := make([]byte, 2)
n := binary.PutUvarint(buf, big)
fmt.Printf("Wrote %d bytes into buffer: [% x]\n", n, buf)
buf := new(bytes.Buffer)
var pi float64 = math.Pi
err := binary.Write(buf, binary.LittleEndian, pi)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
fmt.Printf("% x", buf.Bytes())
哪些产出:
18 2d 44 54 fb 21 09 40
(第二个示例是从软件包文档中借来的,您可以在其中找到其他类似的示例)整数值可以用标准库中的软件包编码到字节数组中 例如,要将
uint64
编码到字节缓冲区中,我们可以使用以下函数:
big := uint64(257)
buf := make([]byte, 2)
n := binary.PutUvarint(buf, big)
fmt.Printf("Wrote %d bytes into buffer: [% x]\n", n, buf)
buf := new(bytes.Buffer)
var pi float64 = math.Pi
err := binary.Write(buf, binary.LittleEndian, pi)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
fmt.Printf("% x", buf.Bytes())
将打印:
Wrote 2 bytes into buffer: [81 02]
我们还可以使用以下函数将通用流写入缓冲区:
big := uint64(257)
buf := make([]byte, 2)
n := binary.PutUvarint(buf, big)
fmt.Printf("Wrote %d bytes into buffer: [% x]\n", n, buf)
buf := new(bytes.Buffer)
var pi float64 = math.Pi
err := binary.Write(buf, binary.LittleEndian, pi)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
fmt.Printf("% x", buf.Bytes())
哪些产出:
18 2d 44 54 fb 21 09 40
(第二个示例是从软件包文档中借来的,您可以在其中找到其他simliar示例)