在同一个包中未定义的函数

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

Function in same package undefined

问题

我的项目结构如下。

packagetest/
    main.go
    lib.go

main.go 中,我有以下代码。

package main

import "fmt"

func main() {
    fmt.Println("Hello from main.go.")
    Test()
}

而在 lib.go 中,我有以下代码。

package main

import "fmt"

func Test() {
    fmt.Println("This is the Test function in lib.go.")
}

当我尝试使用 go build main.go 进行编译时,我得到了 ./main.go:7: undefined: Test 的错误。这种代码结构方式是否可行?

英文:

My project structure is like this.

packagetest/
    main.go
    lib.go

In main.go, I have this code.

package main

import "fmt"

func main() {
	fmt.Println("Hello from main.go.")
	Test()
}

While in lib.go, I have this code.

package main

import "fmt"

func Test() {
	fmt.Println("This is the Test function in lib.go.")
}

When I try to compile with go build main.go, I get ./main.go:7: undefined: Test. Is this way of structuring my code possible?

答案1

得分: 79

尝试只运行go build命令。当你将一个go文件作为参数传递给它时,它不会查找其他的go文件。你也可以使用go build *.go命令。

英文:

Try running just go build. When you give it a go file as an argument, it will not look for other go files. You can also do go build *.go

答案2

得分: 25

这是一篇旧帖子,但它没有清楚地回答我的问题,所以我为了将来的其他人的利益而发帖。

当运行 go run --help 时,你会找到这个手册:

Run 编译并运行由指定的 Go 源文件组成的主包。
Go 源文件被定义为以字面上的 ".go" 后缀结尾的文件。

默认情况下,'go run' 直接运行编译后的二进制文件:'a.out arguments...'。

go run <filename.go> 用于只有几个文件的小程序。如果有多个文件,你会遇到一个问题,即你的 main.go 找不到其他文件,因为 go run 不会隐式地编译和链接它们,除非命名。这就是为什么使用 go build 项目可以工作的原因。

另外,go run *.go(构建所有文件)在大多数情况下应该可以工作。

英文:

This is an old post but it didn't answer my issue clearly, so I'm posting for the benefit of others in the future.

When run go run --help you will find this manual:

>Run compiles and runs the main package comprising the named Go source files.
>A Go source file is defined to be a file ending in a literal ".go" suffix.

>By default, 'go run' runs the compiled binary directly: 'a.out arguments...'.

go run &lt;filename.go&gt; is used for small programs with just a few files. With several files you will run into an issue where your main.go cannot find other files because go run doesn't compile and link them implicitly unless named. That's why go build the project works.

Alternatively, go run *.go (building all files) should work most of the time.

答案3

得分: 9

golang.org网页上,你可以阅读有关build命令的内容:

>如果参数是一组.go文件,build将把它们视为指定单个包的源文件列表。

因此,go build main.go将把main.go视为单个包。相反,你应该使用:

go build

来包含文件夹中的所有文件。

英文:

On the golang.org webpage you can read about the build command that:

>If the arguments are a list of .go files, build treats them as a list of source files specifying a single package.

So, go build main.go will treat main.go as a single package. Instead, you should use:

go build

to include all files in the folder.

答案4

得分: 8

这个问题会在Go找不到你的其他文件时发生(例如在这个例子中的lib.go文件)。

如果你使用的是JetBrains Goland,你应该将"Run kind"更改为"Directory"。Goland会帮助你完成以下操作:

  • go build .
  • 运行这个可执行文件。

如果你使用的是JetBrains Goland,你可以对比一下"Run kind"的区别。在同一个包中未定义的函数

当我选择"File",然后运行时,你会在控制台上看到如下消息:

GOROOT=C:\Go #gosetup
GOPATH=C:\Users\hgs\go #gosetup
C:\Go\bin\go.exe build -o C:\Users\hgs\AppData\Local\Temp\___go_build_select_go__4_.exe &quot;C:/Users/hgs/Google Drive/My Maps/computer/2.programming_lang/Golang/source-code-refer/basic/select.go&quot; #gosetup
# command-line-arguments
.\select.go:14:16: undefined: Add

当你选择"Directory"并选择你的目录,然后运行时,你会看到类似下面的内容:

