GoLand (JetBrains) shows error message "Unresolved Reference". But Code compiles and runs
Solution 1
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
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 3
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 4
I just removed the project from Goland and re-create it from existing files. It was weird but it worked.
Solution 5
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.
Admin
Updated on June 06, 2022Comments
-
Admin about 2 years
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 (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)
andbox.AddItem(item2)
are marked red as an error. If I move my cursor above it, it saysunresolved 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]
-
Reven almost 4 yearsI can still reproduce it in 2020.2
-
dlsniper almost 4 yearsHow? More details would help us understand your problem and replicate it to either provide a solution or fix any problems we may have on our side.
-
Amin Shojaei almost 4 years@disniper same problem. My project is in
go/src/project/test.go
and I keep getting this error for everything in thegithub.com/thedevsaddam/govalidator
package -
dlsniper almost 4 years@AminShojaei 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 not, is Go modules support enabled under Settings/Preferences | Go | Go Modules and then use Alt+Enter | Sync packages of <project>. If neither of these solve the problem, then please open an issue on our tracker at youtrack.jetbrains.com/issues/Go
-
Amin Shojaei almost 4 years@disniper That's right. Git module solved my problem. thanks
-
Marcel Kirsche almost 4 yearsWorked also for IntelliJ Ultimate 2020.2
-
James over 3 yearsTHIS should be the accepted answer, thank you! This will 100% fix the issue for everyone experiencing this issue in GoLand only. My Go Modules were set up perfectly, so suggesting to move from GOPATH to GOMODULES was of no help from comments above
-
James over 3 yearsOpening it VSCode must've cleared the GoLand caching somehow, and re-indexed your code. File > Invalidate Cache/Restart... is the better way to do it
-
Andres over 3 yearsExcelent. This solved my issue with Goland 2020.3
-
Admin about 3 yearsThe problem occurs again if I generate a vendor directory by go mod vendor, and it will disappear after I remove the vendor directory
-
Blafasel42 about 3 yearsthanks. i was getting desperate... File->InvalidateCaches solved this for me in Goland 2021.1
-
Rajvir about 3 yearsThis is a life saver! Worked for me as well on Goland 2019.3.2
-
Edgar Cardona almost 3 yearsI tried to update the Go to the lastest and change de SDK version on the IDE but THIS answer is the right one! This must be marked as accepted!
-
Sindhu Shree almost 3 yearsThis worked for me earlier but doesn't anymore :/ Not sure why.
-
s0xzwasd almost 3 yearsMake sure that you create a backup of your .idea folder since it contains some local environment IDE settings (Go version, etc).
-
JavaQuest over 2 yearsWorked for IntelliJ Ultimate GoLand 2021.3.3 as well. Indeed a life saver. Was getting irritated why even restarting the goland didn't fix it. Thank you!
-
Rafael Eyng over 2 yearsI cleared the cache to try to fix another issue, then I started getting this error after clearing the caches (marked all the options to clear everything). Then cleared the cache again, this time only with the default options checked, and it solved.
-
Camilo Bernal over 2 yearsThank you very much, I was about to abort and switch to vscode. It seems that this tip worked for me.
-
Cyberience over 2 yearsThis should never be the option for Professional products, MS has made us weak.
-
Cyberience over 2 yearslike magic, I had the problem, tried the removing cache, then delete .idea folder, finally found this one, should be the final answer.
-
Nilay over 2 yearsThanks, can confirm this works for mac!
-
Akbarali over 2 yearsThanks, can confirm this works for Windows 10
-
swade about 2 yearsAs this doesn't always work, try the below answer on removing the .idea folder.
-
phemmer about 2 yearsI thought it went without saying that on any question, if one answer isn't acceptable, to consider the others.
-
VsMaX about 2 yearsThanks! Helped when I renamed project folder!
-
Anthony almost 2 yearsThis is what worked for me too, just deleting
.idea