Generally, there is no rule on how to organize the project directory structure. However, the Go language requires consistency.
1. Generally, a go project under gopath has the following three directories:
|--bin|--pkg|--src
Bin stores the compiled executable files, PKG stores the compiled package files, and SRC stores the project source files. Generally, the bin and PKG directories can be not created, and the go command will automatically create (such as Go install). You only need to create the src directory.
For the PKG directory, Someone once asked: Why can't I put the package in go under PKG? He directly put the source file of the Go package into PKG. This is obviously incorrect. PKG files are compiled and generated by go instead of manually entered. (General file suffix.)
For the src directory to store source files, the source files in go are organized in the form of packages. Generally, creating a new package creates a new folder in the src directory.
2. Examples
For example, to create a project named test, the directory structure is as follows:
test--|--src
For compilation convenience, I added an install file in the directory structure:
test/|-- install`-- src
The content of install is as follows: (in Linux)
#!/usr/bin/env bashif [ ! -f install ]; thenecho ‘install must be run within its container folder‘ 1>&2exit 1fiCURDIR=`pwd`OLDGOPATH="$GOPATH"export GOPATH="$CURDIR"gofmt -w srcgo install testexport GOPATH="$OLDGOPATH"echo ‘finished‘
You do not need to configure gopath because you need to add a path to gopath to add a go project)
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: The package name in config. GoRequiredIt is better to be consistent with the directory config, and the file name can be casual. Main. Go indicates the main package. The file name is recommended to be main. Go. (Note:The generated. A file name is the same as the directory name. In this way, the directory name should be used during import, and the package name is required when the package is referenced. For example, if the directory is myconfig and the package name is config, the generated static package file is myconfig. a. Reference this 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, run./install in the project root directory.
The directory structure is as follows:
Test | -- bin | '-- Test | -- install | -- PKG |' -- linux_amd64 | '-- config. a' -- SRC | -- config | '-- config. go '-- test' -- Main. go (linux_amd64 indicates the operating system and architecture I use. You may be different)
Config. A is generated after the config package is compiled; bin/test is the generated binary file.
In this case, run bin/test. Will output: Hello, go!
3. Additional instructions
1) A package can contain multiple directories. For example, the net/http package indicates that the source file is under the src/NET/HTTP directory. However, the package name in the source file is the name of the last directory, such as HTTP
In the import package, the complete path is required, for example, import "net/HTTP"
2) Sometimes we will see local import (not recommended). The syntax is similar to this:
import “./config”
When the Code contains such a statement, the following error occurs frequently: Local Import "./config" in non-local package.
What I know about using this import method is that this import method can be used to write a simple test script and use the go run command.
For example, in the above example, test/main. go moves to the src directory, delete the test directory, and modify main. import "Config" in go is "import". /config ", and then you can execute: Go run main. go
It can be seen that local import does not depend on gopath
4. Install. BAT in Windows
@echo offsetlocalif exist install.bat goto okecho install.bat must be run from its foldergoto end:okset OLDGOPATH=%GOPATH%set GOPATH=%~dp0gofmt -w srcgo install test:endecho finished
Go project directory structure