我正在尝试使用"appengine/memcache“将数据存储在缓存memcache.Item的Value字段是[]byte
如何将结构转换为[]字节来存储它?
例如:
type Link struct {
Files []string
}发布于 2021-05-12 10:40:58
您可以使用gob#Encoder.Encode
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
type link struct {
Files []string
}
func main() {
s := link{
[]string{"south", "north"},
}
b := new(bytes.Buffer)
gob.NewEncoder(b).Encode(s)
// "\x1d\xff\x81\x03\x01\x01\x04link\x01\xff\x82\x00\x01\x01\x01\x05Files\x01\xff\x84\x00\x00\x00\x16\xff\x83\x02\x01\x01\b[]string\x01\xff\x84\x00\x01\f\x00\x00\x11\xff\x82\x01\x02\x05south\x05north\x00"
fmt.Printf("%q\n", b)
}https://stackoverflow.com/questions/16125886
复制相似问题