Golang dependency management tool: glide from entry to proficient use

introduce

Whether you're developing Java or the Golang you're learning, you'll encounter dependency management issues. Java has awesome Maven and Gradle. Golang also has godep, govendor, glide, gvt, gopack, etc. This article mainly introduces gilde . Glide is a package management tool for Golang, which is designed to solve the Golang dependency problem. Why do you need glide? The reason is very simple, the defect of Go language's native package management. List golang's get subcommand management dependencies have many major flaws:

  • The platforms that can pull the source code are very limited, and most of them rely on github.com
  • The version cannot be distinguished, so that the developer uses the last package name as the version division
  • The dependency list/relationship cannot be persisted locally, you need to find all the dependency packages and then go get them one by one
  • Can only rely on the local global warehouse (GOPATH/GOROOT), cannot place the library in the local warehouse ($PROJECT_HOME/vendor)

Install

Golang environment settings

Using the vendor directory feature, Go 1.5 was added as an experimental feature (need to specify the GO15VENDOREXPERIMENT=1 environment variable), and a concept formally introduced in Go 1.6. Most go dependency solutions are based on it. GO15VENDOREXPERIMENT is a new environment variable in Go 1.5. If the value is changed to 1, it means it is enabled. It can add a directory named vendor in the project root directory to Go's library search path to achieve a local dependency effect.
The feature was added as an experimental feature in version 1.5, enabled by default in 1.6, and added to the standard by removing variables in 1.7.
Go provides the original go get, so that third-party package management can be extended based on go get. The GO15VENDOREXPERIMENT feature makes local dependencies a reality. Go official is creating conditions for third-party package management and guiding developers and users to the recommended direction to promote the prosperity of the community. It proves that the ecology of a language technology depends not only on the official or on the perfection of the official.

//设置环境变量 使用vendor目录
GO15VENDOREXPERIMENT=1

Why choose glide? Glide is one of the many package management tools that implement the GO15VENDOREXPERIMENT feature, but it is the most recommended in this article, and the reason why it is recommended is very simple, because it is currently the most concerned. Several main functions:

  • Persist the list of dependencies to the configuration file, including the dependency version (support scope limitation) and private repositories, etc.
  • Persist the relationship tree to the lock file (similar to yarn and cargo) to repeatedly pull the same version dependencies
  • Compatible with version control systems supported by go get: Git, Bzr, HG, and SVN
  • Support GO15VENDOREXPERIMENT feature, so that different projects can depend on different versions of the same project
  • Other tool configurations can be imported, such as: Godep, GPM, Gom, and GB

glideThe Chinese document introduced to you here , thanks to @年紀紅for the translation.

install glide

$ go get github.com/Masterminds/glide
$ go install github.com/Masterminds/glide

verify

$ glide
NAME:
   glide - Vendor Package Management for your Go projects.

   Each project should have a 'glide.yaml' file in the project directory. Files
   look something like this:

       package: github.com/Masterminds/glide
       imports:
       - package: github.com/Masterminds/cookoo
         version: 1.1.0
       - package: github.com/kylelemons/go-gypsy
         subpackages:
         - yaml

   For more details on the 'glide.yaml' files see the documentation at
   https://glide.sh/docs/glide.yaml


USAGE:
   glide [global options] command [command options] [arguments...]

VERSION:
   0.13.0-dev

COMMANDS:
     create, init       Initialize a new project, creating a glide.yaml file
     config-wizard, cw  Wizard that makes optional suggestions to improve config in a glide.yaml file.
     get                Install one or more packages into `vendor/` and add dependency to glide.yaml.
     remove, rm         Remove a package from the glide.yaml file, and regenerate the lock file.
     import             Import files from other dependency management systems.
     name               Print the name of this project.
     novendor, nv       List all non-vendor paths in a directory.
     rebuild            Rebuild ('go build') the dependencies
     install, i         Install a project's dependencies
     update, up         Update a project's dependencies
     tree               (Deprecated) Tree prints the dependencies of this project as a tree.
     list               List prints all dependencies that the present code references.
     info               Info prints information about this project
     cache-clear, cc    Clears the Glide cache.
     about              Learn about Glide
     mirror             Manage mirrors
     help, h            Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --yaml value, -y value  Set a YAML configuration file. (default: "glide.yaml")
   --quiet, -q             Quiet (no info or debug messages)
   --debug                 Print debug verbose informational messages
   --home value            The location of Glide files (default: "/home/users/qiangmzsx/.glide") [$GLIDE_HOME]
   --tmp value             The temp directory to use. Defaults to systems temp [$GLIDE_TMP]
   --no-color              Turn off colored output for log messages
   --help, -h              show help
   --version, -v           print the version

