Now you can Subscribe using RSS

Submit your Email

2017/10/19

Golang dep 使用

asd
目前 golang 上可以使用的套件管理工具之一 dep Github
使用方法如下
$go get -u -v github.com/golang/dep/cmd/dep
先把 dep 用go get 抓下來
抓下來後使用以下指令確認一下
$ dep version
dep:
 version     : devel
 build date  : 
 git hash    : 
 go version  : go1.9.1
 go compiler : gc
 platform    : linux/amd64

如果找不到指令的話,可能是環境變數沒有設置
需要設置 GOPATH/bin 進去 PATH 環境變數裡面
使用 help 查看一下有哪些指令可以使用
$ dep help
dep is a tool for managing dependencies for Go projects

Usage: dep 

Commands:

  init     Initialize a new project with manifest and lock files
  status   Report the status of the project's dependencies
  ensure   Ensure a dependency is safely vendored in the project
  prune    Prune the vendor tree of unused packages
  version  Show the dep version information

Examples:
  dep init                               set up a new project
  dep ensure                             install the project's dependencies
  dep ensure -update                     update the locked versions of all dependencies
  dep ensure -add github.com/pkg/errors  add a dependency to the project

Use "dep help [command]" for more information about a command.
dep 可以使用的指令很少,指令也有範例可以參考與註解

  • init : 初始化專案

到你專案目錄下使用以下指令
位置如下:
| - gopath   
     | - bin
     | - pkg
     | - src
          | main.go   
$dep init
root project import: dep does not currently support using GOPATH/src as the project root
把錯誤訊息丟上網尋找一下發現這個 Dep issue #148
所以正確用法是要在GOPATH/src/src or GOPATH/src/company 下初始化 dep
那錯誤訊息就不是代表說目前不是在GOPATH/src 應該是要顯示
not current support using GOPATH/src/subdir 會比較恰當
但後續應該會支援直接在 GOPATH/src 下使用 dep init
可以看看這裡 Dep issue #417 這 issue 有解釋 Dep issue #313

接下來我們在 GOPATH/src 下創建一個資料夾在進行 dep init
執行成功後可以發現多出幾個檔案
| - gopath
     | - bin
     | - pkg
     | - src
         | - src
             | - main.go
             | - vendor
             | - Gopkg.lock
             | - Gopkg.toml

vendor : 就是放置你要引入的第三方工具
dep 會幫你把你想引入的程式碼複製一份到 vendor 裡面
如果使用 git 進行版本管理請把  vendor 也加入管理中
雖然可以利用 dep 把紀錄在 gopkd 中的相關引用重新在抓回來
但萬一你引用項目被刪除或是發生什麼意外就在也抓不回來

Gopkg.lock : dep ensure 自我確認的資訊請勿自行修改內容否則可能會造成 dep ensure 無法使用
Gopkg.toml紀錄你引用的包相關資訊與引用版本
  • status : 查看依賴狀態

初始化成功後使用以下指令觀看一下狀態
$ dep status
PROJECT  CONSTRAINT  VERSION  REVISION  LATEST  PKGS USED

  • ensure : dep 主要指令 下載套件包或是更新等等
首先來看一下 ensure 有哪些參數可以使用
$ dep  help ensure
Usage: dep ensure [-update | -add] [-no-vendor | -vendor-only] [-dry-run] [...]

Project spec:

  [:alt source URL][@]


Ensure gets a project into a complete, reproducible, and likely compilable state:

  * All non-stdlib imports are fulfilled
  * All rules in Gopkg.toml are respected
  * Gopkg.lock records precise versions for all dependencies
  * vendor/ is populated according to Gopkg.lock

Ensure has fast techniques to determine that some of these steps may be
unnecessary. If that determination is made, ensure may skip some steps. Flags
may be passed to bypass these checks; -vendor-only will allow an out-of-date
Gopkg.lock to populate vendor/, and -no-vendor will update Gopkg.lock (if
needed), but never touch vendor/.

The effect of passing project spec arguments varies slightly depending on the
combination of flags that are passed.


