Goland 看不到来自另一个文件的函数

Goland doesn't see functions from another file

我有main.go个文件

package main

func main() {
    func2()
}

func2 定义在 file2.go:

package main

func func2(){
    ...
}

当我编译并从命令行 运行 时一切正常:

go run main.go file2.go

但是当我通过按 RUNGoland 运行 它时,它给我一个错误:

# command-line-arguments
./main.go:95: undefined: func2

我该如何克服?

Run | Edit Configurations,把运行配置Type改成从File指向一个Package,那么包名必须全限定(例如 github.com/user/package)。

编辑: 您也可以右键单击该文件夹,然后单击 select Run | go test <folder name>。这将创建类型为 Directory 的 运行 配置。

注意: DirectoryPackage 类型配置之间存在差异。