简体   繁体   English

如何导入官方mongodb驱动包

[英]How to import official mongodb driver package

How to import official mongoDB driver package in Go?如何在 Go 中导入官方 mongoDB 驱动程序包?

I am following the official Go-mongoDB-driver package instruction ( https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial ).我正在遵循官方的 Go-mongoDB-driver 包说明( https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial )。 I have installed the mongoDB package using this:我已经使用这个安装了 mongoDB 包:

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我在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:当我尝试导入go.mongodb.org/mongo-driver/bson ,它给了我这个:

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.请帮忙,在 Go 中很新,不知道在哪里看,因为我没有发现很多人有这个问题。

The error actually gives you the answer: You must use that driver as go.mongodb.org/mongo-driver/bson instead.该错误实际上为您提供了答案:您必须将该驱动程序用作go.mongodb.org/mongo-driver/bson The package has apparently changed URLs some time in the past, and the tutorial you're referring to has not yet been updated.该软件包显然在过去的某个时间更改了 URL,并且您所指的教程尚未更新。

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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM