This is a creation in Article, where the information may have evolved or changed.
In Go
the official website document how to Write Go code, the project directory that has been introduced Go
typically contains the following:
- src contains the source code file for the project;
- pkg contains the Package/library files generated after compilation;
- The bin contains the executable file that was built after compilation.
You can use the following examples to illustrate the organization and management of engineering catalogs. (Windows 7 64-bit, go version go1.3.3 windows/amd64)
1. Create a library file
Create a library file a.go
and save it under a subdirectory in the SCR directory.
package myfuncimport "fmt"func Afunc(str string) {fmt.Println("a.go is package mufunc.")fmt.Println(str)}
The directory structure is as follows:
<dirtest> |--<src> |--<mufunc> |--a.go
2. Create the main
About the location of the main package, you can refer to reference 2, personal advice is placed scr/main
below, after all, the official recommendation package name and file folder name is best (although the package name and folder name can be different, that is, a folder can contain multiple packages of .go
files).
package mainimport "myfunc"func main() {myfunc.Afunc("b.go is package main.")}
The directory structure is as follows:
<dirtest> |--<src> |--<mufunc> |--a.go |--<main> |--b.go
3. Use Go Build
If you use this time go build
, you will find the following output:
E:\dirtest>go build src\main\b.gosrc\main\b.go:3:8: cannot find package "myfunc" in any of: C:\Program Files\go\src\pkg\myfunc (from $GOROOT) D:\GoLang\src\myfunc (from $GOPATH)
From the output we can see, Go
first find the package from, $GOROOT
myfunc
if not found on the $GOPATH
search from, the results are not found, we can go env
use Go
the output environment variable settings:
E:\dirtest>go envset GOARCH=amd64set GOBIN=set GOCHAR=6set GOEXE=.exeset GOHOSTARCH=amd64set GOHOSTOS=windowsset GOOS=windowsset GOPATH=D:\GoLangset GORACE=set GOROOT=C:\Program Files\goset GOTOOLDIR=C:\Program Files\go\pkg\tool\windows_amd64set CC=gccset GOGCCFLAGS=-m64 -mthreads -fmessage-length=0set CXX=g++set CGO_ENABLED=1
Obviously E:\dirtest
this directory is not added to $GOPATH
, add the directory to the environment variable:
Once saved, re-execute (may need to reopen the console for the environment variable to take effect) go build
, an executable file is generated in the current directory b.exe
.
E:\dirtest\src\main>go envset GOARCH=amd64set GOBIN=set GOCHAR=6set GOEXE=.exeset GOHOSTARCH=amd64set GOHOSTOS=windowsset GOOS=windowsset GOPATH=D:\GoLang;E:\dirtestset GORACE=set GOROOT=C:\Program Files\goset GOTOOLDIR=C:\Program Files\go\pkg\tool\windows_amd64set CC=gccset GOGCCFLAGS=-m64 -mthreads -fmessage-length=0set CXX=g++set CGO_ENABLED=1E:\dirtest>go build src\main\b.goE:\dirtest>dir E:\dirtest 的目录2015/01/13 23:11 <DIR> .2015/01/13 23:11 <DIR> ..2015/01/13 23:11 1,958,912 b.exe2015/01/13 22:52 <DIR> srcE:\dirtest>b.exea.go is package mufunc.b.go is package main.
Although it ran successfully, it was not generated as expected under the bin directory. In order to achieve such an effect, you need to go install
. Note that go install
this is for the package, not for individual .go
files.
However, if the current state is executed go install
, although it can be successful, but you will find that it is not created in the project root directory E:\dirtest
bin\main.exe
, it is D:\GoLang
created in.
What if the main package is executed go install
?
E:\dirtest\src\main>go installgo install: no install location for E:\dirtest\src\main: hidden by D:\GoLang\src\main
As you can see, the output indicates that the current directory is hidden. Obviously this order is the corresponding $GOPATH
setting, change the $GOPATH
path in the order:
Then in execution go install myfunc
, the discovery was successfully generated under the pkg directory myfunc.a
. The same execution go install main
, also successfully generated in the bin directory main.exe
. The directory structure at this time is as follows:
<dirtest> |--<src> |--<mufunc> |--a.go |--<main> |--b.go |--<pkg> |--<windows_amd64> |--myfunc.a |--<bin> |--main.exe
Now is a successful completion of a sample "project" it ...
4. Common errors
In addition to the errors in the above steps, in fact, the project directory management is slightly careless, there will be other problems, such as:
1. A folder contains several different packages of source files. What happens a.go
when you put the and b.go
all in the myfunc
directory?
The directory structure is as follows:
<dirtest> |--<src> |--<mufunc> |--a.go |--b.go
Then execution go install
and go build
, and even go run
will be the same error:
E:\dirtest\src\myfunc>go installcan't load package: package myfunc: found packages myfunc (a.go) and main (b.go) in E:\dirtest\src\myfuncE:\dirtest\src\myfunc>go buildcan't load package: package myfunc: found packages myfunc (a.go) and main (b.go) in E:\dirtest\src\myfuncE:\dirtest\src\myfunc>go run b.gob.go:3:8: found packages myfunc (a.go) and main (b.go) in E:\dirtest\src\myfunc
As you can see in reference 3, there can only be one package in each subdirectory, otherwise the compilation will error , so a subdirectory cannot contain multiple source files of different packages.
2. Can a project contain more than one main ()?
Under simple tests, create one and c.go
use myfunc
the package (similar to the case where no other packages are imported):
package mainimport "fmt"import "myfunc"func main() {fmt.Println("This is single c.go")myfunc.Afunc("c.go is also package main.")}
Execute the corresponding command with the following result:
E:\dirtest\src\main>go build# main.\c.go:4: main redeclared in this block previous declaration at .\b.go:5E:\dirtest\src\main>go build c.go # 成功,当前目录下生成了c.exeE:\dirtest\src\main>go install# main.\c.go:4: main redeclared in this block previous declaration at .\b.go:5E:\dirtest\src\main>go install c.gogo install: no install location for .go files listed on command line (GOBIN notset)E:\dirtest\src\main>go run c.goThis is single c.goa.go is package mufunc.c.go is also package main.
Obviously can only be go run
and go build c.go
feasible. If you c.go
move it to a separate directory:
E:\dirtest\src\cmain>dir E:\dirtest\src\cmain 的目录2015/01/14 11:27 <DIR> .2015/01/14 11:27 <DIR> ..2015/01/14 11:24 147 c.goE:\dirtest\src\cmain>go buildE:\dirtest\src\cmain>go install
can be performed successfully. The go install
corresponding files are generated under the bin directory exe
. It seems that directory management is still a problem.
3. Go install:no install location for. Go files listed command line (GOBIN not set)
As you can see from the example output above, go install
This error occurs when using a single file. By default, if you set the $GOROOT
and $GOPATH
, you will look for and in turn $GOROOT/bin
$GOPATH/bin
. So what happens if we customize the settings $GOBIN=E:\dirtest\bin
?
E:\dirtest\src\cmain>go envset GOARCH=amd64set GOBIN=E:\dirtest\binset GOCHAR=6set GOEXE=.exeset GOHOSTARCH=amd64set GOHOSTOS=windowsset GOOS=windowsset GOPATH=E:\dirtest;D:\GoLangset GORACE=set GOROOT=C:\Program Files\goset GOTOOLDIR=C:\Program Files\go\pkg\tool\windows_amd64set CC=gccset GOGCCFLAGS=-m64 -mthreads -fmessage-length=0set CXX=g++set CGO_ENABLED=1E:\dirtest\src\cmain>go install c.go# 成功在 E:\dirtest\bin 下面生成 c.exe
Although successful, go install
it should be scoped to the package level, not to individual files.
4. The difference between go build, go install and go run
For a detailed view of reference 4, here's a quick look:
go build
Compile the package, and if it is a main
package, generate the executable file in the current directory, and the other packages will not generate .a
files;
go install
Compile the package while copying the results to $GOPATH/bin
, and $GOPATH/pkg
so on, the corresponding directory;
go run gofiles...
Compiles the listed file and generates the executable file and executes it. Note You can only use main
the package, otherwise go run: cannot run non-main package
the error will occur.
Also, go run
it is not necessary to set $GOPATH
the, but go build
and go install
must be set. go run
often used to test some features that are not typically included in the final project.
5. Summary
- Be sure to manage the catalog.
- Multiple projects are best under one, that
$GOPATH
is src/proj1
, src/proj2
etc
- Use it as much as possible to
go install
standardize the overall structure of the project
6. References
- Golang Project directory Structure organization
- Questions about where to put the main package
- Two or three things about the package in Golang
- Running Multi-file Main Package