如何编译Go程序含有多个文件? [英] How to compile Go program consisting of multiple files?

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

问题描述

所以我有一个小程序,包括三个文件,所有属于同一个包(主),但是当我做去打造main.go构建不会成功。当它只是一个文件(main.go)一切正常。现在,我花了一些功夫来分隔code,它看起来像编译器是无法找到被取出main.go并投入这两个其他文件(即位于同一目录作为主要的东西。走)。这将导致未定义类型的错误。

So 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 at the same directory as the main.go). Which results in "undefined 'type'" errors.

我读一些关于设置GOPATH变量,所以我尝试

I read something about setting the GOPATH variable, so I tried

set GOPATH=%cd%
go build main.go

但同样没有成功。

but that too didn't work.

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

假设你正在写一个程序叫做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 /去来GOPATH

和运行

go install myprog

这样你就可以,如果你想添加其他包和方案的myproject /去/ src目录。

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

参考: http://golang.org/doc/$c$c.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天全站免登陆