LoginSignup
14
9

More than 5 years have passed since last update.

dep initとdep ensureまでの道のり

Last updated at Posted at 2019-02-24

goの勉強の記録です。
dep ensureまでが結構めんどいので備忘録として、、

dep init

適当なディレクトリでdep initをするとエラー

init failed: unable to detect the containing GOPATH: /Users/ac69/practice/go-scraping is not within a known GOPATH/src

GOPATHを設定するため、direnvを使う。
direnvを使うとディレクトリごとにGO_PATHを切り替えることができるようになる。参考

direnv edit .

.envrcに以下を記入

export GOPATH=$(pwd)

こういうエラーがでたら

direnv: error .envrc is blocked. Run `direnv allow` to approve its content.

これを打つ。これでGO_PATHが現在のpwdを指すようになる。

direnv allow

再度dep initをするとまだエラーが出る

init failed: unable to determine the import path for the root project /Users/ac69/practice/go-scraping: /Users/ac69/practice/go-scraping is not within any GOPATH/src

GOPATH/srcのパスが特定できないって言っているのでsrcディレクトリを作って、そこに移動してdep initを再度やってみる

mkdir src
cd src
dep init

まだエラーが出る

init failed: unable to determine the import path for the root project /Users/ac69/practice/go-scraping/src: dep does not currently support using GOPATH/src as the project root

depはGOPATH/src配下にさらにプロジェクトのフォルダをつくらないとだめらしい
なので、適当なフォルダを作成し、そこでdep initする

mkdir go-scraping
cd go-scraping
dep init

今度はちゃんとdep initができた。

ただ、GO_PATH/src配下にプロジェクトのルートのディレクトリを用意したことでこんな感じのディレクトリ構成になってちょっと気持ち悪い。depとdirenvを使う場合こういう感じになるのはしょうがないのだろうか、、、

go-scraping // 名前がかぶる(ここがGO_PATH)
├── pkg
│   └── dep
│       └── sources
└── src
    └── go-scraping // 名前がかぶる
        ├── Gopkg.lock
        ├── Gopkg.toml
        └── vendor

dep ensure

次はパッケージ(goquery)のインストール。

dep ensureでパッケージをインストールするとエラー。dep ensureの場合、Gopkg.toml Gopkg.lock以外にソースコードの依存関係も解決するらしく、goのコードを先に書いておかないとだめだった。

$ dep ensure -add github.com/PuerkitoBio/goquery
no dirs contained any Go code

ちなみにdep ensureは-vendor-onlyのオプションをつけるとソースコードの依存関係はみずに、Gopkg.lockの依存のみを解決するらしい。

dep ensure -vendor-only

goqueryを利用するgoのコードを書いて、再度dep ensureを実行するとエラーなくインストールできた。

まだインストールしていないpkgを利用するコードを書いてからインストールを実行する、というのがなんだかしっくりこないがこういうものなのだろうか、、

dep ensure -add

goのfirebaseSDKをソースコード上ではまだ利用していないけど、パッケージのインストールだけ先にやっとこうと思って、

$ dep ensure -add firebase.google.com/go
Fetching sources...

"firebase.google.com/go" 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/.
$ 

実行してみたら上記のような文章が出てきた。

まだコード上は利用してないようだから、一時的にGopkg.lockとvendorに追加しといたよ。再度dep ensureしたら消すからね。

というような感じ?

コード上でfirebase.google.com/goをimportして、再度dep ensureしたら上記の文言は出なくなった。

14
9
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
14
9