There are no regulations on how to organize the project directory structure. However, Go language has made regulations in this regard, so that consistency can be maintained.
1. Generally, a Go project under GOPATH will have the following three directories:
|--bin
|--pkg
|--src
Among them, bin stores the compiled executable files; pkg stores the compiled package files; src stores the project source files. Generally, bin and pkg directories can not be created, and the go command will be automatically created (such as go install), and you only need to create the src directory.
Regarding the pkg directory, someone once asked: Why can't I put the package in Go into pkg? He directly put the source file of the Go package into pkg. This is obviously wrong. The files in pkg are generated by Go compiled, not manually placed. (General file suffix.a)
For the src directory, the source files are stored, and the source files in Go are organized in the form of packages. Usually, creating a new package creates a new folder in the src directory.
2. Give examples
For example: I create a new project, test, and the directory structure at the beginning is as follows:
test--|--src
For the convenience of compilation, I added an install file to it, directory structure:
test/
|-- install
`-- src
The content of install is as follows: (under linux)
#!/usr/bin/env bash
if [ ! -f install ]; then
echo 'install must be run within its container folder' 1>&2
exit 1
fi
CURDIR=`pwd`
OLDGOPATH="$GOPATH"
export GOPATH="$CURDIR"
gofmt -w src
go install test
export GOPATH="$OLDGOPATH"
echo 'finished'
The reason for adding this install is that you do not need to configure GOPATH (avoid adding a path to GOPATH if you add a new GO project)
Next, add a package: config and a main program. The directory structure is as follows:
test
|-- install
`-- src
|-- config
| `--
`-- test
`--
Note that the package name in it must be consistent with the directory config, and the file name can be as casual as possible. It means main package, and the file name is recommended. (Note: When inconsistent, the generated .a file name is the same as the directory name. In this way, when importing, it should be the directory name, and when referring to a package, the package name is required. For example: the directory is myconfig and the package name is config, the static package file produced is:, refer to this package: import "myconfig", use the package member: ())
The code of and is as follows:
Code
package config
func LoadConfig() {
}
Code
package main
import (
"config"
"fmt"
)
func main() {
()
("Hello, GO!")
}
Next, execute ./install in the project root directory
The directory structure at this time is:
test
|-- bin
| `-- test
|-- install
|-- pkg
| `-- linux_amd64
| `--
`-- src
|-- config
| `--
`-- test
`--
(linux_amd64 means the operating system and architecture I use, yours may be different)
Among them, it is generated after the package config is compiled; bin/test is the generated binary file
At this time, you can execute: bin/test. Will output: Hello, GO!
3. Supplementary Notes
1) Packages can be multi-layered directories, such as: net/http package, which means that the source file is under the src/net/http directory, but the package name in the source file is the name of the last directory, such as http
When importing the package, the complete path must be: import "net/http"
2) Sometimes I see local import (not recommended), and the syntax is similar to this:
import “./config”
When there are such statements in the code, you will often see errors like this: local import "./config" in non-local package
What I understand is the use of this import method: when writing a simple test script and want to use the go run command, you can use this import method.
For example, in the above example, move test/ to the src directory, delete the test directory, modify the import "config" to import "./config", and then execute it in the src directory: go run
It can be seen that local import does not depend on GOPATH
4. Windows
@echo off
setlocal
if exist goto ok
echo must be run from its folder
goto end
: ok
set OLDGOPATH=%GOPATH%
set GOPATH=%~dp0
gofmt -w src
go install test
:end
echo finished
Note: There should be no space between colon and ok, but when put together, it will always be converted into an expression by wordpress. sweat……
5. Update log
1) Published on 2012-12-05
2) 2013-04-13 Fixed: The directory name can be different from the package name, but it is recommended to be the same; change the make file name to install