Using vendor to manage Golang project dependencies

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In addition to the extensive use of Python in our projects, we also use Golang to build efficient basic operational services. In using the Golang process, we found that the lack of dependent library version of the Golang program is a very big problem: some dependencies after a commit after the API changes, if not to modify the code is difficult to be compatible, but the developer is very likely because of the time of participation, resulting in the execution go getcommand gets a different version, resulting in a compile-through problem on different computers. Also, in multiple programs, if you use a different version of commit, you may cause different problems during program compilation.

Before we solved this problem there are two scenarios, one is to disassemble the go get execution of the command, first create the corresponding dependent directory, use the git command to switch to the specified commit, and then execute the go install command. Another easy way to do this is to use the GODEP tool, which does not introduce too much, you can refer to the document or search the Chinese course.

After Golang1.5, go provides an GO15VENDOREXPERIMENT environment variable that adjusts the application path search for Go build to be the 当前项目目录/vendor directory mode. In this form, we can implement godep project-dependent management similar to the way it is. But at least there is no need to deploy a tool on the other side during the process of compiling the program godep .

Before you use it, you need to install an auxiliary tool (if Golang changes from one to the next): go get -u -v github.com/kardianos/govendor .

Below, we use an example to illustrate. First, there is a vendorproj project named. If there is only one file:

 Package MainImport (    "Github.com/yeeuu/echoic")func Main() {    e := Echoic.New()    e.Setdebug(true)    e.Run("127.0.0.1:4321")}

Execute the command to generate the Vendor folder:

$ govendor init$ lsmain.go    vendor$ cd vendor/$ lsvendor.json

This vendor.json will resemble the functionality of the godep description file version in the tool. Next, you need to execute a command to include the file that the current application must have.

$ govendor add +external

If you need to update or remove, refer to the tool's specific documentation update or remove commands. This will move the required compilation files into the vendor directory (note: The dependencies required for testing are not included, and the test files that depend on the project are not included).

$ Lsgithub.com golang.org Vendor.json$ Cat Vendor.json{    "comment":"","Ignore":"Test","Package":[        {            "Path":"Github.com/yeeuu/echoic","revision":"a7d6994f92e2dc60cff071ae38b204fbd4bd2a3f","Revisiontime":"2015-12-18t11:14:29+08:00"        },{            "Path":"Golang.org/x/net/context","revision":"1d9fd3b8333e891c0e7353e1adcfe8a612573033","Revisiontime":"2015-11-13t15:40:13-08:00"        }    ]}$ CDGithub.com/yeeuu/echoic$ Lslicense context.go group.go router.goREADME.md echoic.go response.go

GO15VENDOREXPERIMENT=1Use the Vendor folder to build files by setting environment variables. You can choose export GO15VENDOREXPERIMENT=1 or simply GO15VENDOREXPERIMENT=1 go build perform the compilation.

In this way, you can ensure that a program can implement a pattern similar to virtualenv in Python, and that different programs use different versions to rely on.

golang vendor
  • like
  • Tweet
  • +1
"!--/.social-share-->

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.