Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more

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

Ask Question

I am doing a project in the GoLanguage and use GoLand by Jetbrains as an IDE. GoLand shows me in the IDE an error message ("unresolved reference"), but still 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 ). The same error message appears. But obviously I have implemented the method 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.

Can someone help? Thanks a lot

[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]

I couldn't reproduce the problem, running GoLand 2019.2.5. However, I think that if you use box := &MyBox{} or box := new(MyBox) will "fix" that. Since AddItem is a (box *MyBox). – Inkeliz Jan 1 '20 at 21:59 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 9:05 thanks. i was getting desperate... File->InvalidateCaches solved this for me in Goland 2021.1 – Blafasel42 Apr 12 at 10:18

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
  • 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 4 at 3:04

    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>
  • 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 @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 – dlsniper Jul 27 '20 at 5:16

    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 9:07

    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.

    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
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.