LoginSignup
3
0

More than 5 years have passed since last update.

遅ればせながらdep卒業してgomoduleデビューする

Last updated at Posted at 2018-12-14

概要

go1.11から使えるようになった gomodule を使ってみる。
GO111MODULE=on にするだけで良いとか嘘だと思ってるので検証してみました。

検証環境

環境 項目 内容
ホスト OS mac
ホスト go version go1.11.2 darwin/amd64
ゲスト OS ubuntu
ゲスト go version go1.11.3 linux/amd64

ホストは自分のMacです。
ゲストは Dockerでubuntuを入れて、その中にまっさらなgo環境を入れました。

すでにホストには様々なpackageが入っていますので、ホスト側で書いたコードをゲスト側で実行する際の gomodule の動作を確かめていきます。

使用するコード

ホスト側でプログラムを実行する

準備 :dancers:

ホスト側には使用する外部パッケージが既にある状態の方が説明しやすいので、僕が日頃よくお世話になっている pp をみなさん入れてください。

$ go get github.com/k0kubun/pp
$ git clone https://github.com/bookun/module-test
$ cd module-test
$ cat main.go
main.go
package main

import "github.com/k0kubun/pp"

type person struct {
    name string
    age  int
}

func main() {
    p := person{name: "bookun", age: 5}
    pp.Println(p)
}

実行 :basketball_player:

$ go run main.go

# 出力
main.person{
  name: "bookun",
  age:  5,
}

色付きでみやすいですよね

ゲスト側でプログラムを実行する

準備 :dancers:

$ cd module-test
$ make build
これで ゲスト側のプロンプト(#)が返ってくるはず
# git clone https://github.com/bookun/module-test
# cd module-test

実行 :basketball_player_tone5:

普通に実行すると pp がないよみたいなエラーが出るはずです

# go run main.go

出力
main.go:3:8: cannot find package "github.com/k0kubun/pp" in any of:
        /usr/local/go/src/github.com/k0kubun/pp (from $GOROOT)
        /root/go/src/github.com/k0kubun/pp (from $GOPATH)

予想通りです

  • 環境変数 GO111MODULE を on にして実行してみる
$ GO111MODULE=on go run main.go
go: creating new go.mod: module github.com/bookun/module-test
go: finding github.com/k0kubun/pp v2.3.0+incompatible
go: downloading github.com/k0kubun/pp v2.3.0+incompatible
go: finding github.com/mattn/go-colorable v0.0.9
go: downloading github.com/mattn/go-colorable v0.0.9
go: finding github.com/mattn/go-isatty v0.0.4
go: downloading github.com/mattn/go-isatty v0.0.4
main.person{
  name: "bookun",
  age:  5,
}

すげえええええ!!!

終わった後のディレクトリ内をみてみましょう

$ ls
Dockerfile  Makefile  go.mod  go.sum  main.go

go.modgo.sum が増えています。

見ときましょう

go.mod
module github.com/bookun/module-test

require (
        github.com/k0kubun/pp v2.3.0+incompatible // indirect
        github.com/mattn/go-colorable v0.0.9 // indirect
        github.com/mattn/go-isatty v0.0.4 // indirect
)

ppとそれに依存するパッケージの情報が記載されているようです。

依存するパッケージを追加してみる

どちらでも構いませんがゲスト側で行います。
echo を足してみます。

# export GO111MODULE=on
# go get github.com/labstack/echo

go: finding github.com/labstack/echo v3.3.5+incompatible
go: downloading github.com/labstack/echo v3.3.5+incompatible
go: finding github.com/labstack/gommon/color latest
go: finding github.com/labstack/gommon/log latest
go: finding golang.org/x/crypto/acme/autocert latest
go: finding github.com/labstack/gommon v0.2.8
go: downloading github.com/labstack/gommon v0.2.8
go: finding golang.org/x/crypto/acme latest
go: finding golang.org/x/crypto latest
go: downloading golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9
go: finding github.com/valyala/fasttemplate latest
go: downloading github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4
go: finding github.com/valyala/bytebufferpool v1.0.0
go: downloading github.com/valyala/bytebufferpool v1.0.0

すげええええええええええ!!!

再度 go.mod をみてみます

go.mod

module github.com/bookun/module-test

require (
        github.com/k0kubun/pp v2.3.0+incompatible // indirect
        github.com/labstack/echo v3.3.5+incompatible // indirect
        github.com/labstack/gommon v0.2.8 // indirect
        github.com/mattn/go-colorable v0.0.9 // indirect
        github.com/mattn/go-isatty v0.0.4 // indirect
        github.com/valyala/bytebufferpool v1.0.0 // indirect
        github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 // indirect
        golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 // indirect
)

ちゃんと依存するパッケージが追加されています

まとめ

go1.11以上で GO111MODULE=on にすると

  • 初回実行時に足りないパッケージがあると追加してくれる
  • 開発中に使いたいパッケージをgo getするだけで依存パッケージ一覧を更新してくれる

後片付け

コンテナを削除してイメージを消しましょう

$ make rm
$ make rmi
3
0
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
3
0