Wednesday, June 26, 2024
HomeGolangHandle Dependencies With GODEP

Handle Dependencies With GODEP


Introduction

If you’re utilizing third get together packages, (packages that you just don’t personal or management), you will have a approach to create a reproducible construct each time you construct your initiatives. Should you use third get together packages straight and the bundle authors change issues, your initiatives may break. Even when issues don’t break, code adjustments may create inconsistent conduct and bugs.

Keith Rarick’s software godep is a superb step in the appropriate course for managing third get together dependencies and creating reproducible builds. The godep software provides you two choices for managing dependencies. The primary possibility creates a dependency file with model management data after which with some godep magic, the code is constructed in opposition to these variations. You too can Vendor your third get together packages inside your initiatives as nicely. You by no means want to vary a single supply code file and the whole lot is completed at the side of the go tooling.


Downloading Godep

Obtain godep utilizing go get and ensure your $GOPATH/bin listing is in your PATH.

go get github.com/kr/godep
export PATH=$PATH:$GOPATH/bin


Create A Mission

Construct your mission utilizing the third get together packages as you usually would. Since godep doesn’t require you to vary any import paths within the code, ‘go get’ the code you want and import these packages straight. To maintain the put up easy, I’m going to make use of an current program referred to as Information Search that makes use of one third get together dependency.

export GOPATH=$HOME/instance
go get github.com/goinggo/newssearch

After ‘go get’ completes, I’ve the next code on disk contained in the GOPATH.

The Information Search program is utilizing code from a special Going Go repository, which for this mission is a third get together bundle. Since ‘go get’ was profitable, the code constructed and is put in.

Dependency Administration

After you have a mission that may construct and set up correctly, you should use godep to create the Godeps dependency file. Change to the basis location for the mission and run the godep save command with the -copy=false possibility:

cd $GOPATH/src/github.com/goinggo/newssearch
godep save -copy=false

As soon as the save is full, godep creates a file referred to as Godeps. You’ll save this file along with your mission:

{
    “ImportPath”: “github.com/goinggo/newssearch”,
    “GoVersion”: “go1.1.2”,
    “Deps”: [
        {
            “ImportPath”: “github.com/goinggo/utilities/workpool”,
            “Rev”: “7e6141d61b2a16ae83988907308f8e09f703a0d0”
        }
    ]
}

The Godeps file incorporates the whole lot godep must create a reproducible construct. The Godep file lists every third get together bundle and the git commit quantity for that model of code to make use of.

Screen Shot

Now take away the third get together bundle from its unique location and carry out a construct utilizing the godep software:

godep go construct

Screen Shot

Should you take away the unique third get together code, you may’t use ‘go construct’ straight anymore, the imports don’t exist. To construct the mission use ‘go construct’ from the godep software.

You too can use ‘go set up’ and ‘go check’ so long as you run these instructions by means of godep.

godep go construct
godep go set up
godep go check

To make this work, godep performs a little bit of magic. It makes use of a working listing and manipulates the GOPATH beneath.

Run the godep path command from contained in the mission folder:

cd $GOPATH/src/github.com/goinggo/newssearch
godep path

It is best to see the next output:

/var/folders/8q/d2pfdk_x4qd4__l6gypvzsw40000gn/T/godep/rev/7e/6141d61b2a16ae83988907308f8e09f703a0d0

Should you open that folder you will notice the code for that model. This code is getting used to construct the mission:

Screen Shot

The godep software will proceed to make use of the code from this location to carry out future builds if it exists. Calling godep go construct will obtain the model of code specified within the Godeps file if it doesn’t exist already.

Should you open any of your supply code information you will notice the imports haven’t modified. The way in which godep works, it doesn’t have to vary a factor. This is among the actually superior issues about godep.

Updating Dependencies

When it’s time to replace certainly one of your third get together libraries simply ‘go get’ it. Then you definitely simply must run godep save as soon as once more to replace the Godeps file. As a result of the imports paths within the supply code information are usually not modified, godep will discover and replace the dependencies.

I’ve modified the third get together bundle and pushed it as much as GitHub:

Screen Shot

Now I ‘go get’ the code adjustments and replace the Godeps file:

go get github.com/goinggo/utilities
cd $GOPATH/src/github.com/goinggo/newssearch
godep save

If I open the Godeps file the dependencies have modified:

{
    “ImportPath”: “github.com/goinggo/newssearch”,
    “GoVersion”: “go1.1.2”,
    “Deps”: [
        {
            “ImportPath”: “github.com/goinggo/utilities/workpool”,
            “Rev”: “8ecd01ec035e29915aa6897a3385ee4f8d80cc05”
        }
    ]
}

Now I take advantage of godep to construct the code:

godep go construct

Screen Shot

The godep software downloaded the brand new model and constructed the code efficiently.

Vendoring

Vendoring is the act of constructing your personal copy of the third get together packages your mission is utilizing. These copies are historically positioned inside every mission after which saved within the mission repository. The godep software helps Vendoring and can place the copies contained in the mission which might be utilizing them.

To Vendor code with godep, don’t use any choices with the save command. First clear the workspace and obtain a brand new model of the Information Search program:

export GOPATH=$HOME/instance
go get github.com/goinggo/newssearch
cd $GOPATH/src/github.com/goinggo/newssearch

Now difficulty the godep save command once more however this time with out the copy possibility:

godep save

Screen Shot

This time you should have a Godeps folder with a particular workspace subfolder and the Godeps file. All of the third get together packages are copied into the workspace folder underneath src. That is setup to work with GOPATH.

Model management information are eliminated and no import paths are modified in any of the supply code information.

Subsequent take away the unique code for the third get together bundle and carry out the construct:

godep go construct

The construct is profitable and the whole lot is able to be pushed again into the repository.

Performing an replace is so simple as downloading the brand new model of the third get together bundle and operating godep save once more.

Conclusion

The godep software solves lots of the issues that exist with creating reproducible builds. It’s extremely simple to make use of and sits on high of the go tooling. It doesn’t change something about go, the way you write go applications or the way you import third get together packages. The one downside is that godep doesn’t help Bazaar utilizing the non-vendored possibility.

For the general public packages your are publishing, you may embrace a Godeps file to supply your “steady construct” configuration. Package deal customers can select to make use of it or not. That’s actually cool. Construct the code with go straight or by means of godep.

In the long run, godep is a software that:

1. Helps a Vendor and Non-Vendor answer that gives a reproducible construct
2. Maintains backwards appropriate with all current Go packages
3. Offers a approach to publish and entry the “steady construct” configuration of a product
4. Straightforward to replace bundle dependencies when new bundle variations can be found



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments