简体   繁体   English

在 go 中导入 Github package

[英]Import Github package in go

这就是 go.mod 的样子 I'm trying out AWS Lambda in go and have the following imports.我正在 go 中试用 AWS Lambda 并进行以下导入。

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.其中我无法解决 package aws-sdk-go 的导入问题。 It always suggests to create such directory in GOPATH:/src.它总是建议在 GOPATH:/src 中创建这样的目录。 Here's what I have tried so far:到目前为止,这是我尝试过的:

  1. Checked GOPATH is right.查了GOPATH是对的。
  2. I also did go mod vendor which resulted in vendor directory in my project and it has aws-sdk-go under it我还做了go mod vendor ,它在我的项目中生成了 vendor 目录,它下面有 aws-sdk-go
  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 go 是否获得了github.com/aws/aws-sdk-go并将此 sdk 下载到我的 GOPATH,它看起来像这个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.当我尝试在我的导入中使用pkg/mod/github.com/aws/aws-sdk-go@v1.44.138/aws时,它抱怨导入中不能有@

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我不明白为什么它不像其他导入 github.com/aws/aws-lambda-go/lambda 那样引用供应商目录中的github.com/aws/aws-lambda-go/lambda

Any ideas what's going on?任何想法发生了什么?

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.如上面的评论所示,问题不在于 go 代码本身,而在于 IDE。我使用 IntelliJ 并设置了 GOPATH 并禁用了模块集成。 To resolve the issue I removed GOPATH and enabled Module integration.为了解决这个问题,我删除了 GOPATH 并启用了模块集成。

To understand Modules this blog helped me: https://go.dev/blog/using-go-modules要了解模块,这个博客帮助了我: https://go.dev/blog/using-go-modules

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

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