What does Enable Go modules integration do in Intellij IDE - go

Not having previous knowledge about creating a project from zero within terminal, I've created a folder, cd into of it then run go mod init my_project_name which created a go.mod file for me, then I've created main.go file which built just ok.
Then I created a folder and add a go file inside with package name (being same with directory) and create a struct inside of it. Next I tried to import that package in main package but when I try to build on terminal it gave me this error
go: cannot determine module path for source directory /Users/berkcan/workspace/go/my_project_name (outside GOPATH, module path must be specified)
After googling and not being able to find a solution to my problem, I've imported project to beloved Intellij IDE and I enabled Go modules integration then everything worked flawlessly. First I thought IDE doing some magic inside while building project but even when I try go build command in terminal, it built. But I cant see difference in project structure or a new line in go.mod file.
So what happened, what did Intellij IDE did when I ticked go module integration box and what I can do enable it on terminal without Intellij IDE ?
here is the photo of option in IDE when ticked

IntelliJ IDEA plus Go plugin or GoLand under the hood has two modes to get the information about your packages (simplified):
GOPATH. IDEA scans your $GOPATH directory to build internal indexes of your packages and provides code completion, resolving, etc.
Go Modules. IDEA executes go list -m -json to resolve your dependencies and scans your $GOPATH/pkg/mod directory (default value of GOMODCACHE) for the packages. If they don't exist, IDEA executes go mod download. After these operations, the IDE provides all built-in features like code completion, navigation and so on.
Both modes don't change your Go or environment variables as well as behavior in the terminal. When you check Enable Go Modules integration option in the settings, the IDE just switches the mode from scan $GOPATH to execute go list and resolve your dependencies from the Go Modules cache.
To summarize, IntelliJ IDEA doesn't do any magic. I suppose it relates to your custom Go environment variables inside the terminal, especially GO111MODULE and if you didn't pass these variables to the GUI apps (e.g. you have specified it in .zshrc file and run the IDE via Desktop entry instead of the terminal), IntelliJ IDEA doesn't inherit them. You can compare go env output inside your local terminal and built-in inside the IDE (View | Tool Windows | Terminal) and find differences.

Related

Building go/src packages from golang/go Github Repository

