如何编译由多个文件组成的Go程序? [英] How to compile Go program consisting of multiple files?

查看:41
本文介绍了如何编译由多个文件组成的Go程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由三个文件组成的小程序,它们都属于同一个包(主).但是当我执行 go build main.go 时,构建没有成功.当它只有一个文件 (main.go) 时,一切正常.

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.

现在我花了一些精力来分离代码,看起来编译器无法找到从 main.go 中取出并放入这两个其他文件(驻留在在与 main.go 相同的目录中).这会导致 undefined 'type' 错误.

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?

推荐答案

新方式(推荐):

请看看这个答案.

假设您正在编写一个名为 myprog 的程序:

Supposing you're writing a program called myprog :

将所有文件放在这样的目录中

Put all your files in a directory like this

myproject/go/src/myprog/xxx.go

然后将 myproject/go 添加到 GOPATH

Then add myproject/go to GOPATH

然后运行

go install myprog

通过这种方式,您可以根据需要在 myproject/go/src 中添加其他包和程序.

This way you'll be able to add other packages and programs in myproject/go/src if you want.

参考:http://golang.org/doc/code.html

(这个文档总是被新人错过,而且一开始经常被误解.它应该受到围棋团队 IMO 最大的关注)

(this doc is always missed by newcomers, and often ill-understood at first. It should receive the greatest attention of the Go team IMO)

这篇关于如何编译由多个文件组成的Go程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