What is Go language GOPATH

青灯夜游
Release: 2023-01-11 17:51:27
Original
1996 people have browsed it

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.

What is Go language GOPATH

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

1. The concept of GOPATH

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.

2. Use of GOPATH

##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.

    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.
  • 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
3. The location where src stores the

golang project code

What is Go language GOPATH

## The following is the development directory of a complete Go project:

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
Copy after login

3. Cause problems

When using GOPATH mode, we

need to store the application code in the fixed

$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

Disadvantages of GOPATH

    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.
We set different GoPaths for different projects, and the advantages are very obvious:

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:

    Chapter Mixing third-party dependent packages with our own Golang packages will cause certain troubles in our project file management.
  • Different GoPaths need to download dependencies, so there will be a lot of duplicate dependencies on the disk, which will take up a lot of our disk space.

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.

What is Go language GOPATH

【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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template