In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use GoPath mode and GoMoudle mode". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use GoPath mode and GoMoudle mode.
Go moudle's past life, present life, past life-what is gopathgopath?
I believe there is no need for me to say any more about what GOPATH is. Everyone has been devastated for many years and should have experienced it. Prior to v.1.11, when installing GO, it was necessary to configure GoPath in the environment variables, which we could simply understand as a working directory. The directory structure is as follows
-- bin stores binary executable files generated after compilation
-- pkg stores the compiled .a file
-- src stores the source code of the project, either the code you wrote or the package you downloaded from go get.
The way to manage all your packages or other people's packages in the $GOPATH/src directory is called the GOPATH pattern.
What's wrong with gopath?
Version management issues
GOPATH has no concept of version at all, and if the library you apply needs to be upgraded, it is a global upgrade, so services involving this library will use the new library in the next compilation, which is a very dangerous thing. Version management is very important, and you should manage your own reference library.
Collaborative development problem
When other developers get to the source code to make changes, you can't guarantee that the package he downloaded is the version you want, and this may cause service errors, and it is difficult to find the cause.
This life-GoMoudle
Go modules is officially launched in v1.11, and in v1.14, it is officially said that it is mature enough to be used in production.
Since v1.11, go env has added an environment variable: GO111MODULE, which is actually the symbol of v1.11. Go seems to like this naming method very much. For example, when vendor appeared, there was also a GO15VENDOREXPERIMENT environment variable, of which 15, vendor was born at v1.5.
GO111MODULE is a switch that enables you to turn go mod mode on or off.
It has three optional values: off, on, auto, and the default value is auto.
GO111MODULE=off disables module support and looks for packages in the GOPATH and vendor folders at compile time.
GO111MODULE=on enables module support, ignores GOPATH and vendor folders when compiling, and only depends on go.mod downloads.
GO111MODULE=auto, module support is automatically enabled when the project is outside $GOPATH/src and there are go.mod files in the project root directory.
With the advent of go mod, GOPATH (definitely no one uses it) and GOVENDOR will be phased out and are being phased out, but if your project still uses those soon-to-be-outdated package dependency management solutions, be careful to set GO111MODULE to off.
How to set it up exactly? You can use the command of go env, if I want to open go mod, use this command
Go env-w GO111MODULE= "on" go mod use
Go mod no longer relies on $GOPATH, allowing it to create projects without GOPATH
You can create a folder go_demo anywhere on your computer
Initialize with the go mod command, where there are only 2 files in the directory, and go.mod is the file generated after the command is executed.
Go mod init go_demo
File main.go explanation: introduce a now time processing library, output time
Package mainimport ("fmt"github.com/jinzhu/now") func main () {fmt.Println ("hello world", now.BeginningOfDay ())}
Go.mod content
Module go_demogo 1.15require github.com/jinzhu/now v1.1.1
Content explanation:
First line: the reference path of the module
Second line: the version of go used by the project
Line 3: the direct dependency package and its version required by the project
At this point, we execute go build on the command line to compile and find that there is an extra go.sum file, so what is this file?
Go.sum files are more complex than go.mod files. Although there is a lot of content, it is not difficult to understand.
Each line consists of a module path, a module version, and a hash check value, which is used to ensure that the currently cached module will not be tampered with. Hash is a string that begins with H2:, indicating that the algorithm for generating checksum is the first version of the hash algorithm (sha256).
Go.mod and go.sum are go modules version management guidelines, so both go.mod and go.sum files should be submitted to your Git repository to avoid inconsistencies between the regenerated go.mod and go.sum and the benchmark version you developed when others used you to write the project.
Benefits of go mod
Compared to the gopath approach, the benefits of go mod are obvious. You don't have to work hard for the version. Modularity will do it for you automatically. It's a little bit like Nuget in C # and npm in node. Developers should not worry about such things, but should focus on coding issues.
Go mod commands are commonly used
Go mod init: initialize the go mod and generate the go.mod file, followed by the parameter to specify the module name, which has been demonstrated above.
Go mod download: manually triggers downloading dependent packages to local cache (default is $GOPATH/pkg/mod directory)
Go list-m-json all: print dependency details as json
Not commonly used
Go mod graph: print the module dependency structure of the project
Go mod tidy: add missing packages and delete useless packages
Go mod verify: verify whether the module has been tampered with
Go mod why: see why you need to rely on
Go mod vendor: export all dependencies of the project under vendor
Go mod edit: Editing go.mod fil
How to treat go moudle and apply it to work
Go mode is definitely a recommended way, if it is not based on historical projects-using GOPATH, it is recommended to use go mod as soon as possible, which can save you a lot of worries. But if many of the company's projects are based on gopath, then don't worry. Gopath's old projects also support switching to go mod mode, and the way is very simple. Of course, a formal switch must require the consent of the leader. After all, it is risky to switch the working system at will.
At this point, I believe you have a deeper understanding of "how to use GoPath mode and GoMoudle mode". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.