GOROOT=C:\Go #gosetup
GOPATH=C:\Users\hgs\go #gosetup
C:\Go\bin\go.exe build -o C:\Users\hgs\AppData\Local\Temp\___go_build_basic_.exe . #gosetup
C:\Users\hgs\AppData\Local\Temp\___go_build_basic_.exe #gosetup
...
英文:

This problem will happen when go cannot find your other file (like lib.go in this example).

If you use JetBrains Goland , you should change your "Run kind" to "Directory". Goland will help you to do these:

  • go build .
  • run this execution file.

If you use JetBrains Goland , you can contrast the difference between "Run kind".在同一个包中未定义的函数

When i select "File", and then run, you will see the message on the console, like this:

GOROOT=C:\Go #gosetup
GOPATH=C:\Users\hgs\go #gosetup
C:\Go\bin\go.exe build -o C:\Users\hgs\AppData\Local\Temp\___go_build_select_go__4_.exe &quot;C:/Users/hgs/Google Drive/My Maps/computer/2.programming_lang/Golang/source-code-refer/basic/select.go&quot; #gosetup
# command-line-arguments
.\select.go:14:16: undefined: Add

When you select "Directory" and select your directory, and then run, you will see something like this:

GOROOT=C:\Go #gosetup
GOPATH=C:\Users\hgs\go #gosetup
C:\Go\bin\go.exe build -o C:\Users\hgs\AppData\Local\Temp\___go_build_basic_.exe . #gosetup
C:\Users\hgs\AppData\Local\Temp\___go_build_basic_.exe #gosetup
...

答案5

得分: 7

如果你不想“构建”而只是执行它,你也可以尝试下面的命令:

go run main.go lib.go
英文:

You can also try below command if you don't want to build but simply execute it :

go run main.go lib.go

答案6

得分: 3

如果要在Go主包中使用多个文件,而不是像"go run main.go"那样使用单个文件路径,可以在运行或构建命令中指定主包目录,例如:

#从packagetest目录外部
go run ./packagetest/
go build ./packagetest/

#从packagetest目录内部:
go run ./
go build ./
英文:

If using multiple files for Go main pkg, instead of single file path as go run main.go, go require main pkg dir in the run or build command, like:

#from outside packagetest dir
go run ./packagetest/
go build ./packagetest/

#from within packagetest:
go run ./
go build ./

答案7

得分: 3

运行所有文件后可以调用

go run *.go

或者

go run main.go lib.go
英文:

Run all the file then able to call

go run *.go

or

go run main.go lib.go

答案8

得分: 2

我认为问题在于你只能有一个文件使用package main

尝试将lib.go放在一个不同的包中。这需要:

  1. lib.go存在于与新包同名的文件夹中(例如,myLibs)。

  2. lib.go中添加包名(package myLibs)。

然后,在main中:

import myLibs
myLibs.Test()
英文:

I think the problem is you can only have one file using package main.

Try putting lib.go in a different package. This requires:

  1. lib.go to exist in a folder with the same name as the new package (e.g., myLibs).

  2. Adding the package name to lib.go (package myLibs).

Then, in main:

import myLibs
call myLibs.Test()

答案9

得分: 1

对于其他可能需要的人。

我遇到了类似的问题。然而,我的问题是我想调用的函数在一个名为my_test.go的文件中,而我试图从一个名为testutils.go而不是*_test.go的文件中调用它。(也许有人会告诉我不要有一个testutils.go文件?)

英文:

For others who may need this.

I had a similar issue. However, my problem was that the function I wanted to call was in a my_test.go file, and I was trying to call it from a file that was not called *_test.go but testutils.go. (Maybe someone will tell me not to have a testutils.go file?)

答案10

得分: 0

以下是翻译好的内容:

  • lib.go 应该重命名为 main_test.go
  • 测试函数的函数签名应该是 func Test(t *testing.T) {
  • main_test.go 中应该导入 testing 包。
  • 理想情况下,Test 函数的名称应该反映它正在测试的内容,比如 TestPrinting

然后 go buildgo test 应该都能正常工作。

英文:
  • lib.go should be renamed to main_test.go
  • The function signature of the test function should be func Test(t *testing.T) {
  • The package testing should be imported in main_test.go.
  • Ideally, the Test function should have a name that reflects what it is testing, like TestPrinting.

Then both go build and go test should work.

huangapple
  • 本文由 发表于 2014年8月7日 06:37:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/25171409.html
匿名

发表评论

匿名网友

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

确定