Golang officially relies on management tools: dep

Keywords: github vim JSON Session

To make a statement here, the go dep s seen by Baidu or google are not DEPs in my blog, so what is the relationship between them? according to Peter Bourgon For example, they all have the same authors, but one is that dep is the official version and godep is a third-party tool.
I'm introducing dep today. I've also introduced glide before. Interested ones can come here. Golang dependency management tool: glide from entry to proficiency Have a look. Now there's another question: why do officials now support dependency management? I personally think there are the following reasons (do not spray, if different or omitted, please leave a message to add):

  • Third-party dependency management is a lot of, although very useful, but rarely compatible, the results - chaotic;
  • In order to increase the cohesion of the community and maintain the simple features of Go out-of-the-box, the official package management does not need to install any third-party tools, and third-party tools will come to be compatible with the official version.
  • There is also a mandarin, for the better development of go;
    The FAQ of DEP has a paragraph describing dep and go get. Website Dependency management tools and go get relationships are also mentioned in profile. One sentence summary: Dependency management tools are for application management code, go get is for GOPATH management code.

Now let's go into the tutorial.

introduce

dep is a prototype dependency management tool that needs to be used in Go 1.7 and higher, indicating that third-party tools are still available in the near future.
PS: dep of this blog is based on v0.3.

install

Environmental preparation.

//Setting environment variables using vendor directory
GO15VENDOREXPERIMENT=1

Install dep

By the time dep is formally integrated into Golang, maybe it's Golang 1.10, the majority of people who eat melon can directly use the go dep command. You still need to install it yourself.

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

Verify installation

$ dep
dep is a tool for managing dependencies for Go projects

Usage: dep <command>

Commands:

  init    Initialize a new project with manifest and lock files
  status  Report the status of the project's dependencies
  ensure  Ensure a dependency is safely vendored in the project
  prune   Prune the vendor tree of unused packages

Examples:
  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 project

Use "dep help [command]" for more information about a command.

There is an important option to ensure that the Chinese meaning of assurance is assurance; assurance; assurance, what the author wants to convey is to ensure that all local states - Code trees, lists, locks and suppliers are synchronized with each other.

See that the instructions have been installed.

Use

Old rules, limited space, I will only introduce the often used. First enter a project in GOPATH.

cd $GOPATH/src/foordep

Initialization (dep init)

$ cd foordep/
$ dep init
$ ll
total 12
-rw-rw-r-- 1 qiangmzsx qiangmzsx  286 Aug  7 11:45 Gopkg.lock
-rw-rw-r-- 1 qiangmzsx qiangmzsx  535 Aug  7 11:45 Gopkg.toml
drwxrwxr-x 2 qiangmzsx qiangmzsx 4096 Aug  7 11:45 vendor

You found that two files (Gopkg.lock, Gopkg.toml) and one directory (vendor) appeared in the application foordep directory. What is the relationship between them?

So when Gopkg.toml and Gopkg.lock are out of sync. it's better to execute dep ensure.

Let's look at their contents.

$ cat Gopkg.lock 
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.

[solve-meta]
  analyzer-name = "dep"
  analyzer-version = 1
  inputs-digest = "ab4fef131ee828e96ba67d31a7d690bd5f2f42040c6766b1b12fe856f87e0ff7"
  solver-name = "gps-cdcl"
  solver-version = 1
$ cat Gopkg.toml 

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
#   name = "github.com/user/project"
#   version = "1.0.0"
#
# [[constraint]]
#   name = "github.com/user/project2"
#   branch = "dev"
#   source = "github.com/myfork/project2"
#
# [[override]]
#  name = "github.com/x/y"
#  version = "2.4.0"

dep ensure

Let's write a Gopkg.toml to see the effect.

# Necessary package
required = ["github.com/astaxie/beego"]
# Ignore packages
ignored = ["golang.org/x/crypto"]
# Project metadata
[metadata]
homepage = "https://github.com/qiangmzsx"
license = "MIT"
owners_name_1 = "qiangmzsx"
owners_email_1 = "qiangmzsx@hotmail.com"
owners_homepage_1 = "https://github.com/qiangmzsx"

# constraint condition
[[constraint]]
  name = "github.com/astaxie/beego"
  # Optional: Version
  version = "=1.8.0"
  # branch
  #branch = "master"
  # revise
  #revision = "beego 1.8.0"
  # Optional: Designated source
  source = "github.com/astaxie/beego"

But how to implement it? You can execute the following commands to find help:

