4

看到 Go 与 MongoDB 的交互方式,我想放弃 Go 了

 2 years ago
source link: https://www.v2ex.com/t/810126
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Go 编程语言

看到 Go 与 MongoDB 的交互方式,我想放弃 Go 了

  balabalaguguji · 1 小时 43 分钟前 · 793 次点击

之前习惯了 python/js 这种语法,感觉很自然很方便。

今天看了下 MongoDB 官方的 Go 接口,哎呀,那交互方式,真的是痛苦。

例如查询用户为 1 的用户:{userid: 1},在 Go 里面你还得包裹为 bson.D{{"userid", 1}}

返回的结果是一个索引,要 Decode 下,Docode 还需要传递一个结构体过去。

还得传递一个 context (还没看为啥要这么做,其他语言不用)

感觉一点也不方便,代码很多不美观不优雅,习惯了 js/python 这种比较简单直观的语法,难以接受呀。

感觉 Google 最近出的东西,语法都那么特立独行的,还有一个是死亡嵌套 Flutter ,嵌套到怀疑人生。

官方教程: https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial

filter := bson.D{{"name", "Ash"}}

update := bson.D{
    {"$inc", bson.D{
        {"age", 1},
    }},
}

updateResult, err := collection.UpdateOne(context.TODO(), filter, update)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Matched %v documents and updated %v documents.\n", updateResult.MatchedCount, updateResult.ModifiedCount)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK