函数名没有正确解析

huangapple go评论68阅读模式
英文:

function name is not resolving correctly

问题

我在同一个目录中的另一个go文件中的函数无法正确解析,从我的主go文件中调用时出现问题。它们都在同一个main包中。我得到了一个undefined错误。有什么想法我可能遗漏了什么?我该如何排除这样的问题?

这是一个简要的概述:

main.go

package main

func main() {
    runEx1()
}

ex1.go

package main

func runEx1() {
    // 一些代码
}

错误信息:

set1/main.go:4:2: undefined: runEx1

所有文件都在同一个目录中。

当我只运行go run时,我得到以下错误:go run: no go files listed。我在同一个目录中运行了这个命令。

英文:

I am having an issue with the function in another go file in the same directory is not resolving correctly with a call from my main go file. They're both in the same package main. I get a undefined error exactly. Any ideas on what I could be missing? How could I even troubleshoot something like this?

Here's a quick outline:

main.go

package main

func main() {
    runEx1()
}

ex1.go

package main

func runEx1() {
	// some code
}

error:

set1/main.go:4:2: undefined: runEx1

All files in the same directory.

When I just go run I get hte following:go run: no go files listed. Ran this command in the same directory.

答案1

得分: 2

在GoLand中,根据您点击运行代码的位置,它会创建一个类似于这样的go run配置,这会导致问题,因为它只构建指定的文件。

要修复它:

  • 在左侧的项目资源管理器中,右键单击要运行的包所在的目录。
  • 鼠标移到Run(运行) -> go build <package name>

点击此选项将创建一个构建配置,该配置将构建整个包,这正是您想要的。

英文:

In GoLand, depending on where you click to run your code, it will create a go run configuration like this which leads to issues as it only builds the specified files.

To fix it:

  • Right click on the the directory of the package that you want to run, in the project explorer on the left side.
  • Mouse down to Run -> go build <package name>

Clicking this option will create a build configuration which builds the entire package, which is what you want.

huangapple
  • 本文由 发表于 2021年6月22日 06:34:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/68075491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定