如何配置goland识别"mod"包? [英] How do I configure goland to recognize 'mod' packages?

查看:340
本文介绍了如何配置goland识别"mod"包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试用go1.11rc1,我注意到的第一件事是goland无法识别进口.

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

有人知道如何解决这个问题吗?

问题:

  1. 诸如"github.com/urfave/cli"之类的软件包显示为红色,并且悬停文本显示:无法解析目录..."
  2. 导入的包装项目,如"app:= cli.NewApp()"中的"NewApp",显示为红色,并且悬停文字显示:未解决的引用..."

复制步骤:

  1. 安装go1.11rc1:删除当前安装,安装1.11rc1,检查版本.
  2. 在转到路径之外创建一个新的项目目录: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/
  • goland版本:2018.2.1
  • go版本:go1.11rc1 darwin/amd64

注意:

  • $GOPATH的设置正确-go get将软件包放置在正确的位置,env中的GOPATH匹配goland的首选项.
  • 将goland首选项设置为Go-> GOPATH->将模块GOPATH设置为/Users/starver/code/go/pkg/mod不能解决此问题.

解决方案

GoLand支持

GoLand的最新版本实现了对vgo和go模块的支持,但尚未赶上go1.11rc1语法更改.以防万一在过渡期间对某人有帮助,我将记录我尝试过的事情以及他们的问题和成功.

TL; DR :不要将您的项目放入$GOPATH并以"Go Module(vgo)"类型创建新项目,或者为现有项目启用该设置./p>

将go1.11rc1安装为全局go后,GoLand中的go mod项目有三种基本用例...

在内部 $GOPATH创建一个新项目:

  1. 创建类型为"Go Module(vgo)"的新项目:File-> New,选择"Go Module(vgo)"
  2. 将项目目录设置为$GOPATH中的某个目录:$GOPATH/src/github.com/stevetarver/insidegopath
  3. 引用您的$GOPATH中不存在的程序包,创建您的main.go文件.
  4. 将该软件包添加到您的导入中.

如gif vgo使用go get GoLand方式. ="noreferrer">此处:

  1. 单击导入包.
  2. 单击红色检查灯泡.
  3. 单击同步...的软件包".
  4. 失败:go: go mod -sync is now go mod tidy

使用go get GoLand嵌入式终端方式:

  1. 打开嵌入式终端.
  2. go get您的导入.
  3. 失败: ᐅ go get github.com/urfave/cli go get: warning: modules disabled by GO111MODULE=auto in GOPATH/src; ignoring go.mod; see 'go help modules'

打开该变量,然后重试:

  1. 注意:终端插件首选项无法设置环境变量.
  2. 设置GO111MODULE=on:打开偏好设置"->外观&行为->路径变量,然后添加GO111MODULE=on.
  3. 退出终端,重试,重新启动GoLand,重试,与上述相同.
  4. 终端中的
  5. env | grep GO111MODULE不产生任何结果.
  6. 注意:如果可以解决,那将是一个糟糕的解决方案-GoLand似乎没有针对每个项目的设置-该变量将在所有项目中均已启用这会破坏那些尚未准备就绪的模块.
  7. 根据此答案,您可以创建一个自定义命令行启动器以包含此环境变量,但是eeuuwww-如何您是否会跟踪何时正常启动GoLand以及何时使用命令行启动器?

您可以在shell初始化脚本中设置GO111MODULE=on,但这会破坏所有尚未使用go模块的项目.

您还可以在每个go命令前面加上env var:export GO111MODULE=on; go get github.com/urfave/cli或在您的项目目录中创建一个go Shell脚本包装程序来为您执行此操作.

这些都不是真正可行的解决方案,但是go模块的一部分要从可怕的go工作区中逃脱,所以继续阅读会变得更好

创建新项目外部 $GOPATH:

  1. 创建类型为"Go Module(vgo)"的新项目:File-> New,选择"Go Module(vgo)"
  2. 将项目目录设置为$GOPATH
  3. 以外的目录
  4. 修复您的go.mod:生成的文件包含module "outsidegopath",但是我们需要类似module github.com/stevetarver/outsidegopath的文件.这有点奇怪-GoLand将尝试重写go.mod并删除路径的一部分.重复几次,它将停止尝试.
  5. 创建您的main.go文件.如果通过ide作为go文件创建此文件,它将包含package outsidegopath.将其修复为package main.
  6. 现在您可以go get github.com/urfave/cli了,它已按预期提取到$GOPATH/pkg/mod.

一个现有的新项目添加go mod支持:

事实证明这非常简单-在GoLand中使用go模块的最佳方法:

  1. 打开首选项:转到->转到模块(vgo),并选中启用转到模块(vgo)集成"
  2. 如上所述的工作方式-但您可以使用go mod init module-name创建自己的go.mod.