Examples:

  dep ensure                                 Populate vendor from existing Gopkg.toml and Gopkg.lock
  dep ensure -add github.com/pkg/foo         Introduce a named dependency at its newest version
  dep ensure -add github.com/pkg/foo@^1.0.1  Introduce a named dependency with a particular constraint

For more detailed usage examples, see dep ensure -examples.

Flags:

  -add          add new dependencies, or populate Gopkg.toml with constraints for existing dependencies (default: false)
  -dry-run      only report the changes that would be made (default: false)
  -examples     print detailed usage examples (default: false)
  -no-vendor    update Gopkg.lock (if needed), but do not update vendor/ (default: false)
  -update       update the named dependencies (or all, if none are named) in Gopkg.lock to the latest allowed by Gopkg.toml (default: false)
  -v            enable verbose logging (default: false)
  -vendor-only  populate vendor/ from Gopkg.lock without updating it first (default: false)

剛剛使用 dep status 目前是無任何引用,我們用 gin 來做一個示範
使用以下指令導入 gin framework
$ dep ensure -add -v github.com/gin-gonic/gin
Fetching sources...
(1/1) github.com/gin-gonic/gin

Root project is "src"
 1 transitively valid internal packages
 1 external packages imported from 1 projects
(0)   ✓ select (root)
(1) ? attempt github.com/gin-gonic/gin with 1 pkgs; 22 versions to try
(1)     try github.com/gin-gonic/gin@v1.2
(1) ✓ select github.com/gin-gonic/gin@v1.2 w/3 pkgs
(2) ? attempt github.com/gin-contrib/sse with 1 pkgs; 1 versions to try
(2)     try github.com/gin-contrib/sse@master
(2) ✓ select github.com/gin-contrib/sse@master w/1 pkgs
(3) ? attempt github.com/ugorji/go with 1 pkgs; 1 versions to try
(3)     try github.com/ugorji/go@master
(3) ✓ select github.com/ugorji/go@master w/1 pkgs
(4) ? attempt github.com/golang/protobuf with 1 pkgs; 1 versions to try
(4)     try github.com/golang/protobuf@master
(4) ✓ select github.com/golang/protobuf@master w/1 pkgs
(5) ? attempt github.com/mattn/go-isatty with 1 pkgs; 5 versions to try
(5)     try github.com/mattn/go-isatty@v0.0.3
(5) ✓ select github.com/mattn/go-isatty@v0.0.3 w/1 pkgs
(6) ? attempt golang.org/x/sys with 1 pkgs; 1 versions to try
(6)     try golang.org/x/sys@master
(6) ✓ select golang.org/x/sys@master w/1 pkgs
(7) ? attempt gopkg.in/go-playground/validator.v8 with 1 pkgs; 29 versions to try
(7)     try gopkg.in/go-playground/validator.v8@v8.18.2
(7) ✓ select gopkg.in/go-playground/validator.v8@v8.18.2 w/1 pkgs
(8) ? attempt gopkg.in/yaml.v2 with 1 pkgs; 1 versions to try
(8)     try gopkg.in/yaml.v2@v2
(8) ✓ select gopkg.in/yaml.v2@v2 w/1 pkgs
  ✓ found solution with 10 packages from 8 projects

Solver wall times by segment:
     b-list-versions: 10.972993793s
     b-source-exists:  5.742431058s
  b-deduce-proj-root:  479.597455ms
         b-list-pkgs:  421.100562ms
              b-gmal:    304.8501ms
         select-atom:    1.341224ms
             satisfy:    1.248243ms
            new-atom:     373.493µs
               other:      96.076µs
         select-root:      60.431µs

  TOTAL: 17.924092435s

