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.GoLand can generate your import for you, even when you haven't installed the package. How to use: Press Alt + Enter on Windows/Linux or ⌥ + Enter on macOS to invoke the list of quick-fixes on the red import line and select Sync packages of <project> to import the package into your project.
Modules may be downloaded directly from version control repositories or from module proxy servers. A module is identified by a module path, which is declared in a go. mod file, together with information about the module's dependencies. The module root directory is the directory that contains the go.
The answers on this site worked for me. Basically, enable go modules in GoLand via:
Make sure Go Modules integration is enabled in settings (Preferences / Settings | Go | Go Modules), and GOPATH indexing is disabled (Preferences / Settings | Go | GOPATH | Index entire GOPATH).
As soon as I did this, the red imports went away and everything worked.
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
.The module management should be easier with Go 1.13 (Aug. 2019):
The
GO111MODULE
environment variable continues to default toauto
, but theauto
setting now activates the module-aware mode of thego
command whenever the current working directory contains, or is below a directory containing, ago.mod
file — even if the current directory is withinGOPATH/src
.This change simplifies the migration of existing code within
GOPATH/src
and the ongoing maintenance of module-aware packages alongside non-module-aware importers.
That means the all "Don't put your project inside $GOPATH
" part will no longer be needed.
As long as there is a go.mod
file, modules will be recognized, from command-line or from an IDE like Goland.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With