Can't use installed packages in GoLand - windows

I'm using Go 1.16 and GoLand 2020.3.2.
If I type import "github.com/a/b", GoLand will ask me to install the package. I click install and it does so successfully. The library is installed under C:\Users\user\go\pkg\mod. But GoLand looks for the package in C:\Users\user\go\go1.16\src.
This my GOPATH C:\Users\user\go and GOROOT C:\Users\user\go\go1.16.
What am I doing wrong?

It looks like a problem with GoLand's handling of GOPATH based projects.
I created a ticket for this on our tracker.
As a workaround, you can create a go.mod file or a project based on Go Modules instead and the problem will be solved.

Related

how to resolve definition(GoDef) with gopls when I don't have go.mod file

I have a project, which doesn't use go.mod, and not follow the structure that project is under $GOPATH/src/.... It uses bazel and WORKSPACE file to manage depedencies.
I use gopls in neovim, and it could not resolve imports like import github.com/pkg/errors, so I am thinking to install the package one by one. However, go get needs to be with a go.mod file, and go install only works when installing a binary.
What option I have here? Thanks!

Goland does not recognise my vendor directory with Go 1.14

When I open my Goland project, the following command is run:
/usr/local/Cellar/go/1.14/libexec/bin/go list -m -json all #gosetup
Its output is the following error:
go list -m: can't compute 'all' using the vendor directory
(Use -mod=mod or -mod=readonly to bypass.)
Goland cannot resolve the packages I'm importing.
When I build and run the project from the command line, it works fine.
My project is structured as follows:
app/
bin/
pkg/
src/
app/
cmd/
vendor/
go.mod
My GOROOT is /usr/local/Cellar/go/1.14/libexec and my GOPATH is app/.
In Goland settings, under Go/GOPATH have checked the boxes for "Use GOPATH that's defined in system environment" and "Index entire GOPATH".
Under Go/Go Modules (vgo), I have checked "Enable Go Modules (vgo) integration" and "Vendoring mode"
I have just noticed when I change the GOPATH to /Users/myname/sdk/go1.13.4, the problem is solved. Could this be an issue with Homebrew or a change introduced with go 1.14 that I am not aware of?
I'm being exhaustive when describing the issue because I'm unfamiliar with Go and might be missing something obvious.
Thanks!
This is a known problem with Go Modules and vendoring support due to tooling change in Go 1.14.
As such, please try the EAP version of the IDE, https://jetbrains.com/go/nextversion, which contains a fix for this.
We are tracking this under https://youtrack.jetbrains.com/issue/GO-8855 and thinking about backporting this to the 2019.3 release cycle.
Disabling Go modules integration in Goland works for me
File->Preferences->Go->Go Modules->Enable Go modules integration

Using modules, newly installed package cannot be referenced within project

go version go1.11.4 darwin/amd64
GOPATH has been unset but was previously set to $HOME/Development/go
Project path is under $HOME/code/
I’m able to successfully (no errors at least) install the go-sql-driver/mysql package using the command
go get github.com/go-sql-driver/mysql#v1
When I include a reference to that package in an import statement
import(
_ "github.com/go-sql-driver/mysql")
in my code I see the error
could not import github.com/go-sql-driver/mysql (can’t find import:
“github.com/go-sql-driver/mysql”)
I have run go mod init in my project root and it creates a go.mod file. When I run the go get command I see a require statement added to that file for the package. But it seems the files for the package get installed in the default $HOME/go directory (since I've unset GOPATH).
Should I be doing things differently so that my import statement can find the newly installed package? Using modules shouldn't all the packages be installed in the project path somewhere?
Should I be doing things differently so that my import statement can find the newly installed package?
No. With modules there is no need to install the packages upfront at all.
Using modules shouldn't all the packages be installed in the project path somewhere?
No. They get downloaded somewhere in some format and used from that location but they are not "installed" like in the old GOPATH variant of go get.
Show output of go env and what go mod vendor produces.
I'm pretty sure I was doing things wrong. I was able to resolve this after referencing the following closely the steps documented at golang modules wiki. The summary is that there is no need to "install" a package via 'go get'. Instead simply make sure your project is initialized to use modules using the 'go mod init' command and then include the package name in an import statement. The next build event will pull down the package and all its dependencies.

Automatically import 3rd party go packages

I was wondering if there is a way to automatically download all the imports.
So let's assume I need to use github.com/gorilla/mux and several other packages in my code base. Should I manually go to my ~/go/src and run go get repo or is there a smarter way of doing dependency management. I am using Goland IDE for my development.
if there is a way to automatically download all the imports
You can download all imported pkgs and their dependencies by running go get from the command line.
I am using Goland IDE for my development
I'm using Goland too. When imports can't be found (ie the import path is highlighted in red), you can place your typing caret over it and press alt + enter and select go get ... from the popup window to automatically import.
There are several approaces:
Simply go get github.com/gorilla/mux which will download sources in your $GOPATH and will be resolved automatically when compiling
Use dependency management (godep, glide[deprecated])
Use modules (experimental feature in Go 1.11 - Module. Check more here)
If you want a good and stable solution, use dep (.First you have to install it, then run:
cd $GOPATH/src/path/to/project
dep init
dep ensure -add github.com/gorilla/mux
You will see a new folder vendor in your project and 2 new dependency configuration files Gopkg.lock and Gopkg.toml.
Read more about godep here.
Then run your main file as usual.
You can use dep package manager which will go through your code and automatically import all the packages you use in you code. If you are working with >go1.11 I would suggest to use newly added go mod.

Go vendoring outside $GOPATH

I have a project which is built in Node.js/Express.js. I want to start to rewrite this to go/iris framework. I don't want to re-factor everything into my $GOPATH and I want to keep it together my express / go / docker files for this project.
I tried to clone iris framework's git repo into a ./vendor subfolder, but using import "github.com/kataras/iris" importing nothing.
Is there a package manager which is
copying and installing packages and all of it's dependencies in my-project/vendor folder outside $GOPATH
it can update these import packages
go run/build/install outside $GOPATH
there's now any new files in $GOPATH src/pkg/bin folder when I working on a project, except this package manager
I can define dependency packages for a project like package.json file for node.js
Is there a go package manager like that?
Edit:
Running this with go command is not required.
Not possible with the current go tooling, but looks like we might get it in go 1.12 or bit later.
Proposal accepted:
cmd/go: modify the Go toolchain to work without GOPATH

Resources