$ dep help ensure
Usage: dep ensure [-update | -add] [-no-vendor | -vendor-only] [-dry-run] [<spec>...]

Project spec:

  <import path>[:alt source URL][@<constraint>]

Ensure gets a project into a complete, reproducible, and likely compilable state:

  * All non-stdlib imports are fulfilled
  * All rules in Gopkg.toml are respected
  * Gopkg.lock records precise versions for all dependencies
  * vendor/ is populated according to Gopkg.lock

Ensure has fast techniques to determine that some of these steps may be
unnecessary. If that determination is made, ensure may skip some steps. Flags
may be passed to bypass these checks; -vendor-only will allow an out-of-date
Gopkg.lock to populate vendor/, and -no-vendor will update Gopkg.lock (if
needed), but never touch vendor/.

The effect of passing project spec arguments varies slightly depending on the
combination of flags that are passed.

Examples:

  dep ensure                                 Populate vendor from existing Gopkg.toml and Gopkg.lock
  dep ensure -add github.com/pkg/foo         Introduce a named dependency at its newest version
  dep ensure -add github.com/pkg/foo@^1.0.1  Introduce a named dependency with a particular constraint

For more detailed usage examples, see dep ensure -examples.

Flags:

  -add          add new dependencies, or populate Gopkg.toml with constraints for existing dependencies (default: false)
  -dry-run      only report the changes that would be made (default: false)
  -examples     print detailed usage examples (default: false)
  -no-vendor    update Gopkg.lock (if needed), but do not update vendor/ (default: false)
  -update       update the named dependencies (or all, if none are named) in Gopkg.lock to the latest allowed by Gopkg.toml (default: false)
  -v            enable verbose logging (default: false)
  -vendor-only  populate vendor/ from Gopkg.lock without updating it first (default: false)

Execute

$ dep ensure
all dirs lacked any go code

Error, because there is no go code in the foordep directory, you can only add a look.

$ vim main.go
package main

import (
    "github.com/astaxie/beego"
    "runtime"
)

func main() {
    maxCPU := runtime.NumCPU()
    runtime.GOMAXPROCS(maxCPU)
    beego.Run()
}

Try again.

$ dep ensure
$ ll vendor/
total 4
drwxrwxr-x 3 qiangmzsx qiangmzsx 4096 Aug  7 16:29 github.com

Look at Gopkg.lock.

# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.

[[projects]]
  name = "github.com/astaxie/beego"
  packages = [".","config","context","grace","logs","session","toolbox","utils"]
  revision = "323a1c4214101331a4b71922c23d19b7409ac71f"
  source = "github.com/astaxie/beego"
  version = "v1.8.0"

[solve-meta]
  analyzer-name = "dep"
  analyzer-version = 1
  inputs-digest = "6fb93334da1b165aaab11f170d871a92b40033575e66e2cf4289e77e0d64551d"
  solver-name = "gps-cdcl"
  solver-version = 1

Now that we need to parse json, let's try importing the github.com/bitly/go-simple JSON package from the command line.

$ dep ensure -add github.com/bitly/go-simplejson
$ Gopkg.lock
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.

[[projects]]
  name = "github.com/astaxie/beego"
  packages = [".","config","context","grace","logs","session","toolbox","utils"]
  revision = "323a1c4214101331a4b71922c23d19b7409ac71f"
  source = "github.com/astaxie/beego"
  version = "v1.8.0"

[[projects]]
  name = "github.com/bitly/go-simplejson"
  packages = ["."]
  revision = "aabad6e819789e569bd6aabf444c935aa9ba1e44"
  version = "v0.5.0"

[solve-meta]
  analyzer-name = "dep"
  analyzer-version = 1
  inputs-digest = "6fb93334da1b165aaab11f170d871a92b40033575e66e2cf4289e77e0d64551d"
  solver-name = "gps-cdcl"
  solver-version = 1

You can find more github.com/bitly/go-simple json, but Gopkg.toml hasn't changed. Note: Errors are reported when dep ensure-add is executed

$ dep ensure -add github.com/bitly/go-simplejson
Gopkg.toml and Gopkg.lock are out of sync. Run a plain dep ensure to resync them before attempting to -add

Dependent versions can also be specified:

$ dep ensure -add github.com/bitly/go-simplejson@=0.4.3

Because Gopkg.toml and Gopkg.lock are out of sync, we need to re-execute dep ensure. Reorganize Gopkg.toml.