If you see this, then congratulations, the installation has been successful! ! !

use

The space is limited, I only introduce the frequently used ones. First enter a project in GOPATH.

cd $GOPATH/src/foor

initialization (glide init)

$ glide init
[INFO]  Generating a YAML configuration file and guessing the dependencies
[INFO]  Attempting to import from other package managers (use --skip-import to skip)
[INFO]  Scanning code to look for dependencies
[INFO]  --> Found reference to github.com/urfave/cli
[INFO]  Writing configuration file (glide.yaml)
[INFO]  Would you like Glide to help you find ways to improve your glide.yaml configuration?
[INFO]  If you want to revisit this step you can use the config-wizard command at any time.
[INFO]  Yes (Y) or No (N)?
Y
[INFO]  Loading mirrors from mirrors.yaml file
[INFO]  Looking for dependencies to make suggestions on
[INFO]  --> Scanning for dependencies not using version ranges
[INFO]  --> Scanning for dependencies using commit ids
[INFO]  Gathering information on each dependency
[INFO]  --> This may take a moment. Especially on a codebase with many dependencies
[INFO]  --> Gathering release information for dependencies
[INFO]  --> Looking for dependency imports where versions are commit ids
[INFO]  Here are some suggestions...
[INFO]  The package github.com/urfave/cli appears to have Semantic Version releases (http://semver.org).
[INFO]  The latest release is v1.19.1. You are currently not using a release. Would you like
[INFO]  to use this release? Yes (Y) or No (N)
Y
[INFO]  Would you like to remember the previous decision and apply it to future
[INFO]  dependencies? Yes (Y) or No (N)
Y
[INFO]  Updating github.com/urfave/cli to use the release v1.19.1 instead of no release
[INFO]  The package github.com/urfave/cli appears to use semantic versions (http://semver.org).
[INFO]  Would you like to track the latest minor or patch releases (major.minor.patch)?
[INFO]  Tracking minor version releases would use '>= 1.19.1, < 2.0.0' ('^1.19.1'). Tracking patch version
[INFO]  releases would use '>= 1.19.1, < 1.20.0' ('~1.19.1'). For more information on Glide versions
[INFO]  and ranges see https://glide.sh/docs/versions
[INFO]  Minor (M), Patch (P), or Skip Ranges (S)?
P
[INFO]  Would you like to remember the previous decision and apply it to future
[INFO]  dependencies? Yes (Y) or No (N)
Y
[INFO]  Updating github.com/urfave/cli to use the range ~1.19.1 instead of commit id v1.19.1
[INFO]  Configuration changes have been made. Would you like to write these
[INFO]  changes to your configuration file? Yes (Y) or No (N)
Y
[INFO]  Writing updates to configuration file (glide.yaml)
[INFO]  You can now edit the glide.yaml file.:
[INFO]  --> For more information on versions and ranges see https://glide.sh/docs/versions/
[INFO]  --> For details on additional metadata see https://glide.sh/docs/glide.yaml/
$ ll
glide.yaml
$ cat glide.yaml 
package: foor
import: []

During initialization, glide will ask some questions. glide.yaml records the list of dependent packages and their update rules. Every time glide up is executed, the new version will be downloaded according to the specified rules (such as only downloading patches (patch) and not downloading upgrades (minor)).

a complete gilde.yaml

package: foor
homepage: https://github.com/qiangmzsx
license: MIT
owners: 
- name: qiangmzsx
  email: [email protected]
  homepage: https://github.com/qiangmzsx
# 去除包
ignore:
- appengine
- golang.org/x/net
# 排除目录
excludeDirs:
- node_modules
# 导入包
import:
- package: github.com/astaxie/beego
  version: 1.8.0
- package: github.com/coocood/freecache
- package: github.com/garyburd/redigo/redis
- package: github.com/go-sql-driver/mysql
- package: github.com/bitly/go-simplejson
- package: git.oschina.net/qiangmzsx/beegofreecache
testImport:
- package: github.com/smartystreets/goconvey
  subpackages:
  - convey

Many people are not used to watching yaml. It's fine. I'll turn the json for everyone to see.

{
  "excludeDirs": [
    "node_modules"
  ], 
  "owners": [
    {
      "homepage": "https://github.com/qiangmzsx", 
      "name": "qiangmzsx", 
      "email": "[email protected]"
    }
  ], 
  
  "license": "MIT", 
  "package": "foor", 
  "ignore": [
    "appengine", 
    "golang.org/x/net"
  ], 
  "import": [
    {
      "version": "1.8.0", 
      "package": "github.com/astaxie/beego"
    }, 
    {
      "package": "github.com/coocood/freecache"
    }, 
    {
      "package": "github.com/garyburd/redigo/redis"
    }, 
    {
      "package": "github.com/go-sql-driver/mysql"
    }, 
    {
      "package": "github.com/bitly/go-simplejson"
    }, 
    {
      "package": "git.oschina.net/qiangmzsx/beegofreecache"
    }
  ], 
  "testImport": [
    {
      "subpackages": [
        "convey"
      ], 
      "package": "github.com/smartystreets/goconvey"
    }
  ], 
  "homepage": "https://github.com/qiangmzsx"
}

Version number specification rules

=: equal (aliased to no operator)
!=: not equal
>: greater than
<: less than
>=: greater than or equal to
<=: less than or equal to

1.2 - 1.4.5 which is equivalent to >= 1.2, <= 1.4.5
2.3.4 - 4.5 which is equivalent to >= 2.3.4, <= 4.5
1.2.x is equivalent to >= 1.2.0, < 1.3.0

>= 1.2.x is equivalent to >= 1.2.0
<= 2.x is equivalent to < 3
* is equivalent to >= 0.0.0

~1.2.3 is equivalent to >= 1.2.3, < 1.3.0
~1 is equivalent to >= 1, < 2
~2.3 is equivalent to >= 2.3, < 2.4
~1.2.x is equivalent to >= 1.2.0, < 1.3.0
~1.x is equivalent to >= 1, < 2

^1.2.3 is equivalent to >= 1.2.3, < 2.0.0
^1.2.x is equivalent to >= 1.2.0, < 2.0.0
^2.3 is equivalent to >= 2.3, < 3
^2.x is equivalent to >= 2.0.0, < 3

' 'The specified version reports an error, you need to use ' ' to specify can not fill in

Install dependencies (glide install)

We have prepared glide.yaml, let's try to install it now.

$ glide install
[ERROR]	Failed to parse /home/users/xxxx/golang/src/foor/glide.yaml: yaml: invalid leading UTF-8 octet

Reported wrong! Don't worry to see if your yaml file is utf-8 encoded, if not, just convert it!

$ glide install
[INFO]	Lock file (glide.lock) does not exist. Performing update.
[INFO]	Downloading dependencies. Please wait...
[INFO]	--> Fetching updates for github.com/go-sql-driver/mysql
[INFO]	--> Fetching updates for github.com/astaxie/beego
[INFO]	--> Fetching updates for github.com/coocood/freecache
[INFO]	--> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO]	--> Fetching updates for github.com/bitly/go-simplejson
[INFO]	--> Fetching updates for github.com/garyburd/redigo
[INFO]	--> Fetching updates for github.com/smartystreets/goconvey
[INFO]	--> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.0
[INFO]	Resolving imports
[INFO]	Downloading dependencies. Please wait...
[INFO]	Setting references for remaining imports
[INFO]	Exporting resolved dependencies...
[INFO]	--> Exporting github.com/astaxie/beego
[INFO]	--> Exporting github.com/coocood/freecache
[INFO]	--> Exporting github.com/bitly/go-simplejson
[INFO]	--> Exporting github.com/go-sql-driver/mysql
[INFO]	--> Exporting github.com/garyburd/redigo
[INFO]	--> Exporting github.com/smartystreets/goconvey
[INFO]	--> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO]	Replacing existing vendor dependencies
[INFO]	Project relies on 6 dependencies.
$ ll
total 12
glide.lock
glide.yaml
vendor
$ ll vendor/
git.oschina.net
github.com