(1/8) Wrote gopkg.in/yaml.v2@v2
(2/8) Wrote gopkg.in/go-playground/validator.v8@v8.18.2
(3/8) Wrote github.com/gin-contrib/sse@master
(4/8) Wrote github.com/mattn/go-isatty@v0.0.3
(5/8) Wrote github.com/gin-gonic/gin@v1.2
(6/8) Wrote github.com/ugorji/go@master
(7/8) Wrote github.com/golang/protobuf@master
(8/8) Wrote golang.org/x/sys@master
"github.com/gin-gonic/gin" is not imported by your project, and has been temporarily added to Gopkg.lock and vendor/.
If you run "dep ensure" again before actually importing it, it will disappear from Gopkg.lock and vendor/.ㄈ

-v 是顯示 log 紀錄
-add url  是把你想要引用的 github 項目加入專案中

log 最後有兩段有一個提示說明 gin 不是我們專案中的項目但它已被加入在 Gopkg.lock 還有 vendor 中
假如我們未使用 gin ,再次執行 dep ensure 就會把剛剛抓下來的 gin 刪除
測試一下是不是如它所說的
$ dep ensure -v
Root project is "src"
 1 transitively valid internal packages
 0 external packages imported from 0 projects
(0)   ✓ select (root)
  ✓ found solution with 0 packages from 0 projects

Solver wall times by segment:
  select-root: 63.762µs
        other:  3.662µs

  TOTAL: 67.424µs

可以發現 vendor 與 Gopkg.lock 裡面有關於 gin 都被清空
再把 gin 重新加入依賴,然後做一個簡單的 gin 使用程式碼如下
package main

import "github.com/gin-gonic/gin"

func main() {
 r := gin.Default()
 r.GET("/ping", func(c *gin.Context) {
  c.JSON(200, gin.H{
   "message": "pong",
  })
 })
 r.Run() // listen and serve on 0.0.0.0:8080
}
使用 gin 相關 func 後在執行 dep ensure 指令
 dep ensure -v
Root project is "src"
 1 transitively valid internal packages
 1 external packages imported from 1 projects
(0)   ✓ select (root)
(1) ? attempt github.com/gin-gonic/gin with 1 pkgs; at least 1 versions to try
(1)     try github.com/gin-gonic/gin@v1.2
(1) ✓ select github.com/gin-gonic/gin@v1.2 w/3 pkgs
(2) ? attempt github.com/gin-contrib/sse with 1 pkgs; at least 1 versions to try
(2)     try github.com/gin-contrib/sse@master
(2) ✓ select github.com/gin-contrib/sse@master w/1 pkgs
(3) ? attempt github.com/golang/protobuf with 1 pkgs; at least 1 versions to try
(3)     try github.com/golang/protobuf@master
(3) ✓ select github.com/golang/protobuf@master w/1 pkgs
(4) ? attempt github.com/mattn/go-isatty with 1 pkgs; at least 1 versions to try
(4)     try github.com/mattn/go-isatty@v0.0.3
(4) ✓ select github.com/mattn/go-isatty@v0.0.3 w/1 pkgs
(5) ? attempt github.com/ugorji/go with 1 pkgs; at least 1 versions to try
(5)     try github.com/ugorji/go@master
(5) ✓ select github.com/ugorji/go@master w/1 pkgs
(6) ? attempt golang.org/x/sys with 1 pkgs; at least 1 versions to try
(6)     try golang.org/x/sys@master
(6) ✓ select golang.org/x/sys@master w/1 pkgs
(7) ? attempt gopkg.in/yaml.v2 with 1 pkgs; at least 1 versions to try
(7)     try gopkg.in/yaml.v2@v2
(7) ✓ select gopkg.in/yaml.v2@v2 w/1 pkgs
(8) ? attempt gopkg.in/go-playground/validator.v8 with 1 pkgs; at least 1 versions to try
(8)     try gopkg.in/go-playground/validator.v8@v8.18.2
(8) ✓ select gopkg.in/go-playground/validator.v8@v8.18.2 w/1 pkgs
  ✓ found solution with 10 packages from 8 projects

Solver wall times by segment:
     b-source-exists: 10.677526126s
         b-list-pkgs:  549.251499ms
              b-gmal:  474.074971ms
  b-deduce-proj-root:  340.979105ms
         select-atom:    1.235231ms
             satisfy:    1.225447ms
         select-root:     724.866µs
            new-atom:     536.754µs
               other:      23.229µs

  TOTAL: 12.045577228s

