oinume journal

Scratchpad of what I learned

Recent open source activities

pmd

https://github.com/pmd/pmd/pull/44

CPD (Copy Paste Detector) supported GoLang.

go-xorm

https://github.com/go-xorm/xorm/issues/166

Suggested and implemented "soft delete" for xorm. My pull-request was immediately merged! (very impressive)

goose

https://bitbucket.org/liamstask/goose/pull-request/43/support-mysql-driver

Added mysql driver support. My pull-request hasn't been merged yet. I contacted to the author but he didn't respond.

gunicornをローカル開発環境のWebサーバとして使う

--reloadオプションがバージョン19.0からサポートされたので、下記のような感じで起動するとローカルの開発サーバとして使える。

$ gunicorn --access-logfile - --log-file - --reload -b 127.0.0.1:3000 -w 1 app:app
  • --access-logfile: "-"を指定するとstderrにアクセスログが出る
  • --log-file: ↑と同様に"-"を指定するとstderrにエラーログが出る
  • --reload: ソースコードが変更されたらリロードする
  • -b: bindするアドレスとポートの指定
  • -w: ワーカー数

Python文法詳解

Python文法詳解

Goでnilなsliceやmapを返すと空のsliceやmapになる

タイトルの通りだけど知らなかったので。明示的に空の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言語

基礎からわかる Go言語

golintの-min_confidenceでチェックするレベルを変更する

これは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

The Way to Go: A Thorough Introduction to the Go Programming Language