英文:
How to compile Go program consisting of multiple files?
问题
我有一个由三个文件组成的小程序,它们都属于同一个包(main)。但是当我执行go build main.go
时,构建不成功。当只有一个文件(main.go
)时,一切都正常工作。
现在我花了一些精力将代码分离出来,看起来编译器无法找到从main.go
中取出并放入这两个其他文件中的内容(这两个文件与main.go位于同一个目录中)。这导致了“未定义的'type'”错误。
如何编译这个由多个文件组成的程序?
英文:
I have a small program that consists of three files, all belonging to the same package (main). But when I do go build main.go
the build doesn't succeed. When it was just one file (main.go
), everything worked fine.
Now that I took some effort to separate the code, it looks like the compiler is unable to find the stuff that was taken out of main.go
and put into these two other files (that reside in the same directory as the main.go). Which results in undefined 'type'
errors.
How to compile this program that consists of multiple files?
答案1
得分: 96
新的方式(推荐):
旧的方式:
假设你正在编写一个名为myprog的程序:
将所有文件放在这样的目录中
myproject/go/src/myprog/xxx.go
然后将myproject/go
添加到GOPATH中
然后运行
go install myprog
这样,如果你想要添加其他包和程序到myproject/go/src中,你就可以了。
参考:http://golang.org/doc/code.html
(这个文档经常被新手忽略,而且通常在一开始被误解。在我看来,这应该是Go团队最关注的部分。)
英文:
New Way (Recommended):
Please take a look at this answer.
Old Way:
Supposing you're writing a program called myprog :
Put all your files in a directory like this
myproject/go/src/myprog/xxx.go
Then add myproject/go
to GOPATH
And run
go install myprog
This way you'll be able to add other packages and programs in myproject/go/src if you want.
Reference : http://golang.org/doc/code.html
(this doc is always missed by newcomers, and often ill-understood at first. It should receive the greatest attention of the Go team IMO)
答案2
得分: 71
当你将代码从main.go
分离出来,例如放到more.go
中,你只需要将该文件传递给go build
/go run
/go install
。
所以如果你之前运行了
go build main.go
现在只需要
go build main.go more.go
进一步的信息:
go build --help
说明:
> 如果参数是一系列的.go文件,
> build将把它们视为指定单个包的源文件列表。
请注意,go build
和go install
与go run
的区别在于前两者期望参数是包名,而后者期望参数是go文件。然而,前两者也会接受go文件,就像go install
一样。
如果你想知道:build
只会构建包/文件,install
会在你的GOPATH中生成目标和二进制文件,而run
会编译并运行你的程序。
英文:
When you separate code from main.go
into for example more.go
, you simply pass that file to go build
/go run
/go install
as well.
So if you previously ran
go build main.go
you now simply
go build main.go more.go
As further information:
go build --help
states:
> If the arguments are a list of .go files,
> build treats them as a list of source files specifying a single package.
Notice that go build
and go install
differ from go run
in that the first two state to expect package names as arguments, while the latter expects go files. However, the first two will also accept go files as go install does.
If you are wondering: build will just build
the packages/files, install
will produce object and binary files in your GOPATH, and run
will compile and run your program.
答案3
得分: 56
自Go 1.11+版本开始,不再推荐使用GOPATH,新的方式是使用Go Modules。
假设你正在编写一个名为simple
的程序:
-
创建一个目录:
mkdir simple cd simple
-
创建一个新的模块:
go mod init github.com/username/simple # 这里,模块名为:github.com/username/simple。 # 你可以自由选择任何模块名。 # 只要它是唯一的就可以。 # 最好是一个URL,这样它就可以被go-get获取到。
-
将所有文件放在该目录中。
-
最后,运行:
go run .
-
或者,你可以通过构建来创建一个可执行程序:
go build . # 然后: ./simple # 如果你在类Unix系统上 # 或者,只需: simple # 如果你在Windows上
更多信息,请阅读这篇文章。
> 自Go 1.11版本以来,Go已经包含了对版本化模块的支持,就像在这里提议的那样。最初的原型vgo在2018年2月宣布。在2018年7月,版本化模块已经进入了主要的Go代码库。
> 在Go 1.14中,模块支持被认为已经准备好供生产使用,并且鼓励所有用户从其他依赖管理系统迁移到模块。
英文:
Since Go 1.11+, GOPATH is no longer recommended, the new way is using Go Modules.
Say you're writing a program called simple
:
-
Create a directory:
mkdir simple cd simple
-
Create a new module:
go mod init github.com/username/simple # Here, the module name is: github.com/username/simple. # You're free to choose any module name. # It doesn't matter as long as it's unique. # It's better to be a URL: so it can be go-gettable.
-
Put all your files in that directory.
-
Finally, run:
go run .
-
Alternatively, you can create an executable program by building it:
go build . # then: ./simple # if you're on xnix # or, just: simple # if you're on Windows
For more information, you may read this.
> Go has included support for versioned modules as proposed here since 1.11. The initial prototype vgo was announced in February 2018. In July 2018, versioned modules landed in the main Go repository.
> In Go 1.14, module support is considered ready for production use, and all users are encouraged to migrate to modules from other dependency management systems.
答案4
得分: 34
你也可以在你的项目文件夹myproject/go/src/myprog中运行
go build
然后你只需要输入
./myprog
来运行你的应用程序
英文:
You could also just run
go build
in your project folder myproject/go/src/myprog
Then you can just type
./myprog
to run your app
答案5
得分: 12
这取决于你的项目结构。但最简单的方法是:
go build -o ./myproject ./...
然后运行./myproject
。
假设你的项目结构如下所示:
- hello
|- main.go
然后你只需进入项目目录并运行:
go build -o ./myproject
然后在shell中运行./myproject
。
或者
go run main.go
假设你的主文件嵌套在一个子目录中,比如cmd
- hello
|- cmd
|- main.go
然后你将运行
go run cmd/main.go
英文:
It depends on your project structure. But most straightforward is:
go build -o ./myproject ./...
then run ./myproject
.
Suppose your project structure looks like this
- hello
|- main.go
then you just go to the project directory and run
go build -o ./myproject
then run ./myproject
on shell.
or
# most easiest; builds and run simultaneously
go run main.go
suppose your main file is nested into a sub-directory like a cmd
- hello
|- cmd
|- main.go
then you will run
go run cmd/main.go
答案6
得分: 8
你可以使用
go build *.go
go run *.go
两者都可以工作,你也可以使用
go build .
go run .
英文:
You can use
go build *.go
go run *.go
both will work also you may use
go build .
go run .
答案7
得分: 3
是的!这非常简单,这就是包策略发挥作用的地方。据我所知,有三种方法。
文件夹结构:
GOPATH/src/
github.com/
abc/
myproject/
adapter/
main.go
pkg1
pkg2
警告:adapter文件夹只能包含main包和子目录。
- 进入"adapter"文件夹。运行:
go build main.go
- 进入"adapter"文件夹。运行:
go build main.go
- 进入GOPATH/src
识别相对路径到main包,这里是"myproject/adapter"。运行:
go build myproject/adapter
exe文件将在当前目录下创建。
英文:
Yup! That's very straight forward and that's where the package strategy comes into play. there are three ways to my knowledge.
folder structure:
GOPATH/src/
github.com/
abc/
myproject/
adapter/
main.go
pkg1
pkg2
warning: adapter can contain package main only and sun directories
- navigate to "adapter" folder. Run:
go build main.go
- navigate to "adapter" folder. Run:
go build main.go
- navigate to GOPATH/src
recognize relative path to package main, here "myproject/adapter". Run:
go build myproject/adapter
exe file will be created at the directory you are currently at.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论