如何配置goland识别"mod"包? [英] How do I configure goland to recognize 'mod' packages?
问题描述
我正在试用go1.11rc1,我注意到的第一件事是goland无法识别进口.
goland版本公告说:开箱即用的Go模块支持(以前称为vgo)"
有人知道如何解决这个问题吗?
问题:
- 诸如"github.com/urfave/cli"之类的软件包显示为红色,并且悬停文本显示:无法解析目录..."
- 导入的包装项目,如"app:= cli.NewApp()"中的"NewApp",显示为红色,并且悬停文字显示:未解决的引用..."
复制步骤:
- 安装go1.11rc1:删除当前安装,安装1.11rc1,检查版本.
- 在转到路径之外创建一个新的项目目录:
mkdir pjg && cd pjg
- 创建一个
go.mod
文件:go mod init github.com/stevetarver/pjg
- 将包添加到项目:
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
创建一个新项目:
- 创建类型为"Go Module(vgo)"的新项目:File-> New,选择"Go Module(vgo)"
- 将项目目录设置为
$GOPATH
中的某个目录:$GOPATH/src/github.com/stevetarver/insidegopath
- 引用您的
$GOPATH
中不存在的程序包,创建您的main.go
文件. - 将该软件包添加到您的导入中.
如gif vgo使用go get
GoLand方式. ="noreferrer">此处:
- 单击导入包.
- 单击红色检查灯泡.
- 单击同步...的软件包".
- 失败:
go: go mod -sync is now go mod tidy
使用go get
GoLand嵌入式终端方式:
- 打开嵌入式终端.
-
go get
您的导入. - 失败:
ᐅ go get github.com/urfave/cli go get: warning: modules disabled by GO111MODULE=auto in GOPATH/src; ignoring go.mod; see 'go help modules'
打开该变量,然后重试:
- 注意:终端插件首选项无法设置环境变量.
- 设置
GO111MODULE=on
:打开偏好设置"->外观&行为->路径变量,然后添加GO111MODULE=on
. - 退出终端,重试,重新启动GoLand,重试,与上述相同. 终端中的
-
env | grep GO111MODULE
不产生任何结果. - 注意:如果可以解决,那将是一个糟糕的解决方案-GoLand似乎没有针对每个项目的设置-该变量将在所有项目中均已启用这会破坏那些尚未准备就绪的模块.
- 根据此答案,您可以创建一个自定义命令行启动器以包含此环境变量,但是eeuuwww-如何您是否会跟踪何时正常启动GoLand以及何时使用命令行启动器?
您可以在shell初始化脚本中设置GO111MODULE=on
,但这会破坏所有尚未使用go模块的项目.
您还可以在每个go命令前面加上env var:export GO111MODULE=on; go get github.com/urfave/cli
或在您的项目目录中创建一个go
Shell脚本包装程序来为您执行此操作.
这些都不是真正可行的解决方案,但是go模块的一部分要从可怕的go工作区中逃脱,所以继续阅读会变得更好
创建新项目外部 $GOPATH
:
- 创建类型为"Go Module(vgo)"的新项目:File-> New,选择"Go Module(vgo)"
- 将项目目录设置为
$GOPATH
以外的目录
- 修复您的
go.mod
:生成的文件包含module "outsidegopath"
,但是我们需要类似module github.com/stevetarver/outsidegopath
的文件.这有点奇怪-GoLand将尝试重写go.mod
并删除路径的一部分.重复几次,它将停止尝试. - 创建您的
main.go
文件.如果通过ide作为go文件创建此文件,它将包含package outsidegopath
.将其修复为package main
. - 现在您可以
go get github.com/urfave/cli
了,它已按预期提取到$GOPATH/pkg/mod
.
为一个现有的新项目添加go mod
支持:
事实证明这非常简单-在GoLand中使用go模块的最佳方法:
- 打开首选项:转到->转到模块(vgo),并选中启用转到模块(vgo)集成"
- 如上所述的工作方式-但您可以使用
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:
- Packages like "github.com/urfave/cli" colored red and hover text says: "Cannot resolve directory..."
- Imported package items like "NewApp" in "app := cli.NewApp()" colored red and hover text says: "Unresolved reference ..."
Steps to reproduce:
- Install go1.11rc1: remove the current install, install 1.11rc1, check version.
- Create a new project directory outside the go path:
mkdir pjg && cd pjg
- Create a
go.mod
file:go mod init github.com/stevetarver/pjg
- 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
:
- Create a new project of type "Go Module (vgo)": File -> New, select "Go Module (vgo)"
- Set your project directory to something inside
$GOPATH
:$GOPATH/src/github.com/stevetarver/insidegopath
- Create your
main.go
file referencing a package that does not exist in your$GOPATH
. - Add that package to your imports.
Using go get
the GoLand way via vgo
as described in the gif here:
- Click on the import package.
- Click on the red inspection bulb.
- Click on "Sync packages of ...".
- FAIL:
go: go mod -sync is now go mod tidy
Using go get
the GoLand embedded terminal way:
- Open the embedded terminal.
go get
your import.- 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:
- Note: the terminal plugin preferences provide no way to set environment variables.
- Set
GO111MODULE=on
: Open Preferences -> Appearance & Behavior -> Path Variables, and addGO111MODULE=on
. - Exit the terminal, retry, restart GoLand, retry, same failure as above.
env | grep GO111MODULE
in the terminal produces nothing.- 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.
- 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
:
- Create a new project of type "Go Module (vgo)": File -> New, select "Go Module (vgo)"
- Set your project directory to something outside
$GOPATH
- Fix up your
go.mod
: the generated file containsmodule "outsidegopath"
, but we want something likemodule github.com/stevetarver/outsidegopath
. This is a bit wonky - GoLand will try to rewritego.mod
and remove parts of the path. Repeat a couple times and it will stop trying. - Create your
main.go
file. If you create this through the ide as a go file, it will containpackage outsidegopath
. Fix that to bepackage main
. - 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:
- Open Preferences: Go -> Go Module (vgo), and check "Enable Go Modules (vgo) integration"
- Works as described above - but you create your own
go.mod
withgo mod init module-name
.
这篇关于如何配置goland识别"mod"包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!