fswatchを使うと楽っぽい。
$ brew install fswatch $ fswatch . | xargs -n1 -I{} gofmt -w {}
gofmt -w だと、stdoutに出力せずに直接対象ファイルに書き込まれる。
fswatchを使うと楽っぽい。
$ brew install fswatch $ fswatch . | xargs -n1 -I{} gofmt -w {}
gofmt -w だと、stdoutに出力せずに直接対象ファイルに書き込まれる。
--reloadオプションがバージョン19.0からサポートされたので、下記のような感じで起動するとローカルの開発サーバとして使える。
$ gunicorn --access-logfile - --log-file - --reload -b 127.0.0.1:3000 -w 1 app:app
タイトルの通りだけど知らなかったので。明示的に空のsliceやmapを作らなくてもいいのは楽だ。
http://play.golang.org/p/veOWHGDdcn
package main import "fmt" func emptyMap() map[string]string { return nil } func emptySlice() []string { return nil } func main() { emptySlice := emptySlice() fmt.Printf("slice len = %d\n", len(emptySlice)) emptyMap := emptyMap() fmt.Printf("map len = %d\n", len(emptyMap)) }
これはGo的にはあまりやらない方がいいと思うやつなんだけど、こういうこともできるよ、という紹介。
type Hoge struct { Url string }
みたいに書いてgolintを実行すると "struct field Url should be URL" と怒られる。しかし、golintには -min_confidence という引数を指定できる。デフォルトは0.8なので、試しにこれを1.0に引き上げてみると"struct field Url should be URL"というエラーは出なくなる。https://github.com/golang/lint/blob/master/lint.go のソースを見てみると、いろいろなチェック処理で
f.errorf(vs, 1, link(docCommentsLink), category("comments"), "exported %s %s should have comment%s or be unexported", kind, name, block)
のようにf.errorfの第2引数にConfidenceを指定しているので、該当のエラーを消したい場合は -min_confidence でそのチェック処理のConfidenceより大きい物を指定すれば良さそう。
package main type Session struct { Url string } func (s *Session) Open() bool { return true } func main() { s := Session{"https://github.com/"} s.Open() }
ためしにこんな感じのソースをgolintにかけるとデフォルトでは
$ golint golint.go golint/golint.go:3:6: exported type Session should have comment or be unexported golint/golint.go:7:1: exported method Session.Open should have comment or be unexported golint/golint.go:4:2: struct field Url should be URL
というエラーが出るけど、-min_confidence=1.1にすると全部出なくなる。あんまりやらない方がいいことだとは思うけど、エラー出まくるからという理由でgolintをかけなくなるよりかは -min_confidence を指定してgolintする方がいいのではないかなぁと思った次第です。
The Way to Go: A Thorough Introduction to the Go Programming Language
今使っているRevel Frameworkのテストについてまとめてみた。
サンプル https://github.com/oinume/revel-sample
テストの実行(ブラウザー経由)
revel run revel-sample
http://localhost:9000/@tests にアクセス
テストの実行(CUI)
revel test revel-sample
type AppTest struct { revel.TestSuite }
revel.TestSuiteを埋め込んで使うと、コイツが持っている各種関数が使えるようになる。
type AppTest struct { revel.TestSuite } func (t *AppTest) Before() { //println("Set up") } // Test function must start with 'Test' func (t AppTest) TestThatIndexPageWorks() { t.Get("/") t.AssertOk() // Check HTTP status is 200 t.AssertContentType("text/plain; charset=utf-8") t.AssertContains("Hello world") } func (t *AppTest) After() { //println("Tear down") }
TestSuite(HTTP Client) --> Revel app (Server)
revel test を実行すると裏でサーバーが立ち上がって、TestSuiteがクライアントとしてHTTPリクエストを投げてる。
厳密には...
となっている。
$ revel test revel-sample AppTest.TestThatIndexPageWorks
引数に <TestSuite名>.<Test関数名> という形式で渡せばそのテスト関数だけ実行される
test-results/app.log にログファイルあるよ!