Golang包依赖管理工具gb - 服务端码农的随笔 - SegmentFault



Golang包依赖管理工具gb - 服务端码农的随笔 - SegmentFault

一个Golang项目工程通常由binpkgsrc三个子目录构成,gb在这个概念的基础上新增了一个vendor目录来存放项目依赖的第三方包;一个gb项目的工作目录里包含该项目需要的所有Go代码。

gb项目不放在你的$GOPATH中,也不需要为你的gb项目设置或修改$GOPATH。依赖的第三包需要放到vendor/src目录中,并使用gb来编译和测试你的项目。

安装gb

gb的官网是:http://getgb.io/,github地址是:https://github.com/constabulary/gb/

使用如下命令即可安装gb:

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

安装gb后,会有gbgb-vendor两个可执行文件放入你的$GOPATH/bin目录中,查看或编辑你的~/.bash_profile文件,确保你的$GOPATH/bin目录已经加入$PATH中:

export PATH=$PATH:$GOPATH/bin  

使用gb进行项目开发

我们以一个简单的提供HTTP页面的"Hello World"程序来学习一下gb的使用。为了体现gb管理第三方包依赖的特性,我们引入一个支持HTTP服务优雅重启的第三方包 github.com/tabalt/gracehttp

创建gb项目目录结构:

cd ~/helloworld  mkdir -p src/helloworld  mkdir -p vendor/src  

编写"Hello World"程序

#vim src/helloworld/main.go  package main    import (      "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)      }  }  

添加依赖的第三包

gb vendor fetch github.com/tabalt/gracehttp  

目前为止整个项目目录结构如下:

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

编译执行程序

gb build helloworld  ./bin/helloworld  

打开一个新终端并执行curl http://127.0.0.1:8080/,将会输出:

hello world    

提交所有代码到git仓库

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

gb常用命令

在上面的项目开发中,我们用到了两个命令gb buildgb vendor,实际上,build是我们之前所说的可执行文件$GOPATH/bin/gb包含的,而vendor是gb的一个插件,最终调用的是可执行文件$GOPATH/bin/gb-vendor

可以通过gb help命令查看gb支持的更多命令,命令的具体用法可以通过gb help $command_name查看,很多gb命令都是在go命令行工具的基础上做的包装,用法也都相似,通过gb vendor help可以查看vendor插件具体用法,这里我们简单列举如下:


Read full article from Golang包依赖管理工具gb - 服务端码农的随笔 - SegmentFault


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts