59

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]

mh-cbon
  • 6,683
  • 2
  • 27
  • 51
David
  • 601
  • 1
  • 4
  • 4

14 Answers14

114

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.

phemmer
  • 5,473
  • 3
  • 30
  • 29
  • 4
    Worked also for IntelliJ Ultimate 2020.2 – Marcel Kirsche Aug 18 '20 at 07:04
  • 6
    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 Oct 26 '20 at 09:05
  • Excelent. This solved my issue with Goland 2020.3 – Andres Jan 06 '21 at 05:40
  • 3
    thanks. i was getting desperate... File->InvalidateCaches solved this for me in Goland 2021.1 – Blafasel42 Apr 12 '21 at 10:18
  • This is a life saver! Worked for me as well on Goland 2019.3.2 – Rajvir Apr 19 '21 at 04:31
  • 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! – Edgar Cardona Jul 30 '21 at 02:06
  • This worked for me earlier but doesn't anymore :/ Not sure why. – Sindhu Shree Aug 31 '21 at 07:22
  • 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! – JavaQuest Feb 07 '22 at 17:31
  • 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. – Rafael Eyng Feb 20 '22 at 14:57
  • As this doesn't always work, try the below answer on removing the .idea folder. – swade May 25 '22 at 19:35
  • I thought it went without saying that on any question, if one answer isn't acceptable, to consider the others. – phemmer May 26 '22 at 20:21
15

I found best way to fix this issue.

  1. close your project and IDE
  2. go to your project directory
  3. remove /.idea
  4. try to open it again

woops woops, fixed

panapol-p
  • 365
  • 2
  • 6
10

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
Tomer Shetah
  • 8,103
  • 7
  • 23
  • 34
Benjamin Huo
  • 101
  • 1
  • 2
  • The problem occurs again if I generate a vendor directory by go mod vendor, and it will disappear after I remove the vendor directory – Benjamin Huo Apr 04 '21 at 03:04
7

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.

Temuujin Dev
  • 371
  • 5
  • 10
5

I just removed the project from Goland and re-create it from existing files. It was weird but it worked.

koooyooo
  • 51
  • 1
  • 2
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.

sofshant
  • 93
  • 1
  • 8
  • 1
    Make sure that you create a backup of your .idea folder since it contains some local environment IDE settings (Go version, etc). – s0xzwasd Sep 06 '21 at 14:23
  • Thank you very much, I was about to abort and switch to vscode. It seems that this tip worked for me. – Camilo Bernal Feb 26 '22 at 19:19
2

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 use Alt+Enter | Sync packages of‍‍ <project>
dlsniper
  • 6,662
  • 1
  • 29
  • 42
  • 3
    I can still reproduce it in 2020.2 – Reven Jul 23 '20 at 09:38
  • 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. – dlsniper Jul 25 '20 at 11:12
  • @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 – Amin Shojaei Jul 26 '20 at 11:56
  • 1
    @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 . If neither of these solve the problem, then please open an issue on our tracker at https://youtrack.jetbrains.com/issues/Go – dlsniper Jul 27 '20 at 05:16
  • @disniper That's right. Git module solved my problem. thanks – Amin Shojaei Jul 27 '20 at 06:19
1

In Goland preferences, if you're using the Global GOPATH, check the option "Index entire GOPATH" and hit apply.

Victor Cui
  • 834
  • 1
  • 11
  • 22
  • like magic, I had the problem, tried the removing cache, then delete .idea folder, finally found this one, should be the final answer. – Cyberience Mar 03 '22 at 05:16
1

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

Junaid
  • 2,873
  • 1
  • 18
  • 23
0

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 !

  • 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 – James Oct 26 '20 at 09:07
0

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.

0

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

  1. ctrl + alt + s
  2. Go > GOPATH
  3. Click on plus button + In Project GOPATH
  4. Add your golang's project folder, example: ~/projects/my-golang-projects
Urko
  • 65
  • 13
0

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. GOPATH Setup Go Module Setup

(This fix is for Goland IDE, Mac. Of course we should add GOPATH in .bash_profile or .zshrc)

Che
  • 362
  • 1
  • 2
  • 14
-2

I solved it by reinstall Go to D:\go, then reset Go sdk.