英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论