こんにちは。
現在、とあるお客様のお仕事でGoでWebアプリを作らせていただいています。
新しいパソコンを購入したので、開発環境を作るところからもう一度やるので、せっかくだから残しておこうと思います
環境
HomebrewでGoのインストール
$ brew -v
Homebrew 1.5.0
$ brew install go
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
augustus glances hlint hmmer jdupes libjwt mmseqs2 mpir skafos vis
==> Updated Formulae
〜中略〜
==> Downloading https://homebrew.bintray.com/bottles/go-1.9.2.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring go-1.9.2.high_sierra.bottle.tar.gz
==> Caveats
A valid GOPATH is required to use the `go get` command.
If $GOPATH is not specified, $HOME/go will be used by default:
https://golang.org/doc/code.html#GOPATH
You may wish to add the GOROOT-based install location to your PATH:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
==> Summary
🍺 /usr/local/Cellar/go/1.9.2: 7,646 files, 293.9MB
$ go version
go version go1.9.2 darwin/amd64
よさそう。
と、思ったけど。
ここまでやって、バージョン管理もしたいなって思い始めたので、goenv使います。
brew uninstall go
Uninstalling /usr/local/Cellar/go/1.9.2... (7,646 files, 293.9MB)
消す。
goenvインストール
gvmというバージョン管理ツールもあるようですが、goenvの方が簡単そうだったのとhomebrewでインストールできるのでこっちにしました。
$ brew install goenv
==> Downloading https://github.com/syndbg/goenv/archive/1.7.0.tar.gz
==> Downloading from https://codeload.github.com/syndbg/goenv/tar.gz/1.7.0
######################################################################## 100.0%
🍺 /usr/local/Cellar/goenv/1.7.0: 131 files, 234.5KB, built in 3 seconds
.bash_profileに以下を追記
export PATH="$HOME/.goenv/bin:$PATH"
eval "$(goenv init -)"
$ source ~/.bash_profile
.bash_profileの再読み込み。
$ goenv
goenv 1.0.0
Usage: goenv <command> [<args>]
Some useful goenv commands are:
commands List all available commands of goenv
local Set or show the local application-specific Go version
global Set or show the global Go version
shell Set or show the shell-specific Go version
install Install a Go version using go-build
uninstall Uninstall a specific Go version
rehash Rehash goenv shims (run this after installing executables)
version Show the current Go version and its origin
versions List all Go versions available to goenv
which Display the full path to an executable
whence List all Go versions that contain the given executable
See `goenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/syndbg/goenv#readme
はいった。
goenvのコマンド一覧。
Goのインストールと設定
$ goenv install -l
Available versions:
1.2.2
1.3.0
1.3.1
1.3.2
1.3.3
1.4.0
1.4.1
1.4.2
1.4.3
1.5.0
1.5.1
1.5.2
1.5.3
1.5.4
1.6.0
1.6.1
1.6.2
1.6.3
1.6.4
1.7.0
1.7.1
1.7.3
1.7.4
1.7.5
1.8.0
1.8.1
1.8.3
1.8.4
1.8.5
1.9.0
1.9.1
インストール可能なバージョンの一覧が取得できる。
今動いているアプリは1.8だけどこの機会にバージョンを上げたいので一番新しいのにします。
$ goenv install 1.9.2
〜略〜
$ goenv versions
1.9.2
$ cd projectdir
$ goenv local 1.9.2
$ go version
go version go1.9.2 darwin/amd64
gitで落としてきたディレクトリに移動して、使いたいバージョンを指定します。
今回はこのプロジェクトでのみ1.9.2を適用させたいのでlocalを使いました。
パッケージ管理
さて、すぐにでもgo run ~
したいところですが、パッケージの管理もやってなかったのでこの機会にパッケージ管理もしたいと思います。
パッケージの管理はdepを使います。
github.com
$ brew install dep
$ dep init
root project import: /Users/XXXXXX/Documents/go/projectdir is not within any GOPATH/src
何も考えずにdep init
したら怒られました。
GOPATHを設定
.bash_profileに以下を追加。
export GOPATH="/Users/XXXXXX/Documents/go"
GOPATH/src/projectdir
のように、プロジェクトディレクトリを配置する。
projectdir
のroot
でdep init
する。
Gopkg.lock
とGopkg.toml
が作られる。
$ cat Gopkg.toml
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
[[constraint]]
name = "github.com/cihub/seelog"
version = "2.6.0"
[[constraint]]
branch = "master"
name = "github.com/gin-gonic/contrib"
[[constraint]]
name = "github.com/gin-gonic/gin"
version = "1.2.0"
[[constraint]]
name = "github.com/go-sql-driver/mysql"
version = "1.3.0"
[[constraint]]
name = "gopkg.in/gomail.v2"
version = "2.0.0"
使っているライブラリを自動で吐き出してくれる。
自動で作られたvendor
というディレクトルにライブラリをインストールしてくれる。
とりあえず、すでにimportしてたやつはvendorにいれてくれたみたいです。
これで動かせる!
と、思ったらgomailのバージョンが変わってるのかundefined: gomail.NewDialer
って言われてしまったので、ソースを直さなきゃですね。
では、環境構築はこの辺で。
2018/11/22追記
undefined: gomail.NewDialer
が出てビルドできなかった件ですが、解決策が見つかりました。
Using dep with a project depending on "gomail" results in unbuildable package · Issue #777 · golang/dep · GitHub
吐き出したGopkg.toml
には
[[constraint]]
name = "gopkg.in/gomail.v2"
version = "2.0.0"
とあるのですが、これだとうまくいかないらしいのでbranchを指定します。
[[constraint]]
name = "gopkg.in/gomail.v2"
branch = "v2"
これに書き換えて、dep ensure
したら無事にビルド通りました。