Have you seen glide.lock? This file records the revision determined by the dependent package. The next time you execute glide install, you will directly read this file to download the determined version.

Upgrade version (glide up)

glide up will update the dependent package code according to the semantic version rules. If you need to use the new version of the code during the development process, you can execute this command: Modify a Package in glide.yaml.

- package: github.com/astaxie/beego
  version: 1.8.3

Execute glide up.

$ glide up
[INFO]	Downloading dependencies. Please wait...
[INFO]	--> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO]	--> Fetching updates for github.com/garyburd/redigo
[INFO]	--> Fetching updates for github.com/go-sql-driver/mysql
[INFO]	--> Fetching updates for github.com/astaxie/beego
[INFO]	--> Fetching updates for github.com/bitly/go-simplejson
[INFO]	--> Fetching updates for github.com/coocood/freecache
[INFO]	--> Fetching updates for github.com/smartystreets/goconvey
[INFO]	--> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO]	Resolving imports
[INFO]	Downloading dependencies. Please wait...
[INFO]	Setting references for remaining imports
[INFO]	Exporting resolved dependencies...
[INFO]	--> Exporting github.com/astaxie/beego
[INFO]	--> Exporting github.com/bitly/go-simplejson
[INFO]	--> Exporting github.com/garyburd/redigo
[INFO]	--> Exporting github.com/go-sql-driver/mysql
[INFO]	--> Exporting github.com/coocood/freecache
[INFO]	--> Exporting github.com/smartystreets/goconvey
[INFO]	--> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO]	Replacing existing vendor dependencies
[INFO]	Project relies on 6 dependencies.

