Skip to content

Instantly share code, notes, and snippets.

@sumyapp
Last active March 5, 2019 08:39
Show Gist options
  • Save sumyapp/d9101e27812ef0d271025d5d696ef2e8 to your computer and use it in GitHub Desktop.
Save sumyapp/d9101e27812ef0d271025d5d696ef2e8 to your computer and use it in GitHub Desktop.
Go sample
rules:
- id: com.example.akiko.1
pattern:
regexp: (func) (foo|bar)\([^\(]*\)
message: |
fooやbarといった関数名は使わないでください。
意味のある関数名を定義しましょう。
glob:
- "**/*.go"
fail:
- func foo(), func bar()
pass:
- func say_hello()
- id: com.example.akiko.2
pattern:
regexp: var \p{Upper}+
message: |
変数はパッケージ外からアクセスしていいかどうかよく考えましょう。
変数の名称は短い方が好まれます。グローバル変数は意味のわかる名前にしておきましょう。
https://github.com/golang/go/wiki/CodeReviewComments#variable-names
glob:
- "**/*.go"
- id: com.example.akiko.3
pattern:
regexp: var \p{Lower}+
message: |
ローカル変数やスコープの短い変数は、短い文字列になるように工夫しましょう。
変数の名称は短い方が好まれます。ループの中でのみ使うような場合は、1,2文字をお勧めします
https://github.com/golang/go/wiki/CodeReviewComments#variable-names
glob:
- "**/*.go"
justification:
- 明確な意味をもたせたい場合は適度な長さでも構いません
- id: com.example.akiko.4
pattern:
regexp: errors\.New\(fmt\.Sprintf\("\p{Upper}+
message: |
エラーメッセージの先頭は小文字にしておきましょう。得てして他のコンテキストと合わさって出力されるからです。
Ref: https://github.com/golang/go/wiki/CodeReviewComments
glob:
- "**/*.go"
fail:
- errors.New(fmt.Errorf("Something bad")
pass:
- errors.New(fmt.Errorf("something bad")
- id: com.example.akiko.5
pattern:
regexp: fmt\.Errorf\("\p{Upper}+
message: |
エラーメッセージの先頭は小文字にしておきましょう。得てして他のコンテキストと合わさって出力されるからです。
Ref: https://github.com/golang/go/wiki/CodeReviewComments
glob:
- "**/*.go"
fail:
- fmt.Errorf("Something bad")
pass:
- fmt.Errorf("something bad")
- id: com.example.akiko.6
pattern:
regexp: ^[\s*//]\spanic\([^\(]*\)
message: |
できるだけパニック (panic()) の利用は避けましょう。
基本的にはerrorと複数の戻り値を使ってエラーハンドリングを行なって下さい。
どうしてもRuntimeエラーとして処理を中断させる必要がある場合での利用をして下さい。
Ref: https://golang.org/doc/effective_go.html#panic
glob:
- "**/*.go"
justification:
- 例えば初期化の際に、必須の値が取得できないようなケースで、panicを利用して中断するといった場合が該当します。
- id: com.example.akiko.7
pattern:
regexp: instance_class\s*=\s*"db\..+\..+"
message: |
データベースのスケール変更の際は適用のタイミングに注意してください。
計画停止が難しい、ダウンタイムを減らしたいといった環境の場合は、Multi-AZ構成、最初から大きめのものを検討して
下さい。
ボリュームの拡張は比較的実施しやすいですが、縮小の場合はメンテナンスが必要になります。サイジングも注意してください。
glob:
- "terraform/rds.tf"
- id: com.example.akiko.8
pattern:
token: apply_immediately =
message: |
データベースのスケール変更の際は適用のタイミングに注意してください。
このパラメータを未指定、またはfalseにすると、更新が次回メンテナンスウインドウまでキューされます。
なお、インスタンスタイプ変更時にはデータベースが一瞬落ちるので、少し注意が必要になります。
https://www.terraform.io/docs/providers/aws/r/db_instance.html#apply_immediately
glob:
- "terraform/rds.tf"
- id: com.example.akiko.9
pattern: "math/rand"
message: |
Go でランダムデータ生成を生成する場合は、rand.Seed を設定しましょう。
また、暗号化用の鍵や、何とかトークンのような、長さ分のエントロピーを持つ乱雑な文字列を作りたい場合は、math/rand
ではなく、crypto/rand を使って下さい。
https://github.com/golang/go/wiki/CodeReviewComments#crypto-rand
glob:
- "**/*.go"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment