LoginSignup
4
4

More than 5 years have passed since last update.

Golangの依存関係管理ツールメモ(dep)

Posted at

参考:
公式:https://github.com/golang/dep/blob/master/README.md

depインストール

$ go get -u github.com/golang/dep/cmd/dep

下記のフォルダに入っています。

$GOPATH/bin/dep
$GOPATH/github.com/golang/dep

depコマンド

helpでコマンド確認

$ dep help
Dep is a tool for managing dependencies for Go projects
Usage: "dep [command]"
Commands:
  init     Set up a new Go project, or migrate an existing one
  status   Report the status of the project's dependencies
  ensure   Ensure a dependency is safely vendored in the project
  version  Show the dep version information
  check    Check if imports, Gopkg.toml, and Gopkg.lock are in sync
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

パッケージ管理の初期化コマンド。Gopkg.lockとGopkg.tomlとvendorディレクトリを作成します。

・Gopkg.toml
 ソース内でimportしているパッケージのリストです。

・Gopkg.lock
 パッケージのバージョンを指定。直接的なパッケージとそのパッケージが依存しているパッケージ全てを指定している。

・vendorディレクトリ
 プロジェクトで使用するパッケージがインストールされます

.gitignoreにはvendorを除外しましょう。パッケージは都度インストールすれば良いのでgit管理する必要はありません。

$ vi .gitignore
vendor/*

パッケージインストール

$ dep ensure

パッケージを追加した場合は、dep ensureコマンドを実行。
ソースを解析して追加のパッケージがある場合は更新してくれます。
最新版があってもvendorディレクトリにあるパッケージへの更新は行わない。

$ dep ensure -update

使用している全てのパッケージを最新版に更新してくれます。このタイミングで追加のパッケージがある場合は更新してくれます。

$ dep ensure -vendor-only

Gopkg.toml、Gopkg.lockに設定してあるバージョン、パッケージのみを対象にする。
新規のパッケージがあっても追加でインストールはしない。

状態確認

$ dep status
PROJECT                              CONSTRAINT     VERSION        REVISION  LATEST   PKGS USED
github.com/gin-contrib/sse           *                             22d885f            1
github.com/gin-gonic/gin             ^1.3.0         v1.3.0         b869fe1   v1.3.0   4
github.com/golang/protobuf           *                             5a0f697            1
github.com/json-iterator/go          1.0.0          1.0.0          36b1496   1.0.0    1
github.com/mattn/go-isatty           *                             57fdcb9            1
github.com/ugorji/go                 *                             c88ee25            1
golang.org/x/sys                     branch master  branch master  a2f829d   a2f829d  1
gopkg.in/go-playground/validator.v8  v8.18.1        v8.18.1        5f57d22   v8.18.1  1
gopkg.in/yaml.v2                     *                             a5b47d3            1
dep status  1.22s user 2.42s system 162% cpu 2.240 total

上記はginのチュートリアルをしているプロジェクトの状態

アプリ実行

$ go run main.go

vendor配下のパッケージを利用してアプリ実行ができる。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4