我有一个我定义的结构实例,我想将其转换为一个字节数组。我试过[]字节(my_struct),但是没有奏效。此外,我被指出binary package,但我不知道我应该使用哪个函数,以及如何使用它。一个例子将不胜感激。在Go中,如何将结构转换为字节数组?
回答
我假设你想要的东西就像C处理这件事一样。没有建立的方式来做到这一点。你必须为你的结构定义自己的序列化和反序列化。二进制包将帮助您将结构中的字段编码为可添加到字节数组中的字节,但您将负责指定字节数组中的长度和偏移量,以保存结构中的字段。
您的其他选项是使用编码包之一:http://golang.org/pkg/encoding/如gob或json。
编辑:
既然你想这个制作的哈希,你在你的评论说做easisest就是使用[]byte(fmt.Sprintf("%v", struct))
像这样:http://play.golang.org/p/yY8mSdZ_kf
你有没有考虑其序列以BSON? http://labix.org/gobson
应使用而不是字符串一个字节的缓冲区,另一个建议的方法创建可变长度的SHA1,所述SHA1标准长度必须为20个字节(160位)
package main
import (
"crypto/sha1"
"fmt"
"encoding/binary"
"bytes"
)
type myStruct struct {
ID string
Data string
}
func main() {
var bin_buf bytes.Buffer
x := myStruct{"1", "Hello"}
binary.Write(&bin_buf, binary.BigEndian, x)
fmt.Printf("% x", sha1.Sum(bin_buf.Bytes()))
}
试一试: http://play.golang.org/p/8YuM6VIlLV
这是一个非常简单的方法,它的效果很好。
不支持链接的答案已弃用。 – 2015-01-21 17:38:38
谢谢Der,我会编辑这个。是我的第一个评论。 – 2015-01-22 18:13:20
看起来不像这样,因为'myStruct'不是固定大小。 HTTPS://play.golang。org/p/IGA_lgRVNX – 2016-09-21 21:00:08
一个可能的解决方案是"encoding/gob"
标准包。 gob包创建了一个编码器/解码器,可以将任何结构编码成一个字节数组,然后将该数组解码回结构。有一个很棒的帖子,here。
正如其他人指出的那样,有必要使用这样的包,因为结构本质上具有未知大小,并且不能转换为字节数组。
我已经包含了一些代码和play。
package main
import (
"bytes"
"encoding/gob"
"fmt"
"log"
)
type P struct {
X, Y, Z int
Name string
}
type Q struct {
X, Y *int32
Name string
}
func main() {
// Initialize the encoder and decoder. Normally enc and dec would be
// bound to network connections and the encoder and decoder would
// run in different processes.
var network bytes.Buffer // Stand-in for a network connection
enc := gob.NewEncoder(&network) // Will write to network.
dec := gob.NewDecoder(&network) // Will read from network.
// Encode (send) the value.
err := enc.Encode(P{3, 4, 5, "Pythagoras"})
if err != nil {
log.Fatal("encode error:", err)
}
// HERE ARE YOUR BYTES!!!!
fmt.Println(network.Bytes())
// Decode (receive) the value.
var q Q
err = dec.Decode(&q)
if err != nil {
log.Fatal("decode error:", err)
}
fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y)
}
- 1. 如何将结构转换为C中的字节数组?
- 2. 如何将C#字节数组转换为结构化数据?
- 3. 如何在Go中将[Size]字节转换为字符串?
- 4. 将字节数组转换为Golang中的syscall.InotifyEvent结构
- 5. 如何将Go结构转换为C结构?
- 6. c#如何将结构数组转换为数组结构
- 7. 如何转换C#字节[]为结构[]
- 8. 如何将Java字节数组转换为Scala字节数组?
- 9. 将Go结构转换为JSON
- 10. 将结构转换为字节
- 11. golang将字节数组转换为结构体
- 12. 转:如何将结构转换为[]字节?
- 13. 如何将字节转换为Python中的结构?
- 14. 如何在Go中将一串整数转换为数组?
- 15. 如何通过一个操作将字节数组转换为联合结构?
- 16. 如何将字节数组转换为字符串数组?
- 17. 在Java中,如何将InputStream转换为字节数组(byte [])?
- 18. 如何在Java中将字节数组转换为hexString?
- 19. 如何在J2ME中将图像转换为字节数组?
- 20. 如何将字节数组转换为zip文件在c#中?
- 21. 如何在Android Studio中将imageview转换为字节数组?
- 22. 将结构数组转换为数字或双数组
- 23. 将字符串数组(字节值)转换为字节数组
- 24. 如何将数字转换为字节?
- 25. 如何将char数组转换为结构数组?
- 26. 将结构转换为数组
- 27. 将PHP数组转换为SOAP结构
- 28. 将结构转换为对象数组
- 29. 如何将图像转换为字节数组并将字节数组转换为图像
- 30. 如何将字节[]转换为字节[]
感谢您的快速回答。我试图做到这一点的原因是能够得到我的结构散列(我试图使用SHA-256,但它可能是另一个)。你知道更简单的方法吗? – abw333 2013-05-02 04:56:21
我编辑了答案,以显示一个简单的方法来做你想做的。 – 2013-05-02 05:06:00
再次感谢。我试过这样做,但我收到以下错误消息:“单值上下文中的多值fmt.Printf()”。你知道这是为什么吗? – abw333 2013-05-02 05:18:31