英文:
go build works fine but go run fails
问题
我在一个目录下的主包中有几个文件:
main.go
config.go
server.go
当我执行"go build"时,程序可以成功构建并正常运行。
但是当我执行"go run main.go"时,它会失败。
输出信息如下:
# command-line-arguments
./main.go:7: undefined: Config
./main.go:8: undefined: Server
这些未定义的符号是结构体,它们的首字母大写,应该是可以导出的。
我的Go版本是:go1.1.2 linux/amd64
英文:
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
得分: 58
这应该可以工作。
go run main.go config.go server.go
go run命令接受一个或多个文件,并且只编译并运行这些文件,这解释了原帖中缺失符号的问题。
英文:
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
得分: 33
你可以这样执行它:
go run .
这样你就不需要手动包含所有文件。
英文:
You could execute it as:
go run .
so you don't have to include all files manually.
答案3
得分: 1
良好的做法是为其创建一个包并运行
go run ./app
示例文件夹结构
├──app/
| ├──main.go
| ├──config.go
| ├──server.go
├──bin/
| ├──foo.go
└──pkg/
└──bar.go
英文:
Good practise would be to create a package for it and run
go run ./app
Ex. Folder Structure
├──app/
| ├──main.go
| ├──config.go
| ├──server.go
├──bin/
| ├──foo.go
└──pkg/
└──bar.go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论