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 add GO111MODULE=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 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.
- 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
.
- 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
with go mod init module-name
.