go build正常工作,但go run失败了。

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

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

huangapple
  • 本文由 发表于 2014年1月23日 04:12:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/21293000.html
匿名

发表评论

匿名网友

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

确定