Golang DEP relies on administrative use

Source: Internet
Author: User
Tags arch linux

DEP Golang Package Management usage Records

DEP is one of the Golang project dependency management, is an official experimental project, the current update is very frequent in the high-speed development period, so the choice of DEP as a Golang dependency manager is more reliable. (Known glide only support no longer developing new features)

Currently DEP v0.5.0 release has been released, the latest changelog display only supports Golang 1.9+ or more versions

Golang the most primitive dependency management is go get, after executing the command will be pulled to replace the stacking into SRC below, but it is as gopath under the global dependency, and go get is not version control, and the isolation of the project's package dependencies in the absence of management tools, the Golang project has a directory node The structure is more popular as follows:

.└── src    ├── demo    │   └── main.go    ├── github.com    ├── golang.org    └── gopkg.in

In this case, each project is a gopath, and the above GOPATH=/XX/XX/SRC can be compiled, and the project dependency is the package under SRC, not associated with the global, go get
Get dependencies, also must be under Project Gopath. You can see that the directory structure is still a lot of flaws. In particular, the package directory of the import package can sometimes be strange, there is no unified style, and the IDE or editor support is not ideal.

So at present Golang introduced the vendor directory as a dependent management directory, and the IDE or Golang Editor plug-in can now be very good support for example Gogland index dependency package will first find the project root directory vendor directory, Vscode go plugin is also. So the current directory structure is more popular as follows:

.├── Gopkg.lock├── Gopkg.toml├── main.go└── vendor    ├── github.com    │   ├── gin-contrib    │   ├── gin-gonic    │   ├── golang    │   ├── mattn    │   └── ugorji    ├── golang.org    │   └── x    └── gopkg.in        ├── go-playground        └── yaml.v2

Project directory $GOPATH/src/projectname/.

Getting Started

Install/uninstall

MacOS

brew install depbrew uninstall dep# /usr/local/bin/dep

Windows

# 推荐 go get 安装go get -u github.com/golang/dep/cmd/dep# $GOPATH/bin/dep.exe

Arch Linux

pacman -S dep# 删除 pacman -R dep

Binary installation

curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh# $GOPATH/bin/dep# 删除 > rm $GOPATH/bin/dep

SOURCE Installation

go get -d -u github.com/golang/depcd $(go env GOPATH)/src/github.com/golang/depDEP_LATEST=$(git describe --abbrev=0 --tags)git checkout $DEP_LATESTgo install -ldflags="-X main.version=$DEP_LATEST" ./cmd/depgit checkout master

Recommended installation method for the quick installation of each system, the most convenient and not error-prone, if you want to use the latest/specified version, recommended source installation

Initialize Project

Recommended use of the second directory structure above

mkdir $GOPATH/src/examplecd $GOPATH/src/exampledep init# 生成 vendor/ 目录下 Gopkg.toml Gopkg.lock

Use process

Usage

Dep is a tool for managing dependencies for Go projectsUsage: "dep [command]"Commands:  init     Set up a new Go project, or migrate an existing one  status   Report the status of the project's dependencies  ensure   Ensure a dependency is safely vendored in the project  prune    Pruning is now performed automatically by dep ensure.  version  Show the dep version informationExamples:  dep init                               set up a new project  dep ensure                             install the project's dependencies  dep ensure -update                     update the locked versions of all dependencies  dep ensure -add github.com/pkg/errors  add a dependency to the projectUse "dep help [command]" for more information about a command.

Get dependency Policy

    • Get the latest TAG dependency Package

DEP Ensure-add Github.com/gin-gonic/gin

    • Gets the dependent package for the specified branch

DEP Ensure-add Github.com/gin-gonic/gin@master

    • Gets the dependency package for the specified tag

DEP Ensure-add github.com/gin-gonic/gin@v1.2

    • Gets the dependency package above the specified tag

DEP Ensure-add github.com/gin-gonic/gin@^v1.2

    • Get a child package for a dependent package

DEP Ensure-add Github.com/prometheus/client_golang/prometheus/push
Get two child packages in a case
DEP Ensure-add Github.com/prometheus/client_golang/prometheus/push Github.com/prometheus/client_golang/prometheus /promhttp

Get the procedure: according to the command to determine whether there is a local cache (cache address: $GOPATH/SRC/DEP), the presence of direct use of the cache, does not exist, first get Git version/branch/tag according to the command to find the corresponding version download.

Fqas

    1. If a timeout occurs during the pull process, please bring your own ladder.
    2. when an unknown error occurs, you can manually delete the cache once, and then try again
    3. when the *build appears. Nogoerror error, which may be the current dependency package at build, does not have a main function, which is typically a child package that needs to pull the dependent package
    4. when an existing project DEP init traverses the current project's dependency package and downloads it, so the time will be long
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.