Golang is a static strongly typed, compiled, and hairstyle-based programming language with garbage collection. The Golang provides a convenient installation package that supports Windows, Linux, and Mac systems.
Download the installation package
Golang's official website is https://golang.org/, if the official website cannot open, can visit https://golang.google.cn/this domain name. In the official dot click Download Go will go to the download page, you can see here provides the installation package for each system, also provides the source code, you can download the source code compiled installation.
After the download runs the installation package, execute the Go ENV command in terminal if the following output indicates that the installation was successful.
Goroot and Gopath
Look at the above output, you will find that there is a gopath, there is a goroot, then exactly which is the golang of the operating environment.
First visit Goroot This path, you will find that it contains Bin, Lib and other folders. Goroot is the installation path of Golang, which contains Golang compilation, tools, standard libraries, etc., which will exist after installation.
Unlike Goroot, Gopath is the workspace path, starting with Go 1.8, if Gopath is not set, there will be a default value on Unix for $home/go,%userprofile%/go on Windows, and when you call go build, It will look for the source code in the Gopath. Visit Gopath This path, you will find only pkg, bin, src three folders, and the basic is empty, this is a contractual directory structure, src folder used to store the source code, pkg store compiled after the generated file, bin storage compiled after the executable file. The project code needs to be under the GOPATH/SRC path.
Gopath path out of the storage project code, but also store all the dependencies through the go Get installation, project code and dependency code is peer, when the various projects have a lot of dependencies, the Gopath path of the code is much more scary, and difficult to break apart.
Vendor
In 2015, go 1.5 added a pilot vendor mechanism (the go 1.6 version to 2016 turned on by default), the vendor mechanism is to add the vendor folder to the project to store dependencies so that dependencies on different projects can be separated.
When you use the Go run or go Build command, you will first look for dependencies from the vendor folder under the current path, and if vendor does not exist, it will only look for dependencies from Gopath.
However we install dependencies usually using go get or go Install command, these two commands will still install dependency to Gopath path.
Package Management tools DEP
Vendor is just a mechanism that go officially provides, but the issue of package management is still unresolved and there is no versioning of dependencies. The package management tool is also required if you want to implement the above functions.
Go official gives a comparison of package management tools: Https://github.com/golang/go/wiki/PackageManagementTools
DEP is an official pilot package management tool that can be installed with the following script
Curl Https://raw.githubusercontent.com/golang/dep/master/install.sh | Sh
After the installation is complete, go to the project path and execute
dep init
Two files will appear in the project a directory
Gopkg.tomlGopkg.lockvendor
Process for DEP package management
The solving function, which takes the import package in the current project and the rules in the GOPKG.TOML as input, and the immutable dependency graph as the output after the completion of the transfer, forming the Gopkg.lock.
The vendor feature, which takes the information in the Gopkg.lock as input, ensures that the version that is locked in the Gopkg.lock file can be used when the project is compiled.
Use the following command to add a dependency
dep ensure -add [github.com/gin-gonic/gin](http://github.com/gin-gonic/gin)
Update Gopkg.lock with the following command
dep ensure -update
Welcome to the column "Golang private Cuisine"