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:
Steps to reproduce:
mkdir pjg && cd pjg
go.mod
file: go mod init github.com/stevetarver/pjg
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.
$GOPATH/pkg/mod/
Notes:
$GOPATH
is set correctly - go get
put the package in the right place, GOPATH in env matches goland preferences./Users/starver/code/go/pkg/mod
did not fix this.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...
$GOPATH
:$GOPATH
: $GOPATH/src/github.com/stevetarver/insidegopath
main.go
file referencing a package that does not exist in your $GOPATH
.Using go get
the GoLand way via vgo
as described in the gif here:
go: go mod -sync is now go mod tidy
Using go get
the GoLand embedded terminal way:
go get
your import.
ᐅ 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:
GO111MODULE=on
: Open Preferences -> Appearance & Behavior -> Path Variables, and add GO111MODULE=on
.env | grep GO111MODULE
in the terminal produces nothing.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
$GOPATH
:$GOPATH
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.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
.go get github.com/urfave/cli
and it is fetched to $GOPATH/pkg/mod
as expected.go mod
support to an existing new project:This turned out to be really simple - best way of working with go modules in GoLand:
go.mod
with go mod init module-name
.