I am taking go1.11rc1 for a spin and the first thing I noticed is that goland does not recognize imports.

The goland version announcement says: "support for Go modules out of the box (formerly known as vgo)"

Anyone know how to fix this?

Problem:

  1. Packages like "github.com/urfave/cli" colored red and hover text says: "Cannot resolve directory..."
  2. Imported package items like "NewApp" in "app := cli.NewApp()" colored red and hover text says: "Unresolved reference ..."

Steps to reproduce:

  1. Install go1.11rc1: remove the current install, install 1.11rc1, check version.
  2. Create a new project directory outside the go path: mkdir pjg && cd pjg
  3. Create a go.mod file: go mod init github.com/stevetarver/pjg
  4. Add a package to the project: go get github.com/urfave/cli

go.mod file now looks like:

module github.com/stevetarver/pjg/v1

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

Create 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)
    }
}

View main.go in goland, and hover over red text to see problem.

  • mod packages are stored in $GOPATH/pkg/mod/
  • goland version: 2018.2.1
  • go version: go1.11rc1 darwin/amd64

Notes:

  • $GOPATH is set correctly - go get put the package in the right place, GOPATH in env matches goland preferences.
  • Setting goland preferences Go -> GOPATH -> Module GOPATH to /Users/starver/code/go/pkg/mod did not fix this.

解决方案

GoLand support

The latest version of GoLand implemented support for vgo and go modules, but it hasn't caught up with go1.11rc1 syntax changes. Just in case it helps someone in the interim, I am going to document the things I tried and their problems and successes.

TL;DR: Don't put your project inside $GOPATH AND create your new project as a "Go Module (vgo)" type, OR turn that setting on for existing projects.

With go1.11rc1 installed as your global go, there are three basic use cases for go mod projects in GoLand...

Create a new project inside $GOPATH:

  1. Create a new project of type "Go Module (vgo)": File -> New, select "Go Module (vgo)"
  2. Set your project directory to something inside $GOPATH: $GOPATH/src/github.com/stevetarver/insidegopath
  3. Create your main.go file referencing a package that does not exist in your $GOPATH.
  4. Add that package to your imports.

Using go get the GoLand way via vgo as described in the gif here:

  1. Click on the import package.
  2. Click on the red inspection bulb.
  3. Click on "Sync packages of ...".
  4. FAIL: go: go mod -sync is now go mod tidy

Using go get the GoLand embedded terminal way:

  1. Open the embedded terminal.
  2. go get your import.
  3. FAIL: ᐅ go get github.com/urfave/cli go get: warning: modules disabled by GO111MODULE=auto in GOPATH/src; ignoring go.mod; see 'go help modules'

Let's turn that variable on and try again:

  1. Note: the terminal plugin preferences provide no way to set environment variables.
  2. Set GO111MODULE=on: Open Preferences -> Appearance & Behavior -> Path Variables, and add GO111MODULE=on.
  3. Exit the terminal, retry, restart GoLand, retry, same failure as above.
  4. env | grep GO111MODULE in the terminal produces nothing.
  5. NOTE: if this would have worked, it would have been a bad solution - GoLand does not appear to have a per-project settings for this - that variable would have been turned on for all projects which would break those that aren't ready for go modules.
  6. According to this answer, you can create a custom command line launcher to include this env var, but eeuuwww - how would you keep track of when to start GoLand normally and when to use the command line launcher?

You could set GO111MODULE=on in your shell init script, but that would break all the projects that don't use go modules yet.

You could also prefix each go command with the env var: export GO111MODULE=on; go get github.com/urfave/cli or create a go shell script wrapper in your project directory that does this for you.

None of these are really workable solutions, but part of the point of go modules is escape from the dreaded go workspace, so read on, it gets better

Create a new project outside $GOPATH:

  1. Create a new project of type "Go Module (vgo)": File -> New, select "Go Module (vgo)"
  2. Set your project directory to something outside $GOPATH
  3. Fix up your go.mod: the generated file contains module "outsidegopath", but we want something like module github.com/stevetarver/outsidegopath. This is a bit wonky - GoLand will try to rewrite go.mod and remove parts of the path. Repeat a couple times and it will stop trying.
  4. Create your main.go file. If you create this through the ide as a go file, it will contain package outsidegopath. Fix that to be package main.
  5. Now you can go get github.com/urfave/cli and it is fetched to $GOPATH/pkg/mod as expected.

Add go mod support to an existing new project:

This turned out to be really simple - best way of working with go modules in GoLand:

  1. Open Preferences: Go -> Go Module (vgo), and check "Enable Go Modules (vgo) integration"
  2. Works as described above - but you create your own go.mod with go mod init module-name.

这篇关于如何配置goland识别"mod"包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