1. There are three environment variables: GOROOT, GOPATCH, and GOBIN
Question 1: What is the point of setting up GOPATH?
A: The value of the environment variable GOPATH can be the path of a directory or the path of multiple directories, each representing a workspace of the Go language. These workspaces are used to place Go source files, as well as archive files and executable files after installation.


1. The organization of Go language source code
The basic organization unit of the Go language source code is the code package. A code package is a one-to-one directory. A directory can have subdirectories and a code package can have subpackages.
A code package can contain any number of source files with the.go extension, and these source files need to be declared as belonging to the same code package. The package name is usually the same as the directory in which these source files reside.
Each package has an import path, and before we can actually use the program entity, we must import it into the package. This is done by importing the import path of the code package.
So Go source code is organized by the environment variable GOPATH, workspace, SRC directory, and code package. In general, GO source files need to be stored in a code package directory under the SRC directory in a workspace of the environment variable GOPATH.


2. Obtain the result after the source code is installed
The source files in the SRC subdirectory of the workspace will be generated after go install and placed in the corresponding directory under the PKG subdirectory of the current workspace, or directly in the bin subdirectory of the workspace. Between the relative directory of the archive and the PKG directory are level-1 directories called platform-dependent directories. The name of the platform-related directory is composed of the target operating system, the underscore, and the target computing architecture code. For example: linux_amd64.


3. Understand the process of building and installing a Go program


Build using the go build command, install using the go install command. Compilation, packaging, and so on are performed when the code package is built and installed, and any files generated by these operations are saved to a temporary directory.
The main purpose of the build is to check and verify. When building the library source file, the result of the operation is stored in the temporary directory. When building the command source file, the operation file is moved to the directory where the source file resides.
The installation performs a build and then a link operation to move the result file to the specified directory, or if the library source file is installed, it will be moved to a subdirectory under the PKG directory in the workspace.
If the command source file is installed, the resulting file is moved to the bin directory in the workspace, or to the directory pointed to by the environment variable GOBIN.


4. Some optional uses of the go build command
By default, the go bulid command does not compile packages that the target package depends on. However, if the dependent package archive does not exist or the source file has changed, it will still compile.
Adding -a to the command execution forces compilation of it and the packages it depends on. Adding -i not only compiles dependent packages but also installs their archives.


The operation of determining which code packages are compiled:


1. Go build-x, so that you can see the specific operation of go build. Go build-n allows you to see the actions but not execute them.


2. Go build -v Displays the name of the code package compiled by the go build command. You can use it with -a.




5. Go get orders
The go get command downloads the object code package from the main repository and installs it into the corresponding directory (SRC bin PKG) in the first workspace of the environment variable GOPATH. The rules are the same as before.
There are a few common tags for go get
-u: Downloads the installation packages regardless of whether they already exist in the workspace


-d: only downloads the code package, but does not install the code package


-fix: Run a current based on the code package after downloading


-t: Downloads the required code package at the same time


-insecure: allows you to download and install code packages using insecure network protocols.


Sometimes we will change the path of the remote repository. In this case, we can customize the path so as not to change the remote import path of the package.


A full note on custom code package import paths:
https://github.com/hyper0x/go_command_tutorial/blob/master/0.3.md