snippetgoMajor
How to compile Go program consisting of multiple files?
Viewed 0 times
programhowcompilefilesmultipleconsisting
Problem
I have a small program that consists of three files, all belonging to the same package (main). But when I do
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
How to compile this program that consists of multiple files?
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?
Solution
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
Then add
And run
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)
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.goThen add
myproject/go to GOPATHAnd run
go install myprogThis 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)
Code Snippets
myproject/go/src/myprog/xxx.gogo install myprogContext
Stack Overflow Q#10838469, score: 99
Revisions (0)
No revisions yet.