GoLand (JetBrains) shows error message "Unresolved Reference". But Code compiles and runs
GoJetbrains IdeGolandGo Problem Overview
I am writing a project using the Go language with GoLand IDE by Jetbrains.
While writing the code, GoLand shows me an error message such as "unresolved reference"
when the reference do exist and that the program compiles and runs correctly.
Here is a similar (but simpler) example of some code that I have found here on stackoverflow (https://stackoverflow.com/questions/18042439/go-append-to-slice-in-struct) to reproduce this issue.
The same error message appears even though I have implemented the methods just a few lines above.
package main
import (
"fmt"
)
type MyBoxItem struct {
Name string
}
type MyBox struct {
Items []MyBoxItem
}
func (box *MyBox) AddItem(item MyBoxItem) {
box.Items = append(box.Items, item)
}
func main() {
item1 := MyBoxItem{Name: "Test Item 1"}
item2 := MyBoxItem{Name: "Test Item 2"}
box := MyBox{}
box.AddItem(item1)
box.AddItem(item2)
// checking the output
fmt.Println(len(box.Items))
fmt.Println(box.Items)
}
box.AddItem(item1)
and box.AddItem(item2)
are marked red as an error. If I move my cursor above it, it says unresolved reference "AddItem"
. Yet the code compiles and runs. And as this was the solution to an other stackoverflow question, I do not think that the code is wrong. Furthermore I cannot find any mistakes in it.
[EDIT: I load the code from a remote server and edit it locally on my private pc. After finishing my changes, I upload it to the remote server (using GoLands tools like "Browse remote host") and build and compile it there. After trying it out locally with the very same code, the error message sometimes is there and sometimes not. I am totally confused]
Go Solutions
Solution 1 - Go
I experienced a similar issue, but it was a lot more prevalent. Even things like fmt.Printf()
were showing as unresolved. Was able to resolve the issue by going to File -> Invalidate Caches / Restart.
Solution 2 - Go
I found best way to fix this issue.
- close your project and IDE
- go to your project directory
- remove /.idea
- try to open it again
woops woops, fixed
Solution 3 - Go
I'm using go module and it's solved by:
- Deselect Preferences->Go->GOPATH->Use GOPATH that's defined in system environment
- File->Invalidate caches / Restart
Solution 4 - Go
Today I faced that problem I fixed it to enable go module integration. For that Settings -> Go -> Go modules then enable go modules integration. This will work if you using go modules in your project.
Solution 5 - Go
I just removed the project from Goland and re-create it from existing files. It was weird but it worked.
Solution 6 - Go
I'm a bit late to the answer lol but incase anyone is still running into this, all I did was delete the .idea
file and reloaded the project on GoLand (by clicking File -> Open -> file location). Did the trick for me.
Solution 7 - Go
I cannot reproduce the issue in GoLand 2020.2. I suggest upgrading to it.
If that doesn't fix the issue then you can take the following steps to investigate the issue:
- Is your project using Go modules or the traditional GOPATH?
- If it's using GOPATH, have you enabled indexing of GOPATH under
Settings/Preferences | Go | GOPATH
? - If it's using Go modules, check to see that the support is enabled under
Settings/Preferences | Go | Go Modules
and then useAlt+Enter | Sync packages of <project>
Solution 8 - Go
In Goland preferences, if you're using the Global GOPATH, check the option "Index entire GOPATH" and hit apply.
Solution 9 - Go
I had a similar issue for gin.WrapH
function utils.go. Tried the Override File Type
option for utils.go in local module path which changed the file as a Go file but had a little cross sign where a tooltip marked that the file is excluded from compilation. The error Unresolved reference
only went away when I selected the file, navigated to File -> File Properties -> Associate with File Type -> Register new file type association
, and chose Go files
Solution 10 - Go
I had the same problem and it got fix weirdly.So I installed and opened project in vscode
in order to continue coding.It started installing a extension called gopls
. After installation completed I returned to GoLand to close project, but I waited for indexing to complete.Suddenly references were green !
Solution 11 - Go
No option from other comments helped me. I had to switch GO SDK version in Setting -> Go -> GOROOT. Goland automatically downloaded 1.16 beta 1 version and it worked.
Solution 12 - Go
Goland version 2020.1: I opened a folder with subfolders of golang projects and goland didn't recognize dependencies. I solved this problem setting Project GOPATH
ctrl + alt + s
- Go > GOPATH
- Click on plus button
+
In Project GOPATH - Add your golang's project folder, example:
~/projects/my-golang-projects
Solution 13 - Go
I faced the same issue when I do bazel run //:gazelle
, I fixed this issue by doing bazel sync (Bazel -> Sync -> Sync Project with BUILD Files
). But you should have the below settings.
(This fix is for Goland IDE, Mac. Of course we should add GOPATH in .bash_profile
or .zshrc
)
Solution 14 - Go
I solved it by reinstall Go to D:\go, then reset Go sdk.