Asking to see what the appropriate workaround is for building individual go packages from the go/std library when working on a github repo fork. The issue encountered is summarized by the following error when building for example the src/go/parser package.
parser.go 21:2 use of internal package go/internal/typeparams is not allowed
Of course the project import paths all reference GOROOT library paths
usr/local/go
And I am typically working on projects from my own directory where my go path is set.
/Users/andewx/github/go/src/github.com/andewx/my_go_extension_project
The crux of the issue is that the clone/fork project uses standard library import paths and I would like to be able to work on my repo from my own GOPATH. But internal packages can't be imported in this case because the import directives will always point to my GOROOT.
The only two options I can think of is:
Extending my personal go project by initiating the repo inside of my /usr/local/go and working on the packages from there.
Changing my GOROOT to point to my current project and including current go binary go fmt and toolchains in my project
Ideally I can just leave everything as far as directory structure is concerned as is and I can redirect the standard import path for go standard library to my current project for this project only...
Any ideas from the community for solution #3 is what I'm looking for.
Extending my personal go project by initiating the repo inside of my /usr/local/go and working on the packages from there.
Changing my GOROOT to point to my current project and including current go binary go fmt and toolchains in my project
If you are trying to work on the go compiler and extend it I actually found that changing your GOROOT to point to your workspace and then copying over the binaries and tools you need as a suitable fix and it doesn't create any additional issues.
Export your environment variable export GOROOT="/my/go/project"
Find out where your go tools are located with go env GOTOOLDIR copy this directory structure into your project folder.
Copy go bin cp /usr/local/bin/* ${GOROOT}"/bin" copy in your go binary
Copy toolscp /usr/local/go/pkg/tool/linux_amd64/* ${GOROOT}/pkg/tool/linux_amd64
Now your project should be able to run the copied binaries with your project directories temporarily set as the GOROOT.

How to make VScode Go work in a Multi-Module Repo

I have a go.mod file in a golang project, and I also have another go project which is embedded in this project, but vscode does not seem to recognize this embedded project. Is there a way to work with multiple golang project with vscode?
You have to add the modules directory to your workspace using the "file / add folder to work space" menu.
What you have is a multi-module workspace and this is supported by Go and VSCode. If you are using Go 1.17 and earlier then fix is a configuration option on VSCode (experimentalWorkspaceModule setting). If you have Go 1.18+ then fix is using the new go.work file.
Check this article for more details https://github.com/golang/tools/blob/master/gopls/doc/workspace.md

How can make my GoLand to detect dependency packages under $GOPATH/pkg/mod?

I'm trying through kubebuilder tutorial, and just imported existing project to GoLand.
Workthrough with kubebuilder auto-generated scaffold codes, and auto-downloaded pkgs with go mod for me. I had no problem when I was working with commandline environment, but turns out, after opening the project with GoLand, IDE is failing to resolve imported package names, which means it is failing to detect - or link - packages installed by go mod.
I enabled go mod(with vendoring) and dep both from IDE preferences, set GOPATH and Go runtime properly, but the error keeps to appear.
I don't know why I'm getting this error, and how to solve it.
+) Working Directory is $GOPATH/src/example, all the logics and settings are placed in the directory. Installed dependencies are placed under $GOPATH/pkg/mod.
I enabled go mod(with vendoring) and dep both from IDE preferences, set GOPATH and GOROOT properly, but the error keeps to appear.
Let's assume that you have the following setup on your machine:
Go is installed under /usr/lib/go
GOPATH is set to /home/florin/go
the KubeBuilder project named demobuilder is created under /home/florin/projects/demobuilder. I recommend this as opposed to using GOPATH, as you do, for Go Modules projects because they behave in a different way while in GOPATH.
First, make sure that you have GoLand 2019.3.1 or newer.
Then, after creating the demobuilder project, start GoLand, then click on the Open Project button.
When the project is opened, if you have not configured yet, the IDE will ask you for the Go SDK configuration, aka GOROOT. It will be a yellow bar at the top of the editor. Click on the link on the right side to configure it. You can select the local installation and point it to /usr/local/go. If you don't have Go installed, you can also download it in a directory of your choice.
Then, the IDE should automatically notice that the project is a Go Modules based project and enable the support for them. If it's not, then go to Settings/Preferences | Go | Go Modules and enable that. DO NOT enable both Go Modules and dep support at the same time. If you did that, disable the dep integration and try again.
You can see all of these in the help page.
Is Go modules integration enabled via File | Settings | Languages & Frameworks | Go | Go Modules

How can I get GoLand to build everything?

Is it possible to do the equivalent of go build ./... in GoLand, so I can see all the errors in the IDE in one go? I have modified some widely used structs and I want to find all the places I need to change.
I have tried a package build at the top of my tree but that "succeeds" very quickly whereas build ./... in the same directory fails as expected.
I also tried a directory build with ./... as a tool argument but that gives an error
can't load package: package -o: cannot find package "-o" in any of:
which I assume is because whatever GoLand is putting around the configuration is not compatible with that argument.
As #nestor-sokil, mentioned above, if you perform the refactoring operations via the IDE tools, Refactor | Refactor This..., then the IDE manages all the changes automatically for you. If you do find places where this doesn't happen or a refactoring tool is missing, you can open an issue on the issue tracker.
That being said, you can follow the instructions below to configure the IDE to run go build ./... for you. The caveat is that it may show problems that are not normally encountered in operations as the command is unaware of build tags, multiple main() function containing files in the same directory, and so on.
You can create an external tool via Settings/Preferences | Tools | External Tools and configure like this:
You can then go to Settings/Preferences | Keymap and search for the name of the tool and assign it a shortcut to execute it more conveniently.

Intellij Golang cannot find a module

I am using Golang plugin for IntelliJ, after set the GO Sdk for the module and project. I tried to edit configuration for the project. When I click the edit configuration, in module dropdown box, it shows no module. it seems the configuration cannot find the module. Anything that I missed? I have issue the following command in order to make Go SDK work in Intellij.
# mkdir /usr/lib/go/bin
# ln -s /usr/bin/go /usr/lib/go/bin/go
# ln -s /usr/bin/godoc /usr/lib/go/bin/godoc
# ln -s /usr/bin/gofmt /usr/lib/go/bin/gofmt
In the recent version of IntelliJ (here is screenshot from Ultimate 2019.1.3) you can find the GOROOT and GOPATH in Preferences -> Language & Frameworks -> Go -> GOROOT/GOPATH.
Install to Golang in your system (I used brew):
Since OSX ElCapitan, the OSX forbid access to /bin directory, which would cause problems with compiling the project with the default settings and having error like:
Cannot run program "/bin/go" (in directory "/Users/XXX/go/src/YYY/ZZZ/service"): error=2, No such file or directory
So this should be solved by updating GO preferences.
Open IntelliJ and your go project and update GOROOT in your project
If you have a custom directory of GOPATH do update it as well.
Click to run your go program.
Try the recent latest releases of that plugin, to see if the issue persists.
Note that you shouldn't have to link anything, just set your $GOROOT environment variable (or not, as mentioned in this article), and your $GOPATH.
this works for me:
Close GoLand.
Navigate to the project root, backup and delete .idea
directory.
Open the IDE again.
by https://youtrack.jetbrains.com/issue/GO-11854

Resources