我做了很多查询,结果生成了 map 或 map 的 slice/数组,如下所示:
// package M
type SX map[string]interface{}
type IX map[int64]interface{}
type IAX map[int64][]interface{}
type SAX map[string][]interface{}
type SS map[string]string
type SF map[string]float64
type II map[int64]int64
type IB map[int64]bool
type SI map[string]int64
type IS map[int64]string
type SB map[string]bool
// package A
type X []interface{}
type MSX []map[string]interface{}
所以我可以这样声明它:
// import `gitlab.com/kokizzu/gokil/A`
// import `gitlab.com/kokizzu/gokil/M`
values := M.SX{
`orderId`: `1-12-1`,
`apiKey`: `16313c061a8e3288528123bd8`,
`country`: `360`,
`currency`: `360`,
`payType`: 1,
`items`: A.MSX{
M.SX{
`code`: `subscription for 7 days`,
`name`: `Bla bla`,
`price`: price,
},
},
`profile`: M.SX{
`entry`: A.MSX{
M.SX{
`key`: `need_mno_id`,
`value`: `yes`,
},
M.SX{
`key`: `foo`,
`value`: `bar`,
},
},
},
`profile`: A.MSX{
M.SX{`foo`:`bar`,`age`:123},
M.SX{`foo`:`wow`,`age`:234,`currency`:360},
M.SX{`foo`:`such`,`age`:45,`is_admin`:true},
M.SX{`foo`:`wow`,`age`:57,`is_deleted`:true},
},
}
list 中的哪一个?除了encoding/gob
和encoding/json
,还有哪些支持这种序列化(不需要生成struct/schema)?
github.com/alecthomas/binary
github.com/davecgh/go-xdr/xdr
github.com/Sereal/Sereal/Go/sereal
github.com/ugorji/go/codec
gopkg.in/vmihailenco/msgpack.v2 --> has example for enc/dec-ing a map
labix.org/v2/mgo/bson
github.com/tinylib/msgp (code generator for msgpack)
github.com/golang/protobuf (generated code)
github.com/gogo/protobuf (generated code, optimized version of goprotobuf)
github.com/DeDiS/protobuf (reflection based)
github.com/google/flatbuffers
github.com/hprose/hprose-go/io
github.com/glycerine/go-capnproto
zombiezen.com/go/capnproto2
github.com/andyleap/gencode
github.com/pascaldekloe/colfer
注意:Gob
(目前我正在使用它们)没有任何问题,我只需要准备替代方案(或从最好的方案开始)Gob
不再足够(不够快/不够小),因为我用它来缓存数据库(具有不断变化的架构)查询结果在 RAM 上。
最佳答案
您的数据结构看起来可以表示为多个表 数据。也就是说,规范化为表(如数据库表)。如果那是 在这种情况下,然后考虑使用 FlatBuffers 的基于表的子集。
我刚刚发布了一个执行此操作的 Go 库和实用程序。所有你需要的 要做的是将您的数据结构重新设计为表格格式并表示它们 以简单的表格数据格式,然后安装并运行一个实用程序 gotflat 将为您生成所有胶水。以下链接显示了如何安装 并使用它。希望对您有所帮助。
https://github.com/urban-wombat/gotablesutils/releases/tag/v0.2-alpha
关于serialization - Golang 映射/数组(非结构)序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40605630/