douduonang3169 2019-02-06 07:19
浏览 169
已采纳

复合文字使用无键字段

I am working with new official mongodb driver for golang. I have created one complex query to insert the data into mongo db and then sort it according to an element value. I am using a filter in which I have created the bson type using :-

filter := bson.D{{"autorefid", "100"}}

But It is showing a warning saying:

primitive.E composite literal uses unkeyed fields

The warnings are creating a mess in my code.

  • 写回答

1条回答 默认 最新

  • dongzouban9871 2019-02-06 07:24
    关注

    The warnings can be stopped by setting the check flag to false.

    $ go doc cmd/vet
    

    By default all checks are performed. If any flags are explicitly set to true, only those tests are run. Conversely, if any flag is explicitly set to false, only those tests are disabled. Thus -printf=true runs the printf check, -printf=false runs all checks except the printf check.

    Unkeyed composite literals
    
    Flag: -composites
    
    Composite struct literals that do not use the field-keyed syntax.
    

    But the warning is due to not providing the keys name when setting the value in primtive.E struct.

    Setting keys for primitive.E struct will remove the warning messages. For example

    filter := bson.D{primitive.E{Key: "autorefid", Value: "100"}}
    

    Package primitive contains types similar to Go primitives for BSON types can do not have direct Go primitive representations.

    type E struct {
        Key   string
        Value interface{}
    }
    

    E represents a BSON element for a D. It is usually used inside a D.

    For more information have a look at primitive.E

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度