search
HomeBackend DevelopmentGolangWhat is Go language GOPATH

What is Go language GOPATH

Jan 11, 2023 pm 05:51 PM
golanggo 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

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
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
The Performance Race: Golang vs. CThe Performance Race: Golang vs. CApr 16, 2025 am 12:07 AM

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

Golang vs. C  : Code Examples and Performance AnalysisGolang vs. C : Code Examples and Performance AnalysisApr 15, 2025 am 12:03 AM

Golang is suitable for rapid development and concurrent programming, while C is more suitable for projects that require extreme performance and underlying control. 1) Golang's concurrency model simplifies concurrency programming through goroutine and channel. 2) C's template programming provides generic code and performance optimization. 3) Golang's garbage collection is convenient but may affect performance. C's memory management is complex but the control is fine.

Golang's Impact: Speed, Efficiency, and SimplicityGolang's Impact: Speed, Efficiency, and SimplicityApr 14, 2025 am 12:11 AM

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

C   and Golang: When Performance is CrucialC and Golang: When Performance is CrucialApr 13, 2025 am 12:11 AM

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.

Golang in Action: Real-World Examples and ApplicationsGolang in Action: Real-World Examples and ApplicationsApr 12, 2025 am 12:11 AM

Golang excels in practical applications and is known for its simplicity, efficiency and concurrency. 1) Concurrent programming is implemented through Goroutines and Channels, 2) Flexible code is written using interfaces and polymorphisms, 3) Simplify network programming with net/http packages, 4) Build efficient concurrent crawlers, 5) Debugging and optimizing through tools and best practices.

Golang: The Go Programming Language ExplainedGolang: The Go Programming Language ExplainedApr 10, 2025 am 11:18 AM

The core features of Go include garbage collection, static linking and concurrency support. 1. The concurrency model of Go language realizes efficient concurrent programming through goroutine and channel. 2. Interfaces and polymorphisms are implemented through interface methods, so that different types can be processed in a unified manner. 3. The basic usage demonstrates the efficiency of function definition and call. 4. In advanced usage, slices provide powerful functions of dynamic resizing. 5. Common errors such as race conditions can be detected and resolved through getest-race. 6. Performance optimization Reuse objects through sync.Pool to reduce garbage collection pressure.

Golang's Purpose: Building Efficient and Scalable SystemsGolang's Purpose: Building Efficient and Scalable SystemsApr 09, 2025 pm 05:17 PM

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Why do the results of ORDER BY statements in SQL sorting sometimes seem random?Why do the results of ORDER BY statements in SQL sorting sometimes seem random?Apr 02, 2025 pm 05:24 PM

Confused about the sorting of SQL query results. In the process of learning SQL, you often encounter some confusing problems. Recently, the author is reading "MICK-SQL Basics"...

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor