如何配置 goland 以识别“mod”包?

新手上路,请多包涵

我正在试用 go1.11rc1,我注意到的第一件事是 goland 不识别导入。

goland 版本公告 说:“开箱即用地支持 Go 模块(以前称为 vgo)”

有人知道怎么修这个东西吗?

问题:

  1. 像“github.com/urfave/cli”这样的包颜色为红色,悬停文本显示:“无法解析目录…”
  2. 导入的包项目,如“app := cli.NewApp()”中的“NewApp”,颜色为红色,悬停文本显示:“未解决的参考……”

重现步骤:

  1. 安装go1.11rc1:删除当前安装,安装1.11rc1,检查版本。
  2. 在go路径外新建工程目录: mkdir pjg && cd pjg
  3. 创建一个 go.mod 文件: go mod init github.com/stevetarver/pjg
  4. 向项目添加包: go get github.com/urfave/cli

go.mod 文件现在看起来像:

 module github.com/stevetarver/pjg/v1

require github.com/urfave/cli v1.20.0 // indirect

创建 main.go

 package main

import (
    "fmt"
    "log"
    "os"

    "github.com/urfave/cli"
)

func main() {
    app := cli.NewApp()
    app.Name = "boom"
    app.Usage = "make an explosive entrance"
    app.Action = func(c *cli.Context) error {
        fmt.Println("boom! I say!")
        return nil
    }

    err := app.Run(os.Args)
    if err != nil {
        log.Fatal(err)
    }
}

在 goland 中查看 main.go ,并将鼠标悬停在红色文本上以查看问题。

  • mod包存储在 $GOPATH/pkg/mod/
  • 戈兰版本:2018.2.1
  • 去版本:go1.11rc1 darwin/amd64

笔记:

  • $GOPATH 设置正确- go get 将包放在正确的位置,env中的GOPATH匹配goland首选项。
  • 将 goland 偏好设置 Go -> GOPATH -> Module GOPATH to /Users/starver/code/go/pkg/mod 没有解决这个问题。

原文由 Steve Tarver 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 933
1 个回答

这个网站 上的答案对我有用。基本上,通过以下方式在 GoLand 中启用 go 模块:

确保在设置中启用了 Go Modules 集成(Preferences / Settings | Go | Go Modules),并且禁用了 GOPATH 索引(Preferences / Settings | Go | GOPATH | Index entire GOPATH)。

我一这样做,红色进口就消失了,一切正常。

原文由 gMale 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题