# Necessary package
required = ["github.com/astaxie/beego"]
# Ignore packages
ignored = ["golang.org/x/crypto"]
# Project metadata
[metadata]
homepage = "https://github.com/qiangmzsx"
license = "MIT"
owners_name_1 = "qiangmzsx"
owners_email_1 = "qiangmzsx@hotmail.com"
owners_homepage_1 = "https://github.com/qiangmzsx"

# constraint condition
[[constraint]]
  name = "github.com/astaxie/beego"
  # Optional: Version
  version = "=1.8.0"
  # branch
  #branch = "master"
  # revise
  #revision = "beego 1.8.0"
  # Optional: Designated source
  source = "github.com/astaxie/beego"

[[constraint]]
  name = "github.com/bitly/go-simplejson"
  branch = "master"
  source = "https://github.com/bitly/go-simplejson.git"

The version rule of Gopkg.toml: ~ And = is the operator rule used by version. If only version = 1.8.0 is specified, dep automatically adds ^ to indicate the leftmost non-zero version plus one.

^ 1.2.3 means 1.2.3 <= X < 2.0.0
 ^ 0.2.3 means 0.2.3 <= X < 0.3.0
 ^ 0.0.3 means 0.0.3 <= X < 0.1.0

If dep ensure occurs

$ dep  ensure 
error while parsing /home/users/qiangmzsx/golang/src/foordep/Gopkg.toml: multiple constraints specified for github.com/astaxie/beego, can only specify one

The instructions are incorrectly written. You need to see if version, branch and revision are configured in the Gopkg.toml file at the same time.

Empty Configuration

Now we're trying to leave the foordep directory with main.go.

package main

import (
    "github.com/astaxie/beego"
    "github.com/bitly/go-simplejson"
    "runtime"
)

func main() {
    maxCPU := runtime.NumCPU()
    runtime.GOMAXPROCS(maxCPU)
    strJson := `{"announcer": {"nickname": "On History", "kind": "user", "created_at": 1494904539000, "updated_at": 1494983507000, "track_id": 38088960}}`
    mapJson,_:=simplejson.NewJson([]byte(strJson))
    println(mapJson)
    beego.Run()
}

Execute dep ensure to better see the process, with the parameter - v.

$ dep init -v
Root project is "foordep"
 1 transitively valid internal packages
 2 external packages imported from 2 projects
(0)   ✓ select (root)
(1) ? attempt github.com/bitly/go-simplejson with 1 pkgs; 5 versions to try
(1)     try github.com/bitly/go-simplejson@v0.5.0
(1) ✓ select github.com/bitly/go-simplejson@v0.5.0 w/1 pkgs
(2) ? attempt github.com/astaxie/beego with 1 pkgs; 23 versions to try
(2)     try github.com/astaxie/beego@v1.8.3
(2) ✓ select github.com/astaxie/beego@v1.8.3 w/9 pkgs
  ✓ found solution with 10 packages from 2 projects

Solver wall times by segment:
     b-list-versions: 3.926086724s
         b-list-pkgs: 285.209471ms
              b-gmal: 266.828805ms
         select-atom:   3.417834ms
             satisfy:   3.126864ms
         select-root:    428.276µs
            new-atom:    234.106µs
               other:     45.014µs
     b-source-exists:      6.946µs
  b-deduce-proj-root:      3.472µs

  TOTAL: 4.485387512s

  Using ^0.5.0 as constraint for direct dep github.com/bitly/go-simplejson
  Locking in v0.5.0 (aabad6e) for direct dep github.com/bitly/go-simplejson
  Using ^1.8.3 as constraint for direct dep github.com/astaxie/beego
  Locking in v1.8.3 (cab8458) for direct dep github.com/astaxie/beego

Then check the Gopkg.toml and Gopkg.lock files:

$ vim Gopkg.toml
[[constraint]]
  name = "github.com/astaxie/beego"
  version = "1.8.3"

[[constraint]]
  name = "github.com/bitly/go-simplejson"
  version = "0.5.0"

$ vim Gopkg.lock
[[projects]]
  name = "github.com/astaxie/beego"
  packages = [".","config","context","context/param","grace","logs","session","toolbox","utils"]
  revision = "cab8458c1c4a5a3b4bf5192922be620e6dede15b"
  version = "v1.8.3"

[[projects]]
  name = "github.com/bitly/go-simplejson"
  packages = ["."]
  revision = "aabad6e819789e569bd6aabf444c935aa9ba1e44"
  version = "v0.5.0"

[solve-meta]
  analyzer-name = "dep"
  analyzer-version = 1
  inputs-digest = "dc040fb12390d61768be6388ad2935bdd7f9cc93d4b1fdda421a284058277c80"
  solver-name = "gps-cdcl"
  solver-version = 1

