Go Common Concepts and Definitions

A workspace and GOPATH
The workspace is the directory where the Go source files are placed.
In general, Go source files need to be stored in the workspace.
But for command source files, this is not necessary.
The structure of each workspace is similar to the following figure:


 
1. src directory
The file used to store the source code
Organized in code packages
2. pkg directory
Used to store archive files (files whose names are suffixed with .a)
All archive files will be stored in platform-specific directories under this directory, also organized as code packages.
Platform-dependent directory: Named with $GOOS_$GOARCH, such as linux_amd64, where GOOS and GOARCH are two implicit Go language environment variables.
For example: <workspace directory>/pkg/<platform-related directory>/<first-level code package>/<second-level code package>/<last-level code package>.a
3. Bin directory
The executable file used to store the Go program in the current workspace.
There are two points to note
a: This directory becomes meaningless when the environment variable GOBIN has been effectively set.
b: When the value of GOPATH contains the paths of multiple workspaces, GOBIN must be set, otherwise the executable file of the Go program cannot be successfully installed,
 
Classification and meaning of two source code files
A file whose name is suffixed with .go and whose content is organized in Go language code
Multiple Go source files need to be organized in code packages
There are three types of source files:
1. Command source file:
A main function that declares itself to belong to the main code package, contains a no-argument declaration and a result declaration.
After being installed, the corresponding executable file will be stored in the directory pointed to by GOBIN or under <current workspace directory>/bin.
The command source file is the entry point of the Go program, but it is not recommended to write the program in one file.
Note: It is strongly not recommended to directly include multiple command source files in the same code package.
2. Library source code file:
A source file that does not have the two characteristics of a command source file.
After being installed, the corresponding archive files will be stored in <current workspace directory>/pkg/<platform specific directory>
3、测试源码文件:
不具备命令源码文件的那两个特征的源码文件。
名称以_test.go为后缀。


 
其中命令源码文件和库源码文件是一般意义上的go语言程序,测试源码文件是辅助源码文件。
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326942337&siteId=291194637