gom とは
Go のパッケージを gem のように管理するツールという認識です。
github.com
試した環境
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.11.6
BuildVersion: 15G1421
$ go version
go version go1.8.3 darwin/amd64
gom の導入
go get github.com/mattn/gom
以上です。簡単ですな。
gom を使ったパッケージの導入
Gomfile という名前のファイルに以下のように導入したいパッケージ名を記載します。
gom 'github.com/gin-gonic/gin'
あとは gom install
を実行するだけ。
$ gom install
downloading github.com/gin-gonic/gin
...
とっても簡単です。
ハマった
ところが、何も考えずに以下のように記載して、gom run
したところ…
$ gom run main.go
main.go:3:8: cannot find package "github.com/gin-gonic/gin" in any of:
/usr/local/Cellar/go/1.8.3/libexec/src/github.com/gin-gonic/gin (from $GOROOT)
/Users/xxxxxxx/sandboxies/go-gin/vendor/src/github.com/gin-gonic/gin (from $GOPATH)
/Users/xxxxxxx/sandboxies/go-gin/src/github.com/gin-gonic/gin
/Users/xxxxxxx/golang/src/github.com/gin-gonic/gin
gom: exit status 1
あれー、なんでやろー。
vendroing
Golang の vendroing という機能を利用すると、プロジェクトディレクトリ以下に vendor
というディレクトリがあれば、import 分で読み込むパッケージの依存解決順序を vendor
→ $GOROOT
→ $GOPATH
という風にしてくれるものらしい。
h3poteto.hatenablog.com
但し、$GOPATH
配下にプロジェクトディレクトリが存在していないといけないという制約があるとのこと。
ということで、プロジェクトディレクトリ(gingingin
)を以下のように配置してやり直し。
$GOPATH/src/gingingin
gingingin
ディレクトリ以下に Gomfile
を設置して、改めて gom install
する。
$ pwd
$GOPATH/src/gingingin
$ cat Gomfile
gom 'github.com/gin-gonic/gin'
$ gom install
...
$ tree -L 2
.
├── Gomfile
├── main.go
└── vendor
├── github.com
├── golang.org
├── gopkg.in
└── pkg
5 directories, 2 files
Gin を起動
github.com
WAF の Gin を試してみたかったので、以下のように Gin の README を参考にしてアプリケーションを作成してみた。
$ cat example.go
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()
}
以下のように gom run
してみる。
$ gom run example.go
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
おお、起動した。
以上
メモでした。
以下、gom のヘルプ。色々と機能があるので、ボチボチ使っていきたいと思います。
$ gom -h
Usage of gom:
Tasks:
gom build [options] : Build with _vendor packages
gom install [options] : Install bundled packages into _vendor directory, by default.
GOM_VENDOR_NAME=. gom install [options], for regular src folder.
gom test [options] : Run tests with bundles
gom run [options] : Run go file with bundles
gom doc [options] : Run godoc for bundles
gom exec [arguments] : Execute command with bundle environment
gom tool [options] : Run go tool with bundles
gom env [arguments] : Run go env
gom fmt [arguments] : Run go fmt
gom list [arguments] : Run go list
gom vet [arguments] : Run go vet
gom update : Update all dependencies (Experiment)
gom gen travis-yml : Generate .travis.yml which uses "gom test"
gom gen gomfile : Scan packages from current directory as root
recursively, and generate Gomfile
gom lock : Generate Gomfile.lock