• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Zach369
V2EX  ›  程序员

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

  •  
  •   Zach369 · Apr 2, 2020 · 2241 views
    This topic created in 2260 days ago, the information mentioned may be changed or developed.

    我遇到了这样一个问题:

    我需要生成的 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 如何处理那?

    4 replies    2020-04-03 10:41:40 +08:00
    tairan2006
        1
    tairan2006  
       Apr 2, 2020 via Android
    大哥错别字太多了吧…用 string 就行
    whalegao
        2
    whalegao  
       Apr 3, 2020 via iPhone
    自己定义一个就行了
    Zach369
        3
    Zach369  
    OP
       Apr 3, 2020
    @tairan2006 额 昨天写的有的急 ,,,,, 确实错别字有点多...... 不过我用 string 生成 string 的 id mgo 插入的时候报错



    @whalegao 能说下具体怎么做吗? 自己定义, 就得实现这个生成 id 的方法啊
    tairan2006
        4
    tairan2006  
       Apr 3, 2020
    @Zach369 string 和 ObjectId 可以相互转换的,你 insert 的时候不用指定 id,让 mongo 自动生成;读取的时候把 ObjectId 转给客户端。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5271 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 08:38 · PVG 16:38 · LAX 01:38 · JFK 04:38
    ♥ Do have faith in what you're doing.