GoLand (JetBrains) shows error message "Unresolved Reference". But Code compiles and runs

19,645

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.

Share:
19,645
Admin
Author by

Admin

Updated on June 06, 2022

Comments

  • Admin
    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) 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.

    enter image description here

    [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
    Reven almost 4 years
    I can still reproduce it in 2020.2
  • dlsniper
    dlsniper almost 4 years
    How? 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
    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 the github.com/thedevsaddam/govalidator package
  • dlsniper
    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
    Amin Shojaei almost 4 years
    @disniper That's right. Git module solved my problem. thanks
  • Marcel Kirsche
    Marcel Kirsche almost 4 years
    Worked also for IntelliJ Ultimate 2020.2
  • James
    James over 3 years
    THIS 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
    James over 3 years
    Opening 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
    Andres over 3 years
    Excelent. This solved my issue with Goland 2020.3
  • Admin
    Admin about 3 years
    The problem occurs again if I generate a vendor directory by go mod vendor, and it will disappear after I remove the vendor directory
  • Blafasel42
    Blafasel42 about 3 years
    thanks. i was getting desperate... File->InvalidateCaches solved this for me in Goland 2021.1
  • Rajvir
    Rajvir about 3 years
    This is a life saver! Worked for me as well on Goland 2019.3.2
  • Edgar Cardona
    Edgar Cardona almost 3 years
    I 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
    Sindhu Shree almost 3 years
    This worked for me earlier but doesn't anymore :/ Not sure why.
  • s0xzwasd
    s0xzwasd almost 3 years
    Make sure that you create a backup of your .idea folder since it contains some local environment IDE settings (Go version, etc).
  • JavaQuest
    JavaQuest over 2 years
    Worked 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
    Rafael Eyng over 2 years
    I 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
    Camilo Bernal over 2 years
    Thank you very much, I was about to abort and switch to vscode. It seems that this tip worked for me.
  • Cyberience
    Cyberience over 2 years
    This should never be the option for Professional products, MS has made us weak.
  • Cyberience
    Cyberience over 2 years
    like magic, I had the problem, tried the removing cache, then delete .idea folder, finally found this one, should be the final answer.
  • Nilay
    Nilay over 2 years
    Thanks, can confirm this works for mac!
  • Akbarali
    Akbarali over 2 years
    Thanks, can confirm this works for Windows 10
  • swade
    swade about 2 years
    As this doesn't always work, try the below answer on removing the .idea folder.
  • phemmer
    phemmer about 2 years
    I thought it went without saying that on any question, if one answer isn't acceptable, to consider the others.
  • VsMaX
    VsMaX about 2 years
    Thanks! Helped when I renamed project folder!
  • Anthony
    Anthony almost 2 years
    This is what worked for me too, just deleting .idea