douwo1517 2015-01-05 21:47
浏览 117
已采纳

Golang levelDB结构

I'm trying to use following DB API: https://godoc.org/github.com/syndtr/goleveldb/leveldb# (simple file based key/value DB)

I was able to put and get "key"s into the database. However, I'm wondering if value can be a struct such as:

type Thm struct {
    Name string
    Age  int
}

Then,

var Tmp Thm
Tmp.Name = "Gon"
Tmp.Age = 33

db.Put([]byte("test3"), []byte(Tmp), nil)

Right now, the error I'm getting is "cannot covert Tmp (type Thm) to type []byte.

If you have experiences with levelDB, could you help me how normally this will be done? OR, should I convert struct into byte in order to make this work?

Thank you

  • 写回答

1条回答

  • duananyantan04633 2015-01-05 21:54
    关注

    levelDB only supports strings/byte arrays as keys and values. This is actually a pretty smart feature, because it keeps serialization of complex data structures at the application level. To serialize your Thm struct you can try the gob package if you don't need applications in other languages to be able to read the values, or protobufs, json, or msgpack if you need the serialized data to be accessible to other languages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?