# Go パフォーマンスチェッカー ## 文字列 - [ ] 文字列結合で `+`演算子を使わない(`bytes.Buffer`か`strings.Join()`を使う) - [ ] 正規表現をなるべく使わず、`strings`パッケージを使う ## IO - [ ] `fmt.Print`系を使わず、バッファリングする ```go // 良い例 b := bufio.NewWriter(os.Stdout) defer b.Flush() for i:=0;i<10;i++{ fmt.Fprintln(b,strings.Repeat("a",100)) } ``` ## 並行処理 - [ ] 重い処理を直列で処理している部分はないか - [ ] チャネルを必ず`close`する - [ ] Mutexのロック処理は必要なところだけ - [ ] `context.WithCancel`や`context.WithTimeout`を使うときは必ず`defer cancel()`を書く ## http関連 - [ ] タイムアウトを設定していい場合は設定する - [ ] HTTPリクエストを送る時`defer resp.Body.Close()` - [ ] 静的ファイルをアプリケーションから配信せず、Nginxなどに移譲 ## DB - [ ] N+1はダメよダメダメ - [ ] 何度も`sql.Open`を呼び出さない - Goの`sql.DB`構造体は0以上のコネクションプールを持っていて、複数のGoroutineからのアクセスにも対応している - そのため、何回も`sql.Open`を使うとコネクションリークを起こしてしまう - [ ] `sql.SetMaxIdleConns`と`SetMaxOpenConns()`でコネクション数を変更する - [ ] このときMySQL側のmax connectionも確認する - ref : [https://qiita.com/methane/items/ccd3fd856b02b06c9452](https://qiita.com/methane/items/ccd3fd856b02b06c9452) ## reflect - [ ] そもそも使わない
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.