Go language core programming (8)-project management

Go language core programming (8)-project management

Note: This article is a personal reading note of "Go Language Core Programming" (Li Wenta/Work)

Compiler Environment

Compiling Go source code Go 1.5 and Go compilers are completely rewritten in Go. To install Go from source code, a Go compiler environment is required. You need to download version 1.4 of the Go compiler source code written in C language, through the Linux built-in gcc. Compile a Go environment, and then use this Go environment to compile a new version of the Go environment. The important environment variable $GOROOT $GOROOTis the installation root directory of Go. The default environment under Linux is/usr/local/go. If it $GOROOTis located in the above location, you do not need to explicitly set the $GO ROOTenvironment variable; if it is not the default installation directory, you need to explicitly set the $GOROOTenvironment variable $GOPATH $GOPATH is Go language programming If the GOPATH environment variable is not set for the working directory (workspace), the system defaults to $HOME/go under Linux.

Third package management

Vendor Go1.5 introduces the vendor mechanism. Manually set the environment variable GO15VENDOREXPERIMENT = 1, and the compiler can enable the vendor. Starting from Go 1.6, the vendor directory search is enabled by default. The process of finding a third-party package: 1 If there is a vendor directory under the current package, search for a third-party package under it, if not found, continue to the next step. 2 If there is no vendor directory in the current package directory, then follow the current package directory to look up the vendor directory level by level until it finds

The third-party package of GOPATH/src is placed in the vendor directory of the current project for management. It provides a guarantee for the independent management of third-party packages that projects rely on. Multiple projects independently manage their own third-party dependent packages, and they will not affect each other. Vendor converts the original package sharing mode to the mode of independent maintenance for each project. Another advantage of vendor is to ensure the integrity of the code in the project directory, copy the project code to other Go compilation environments, and no need to download third-party packages. , You can compile directly. The vendor has an important issue that has not resolved the version management of the third package. Go get -u updates the third-party package. The default is to pull the latest version of the default branch of the project to the local.


Install dep

go get-u github.com/golang/dep/cmd/dep

Use dep initthe command to initialize the project, the command can be used for new projects, but also for projects already exist. Gopkg.toml can be freely configured by users, including dependent source, branch, version, etc. Gopkg.lock only describes the current view of the third-party package version of the project. dep ensure manually update Gopkg.toml need to run dep ensureto regenerate Gopkg.lock and update vendor,

Reference: https://cloud.tencent.com/developer/article/1438477 Go Language Core Programming (8)-Project Management-Cloud + Community-Tencent Cloud