Package Management for Go language

Source: Internet
Author: User
Tags git clone

Before touching the go language to write some Nodejs app, so for NPM package management method still feel very convenient, but transferred to the go language platform, because the official did not launch their own package management tools, so can only rely on some third-party tools to complete, This article focuses on some of the currently used go package dependency management tools to assist in the development of the program. gpm

The GPM tool is very simple to use to complete the Go dependent library download, simply to create your own godeps file and write the dependent package to the file so that we can execute the download task directly from the command line and download the dependency to the current Gopath path.
A typical godeps file, we can not flag any library dependent version information, so that the default download the latest dependent library, you can also download the specified version through the tag, just need to write a version or Git tag on the back of the dependent library, as follows:

$ cat Godeps
  # This is a comment
  github.com/nu7hatch/gotrail         v0.0.2
  github.com/replicon/ Fast-archiver   v1.02   #This is another comment!
  Github.com/nu7hatch/gotrail         2eb79d1f03ab24bacbc32b15b75769880629a865

The specific way to install the GPM, you can refer to the official gpm simple tutorial, Mac can be directly downloaded through brew, below is the Linux operating system under the installation method.

$ git clone https://github.com/pote/gpm.git && cd gpm
$ git checkout v1.4.0 # can ignore this part if you Want to install HEAD.
$./configure
$ make Install

Here is a simple example of using the GPM, we wrote a simple Go program, and included a source code main.go inside relies on an external go library, the operation flow is as follows:

  Export Gopath=/home/mike/go-project
  cd/home/mike/go-project
  mkdir src bin pkg

Main.go Source Code

  Package main

  Import (
    "Github.com/aokoli/goutils"
  )

  func main () {
    r,_:=goutils. Randomascii ()
    println (r)
  }

Create a new godeps file under SRC that contains the version of the dependent external library, with the following text content:

Github.com/nu7hatch/gotrail

After adding to the direct save exit, the file we can push into the git library, when we git download the git source code, the SRC path to execute the gpm get to download all the dependencies. GVP

If we use Gopath to point to our project directory, by default go get or the gpm get above will download all dependencies in the SRC directory, so that we write our own code and dependent code together, how to separate dependencies and source code, we can use the GVP tool, This tool will create a new directory locally and help us to add this directory to the Gopath path. So that our program can do the source and dependency separation.

GVP tool installation, this tool is installed in the same manner as the gpm basically:

$ git clone https://github.com/pote/gvp.git && CD GVP
$ git checkout v0.2.1 # can ignore this part if you Want to install HEAD.
$./configure
$ make Install

After completion, we can execute the following command to the root of the project

$ source GVP

We'll see one more. godeps file under the project, and here we go to check our own Gopath variables to change

$ echo $GOPATH
/home/mike/go-project/.godeps:/home/mike/go-project

Next, if we download the new dependency package by using the GPM, then all the dependency code will go into the src file under Godeps, and we can still write the source code under the SRC of the project itself, and the two tools can help us manage the package dependencies conveniently. Yes, we can exit the current project directly through GVP out.

This article is placed in the personal blog, welcome everyone to visit

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.