Table of Contents
1. The concept of GOPATH
Home Backend Development Golang What is Go language GOPATH

What is Go language GOPATH

Jan 11, 2023 pm 05:51 PM
golang go language

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

How to ensure concurrency is safe and efficient when writing multi-process logs? How to ensure concurrency is safe and efficient when writing multi-process logs? Apr 02, 2025 pm 03:51 PM

Efficiently handle concurrency security issues in multi-process log writing. Multiple processes write the same log file at the same time. How to ensure concurrency is safe and efficient? This is a...

See all articles