Golang Package Dependency Management tool GB

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

Original link: http://tabalt.net/blog/golang-package-dependency-management-tool-gb/

A Golang project is usually composed of bin , and pkg src three subdirectories, GB on the basis of the concept of a new vendor directory to hold project-dependent third-party packages; The working directory for a GB project contains all the go codes required for the project.

GB projects are not placed in your $gopath, and you do not need to set or modify $gopath for your GB project. The third package you rely on needs to be placed vendor/src in the directory and used in gigabytes to compile and test your project.

Install GB

GB's official website is: Http://getgb.io/,github address is: https://github.com/constabulary/gb/.

Use the following command to install GB:

go get github.com/constabulary/gb/...

After installing GB, there will gb be gb-vendor two executable files $GOPATH/bin in your directory, view or edit your ~/.bash_profile files, make sure your $GOPATH/bin directory is joined $PATH in:

export PATH=$PATH:$GOPATH/bin

Use GB for project development

We use a simple "Hello World" program that provides HTTP pages to learn about GB. To reflect the features of the GB management third-party package dependency, we introduced a third-party package github.com/tabalt/gracehttp that supports graceful reboots of HTTP services.

To create a GB project directory structure:

cd ~/helloworldmkdir -p src/helloworldmkdir -p vendor/src

Write "Hello World" program

#vim src/helloworld/main.gopackage mainimport (    "fmt"    "net/http"    "github.com/tabalt/gracehttp")func main() {    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {        fmt.Fprintf(w, "hello world")    })    err := gracehttp.ListenAndServe(":8080", nil)    if err != nil {        fmt.Println(err)    }}

Adding a dependent third package

gb vendor fetch github.com/tabalt/gracehttp

So far the entire project directory structure is as follows:

./|-- src|   `-- helloworld|       `-- main.go`-- vendor    |-- manifest    `-- src        `-- github.com            `-- tabalt                `-- gracehttp                    |-- README.md                    |-- connection.go                    |-- gracehttpdemo                    |   `-- main.go                    |-- listener.go                    `-- server.go

Compiling the Execution program

gb build helloworld./bin/helloworld

Open a new terminal and execute curl http://127.0.0.1:8080/ it and the output will be:

hello world

Commit all code to git repository

git initgit add .git commit -am 'init hello world project with gb'git add remote -v $your_remote_git_repositorygit push origin master:master

GB Common Commands

In the project development above, we used two commands gb build and gb vendor , in fact, the build executable file we said before, and $GOPATH/bin/gb vendor is a GB plug-in, the final call is an executable file $GOPATH/bin/gb-vendor .

More commands can be viewed through the gb help command, the specific use of the command can be gb help $command_name viewed, many GB commands are based on the GO command-line tool packaging, usage is similar, through the gb vendor help vendor plug-in specific usage can be viewed here, we briefly listed as follows:

GB Command List

Command function
Build Compiling packages
Doc Display Document
Env Print an environment variable for a project
Generate Working with source code generation go files
Info Displaying information about a project
List Show all packages under a project
Test Perform tests

GB Vendor Feature List

Parameters function
Fetch Get a remote dependency
Update Update a local dependency
List Each row is a list of all dependent
Delete Delete a local dependency
Purge Clear all unreferenced dependencies
Restore Restoring dependencies from the manifest manifest file

Original link: http://tabalt.net/blog/golang-package-dependency-management-tool-gb/

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.