(1/8) Wrote github.com/mattn/go-isatty@v0.0.3
(2/8) Wrote github.com/gin-contrib/sse@master
(3/8) Wrote github.com/gin-gonic/gin@v1.2
(4/8) Wrote gopkg.in/go-playground/validator.v8@v8.18.2
(5/8) Wrote github.com/golang/protobuf@master
(6/8) Wrote github.com/ugorji/go@master
(7/8) Wrote gopkg.in/yaml.v2@v2
(8/8) Wrote golang.org/x/sys@master

很明顯的引用第三方東西之後在執行 ensure 做確認是否要把不引用項目清除
由於第二次我們寫一個簡單範例後在執行 ensure 就保留 gin 相關的套件也在 Gopkg.lock 中保留紀錄

假如要更改引用的版本號
可以修改 Gopkg.toml 檔案進行修改
先使用 status 查看目前依賴狀態
$ dep status
PROJECT                              CONSTRAINT  VERSION        REVISION  LATEST   PKGS USED
github.com/gin-contrib/sse           *           branch master  22d885f   22d885f  1  
github.com/gin-gonic/gin             ^1.2.0      v1.2           d459835   d459835  3  
github.com/golang/protobuf           *           branch master  130e6b0   130e6b0  1  
github.com/mattn/go-isatty           *           v0.0.3         0360b2a   0360b2a  1  
github.com/ugorji/go                 *           branch master  c3953de   c3953de  1  
golang.org/x/sys                     *           branch master  8dbc5d0   8dbc5d0  1  
gopkg.in/go-playground/validator.v8  *           v8.18.2        5f1438d   5f1438d  1  
gopkg.in/yaml.v2                     *           branch v2      eb3733d   eb3733d  1  

開始進行版本修改
#原本
[[constraint]]
  name = "github.com/gin-gonic/gin"
  version = "1.2.0"

#修改過後
[[constraint]]
  name = "github.com/gin-gonic/gin"
  version = "<1.2.0"

修改完 Gopkg.toml 無法執行 dep status 指令
它會告你以下訊息
$ dep status
Lock inputs-digest mismatch. This happens when Gopkg.toml is modified.
Run `dep ensure` to regenerate the inputs-digest.

執行以下指令進行修改
$ dep ensure  -update -v
Root project is "src"
 1 transitively valid internal packages
 1 external packages imported from 1 projects
(0)   ✓ select (root)
(1) ? attempt github.com/gin-gonic/gin with 1 pkgs; 22 versions to try
(1)     try github.com/gin-gonic/gin@v1.2
(1) ✓ select github.com/gin-gonic/gin@v1.2 w/3 pkgs
(2) ? attempt github.com/gin-contrib/sse with 1 pkgs; 1 versions to try
(2)     try github.com/gin-contrib/sse@master
(2) ✓ select github.com/gin-contrib/sse@master w/1 pkgs
(3) ? attempt github.com/golang/protobuf with 1 pkgs; 1 versions to try
(3)     try github.com/golang/protobuf@master
(3) ✓ select github.com/golang/protobuf@master w/1 pkgs
(4) ? attempt github.com/mattn/go-isatty with 1 pkgs; 5 versions to try
(4)     try github.com/mattn/go-isatty@v0.0.3
(4) ✓ select github.com/mattn/go-isatty@v0.0.3 w/1 pkgs
(5) ? attempt github.com/ugorji/go with 1 pkgs; 1 versions to try
(5)     try github.com/ugorji/go@master
(5) ✓ select github.com/ugorji/go@master w/1 pkgs
(6) ? attempt golang.org/x/sys with 1 pkgs; 1 versions to try
(6)     try golang.org/x/sys@master
(6) ✓ select golang.org/x/sys@master w/1 pkgs
(7) ? attempt gopkg.in/yaml.v2 with 1 pkgs; 1 versions to try
(7)     try gopkg.in/yaml.v2@v2
(7) ✓ select gopkg.in/yaml.v2@v2 w/1 pkgs
(8) ? attempt gopkg.in/go-playground/validator.v8 with 1 pkgs; 29 versions to try
(8)     try gopkg.in/go-playground/validator.v8@v8.18.2
(8) ✓ select gopkg.in/go-playground/validator.v8@v8.18.2 w/1 pkgs
  ✓ found solution with 10 packages from 8 projects

