Import Github package in go

huangapple go评论76阅读模式
英文:

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中创建此目录。
以下是我迄今为止尝试过的方法:

  1. 检查GOPATH是否正确。
  2. 我还执行了go mod vendor,结果在我的项目中生成了vendor目录,并且其中包含aws-sdk-go。
  3. 执行go get github.com/aws/aws-sdk-go,这将该SDK下载到我的GOPATH中,路径为pkg/mod/github.com/aws/aws-sdk-go@v1.44.138/aws
  4. 当我尝试在我的导入中使用pkg/mod/github.com/aws/aws-sdk-go@v1.44.138/aws时,它会报错,说导入中不能有@符号。

我不明白为什么它不像其他导入github.com/aws/aws-lambda-go/lambda一样引用vendor目录中的包。

有任何想法是怎么回事吗?

英文:

Import Github package in goI'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:

  1. Checked GOPATH is right.
  2. I also did go mod vendor which resulted in vendor directory in my project and it has aws-sdk-go under it
  3. Did go get github.com/aws/aws-sdk-go and this downloaded this sdk to my GOPATH and it looks like this pkg/mod/github.com/aws/aws-sdk-go@v1.44.138/aws
  4. 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

huangapple
  • 本文由 发表于 2022年11月16日 16:58:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/74457668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定