我试图实现2个简单的结构如下:
package main
import (
"fmt"
)
type MyBoxItem struct {
Name string
}
type MyBox struct {
Items []MyBoxItem
}
func (box *MyBox) AddItem(item MyBoxItem) []MyBoxItem {
return append(box.Items, item)
}
func main() {
item1 := MyBoxItem{Name: "Test Item 1"}
item2 := MyBoxItem{Name: "Test Item 2"}
items := []MyBoxItem{}
box := MyBox{items}
AddItem(box, item1) // This is where i am stuck
fmt.Println(len(box.Items))
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我只想在box结构上调用addItem方法并传入一个项目
在Goland (2022.1.3)中,使用go (1.19.1),它无法解析os.Remove(),但如果我改为os.RemoveAll(),就可以了。
那么,出了什么问题呢?那是戈兰虫吗?
(顺便说一句,我正在使用 Linux 操作系统,如果这很重要的话。)
截图 (戈兰):
更新:可以运行的示例代码
package main
import (
"os"
)
func main() {
os.Create("/tmp/a.txt")
os.Remove("/tmp/a.txt")
}
Run Code Online (Sandbox Code Playgroud)
代码可以运行没有错误,所以我认为这是goland的bug。
我输入Process了一个名为的库lib.
我正在尝试导入该库并添加与lib包中的类型相关联的方法.
func(p *lib.Process) DoSomething(pp *lib.Process)
Run Code Online (Sandbox Code Playgroud)
但我unresolved type 'lib'内心有一个错误func(...).这让我感到惊讶,因为里面没有任何错误DoSomething.
怎么可能克服它?