douchensou6495
2019-03-24 18:17
浏览 581

如何导入官方的mongodb驱动程序包

How to import official mongoDB driver package in Go?

I am following the official Go-mongoDB-driver package instruction (https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial). I have installed the mongoDB package using this:

go get github.com/mongodb/mongo-go-driver

but I just can't import the package

I am doing a very simple snippet in my main.go

package main

import "github.com/mongodb/mongo-go-driver/mongo

func main() {
}

This gives me:

main.go:8:8: code in directory $GOPATH/src/github.com/mongodb/mongo-go-driver/bson expects import "go.mongodb.org/mongo-driver/bson"

When I tried to import go.mongodb.org/mongo-driver/bson, It gives me this:

main.go:10:8: cannot find package "go.mongodb.org/mongo-driver/bson" in any of:
    /usr/local/go/src/go.mongodb.org/mongo-driver/bson (from $GOROOT)
    $GOPATH/src/go.mongodb.org/mongo-driver/bson (from $GOPATH)

Kindly help, quite new in Go and not sure where to look since I don't find people having this issue a lot.

图片转代码服务由CSDN问答提供 功能建议

如何在Go中导入正式的mongoDB驱动程序包?

我正在关注 官方Go-mongoDB驱动程序软件包说明( https:// www。 mongodb.com/blog/post/mongodb-go-driver-tutorial )。 我已经使用以下方法安装了mongoDB软件包:

 去获取github.com/mongodb/mongo-go-driver

但是我只是无法导入软件包

我在我的 main.go 中做了一个非常简单的代码段

 包main 
 
import“ github.com/mongodb/mongo-go-driver/mongo

func main(){
} 
   
 \  n 

这给了我

  main.go:8:8:$ GOPATH / src / github.com / mongodb / mongo-go-driver /目录中的代码 当我尝试导入 go.mongodb.org/mongo-driver时,bson希望导入“ go.mongodb.org/mongo-driver/bson"
  
 
 

/ bson ,它给了我这个:

  main.go:10:8:找不到软件包“ go.mongodb.org/mongo-driver/bson” 在以下任何位置:
 /usr/local/go/src/go.mongodb.org/mongo-driver/bson(来自$ GOROOT)
 $ GOPATH / src / go.mongodb.org / mongo-driver / bson( 来自$ GOPATH)
   
 
 

请帮忙,这是Go中的一个新功能,由于我找不到很多人遇到这个问题,所以不确定在哪里寻找。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doubai9014 2019-03-24 18:24
    已采纳

    The error actually gives you the answer: You must use that driver as go.mongodb.org/mongo-driver/bson instead. The package has apparently changed URLs some time in the past, and the tutorial you're referring to has not yet been updated.

    You should instead refer to the installation instructions here. In short, do this:

    go get go.mongodb.org/mongo-driver/mongo
    

    then import it using the same path:

    import (
        "go.mongodb.org/mongo-driver/mongo"
        "go.mongodb.org/mongo-driver/bson"
        // etc
    )
    
    打赏 评论

相关推荐 更多相似问题