GOPATH is an environment variable used in the Go language. It uses an absolute path to provide the working directory of the project (also called the workspace), which is the file path where the Golang project code is stored. The GOPATH directory is generally: 1. bin, which stores the binary files generated by compilation; 2. pkg, which includes the three folders XX_amd64, mod and sumdb; 3. src, where the golang project code is stored.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
GOPATH is an environment variable used in the Go language. It uses an absolute path to provide the working directory of the project ( is also called workspace), is the file path where the Golang project code is stored, and GOPATH is suitable for processing complex projects composed of a large number of Go language source codes and multiple packages.
The working directory is a relative reference directory for engineering development. For example, when you want to write a set of server code in the company, the desktop, computer and chair included in your workstation are your workspace.
The concept of workspace is similar to the concept of working directory. If you do not use the concept of working directories, when developing with multiple people, each person has his own directory structure, and the location for reading configuration files is not uniform.
##GOPATH The directory is generally:
1. bin stores the binary files generated by compilation. For example, if you execute the command go get github.com/google/gops, the binary file of gops will be generated in the bin directory. 2. pkg Among them, there are three folders under pkg.## The following is the development directory of a complete Go project:XX_amd64: XX is the target operating system. For example, the mac system corresponds to darwin_amd64, the linux system corresponds to linux_amd64, and files ending in .a are stored.
3. The location where src stores the- mod: When the go Modules mode is enabled, the location where the dependent packages are stored in the go get command cache
- sumdb: The location where the checksum data downloaded by the go get command cache is stored
golang project code
my-go // my-go为GOPATH目录
-- bin
-- myApp1 // 编译生成
-- myApp2 // 编译生成
-- myApp3 // 编译生成
-- pkg 依赖包编译后的*.a文件//
-- src
-- MyApp1 // 项目1
-- models
-- controllers
-- others
-- main.go
-- MyApp2 // 项目2
-- models
-- controllers
-- others
-- main.go
$GOPATH/ in the src directory, and if you execute
go get to use a third-party class library, it will be automatically downloaded and installed in the $GOPATH
directory. The Golang code of the project is mixed with third-party Golang files.
If each project requires the same dependencies, then we will download a large number of duplicate third-party dependency packages in different GoPath srcs. This will also take up a lot of disk space
We set different GoPaths for different projects, and the advantages are very obvious:Must specify the directory,
- When using the go get command, the version to be obtained cannot be specified.
- When referencing third-party projects, reference problems with different versions such as v1, v2, v3, etc. cannot be handled because in In GOPATH mode, the project paths are all github.com/foo/project
- . The third-party version number cannot be synchronized. When running a Go application, there is no guarantee that others will have the expected dependencies. The third-party libraries are the same version.
It is easy to manage projects, and each project is Different GoPath, which can handle the project structure very clearly for us to manage multiple Golang projects. If we put all projects under the same GoPath src package, the project structure will become very confusing and difficult to manage.
But when we need to rely on third-party packages, the disadvantages of setting different GoPaths for different projects are also very obvious:
So, setting up a GoPath directory to solve the problem of duplicate dependencies, setting up different GoPath directories to solve the problem of confusing Golang project structure, is an interesting idea in itself. Controversial issues. In order to solve all these problems, Golang finally introduced the concept of GoModule. 【Related recommendations: Go video tutorial, Programming teaching】
The above is the detailed content of What is Go language GOPATH. For more information, please follow other related articles on the PHP Chinese website!