Add and download dependencies (glide get)

In addition to automatically resolving imports from the code, glide can also directly download dependencies that are not in the code through glide get, which is basically the same as the usage of go get:

$ glide get github.com/orcaman/concurrent-map
[INFO]	Preparing to install 1 package.
[INFO]	Attempting to get package github.com/orcaman/concurrent-map
[INFO]	--> Gathering release information for github.com/orcaman/concurrent-map
[INFO]	--> Adding github.com/orcaman/concurrent-map to your configuration
[INFO]	Downloading dependencies. Please wait...
[INFO]	--> Fetching updates for github.com/garyburd/redigo
[INFO]	--> Fetching updates for github.com/astaxie/beego
[INFO]	--> Fetching updates for github.com/go-sql-driver/mysql
[INFO]	--> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO]	--> Fetching updates for github.com/bitly/go-simplejson
[INFO]	--> Fetching github.com/orcaman/concurrent-map
[INFO]	--> Fetching updates for github.com/coocood/freecache
[INFO]	--> Fetching updates for github.com/smartystreets/goconvey
[INFO]	Resolving imports
[INFO]	Downloading dependencies. Please wait...
[INFO]	--> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO]	Exporting resolved dependencies...
[INFO]	--> Exporting github.com/smartystreets/goconvey
[INFO]	--> Exporting github.com/garyburd/redigo
[INFO]	--> Exporting github.com/go-sql-driver/mysql
[INFO]	--> Exporting github.com/orcaman/concurrent-map
[INFO]	--> Exporting github.com/astaxie/beego
[INFO]	--> Exporting github.com/bitly/go-simplejson
[INFO]	--> Exporting github.com/coocood/freecache
[INFO]	--> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO]	Replacing existing vendor dependencies

Using a glide mirror

[WARN]	Unable to checkout golang.org/x/crypto
[ERROR]	Update failed for golang.org/x/crypto: Cannot detect VCS
[ERROR]	Failed to do initial checkout of config: Cannot detect VCS

It is estimated that many people have encountered these lines of information. In China or within the company, some sites may not be accessible, resulting in Golang-dependent packages that cannot be downloaded through go get. At this point, it is time for glide to show its power. You can map the URL of the walled repository to the URL that is not blocked by configuration, or even map to the local repository. Map golang.org to github: Modify glide.yaml to add

- package: golang.org/x/crypto

If your network is accessible, you don't need to use the glide mirroring function, you can skip it.

$ glide mirror set golang.org/x/crypto github.com/golang/crypto
[INFO]	golang.org/x/crypto being set to github.com/golang/crypto
[INFO]	mirrors.yaml written with changes
$ glide up
[INFO]	Loading mirrors from mirrors.yaml file
[INFO]	Downloading dependencies. Please wait...
[INFO]	--> Fetching updates for github.com/orcaman/concurrent-map
[INFO]	--> Fetching golang.org/x/crypto
[INFO]	--> Fetching updates for github.com/astaxie/beego
[INFO]	--> Fetching updates for github.com/go-sql-driver/mysql
[INFO]	--> Fetching updates for github.com/garyburd/redigo
[INFO]	--> Fetching updates for github.com/coocood/freecache
[INFO]	--> Fetching updates for github.com/bitly/go-simplejson
[INFO]	--> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO]	--> Fetching updates for github.com/smartystreets/goconvey
[INFO]	--> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO]	Resolving imports
[INFO]	Downloading dependencies. Please wait...
[INFO]	Setting references for remaining imports
[INFO]	Exporting resolved dependencies...
[INFO]	--> Exporting github.com/astaxie/beego
[INFO]	--> Exporting github.com/coocood/freecache
[INFO]	--> Exporting github.com/smartystreets/goconvey
[INFO]	--> Exporting github.com/garyburd/redigo
[INFO]	--> Exporting github.com/go-sql-driver/mysql
[INFO]	--> Exporting github.com/bitly/go-simplejson
[INFO]	--> Exporting github.com/orcaman/concurrent-map
[INFO]	--> Exporting golang.org/x/crypto
[INFO]	--> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO]	Replacing existing vendor dependencies
[INFO]	Project relies on 8 dependencies.
$ ll vendor/
git.oschina.net
github.com
golang.org

