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.
[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]
box := &MyBox{}
orbox := new(MyBox)
will "fix" that. SinceAddItem
is a(box *MyBox)
. – Sapsuckergo mod vendor
solved it (just copy the modules to the local folder). This does not answer OP's specific issue but this was the first link I came across for my issue so adding this here for my future self, sorry. – Mercifulgo.mod
file by mistake and then undoing the delete. Invalidate Cache helped – ComplexityInvalidate and Restart
. It will re-index caches and all references will be resolved. per: https://mcmap.net/q/187506/-goland-jetbrains-shows-error-message-quot-unresolved-reference-quot-but-code-compiles-and-runs – Immense