我正在 GoLanguage 中做一个项目,并使用 Jetbrains 的 GoLand 作为 IDE。GoLand 在 IDE 中向我显示一条错误消息(“未解析的引用”),但仍然可以正确编译和运行。
这是我在 stackoverflow 上找到的一些代码的类似(但更简单)示例(Go - append to slice in struct)。出现相同的错误消息。但显然我已经实现了上面几行的方法。
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)
} …Run Code Online (Sandbox Code Playgroud)