Finally saw golang.org! ! ! Careful you must have found

[INFO]	mirrors.yaml written with changes

It means that the mirror configuration is written to Yes when glide mirror is executed $HOME/.glide/mirrors.yaml, open it and see.

repos:
- original: golang.org/x/crypto
  repo: github.com/golang/crypto

Can also be mapped to a local directory. It is recommended that you go to https://www.golangtc.com/download/package to download many Golang class libraries. Now I go to download: https://www.golangtc.com/static/download/packages/golang.org.x.text.tar.gz , extract it to a local directory /home/users/qiangmzsx/var/golang/golang.org/x/text.

$ glide mirror set golang.org/x/text /home/users/qiangmzsx/var/golang/golang.org/x/text
[INFO]	golang.org/x/text being set to /home/users/qiangmzsx/var/golang/golang.org/x/text
[INFO]	mirrors.yaml written with changes
$ glide up
[INFO]	Loading mirrors from mirrors.yaml file
[INFO]	Downloading dependencies. Please wait...
[INFO]	--> Fetching golang.org/x/text
[INFO]	--> Fetching updates for github.com/garyburd/redigo
[INFO]	--> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO]	--> Fetching updates for github.com/astaxie/beego
[INFO]	--> Fetching updates for github.com/bitly/go-simplejson
[INFO]	--> Fetching updates for github.com/go-sql-driver/mysql
[INFO]	--> Fetching updates for github.com/coocood/freecache
[INFO]	--> Fetching updates for github.com/orcaman/concurrent-map
[INFO]	--> Fetching updates for golang.org/x/crypto
[INFO]	--> Fetching updates for github.com/smartystreets/goconvey
[INFO]	--> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO]	Resolving imports
[INFO]	Downloading dependencies. Please wait...
[INFO]	Setting references for remaining imports
[INFO]	Exporting resolved dependencies...
[INFO]	--> Exporting github.com/astaxie/beego
[INFO]	--> Exporting github.com/go-sql-driver/mysql
[INFO]	--> Exporting github.com/bitly/go-simplejson
[INFO]	--> Exporting github.com/coocood/freecache
[INFO]	--> Exporting github.com/smartystreets/goconvey
[INFO]	--> Exporting github.com/garyburd/redigo
[INFO]	--> Exporting github.com/orcaman/concurrent-map
[INFO]	--> Exporting golang.org/x/text
[INFO]	--> Exporting golang.org/x/crypto
[INFO]	--> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO]	Replacing existing vendor dependencies
[INFO]	Project relies on 9 dependencies.

global options

Run glide, you can see at the end

GLOBAL OPTIONS:
   --yaml value, -y value  Set a YAML configuration file. (default: "glide.yaml")
   --quiet, -q             Quiet (no info or debug messages)
   --debug                 Print debug verbose informational messages
   --home value            The location of Glide files (default: "/home/users/qiangmzsx/.glide") [$GLIDE_HOME]
   --tmp value             The temp directory to use. Defaults to systems temp [$GLIDE_TMP]
   --no-color              Turn off colored output for log messages
   --help, -h              show help
   --version, -v           print the version

If you want to change the yaml file of glide to another default name, you can execute

 $ glide -y qiangmzsx.yaml

On the official website, you will see a GLIDE_HOME variable, which is just that /home/users/qiangmzsx/.glide. This directory has been mentioned before. In addition to containing mirrors.yaml, there is also a very important directory cachelocal cache. Every time the code is updated, glide will save the cache locally for the next glide install.

GLIDE_HOMEIt can be modified by the following command.

 $ glide  --home /home/glide 

Summarize

In addition to the functions mentioned above, glide also has many good functions, and I will have the opportunity to write them in the future.
To sum up, glide is a feature-rich dependency management tool that fully meets your needs and is strongly used by everyone.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325458027&siteId=291194637