如何修复这个错误“runtime.main: undefined main.init”

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

How to fix this error "runtime.main: undefined main.init"

问题

尝试运行一个简单的hello world go程序返回一个错误。

# command-line-arguments
runtime.main: undefined: main.init
runtime.main: undefined: main.main

文件内容:

package main    
import "fmt"
func main() {
  fmt.Println("Hello, World!")
}

main显然是定义好的,添加func init() {}也没有帮助。

无论是run命令还是build命令都会导致相同的错误。

go build hello_test.go
go run hello_test.go
英文:

Trying run a simple hello world go program returns an error.

# command-line-arguments
runtime.main: undefined: main.init
runtime.main: undefined: main.main

The file contents:

package main    
import "fmt"
func main() {
  fmt.Println("Hello, World!")
}

main is obviously defined and adding func init() {} doesn't help.

Both the run and build commands result in the same error.

go build hello_test.go
go run hello_test.go

答案1

得分: 31

将hello_test.go重命名为hello.go,它应该按预期工作。以_test结尾的Go源文件是特殊的(用于go构建系统)。它们被保留给go test命令使用。

英文:

Rename hello_test.go to e.g. hello.go and it should work as expected. Go source files ending in _test are special (for the go build system). They're reserved for the go test command.

huangapple
  • 本文由 发表于 2013年2月7日 05:30:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/14739188.html
匿名

发表评论

匿名网友

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

确定