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

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

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

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

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.

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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

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
The most popular open source editor