英文:
Import Github package in go
问题
以下是代码部分的翻译:
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
)
在这些导入中,我无法解析aws-sdk-go包的导入。它总是建议在GOPATH:/src中创建此目录。
以下是我迄今为止尝试过的方法:
- 检查GOPATH是否正确。
- 我还执行了
go mod vendor
,结果在我的项目中生成了vendor目录,并且其中包含aws-sdk-go。 - 执行
go get github.com/aws/aws-sdk-go
,这将该SDK下载到我的GOPATH中,路径为pkg/mod/github.com/aws/aws-sdk-go@v1.44.138/aws
。 - 当我尝试在我的导入中使用
pkg/mod/github.com/aws/aws-sdk-go@v1.44.138/aws
时,它会报错,说导入中不能有@
符号。
我不明白为什么它不像其他导入github.com/aws/aws-lambda-go/lambda
一样引用vendor目录中的包。
有任何想法是怎么回事吗?
英文:
I'm trying out AWS Lambda in go and have the following imports.
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
)
Among these I'm unable to resolve the import for package aws-sdk-go. It always suggests to create such directory in GOPATH:/src.
Here's what I have tried so far:
- Checked GOPATH is right.
- I also did
go mod vendor
which resulted in vendor directory in my project and it has aws-sdk-go under it - Did go get
github.com/aws/aws-sdk-go
and this downloaded this sdk to my GOPATH and it looks like thispkg/mod/github.com/aws/aws-sdk-go@v1.44.138/aws
- When I try to use
pkg/mod/github.com/aws/aws-sdk-go@v1.44.138/aws
in my import, it complains that there cannot be@
in import.
I don't get why it doesn't refer to the package in vendor directory just like the other import github.com/aws/aws-lambda-go/lambda
Any ideas what's going on?
答案1
得分: 2
如上面的评论所示,问题不在于Go代码本身,而是在于IDE。
我使用的是IntelliJ,并且已经设置了GOPATH并禁用了模块集成。
为了解决这个问题,我移除了GOPATH并启用了模块集成。
为了理解模块,这篇博客对我很有帮助:https://go.dev/blog/using-go-modules
英文:
As indicated in the comments above, the issue was not with go code itself but the IDE.
I use IntelliJ and had GOPATH set and Module integration disabled.
To resolve the issue I removed GOPATH and enabled Module integration.
To understand Modules this blog helped me: https://go.dev/blog/using-go-modules
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论