Like glide, it has the function of bootstrapping. I don't know if this noun is used correctly. dep automatically generates Gopkg.toml and Gopkg.lock configuration files based on the code.
PS: But it is not recommended to use, because the dependency packages it pulls are the latest, may appear incompatible, and our country is a walled place.

dep cache

When you see this, many people will have questions. Do dep's dependency packages pull new ones every time, or do they give preference to local cache? To be sure Dep also has a local cache, you can open $GOPATH/pkg/dep / see if it exists!?
Now let's do two tests.

GOPATH/src does not have dependency packages

Environment preparation, empty the original cache and vendor, and don't miss github.com/bitly/go-simplejson in $GOPATH/src.

$ ll
total 4
-rwxr--r-- 1 qiangmzsx qiangmzsx 990 Aug  7 16:39 main.go
$ vim main.go 
package main

import (
    "github.com/astaxie/beego"
    "github.com/bitly/go-simplejson"
    "runtime"
)

func main() {
    maxCPU := runtime.NumCPU()
    runtime.GOMAXPROCS(maxCPU)
    strJson := `{"announcer": {"nickname": "On History", "kind": "user", "updated_at": 1494983507000, "track_id": 38088960}}`
    mapJson,_:=simplejson.NewJson([]byte(strJson))
    println(mapJson)
    beego.Run()
}

Execute dep init-gopath-v to view the initialization process.

$ ll
total 4
-rwxr--r-- 1 qiangmzsx qiangmzsx 382 Aug  8 12:47 main.go
$ dep  init -gopath -v
Searching GOPATH for projects...
Following dependencies were not found in GOPATH. Dep will use the most recent versions of these projects.
  github.com/bitly/go-simplejson
Root project is "foordep"
 1 transitively valid internal packages
 2 external packages imported from 2 projects
(0)   ✓ select (root)
(1) ? attempt github.com/astaxie/beego with 1 pkgs; at least 1 versions to try
(1)     try github.com/astaxie/beego@76ce912a30f9255d8d353d365ab99cb069fd29e0
(1) ✗   Unable to update checked out version: fatal: reference is not a tree: 76ce912a30f9255d8d353d365ab99cb069fd29e0
(1)     try github.com/astaxie/beego@v1.8.3
(1) ✓ select github.com/astaxie/beego@v1.8.3 w/9 pkgs
(2) ? attempt github.com/bitly/go-simplejson with 1 pkgs; 5 versions to try
(2)     try github.com/bitly/go-simplejson@v0.5.0
(2) ✓ select github.com/bitly/go-simplejson@v0.5.0 w/1 pkgs
  ✓ found solution with 10 packages from 2 projects

Solver wall times by segment:
         b-list-pkgs: 320.955115ms
              b-gmal: 274.950203ms
             satisfy:   8.179966ms
         select-atom:    2.62224ms
            new-atom:    392.168µs
     b-list-versions:    254.937µs
         select-root:    209.152µs
  b-deduce-proj-root:      40.45µs
               other:      37.01µs
     b-source-exists:       5.83µs

  TOTAL: 607.647071ms

  Using ^1.8.3 as constraint for direct dep github.com/astaxie/beego
  Locking in v1.8.3 (cab8458) for direct dep github.com/astaxie/beego
  Using ^0.5.0 as constraint for direct dep github.com/bitly/go-simplejson
  Locking in v0.5.0 (aabad6e) for direct dep github.com/bitly/go-simplejson

The log shows that dep first looks up github.com/bitly/go-simplejson from $GOPATH because it is not found before downloading from the network.

GOPATH has dependency packages

Environment preparation, empty the original cache and vendor, and note the existence of github.com/bitly/go-simple JSON in $GOPATH/src.

$ ll
total 4
-rwxr--r-- 1 qiangmzsx qiangmzsx 382 Aug  8 12:47 main.go
$ dep  init -gopath -v
Searching GOPATH for projects...
  Using master as constraint for direct dep github.com/bitly/go-simplejson
  Locking in master (da1a892) for direct dep github.com/bitly/go-simplejson
Root project is "foordep"
 1 transitively valid internal packages
 2 external packages imported from 2 projects