Solver wall times by segment:
     b-source-exists: 6.504068048s
              b-gmal: 297.736106ms
         b-list-pkgs:  90.970779ms
         select-atom:   1.915448ms
         select-root:   1.480164ms
             satisfy:   1.158372ms
            new-atom:     338.88µs
     b-list-versions:     89.164µs
               other:     26.344µs
  b-deduce-proj-root:     16.032µs

  TOTAL: 6.897799337s

這裡使用 update 參數,不管有沒有添加 updata 結果都會是一樣的
然後看一下目前依賴狀態
$ dep status
PROJECT                              CONSTRAINT  VERSION        REVISION  LATEST   PKGS USED
github.com/gin-contrib/sse           *           branch master  22d885f   22d885f  1  
github.com/gin-gonic/gin             <1.2.0      v1.1.4         d459835   d459835  3  
github.com/golang/protobuf           *           branch master  130e6b0   130e6b0  1  
github.com/mattn/go-isatty           *           v0.0.3         0360b2a   0360b2a  1  
github.com/ugorji/go                 *           branch master  c3953de   c3953de  1  
golang.org/x/sys                     *           branch master  8dbc5d0   8dbc5d0  1  
gopkg.in/go-playground/validator.v8  *           v8.18.2        5f1438d   5f1438d  1  
gopkg.in/yaml.v2                     *           branch v2      eb3733d   eb3733d  1  

看到現在的 version 從 1.2 變成 1.1.4
那我如果直接修改為指定 1.1.4 版本你會發現它還是維持在 1.2 版本
這是因為操作符的關係,以下為 github 上解釋的操作符用途
=: equal
!=: not equal
>: greater than
<: less than
>=: greater than or equal to
<=: less than or equal to
-: literal range. Eg: 1.2 - 1.4.5 is equivalent to >= 1.2, <= 1.4.5
~: minor range. Eg: ~1.2.3 is equivalent to >= 1.2.3, < 1.3.0
^: major range. Eg: ^1.2.3 is equivalent to >= 1.2.3, < 2.0.0
[xX*]: wildcard. Eg: 1.2.x is equivalent to >= 1.2.0, < 1.3.0
You might, for example, include a constraint in your manifest that specifies version = "=2.0.0" to pin a dependency to version 2.0.0, or constrain to minor releases with: version = "2.*". Refer to the semver library documentation for more info.

Note: When you specify a version without an operator, dep automatically uses the ^ operator by default. dep ensure will interpret the given version as the min-boundry of a range, for example:

1.2.3 becomes the range >=1.2.3, <2.0.0
0.2.3 becomes the range >=0.2.3, <0.3.0
0.0.3 becomes the range >=0.0.3, <0.1.0

很明顯的看得出來沒有指定操作符預設是用 ^ 所以當我們指定 1.1.4
系統會已 ^ 操作符為指令進行尋找 >=1.1.4 < 1.2 的版本
假如要指定特定版本就要使用 = 來指定該版本

  • prune : 移除沒有引用到的相關套件

剛剛已經使用了 gin 相關 func 接下來我們把相關程式碼註解起來
然後使用以下指令
dep prune -v
Gopkg.lock is out of sync; run dep ensure before pruning.

然後在執行 dep ensure
再去確認 Gopkg.lock 與 vendor 資料夾可以發現 gin 相關引用已經被清除
以上是 dep 一些操作
對於 ensure 指令還有一些未使用
可以使用 dep ensure -examples 來觀看文檔說明會比較清楚

0 意見:

張貼留言

Coprights @ 2016, Blogger Templates Designed By Templateism | Templatelib