This is a creation in Article, where the information may have evolved or changed.
Source: Go Language Chinese blog "Go Project directory Structure" project directory structure How to organize, general language is not specified. But the go language has made the rules so that consistency can be maintained
1, general, a go project under Gopath, there will be the following three directories:
|--bin|--pkg|--src
Where the bin holds the compiled executable file, the PKG holds the compiled package file, and SRC holds the project source file. Generally, the bin and pkg directories can be created without the GO command being created automatically (like go install), just create the SRC directory.
For the Pkg directory, someone once asked: I put the package go under the pkg, how can not ah? He directly put the go package source files into the pkg. This is obviously wrong. The files in the pkg are generated by go compilation, rather than manually put in. (General file suffix. a)
For the SRC directory, the source files are stored in the go, and the source files are organized in the form of packages. In general, creating a new package creates a new folder in the SRC directory.
2. Examples and explanations
For example: I create a new project, test, the starting directory structure is as follows:
Test--|--src
In order to compile conveniently, I added an install file in it, directory structure:
test/|-- Install '--src
The contents of the install are as follows: (Linux)
#!/usr/bin/ENV Bashif[ ! -F Install]; Thenecho'install must is run within its container folder' 1>&2Exit1Ficurdir=' pwd ' Oldgopath="$GOPATH"Export Gopath="$CURDIR"gofmt-w srcgo Install Testexport Gopath="$OLDGOPATH"Echo'finished'
Add this install, do not configure Gopath (avoid adding a go project will add a path to the Gopath)
Next, add a package: Config and a main program. The directory structure is as follows:
Test|-- Install '-- src |-- config | '-- config.go '-- test '--main.go
Note that the package name in Config.go must be the best match for the directory config, and the file name can be random. Main.go represents the main package, and the file name is suggested as Main.go. (Note: when inconsistent, the resulting. A file name and directory name are the same, so that when you import, it should be the directory name, and the package name is required when the package is referenced.) For example, if the directory is myconfig and the package name is config, then the production static package file is: MYCONFIG.A, referencing the package: import "Myconfig", using the package Member: CONFIG. Loadconfig ())
The code for CONFIG.GO and Main.go is as follows:
Config.go Code
Package Configfunc Loadconfig () {}
Main.go Code
Package Mainimport ( "config" "fmt") Func Main () { config. Loadconfig () FMT. Println ("Hello, go! " )}
Next, execute the./install in the project root directory.
The directory structure at this time is:
Test|-- bin| '-- test|-- install|-- pkg| '-- linux_amd64| '-- config.a '-- src |-- config | '-- config.go '-- test '-- main.go (linux_amd64 means I use the operating system and architecture, you may not be the same)
Where CONFIG.A is generated after the package config is compiled; bin/test is a binary file that is generated
This time can be carried out: Bin/test. Will output: Hello, go!
3. Supplementary instructions
1) package can be multi-level directory, such as: Net/http package, indicating that the source file under the Src/net/http directory, but the source file is the name of the last directory, such as HTTP
When you import a package, you must complete the path, such as: Import "Net/http"
2) Sometimes you will see local import (not recommended), similar syntax:
Import "./config"
When you have such a statement in your code, you will often see errors like this: local import "./config" in the Non-local package
The way I know how to import this is: When you write a simple test script that you want to use with the Go Run command, you can use this type of import.
For example above, move test/main.go to src directory, test directory Delete, modify main.go import "config" to import "./config", then can be executed in src directory: Go run main.go
Visible, local import does not depend on Gopath
4. Install.bat under Windows
@echo offsetlocal if Goto from its foldergoto end:okset oldgopath=%gopath%set gopath=%~-w srcgo install Test:endecho finished