(0)   ✓ select (root)
(1) ? attempt github.com/astaxie/beego with 1 pkgs; at least 1 versions to try
(1)     try github.com/astaxie/beego@76ce912a30f9255d8d353d365ab99cb069fd29e0
(1) ✗   Unable to update checked out version: fatal: reference is not a tree: 76ce912a30f9255d8d353d365ab99cb069fd29e0
(1)     try github.com/astaxie/beego@v1.8.3
(1) ✓ select github.com/astaxie/beego@v1.8.3 w/9 pkgs
(2) ? attempt github.com/bitly/go-simplejson with 1 pkgs; at least 1 versions to try
(2)     try github.com/bitly/go-simplejson@master
(2) ✓ select github.com/bitly/go-simplejson@master w/1 pkgs
  ✓ found solution with 10 packages from 2 projects

Solver wall times by segment:
         b-list-pkgs: 312.646734ms
              b-gmal: 265.066047ms
             satisfy:   6.488056ms
         select-atom:   3.287416ms
            new-atom:    397.837µs
         select-root:    373.267µs
     b-list-versions:    108.466µs
               other:      47.43µs
     b-source-exists:       7.71µs
  b-deduce-proj-root:      6.568µs

  TOTAL: 588.429531ms

  Using ^1.8.3 as constraint for direct dep github.com/astaxie/beego
  Locking in v1.8.3 (cab8458) for direct dep github.com/astaxie/beego

You can see that github.com/bitly/go-simplejson is preferred from $GOPATH. I personally think there are two advantages:

  • Save time;
  • The stability and compatibility of local class libraries have been verified by users.

When dep v0.1 does not need to add the - gopath option manually, the dep tool will automatically judge, but after dep v0.3, if no - gopath is added, the default is to download from the network.

Update configuration (dep ensure-update)

Now modify the Gopkg.toml content of the foordep project as follows:

$ vim  Gopkg.toml
[[constraint]]
  name = "github.com/astaxie/beego"
  # Constraint 1.8.0
  version = "=1.8.0"

[[constraint]]
  name = "github.com/bitly/go-simplejson"
  version = "0.5.0"
$ dep  ensure -update
Gopkg.toml and Gopkg.lock are out of sync. Run a plain dep ensure to resync them before attempting to -update
$ dep  ensure 
$ dep  ensure -update -v
Root project is "foordep"
 1 transitively valid internal packages
 2 external packages imported from 2 projects
(0)   ✓ select (root)
(1) ? attempt github.com/astaxie/beego with 1 pkgs; 23 versions to try
(1)     try github.com/astaxie/beego@v1.8.3
(2) ✗   github.com/astaxie/beego@v1.8.3 not allowed by constraint 1.8.0:
(2)     1.8.0 from (root)
(1)     try github.com/astaxie/beego@v1.8.2
(2) ✗   github.com/astaxie/beego@v1.8.2 not allowed by constraint 1.8.0:
(2)     1.8.0 from (root)
(1)     try github.com/astaxie/beego@v1.8.1
(2) ✗   github.com/astaxie/beego@v1.8.1 not allowed by constraint 1.8.0:
(2)     1.8.0 from (root)
(1)     try github.com/astaxie/beego@v1.8.0
(1) ✓ select github.com/astaxie/beego@v1.8.0 w/8 pkgs
(2) ? attempt github.com/bitly/go-simplejson with 1 pkgs; 5 versions to try
(2)     try github.com/bitly/go-simplejson@v0.5.0
(2) ✓ select github.com/bitly/go-simplejson@v0.5.0 w/1 pkgs
  ✓ found solution with 9 packages from 2 projects

Solver wall times by segment:
  b-source-exists:  4.00794324s
      b-list-pkgs: 2.545452669s
           b-gmal: 276.070372ms
          satisfy:   5.179016ms
      select-atom:   4.337704ms
         new-atom:   1.359055ms
  b-list-versions:    467.799µs
        b-matches:    371.239µs
      select-root:    193.471µs
       b-pair-rev:    127.992µs
            other:     25.962µs
   b-pair-version:      7.866µs

  TOTAL: 6.841536385s

$ dep status
PROJECT                         CONSTRAINT  VERSION  REVISION  LATEST   PKGS USED
github.com/astaxie/beego        1.8.0       v1.8.0   323a1c4   323a1c4  8  
github.com/bitly/go-simplejson  ^0.5.0      v0.5.0   aabad6e   aabad6e  1 

Epilogue

See here, then dep has been able to use basic, but at present, dep is not stable enough, no one knows how to change the follow-up, tasting fresh can be, personal does not recommend the use of online. If you like this blog, please comment or leave a message. If you have different opinions, please leave a message for discussion.

Posted by dsds1121 on Thu, 06 Jun 2019 15:14:52 -0700