go build works fine but go run fails

China☆狼群 提交于 2019-12-17 06:48:24

问题


I have a few files in the main package under one directory:

main.go config.go server.go

When I do: "go build" the program builds perfect and runs fine. When I do: "go run main.go" it fails.

Output:

# command-line-arguments
./main.go:7: undefined: Config
./main.go:8: undefined: Server

The symbols that are undefined are structs and they are capitalised so should be exported.

My Go version: go1.1.2 linux/amd64


回答1:


This should work

go run main.go config.go server.go

Go run takes a file or files and it complies those and only those files which explains the missing symbols in the original post.




回答2:


You could execute it as:

go run .

so you don't have to include all files manually.



来源:https://stackoverflow.com/questions/21293000/go-build-works-fine-but-go-run-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!