如何使用 Threft struct 定义 mgo 的 objectId?

2020-04-02 23:41:28 +08:00
 Zach369

我遇到了这样一个问题:

我需要生成的 go struct 是:

type Base struct {
	ID bson.ObjectId `bson:"_id" json:"id,omitempty"`
}

但是 thrift,我不知道怎么写?

struct Base {
    1:?  ID (go.tag = 'bson:"_id" json:"id,omitempty"')
}

bson.ObjectId 是 github.com/globalsign/mgo/bson 的内容

有使用过 threft 生成 go 的 struct 的吗? mongodb 的 objectId 如何处理那?

1324 次点击
所在节点    程序员
4 条回复
tairan2006
2020-04-02 23:44:36 +08:00
大哥错别字太多了吧…用 string 就行
whalegao
2020-04-03 00:11:41 +08:00
自己定义一个就行了
Zach369
2020-04-03 09:35:02 +08:00
@tairan2006 额 昨天写的有的急 ,,,,, 确实错别字有点多...... 不过我用 string 生成 string 的 id mgo 插入的时候报错



@whalegao 能说下具体怎么做吗? 自己定义, 就得实现这个生成 id 的方法啊
tairan2006
2020-04-03 10:41:40 +08:00
@Zach369 string 和 ObjectId 可以相互转换的,你 insert 的时候不用指定 id,让 mongo 自动生成;读取的时候把 ObjectId 转给客户端。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/658828

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX