diff --git a/site/index.html b/site/index.html index 9e75cfd9..80e8d7cd 100644 --- a/site/index.html +++ b/site/index.html @@ -3069,7 +3069,7 @@
To check if a slice doesn’t contain any element, check its length. This check works regardless of whether the slice is nil
or empty. The same goes for maps. To design unambiguous APIs, you shouldn’t distinguish between nil and empty slices.
To determine whether a slice has elements, we can either do it by checking if the slice is nil or if its length is equal to 0. Checking the length is the best option to follow as it will cover both if the slice is empty or is the slice is nil.
+To determine whether a slice has elements, we can either do it by checking if the slice is nil or if its length is equal to 0. Checking the length is the best option to follow as it will cover both if the slice is empty or if the slice is nil.
Meanwhile, when designing interfaces, we should avoid distinguishing nil and empty slices, which leads to subtle programming errors. When returning slices, it should make neither a semantic nor a technical difference if we return a nil or empty slice. Both should mean the same thing for the callers. This principle is the same with maps. To check if a map is empty, check its length, not whether it’s nil.
We can prevent a data race from happening using different techniques. For example:
sync/atomic
package変数のシャドーイングを避けることは、誤った変数の参照や読者の混乱を防ぎます。
+変数のシャドーイングを避けることは、誤った変数の参照や読み手の混乱を防ぎます。
変数のシャドーイングは、変数名がブロック内で再宣言されることで生じますが、これは間違いを引き起こしやすくします。変数のシャドーイングを禁止するかどうかは個人の好みによります。たとえば、エラーに対して err
のような既存の変数名を再利用すると便利な場合があります。とはいえ、コードはコンパイルされたものの、値を受け取った変数が予期したものではないというシナリオに直面する可能性があるため、原則として引き続き注意を払う必要があります。
変数を初期化するときは、init関数のエラー処理が制限されており、状態の処理とテストがより複雑になることに注意してください。ほとんどの場合、初期化は特定の関数として処理されるべきです。
-init関数は、アプリケーションの状態を初期化するために使用される関数です。引数を取らず、結果も返しません ( func()
関数)。 パッケージが初期化されると、パッケージ内のすべての定数および変数の宣言が評価されます。次に、init関数が実行されます。
init関数はいくつかの問題を引き起こす可能性があります。
+init関数は、アプリケーションの状態を初期化するために使用される関数です。引数を取らず、結果も返しません( func()
関数)。パッケージが初期化されると、パッケージ内のすべての定数および変数の宣言が評価されます。次に、init関数が実行されます。
init関数はいくつかの問題を引き起こす可能性があります。
init関数には注意が必要です。ただし、静的構成の定義など、状況によっては役立つ場合があります。それ以外のほとんどの場合、初期化処理はアドホック関数を通じて行われるべきです。
+init関数には注意が必要です。ただし、静的構成の定義など、状況によっては役立つ場合があります。それ以外のほとんどの場合、初期化処理はそのためだけに存在する関数を通じて行われるべきです。
Go言語では、慣用的にゲッターとセッターの使用を強制することはありません。 実利を重視し、効率性と特定の慣習に従うこととの間の適切なバランスを見つけることが、進むべき道であるはずです。
+Go言語では、慣用的にゲッターとセッターの使用を強制することはありません。実利を重視し、効率性と特定の慣習に従うこととの間の適切なバランスを見つけることが、進むべき道であるはずです。
データのカプセル化とは、オブジェクトの値または状態を隠すことを指します。ゲッターとセッターは、エクスポートされていないオブジェクトフィールドの上にエクスポートされたメソッドを提供することでカプセル化を可能にする手段です。
-Go言語では、一部の言語で見られるようなゲッターとセッターの自動サポートはありません。また、ゲッターとセッターを使用して構造体フィールドにアクセスすることは必須でも慣用的でもありません。値をもたらさない構造体のゲッターとセッターでコードを埋めるべきではありません。実利を重視し、他のプログラミングパラダイムで時には議論の余地がないと考えられている慣習に従うことと、効率性との間の適切なバランスを見つけるよう努めるべきです。
+データのカプセル化とは、オブジェクトの値または状態を隠すことを指します。ゲッターとセッターは、エクスポートされていないオブジェクトフィールドの上にエクスポートされたメソッドを提供することでカプセル化を可能にする手段です。
+Go言語では、一部の言語で見られるようなゲッターとセッターの自動サポートはありません。また、ゲッターとセッターを使用して構造体フィールドにアクセスすることは必須でも慣用的でもありません。値をもたらさない構造体のゲッターとセッターでコードを埋めるべきではありません。実利を重視し、他のプログラミングパラダイムで時には議論の余地がないと考えられている慣習に従うことと、効率性との間の適切なバランスを見つけるよう努めるべきです。
Go言語は、シンプルさを含む多くの特性を考慮して設計された独自の言語であることを忘れないでください。ただし、ゲッターとセッターの必要性が見つかった場合、または前述のように、前方互換性を保証しながら将来の必要性が予測される場合は、それらを使用することに問題はありません。
-抽象化は作成されるべきものではなく、発見されるべきものです。 不必要な複雑さを避けるために、インターフェイスは、必要になると予測したときではなく、必要になったときに作成するか、少なくとも抽象化が有効であることを証明できる場合に作成してください。
+抽象化は作成されるべきものではなく、発見されるべきものです。不必要な複雑さを避けるために、インタフェースは、必要になると予測したときではなく、必要になったときに作成するか、少なくとも抽象化が有効であることを証明できる場合に作成してください。
インターフェイスは、オブジェクトの動作を指定する方法を提供します。複数のオブジェクトが実装できる共通項を抽出するために、インターフェイスは使用されます。Go言語のインターフェイスが大きく異なるのは、暗黙的に満たされることです。オブジェクト X
がインターフェイス Y
を実装していることを示す implements
のような明示的なキーワードはありません。
一般に、インターフェイスが価値をもたらすと考えられる主要な使用例は3つあります。それは、共通の動作を除外する、何らかの分離を作成する、および型を特定の動作に制限するというものです。ただし、このリストはすべてを網羅しているわけではなく、直面する状況によっても異なります。
-多くの場合、インターフェイスは抽象化するために作成されます。そして、プログラミングで抽象化するときの主な注意点は、抽象化は作成されるべきではなく、発見されるべきであるということを覚えておくことです。すなわち、そうする直接の理由がない限り、コード内で抽象化すべきではないということです。 インターフェイスを使って設計するのではなく、具体的なニーズを待つべきです。別の言い方をすれば、インターフェイスは必要になると予測したときではなく、必要になったときに作成する必要があります。 -インターフェイスの過度な使用をした場合の主な問題は何でしょうか。答えは、コードフローがより複雑になることです。役に立たない間接参照を追加しても何の価値もありません。それは価値のない抽象化をすることで、コードを読み、理解し、推論することをさらに困難にします。インターフェイスを追加する明確な理由がなく、インターフェイスによってコードがどのように改善されるかが不明瞭な場合は、そのインターフェイスの目的に異議を唱える必要があります。実装を直接呼び出すのも一つの手です。
-コード内で抽象化するときは注意が必要です (抽象化は作成するのではなく、発見する必要があります)。後で必要になる可能性があるものを考慮し、完璧な抽象化レベルを推測して、私たちソフトウェア開発者はコードをオーバーエンジニアリングすることがよくあります。ほとんどの場合、コードが不必要な抽象化で汚染され、読みにくくなるため、このプロセスは避けるべきです。
+インタフェースは、オブジェクトの動作を指定する方法を提供します。複数のオブジェクトが実装できる共通項を抽出するために、インタフェースは使用されます。Go言語のインタフェースが大きく異なるのは、暗黙的に満たされることです。オブジェクト X
がインタフェース Y
を実装していることを示す implements
のような明示的なキーワードはありません。
一般に、インタフェースが価値をもたらすと考えられる主要な使用例は3つあります。それは、共通の動作を除外する、何らかの分離を作成する、および型を特定の動作に制限するというものです。ただし、このリストはすべてを網羅しているわけではなく、直面する状況によっても異なります。
+多くの場合、インタフェースは抽象化するために作成されます。そして、プログラミングで抽象化するときの主な注意点は、抽象化は作成されるべきではなく、発見されるべきであるということを覚えておくことです。すなわち、そうする直接の理由がない限り、コード内で抽象化すべきではないということです。インタフェースを使って設計するのではなく、具体的なニーズを待つべきです。別の言い方をすれば、インタフェースは必要になると予測したときではなく、必要になったときに作成する必要があります。 +インタフェースの過度な使用をした場合の主な問題は何でしょうか。答えは、コードフローがより複雑になることです。役に立たない間接参照を追加しても何の価値もありません。それは価値のない抽象化をすることで、コードを読み、理解し、推論することをさらに困難にします。インタフェースを追加する明確な理由がなく、インタフェースによってコードがどのように改善されるかが不明瞭な場合は、そのインタフェースの目的に異議を唱える必要があります。実装を直接呼び出すのも一つの手です。
+コード内で抽象化するときは注意が必要です(抽象化は作成するのではなく、発見する必要があります)。後で必要になる可能性があるものを考慮し、完璧な抽象化レベルを推測して、私たちソフトウェア開発者はコードをオーバーエンジニアリングすることがよくあります。ほとんどの場合、コードが不必要な抽象化で汚染され、読みにくくなるため、このプロセスは避けるべきです。
ロブ・パイク
-インターフェイスでデザインするな。インターフェイスを見つけ出せ。
+インタフェースでデザインするな。インタフェースを見つけ出せ。
抽象的に問題を解決しようとするのではなく、今解決すべきことを解決しましょう。最後に重要なことですが、インターフェイスによってコードがどのように改善されるかが不明瞭な場合は、コードを簡素化するためにインターフェイスを削除することを検討する必要があるでしょう。
+抽象的に問題を解決しようとするのではなく、今解決すべきことを解決しましょう。最後に重要なことですが、インタフェースによってコードがどのように改善されるかが不明瞭な場合は、コードを簡素化するためにインタフェースを削除することを検討する必要があるでしょう。
-インターフェイスをクライアント側で保持することで不必要な抽象化を回避できます。
+インタフェースをクライアント側で保持することで不必要な抽象化を回避できます。
Go言語ではインターフェイスが暗黙的に満たされます。これは、明示的な実装を持つ言語と比較して大きな変化をもたらす傾向があります。ほとんどの場合、従うべきアプローチは前のセクションで説明したもの――抽象は作成するのではなく、発見する必要がある――に似ています。これは、すべてのクライアントに対して特定の抽象化を強制するのはプロデューサーの役割ではないことを意味します。代わりに、何らかの形式の抽象化が必要かどうかを判断し、そのニーズに最適な抽象化レベルを決定するのはクライアントの責任です。
-ほとんどの場合、インターフェイスはコンシューマー側に存在する必要があります。ただし、特定の状況 (たとえば、抽象化がコンシューマーにとって役立つことがわかっている――予測はしていない――場合) では、それをプロデューサー側で使用したい場合があります。そうした場合、可能な限り最小限に抑え、再利用可能性を高め、より簡単に構成できるように努めるべきです。
+Go言語ではインタフェースが暗黙的に満たされます。これは、明示的な実装を持つ言語と比較して大きな変化をもたらす傾向があります。ほとんどの場合、従うべきアプローチは前のセクションで説明したもの――抽象は作成するのではなく、発見する必要がある――に似ています。これは、すべてのクライアントに対して特定の抽象化を強制するのは生産者の役割ではないことを意味します。代わりに、何らかの形式の抽象化が必要かどうかを判断し、そのニーズに最適な抽象化レベルを決定するのはクライアントの責任です。
+ほとんどの場合、インタフェースは消費者側に存在する必要があります。ただし、特定の状況(たとえば、抽象化が消費者にとって役立つことがわかっている――予測はしていない――場合)では、それを生産者側で使用したい場合があります。そうした場合、可能な限り最小限に抑え、再利用可能性を高め、より簡単に構成できるように努めるべきです。
-柔軟性に問題がないようにするために、関数はほとんどの場合、インタフェースではなく具体的な実装を返す必要があります。逆に、関数は可能な限りインタフェースを受け入れる必要があります。
+ほとんどの場合、インタフェースではなく具体的な実装を返す必要があります。そうでないとパッケージの依存関係により設計がいっそう複雑になり、すべてのクライアントが同じ抽象化に依存する必要があるため、柔軟性に欠ける可能性があります。結論は前のセクションと似ています。抽象化がクライアントにとって役立つことが(予測されるではなく)わかっている場合は、インタフェースを返すことを検討してもよいでしょう。それ以外の場合は、抽象化を強制すべきではありません。それらはクライアントによって発見される必要があります。何らかの理由でクライアントが実装を抽象化する必要がある場合でも、クライアント側でそれを行うことができます。
+any
は何も言わない (#8)json.Marshal
など考えうるすべての型を受け入れるか返す必要がある場合にのみ any
を使用してください。それ以外の場合、any
は意味のある情報を提供せず、呼び出し元が任意のデータ型のメソッドを呼び出すことを許可するため、コンパイル時に問題が発生する可能性があります。
any
型は、考えうるすべての型を受け入れるか返す必要がある場合(たとえば、マーシャリングやフォーマットの場合)に役立ちます。原則としてコードを過度に一般化することは何としても避けるべきです。コードの表現力などの他の側面が向上する場合は、コードを少し重複させたほうが良いこともあります。
ジェネリックスと型パラメーターを利用することで、要素や動作を除外するためのボイラープレートコードを避けることができます。ただし、型パラメータは時期尚早に使用せず、具体的な必要性がわかった場合にのみ使用してください。そうでなければ、不必要な抽象化と複雑さが生じます。
+セクションの全文はこちら。
+ +型埋め込みを使用すると、ボイラープレートコードを回避することもできます。ただし、そうすることで、一部のフィールドを非表示にしておく必要がある場合に問題が発生しないようにしてください。
+構造体を作成するとき、Go言語は型を埋め込むオプションを提供します。ただし、型埋め込みの意味をすべて理解していないと、予期しない動作が発生する可能性があります。このセクションでは、型を埋め込む方法、それがもたらすもの、および考えられる問題について見ていきます。
+Go言語では、名前なしで宣言された構造体フィールドは、埋め込みと呼ばれます。たとえば、次のようなものです。
+ +Foo
構造体では、Bar
型が関連付けられた名前なしで宣言されています。したがって、これは埋め込みフィールドです。
埋め込みを使用することで、埋め込み型のフィールドとメソッドは昇格します。Bar には Baz フィールドが含まれているため、このフィールドは Foo
に昇格します。したがって、Foo から Baz を利用できるようになります。
型の埋め込みについて何が言えるでしょうか。まず、これが必要になることはほとんどなく、ユースケースが何であれ、おそらく型埋め込みなしでも同様に解決できることを意味します。型の埋め込みは主に利便性を目的として使用されます。ほとんどの場合、それは動作を昇格するために使用されます。
+型埋め込みを使用する場合は、次の 2 つの主な制約を念頭に置く必要があります。
+Foo.Bar.Baz()
の代わりに Foo.Baz()
など)。 これが唯一の根拠である場合は、内部型を埋め込まず、代わりにフィールドを使いましょう。これらの制約を念頭に置いて型埋め込みを意識的に使用すると、追加の転送メソッドによるボイラープレートコードを回避するのに役立ちます。ただし、見た目だけを目的としたり、隠すべき要素を昇格したりしないように注意しましょう。
+ +API に適した方法でオプションを便利に処理するには、Functional Options パターンを使用しましょう。
+さまざまな実装方法が存在し、多少の違いはありますが、主な考え方は次のとおりです。
+type Option func(options *options)
エラーを返す関数です。たとえば、WithPort
はポートを表す int
引数を受け取り、options
構造体の更新方法を表す Option
型を返します。type options struct {
+ port *int
+}
+
+type Option func(options *options) error
+
+func WithPort(port int) Option {
+ return func(options *options) error {
+ if port < 0 {
+ return errors.New("port should be positive")
+ }
+ options.port = &port
+ return nil
+ }
+}
+
+func NewServer(addr string, opts ...Option) ( *http.Server, error) { <1>
+ var options options <2>
+ for _, opt := range opts { <3>
+ err := opt(&options) <4>
+ if err != nil {
+ return nil, err
+ }
+ }
+
+// この段階で、オプション構造体が構築され、構成が含まれます。
+// したがって、ポート設定に関連するロジックを実装できます。
+ var port int
+ if options.port == nil {
+ port = defaultHTTPPort
+ } else {
+ if *options.port == 0 {
+ port = randomPort()
+ } else {
+ port = *options.port
+ }
+ }
+
+ // ...
+}
+
Functional Options パターンは、オプションを処理するための手軽で API フレンドリーな方法を提供します。 Builder パターンは有効なオプションですが、いくつかの小さな欠点(空の可能性がある構成構造体を渡さなければならない、またはエラーを処理する方法があまり便利ではない)があり、この種の問題において Functional Options パターンがGo言語における慣用的な対処方法になる傾向があります。
+ +全体的な構成に関しては、さまざまな考え方があります。たとえば、アプリケーションをコンテキストごとに整理すべきか、それともレイヤーごとに整理すべきか、それは好みによって異なります。コンテキスト(顧客コンテキスト、契約コンテキストなど)ごとにコードをグループ化することを選ぶ場合もあれば、六角形のアーキテクチャ原則に従うことと、技術層ごとにグループ化することを選ぶ場合もあります。私たちが行う決定が一貫している限り、それがユースケースに適合するなら、それが間違っていることはありません。
+パッケージに関しては、従うべきベストプラクティスが複数あります。まず、プロジェクトが過度に複雑になる可能性があるため、時期尚早なパッケージ化は避けるべきです。場合によっては、完璧な構造を最初から無理に作ろうとするよりも、単純な構成を使用し、その内容を理解した上でプロジェクトを発展させるほうが良い場合があります。 +粒度も考慮すべき重要な点です。 1 つまたは 2 つのファイルだけを含む数十のナノパッケージを作成することは避けるべきです。その場合、おそらくこれらのパッケージ間の論理的な接続の一部が抜け落ち、読み手にとってプロジェクトが理解しにくくなるからです。逆に、パッケージ名の意味を薄めるような巨大なパッケージも避けるべきです。
+パッケージの名前付けも注意して行う必要があります。(開発者なら)誰もが知っているように、名前を付けるのは難しいです。クライアントが Go プロジェクトを理解しやすいように、パッケージに含まれるものではなく、提供するものに基づいてパッケージに名前を付ける必要があります。また、ネーミングには意味のあるものを付ける必要があります。したがって、パッケージ名は短く、簡潔で、表現力豊かで、慣例により単一の小文字にする必要があります。
+何をエクスポートするかについてのルールは非常に簡単です。パッケージ間の結合を減らし、エクスポートされる不要な要素を非表示にするために、エクスポートする必要があるものをできる限り最小限に抑える必要があります。要素をエクスポートするかどうか不明な場合は、デフォルトでエクスポートしないようにする必要があります。後でエクスポートする必要があることが判明した場合は、コードを調整できます。また、構造体を encoding/json でアンマーシャリングできるようにフィールドをエクスポートするなど、いくつかの例外にも留意してください。
+プロジェクトを構成するのは簡単ではありませんが、これらのルールに従うことで維持が容易になります。ただし、保守性を容易にするためには一貫性も重要であることに注意してください。したがって、コードベース内で可能な限り一貫性を保つようにしましょう。
+Go チームは Go プロジェクトの組織化/構造化に関する公式ガイドラインを2023年に発行しました: go.dev/doc/modules/layout
+名前付けはアプリケーション設計の重要な部分です。common
、util
、shared
のようなパッケージを作成しても、読み手にそれほどの価値をもたらしません。このようなパッケージを意味のある具体的なパッケージ名にリファクタリングしましょう。
また、パッケージに含まれるものではなく、パッケージが提供するものに基づいてパッケージに名前を付けると、その表現力を高める効率的な方法になることにも留意してください。
+ +混乱、さらにはバグにつながりかねない、変数とパッケージ間の名前の衝突を回避するために、それぞれに一意の名前を使用しましょう。これが不可能な場合は、インポートエイリアスを使用して修飾子を変更してパッケージ名と変数名を区別するか、より良い名前を考えてください。
+パッケージの衝突は、変数名が既存のパッケージ名と衝突する場合に発生し、パッケージの再利用が妨げられます。曖昧さを避けるために、変数名の衝突を防ぐ必要があります。衝突が発生した場合は、別の意味のある名前を見つけるか、インポートエイリアスを使用する必要があります。
+クライアントとメンテナがコードの意図を理解できるように、エクスポートされた要素を文章化しましょう。
+文章化はコーディングの重要な側面です。これにより、クライアントが API をより簡単に使用することができますが、プロジェクトの維持にも役立ちます。Go言語では、コードを慣用的なものにするために、いくつかのルールに従う必要があります。
+まず、エクスポートされたすべての要素を文章化する必要があります。構造、インタフェース、関数など、エクスポートする場合は文章化する必要があります。慣例として、エクスポートされた要素の名前から始まるコメントを追加します。
+慣例として、各コメントは句読点で終わる完全な文である必要があります。また、関数(またはメソッド)を文章化するときは、関数がどのように実行するかではなく、その関数が何を実行するつもりであるかを強調する必要があることにも留意してください。これはドキュメントではなく、関数とコメントについてです。ドキュメントは理想的には、消費者がエクスポートされた要素の使用方法を理解するためにコードを見る必要がないほど十分な情報を提供する必要があります。
+変数または定数を文章化する場合、その目的と内容という2つの側面を伝えることが重要かもしれません。前者は、外部クライアントにとって役立つように、コードドキュメントとして存在する必要があります。ただし、後者は必ずしも公開されるべきではありません。
+クライアントとメンテナがパッケージの目的を理解できるように、各パッケージをドキュメントする必要もあります。慣例として、コメントは //Package
で始まり、その後にパッケージ名が続きます。パッケージコメントの最初の行は、パッケージに表示されるため簡潔にする必要があります。そして、次の行に必要な情報をすべて入力します。
コードを文章化することが制約になるべきではありません。クライアントやメンテナがコードの意図を理解するのに役立つ必要があります。
+コードの品質と一貫性を向上させるには、リンターとフォーマッターを使用しましょう
+リンターは、コードを分析してエラーを検出する自動ツールです。このセクションの目的は、既存のリンターの完全なリストを提供することではありません。そうした場合、すぐに使い物にならなくなってしまうからです。しかし、ほとんどの Go プロジェクトにリンターが不可欠である理由を理解し、覚えておきましょう。
+リンターとフォーマッターは、コードベースの品質と一貫性を向上させる強力な方法です。時間をかけてどれを使用すべきかを理解し、それらの実行( CI や Git プリコミットフックなど)を自動化しましょう。
+既存のコードを読むときは、 0
で始まる整数リテラルが8進数であることに留意してください。また、接頭辞 0o
を付けることで8進整数であることを明確にし、読みやすさを向上させましょう。
8 進数は 0 で始まります(たとえば、010
は 10 進数の 8 に相当します)。可読性を向上させ、将来のコードリーダーの潜在的な間違いを回避するには、 0o
接頭辞を使用して 8 進数であることを明らかにしましょう(例: 0o10
)。
他の整数リテラル表現にも注意してください。
+0b
あるいは 0B
を使用します(たとえば、 0b
は10進数の 4 に相当します)0x
あるいは 0X
を使用します(たとえば、 0xF
は10進数の 15 に相当します)。 i
を使用します(たとえば、 3i
)読みやすくするために、区切り文字としてアンダースコア( _ )を使用することもできます。たとえば、 10 億は 1_000_000_000
のように書くことができます。アンダースコアは 0b)00_00_01
のように他の表現と併用することもできます。
Go言語では整数のオーバーフローとアンダーフローが裏側で処理されるため、それらをキャッチする独自の関数を実装できます。
+Go言語では、コンパイル時に検出できる整数オーバーフローによってコンパイルエラーが生成されます。たとえば、次のようになります。
+ + +ただし、実行時には、整数のオーバーフローまたはアンダーフローは発生しません。これによってアプリケーションのパニックが発生することはありません。この動作はやっかいなバグ(たとえば、負の結果につながる整数の増分や正の整数の加算など)につながる可能性があるため、頭に入れておくことが重要です。
+ +特定のデルタ内で浮動小数点比較を行うと、コードの移植性を確保できます。加算または減算を実行するときは、精度を向上させるために、同程度の大きさの演算をグループ化してください。また、乗算と除算は加算と減算の前に実行してください。
+Go言語には、(虚数を省略した場合) float32
と float64
という2つの浮動小数点型があります。浮動小数点の概念は、小数値を表現できないという整数の大きな問題を解決するために発明されました。予期せぬ事態を避けるために、浮動小数点演算は実際の演算の近似であることを知っておく必要があります。
そのために、乗算の例を見てみましょう。
+ +このコードにおいては 1.0001 * 1.0001 = 1.00020001 という結果が出力されることを期待すると思います。しかしながら、ほとんどの x86 プロセッサでは、代わりに 1.0002 が出力されます。
+Go言語の float32
および float64
型は近似値であるため、いくつかのルールを念頭に置く必要があります。
Go 開発者ならば、スライスの長さと容量の違いを理解するべきです。スライスの長さはスライス内の使用可能な要素の数であり、スライスの容量はバッキング配列内の要素の数です。
+セクション全文はこちら.
+ +スライスを作成するとき、長さがすでにわかっている場合は、指定された長さまたは容量でスライスを初期化しましょう。これにより、割り当ての数が減り、パフォーマンスが向上します。
+make
を使用してスライスを初期化するときに、長さとオプションの容量を指定できます。これらのパラメータの両方に適切な値を渡すことが適当であるにもかかわらず、それを忘れるのはよくある間違いです。実際、複数のコピーが必要になり、一時的なバッキング配列をクリーンアップするために GC に追加の労力がかかる可能性があります。パフォーマンスの観点から言えば、Go ランタイムに手を差し伸べない理由はありません。
オプションは、指定された容量または指定された長さのスライスを割り当てることです。 これら 2 つの解決策のうち、2 番目の解決策の方がわずかに高速である傾向があることがわかりました。ただし、特定の容量と追加を使用すると、場合によっては実装と読み取りが容易になることがあります。
+ +encoding/json
や reflect
パッケージなどを使用するときによくある混乱を避けるためには、nil スライスと空のスライスの違いを理解する必要があります。どちらも長さゼロ、容量ゼロのスライスですが、割り当てを必要としないのは nil スライスだけです。
Go言語では、nil と空のスライスは区別されます。nil スライスは nil
に等しいのに対し、空のスライスの長さはゼロです。nil スライスは空ですが、空のスライスは必ずしもnil
であるとは限りません。一方、nil スライスには割り当ては必要ありません。このセクション全体を通して、以下の方法を使用することによって、状況に応じてスライスを初期化することを見てきました。
var s []string
[]string(nil)
make([]string, length)
要素なしでスライスを初期化する場合、最後のオプション []string{}
は避けるべきです。最後に、予期しない動作を防ぐために、使用するライブラリが nil と空のスライスを区別しているかどうかを確認してみましょう。
スライスに要素が含まれていないことを確認するには、その長さを確認しましょう。これは、スライスが nil
であるか空であるかに関係なく機能します。マップについても同様です。明確な API を設計するには、nil スライスと空のスライスを区別しないでください。
スライスに要素があるかどうかを判断するには、スライスが nil かどうか、またはその長さが 0 に等しいかどうかを確認することで判断できます。スライスが空である場合とスライスが nil である場合の両方をカバーできるため、長さを確かめることが最良の方法です。
+一方、インタフェースを設計するときは、軽微なプログラミングエラーを起こさないよう nil スライスと空のスライスを区別しないようにする必要があります。スライスを返すときに、nil または空のスライスを返すかどうかは、意味的にも技術的にも違いはありません。コーラーにとってはどちらも同じことを意味するはずです。この原理は連想配列でも同じです。連想配列が空かどうかを確認するには、それが nil かどうかではなく、その長さを確認しましょう。
+ +組み込み関数 copy
を使用してあるスライスを別のスライスにコピーするには、コピーされる要素の数が2つのスライスの長さの間の最小値に相当することに注意してください。
要素をあるスライスから別のスライスにコピーする操作は、かなり頻繁に行われます。コピーを使用する場合、コピー先にコピーされる要素の数は 2 つのスライスの長さの間の最小値に相当することに注意する必要があります。また、スライスをコピーするための他の代替手段が存在することにも留意してください。そのため、コードベースでそれらを見つけても驚くことはありません。
+ +2つの異なる関数が同じ配列に基づくスライスを使用する場合に、copy または完全なスライス式を使用することで append
による衝突を防ぐことができます。ただし、大きなスライスを縮小する場合、メモリリークを防ぐことができるのはスライスコピーだけです。
スライスを使用するときは、予期せぬ副作用につながる状況に直面する可能性があることを覚えておく必要があります。結果のスライスの長さがその容量より小さい場合、追加によって元のスライスが変更される可能性があります。起こり得る副作用の範囲を制限したい場合は、スライスコピーまたは完全なスライス式を使用できます。これにより、コピーを実行できなくなります。
+???+ note "補足"
+`s[low:high:max]`(完全なスライス式):この命令文は、容量が `max - low` に等しいことを除けば、`s[low:high]` で作成されたスライスと同様のスライスを作成します。
+
ポインタのスライスまたはポインタフィールドを持つ構造体を操作する場合、スライス操作によって除外された要素を nil とすることでメモリリークを回避できます。
+大きなスライスまたは配列をスライスすると、メモリ消費が高くなる可能性があることに注意してください。残りのスペースは GC によって再利用されず、少数の要素しか使用しないにもかかわらず、大きなバッキング配列が保持されます。スライスコピーを使用することで、このような事態を防ぐことができます。
+ +ポインタまたはポインタフィールドを含む構造体を使用してスライス操作をする場合、GC がこれらの要素を再利用しないことを知っておく必要があります。その場合の選択肢は、コピーを実行するか、残りの要素またはそのフィールドを明示的に nil
とすることです。
連想配列を作成するとき、その長さがすでにわかっている場合は、指定された長さで初期化します。これにより、割り当ての数が減り、パフォーマンスが向上します。
+連想配列は、キー・値ペアの順序なしコレクションを提供します。なお、それぞれのペアは固有のキーを持ちます。Go言語では、連想配列はハッシュテーブルデータ構造に基づいています。内部的には、ハッシュテーブルはバケットの配列であり、各バケットはキー・値ペアの配列へのポインタです。
+連想配列に含まれる要素の数が事前にわかっている場合は、その初期サイズを指定して作成する必要があります。連想配列の増大は、十分なスペースを再割り当てし、すべての要素のバランスを再調整する必要があるため、計算量が非常に多くなりますが、これによりそれを回避することができます。
+ +連想配列はメモリ内で常に増大する可能性がありますが、縮小することはありません。 したがって、メモリの問題が発生する場合は、連想配列を強制的に再作成したり、ポインタを使用したりするなど、さまざまな手段を試すことができます。
+セクション全文はこちら.
+ +Go言語で型を比較するには、2 つの型が比較可能ならば、== 演算子と != 演算子を使用できます。真理値、数値、文字列、ポインタ、チャネル、および構造体が完全に比較可能な型で構成されています。それ以外は、 reflect.DeepEqual
を使用してリフレクションの代償を支払うか、独自の実装とライブラリを使用することができます。
効果的に比較するには、 ==
と !=
の使用方法を理解することが不可欠です。これらの演算子は、比較可能な被演算子で使用できます。
?
、 >=
、 <
、および >
演算子を数値型で使用して値を比較したり、文字列で字句順序を比較したりすることもできます。
被演算子が比較できない場合(スライスと連想配列など)、リフレクションなどの他の方法を利用する必要があります。リフレクションはメタプログラミングの一種であり、アプリケーションがその構造と動作を内省して変更する機能を指します。たとえば、Go言語では reflect.DeepEqual
を使用できます。この関数は、2つの値を再帰的に調べることによって、2つの要素が完全に等しいかどうかを報告します。受け入れられる要素は、基本的な型に加えて、配列、構造体、スライス、連想配列、ポインタ、インタフェース、関数です。しかし、最大の落とし穴はパフォーマンス上のペナルティです。
実行時のパフォーマンスが重要な場合は、独自のメソッドを実装することが最善の解決策となる可能性があります。
+追記:標準ライブラリには既に比較メソッドがいくつかあることを覚えておく必要があります。たとえば、最適化された bytes.Compare
関数を使用して、2つのバイトスライスを比較できます。独自のメソッドを実装する前に、車輪の再発明をしないようにしましょう。
range
ループ内でコピーされることを知らない (#30)range
ループ内の value 要素はコピーです。したがって、たとえば構造体を変更するには、そのインデックスを介してアクセスするか、従来の for
ループを介してアクセスしましょう(変更する要素またはフィールドがポインタである場合を除く)。
range ループを使用すると、さまざまなデータ構造に反復処理を行うことができます。
+古典的な for
ループと比較すると、range
ループはその簡潔な構文のおかげで、これらのデータ構造のすべての要素に反復処理をするのに便利です。
ただし、range ループ内の値要素はコピーであることを覚えておく必要があります。したがって、値を変更する必要がある構造体の場合、変更する値またはフィールドがポインタでない限り、要素自体ではなくコピーのみを更新します。range ループまたは従来の for ループを使用してインデックス経由で要素にアクセスすることが推奨されます。
+ +range
ループ(チャネルと配列)での引数の評価方法を知らない (#31)range
演算子に渡される式はループの開始前に 1 回だけ評価されることを理解すると、チャネルまたはスライスの反復処理における非効率な割り当てなどのありがちな間違いを回避できます。
range ループは、(タイプに関係なく)コピーを実行することにより、ループの開始前に、指定された式を 1 回だけ評価します。たとえば、誤った要素にアクセスしてしまう、というようなありがちな間違いを避けるために、この動作を覚えておく必要があります。たとえば
+ +このコードは、最後のインデックスを 10 に更新します。しかし、このコードを実行すると、10 は出力されません。 2 が出力されます。
+ +range
ループ内におけるポインタ要素の使用が及ぼす影響を分かっていない (#32)ローカル変数を使用するか、インデックスを使用して要素にアクセスすると、ループ内でポインタをコピーする際の間違いを防ぐことができます。
+range
ループを使用してデータ構造に反復処理を施す場合、すべての値が単一の一意のアドレスを持つ一意の変数に割り当てられることを思い出してください。ゆえに、各反復処理中にこの変数を参照するポインタを保存すると、同じ要素、つまり最新の要素を参照する同じポインタを保存することになります。この問題は、ループのスコープ内にローカル変数を強制的に作成するか、インデックスを介してスライス要素を参照するポインタを作成することで解決できます。どちらの解決策でも問題ありません。
連想配列を使用するときに予測可能な出力を保証するには、連想配列のデータ構造が次のとおりであることに注意してください。
+break
文がどのように機能するかを分かっていない (#34)ラベルと break
または continue
の併用は、特定の命令文を強制的に中断します。これは、ループ内の switch
または select
文で役立ちます。
通常、break 文はループの実行を終了するために使用されます。ループが switch または select と組み合わせて使用される場合、目的の命令文ではないのに中断させてしまう、というミスをすることが開発者にはよくあります。たとえば
+for i := 0; i < 5; i++ {
+ fmt.Printf("%d ", i)
+
+ switch i {
+ default:
+ case 2:
+ break
+ }
+}
+
break 文は for
ループを終了させるのではなく、代わりに switch
文を終了させます。したがって、このコードは 0 から 2 までを反復する代わりに、0 から 4 までを反復します(0 1 2 3 4
)。
覚えておくべき重要なルールの1つは、 break
文は最も内側の for
、switch
、または select
文の実行を終了するということです。前の例では、switch
文を終了します。
switch
文の代わりにループを中断する最も慣用的な方法はラベルを使用することです。
loop:
+ for i := 0; i < 5; i++ {
+ fmt.Printf("%d ", i)
+
+ switch i {
+ default:
+ case 2:
+ break loop
+ }
+ }
+
ここでは、loop
ラベルを for
ループに関連付けます。 次に、break
文に loop
ラベルを指定するので、switch ではなく loop が中断されます。よって、この新しいバージョンは予想どおり 0 1 2
を出力します。
defer
を使用する (#35)関数内のループロジックを抽出すると、各反復の最後に defer
文が実行されます。
defer
文は、上位ブロックの関数が戻るまで呼び出しの実行を遅らせます。これは主に定型コードを削減するために使用されます。たとえば、リソースを最終的に閉じる必要がある場合は、defer
を使用して、return
を実行する前にクロージャ呼び出しを繰り返すことを避けることができます。
defer
でよくあるミスの1つは、上位ブロック の関数が戻ったときに関数呼び出しがスケジュールされることを忘れることです。たとえば
func readFiles(ch <-chan string) error {
+ for path := range ch {
+ file, err := os.Open(path)
+ if err != nil {
+ return err
+ }
+
+ defer file.Close()
+
+ // ファイルに何らかの処理をする
+ }
+ return nil
+}
+
defer
呼び出しは、各ループ反復中ではなく、readFiles
関数が返されたときに実行されます。 readFiles
が返らない場合、ファイル記述子は永久に開いたままになり、リークが発生します。
この問題を解決するための一般的な手段の1つは、 defer
の後に、各反復中に呼び出される上位ブロックの関数を作成することです。
func readFiles(ch <-chan string) error {
+ for path := range ch {
+ if err := readFile(path); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func readFile(path string) error {
+ file, err := os.Open(path)
+ if err != nil {
+ return err
+ }
+
+ defer file.Close()
+
+ // ファイルに何らかの処理をする
+ return nil
+}
+
別の解決策は、readFile
関数をクロージャにすることですが、本質的には同じです。別の上位ブロックの関数を追加して、各反復中に defer
呼び出しを実行します。
ルーンが Unicode コードポイントの概念に対応し、複数のバイトで構成される可能性があることを理解することは、 Go 開発者が文字列を正確に操作するために不可欠です。
+Go言語ではルーンがあらゆる場所に使用されるため、次の点を理解することが重要です。
+rune
は Unicode コードポイントの概念に対応し、単一の値で表されるアイテムを意味します。len()
を使用すると、ルーン数ではなくバイト数が返されます。range
演算子を使用して文字列を反復処理すると、ルーンのバイトシーケンスの開始インデックスに対応するインデックスを使用してルーンが反復処理されます。特定のルーンインデックス( 3 番目のルーンなど)にアクセスするには、文字列を []rune
に変換します。
文字列の反復処理は、開発者にとって一般的な操作です。おそらく、文字列内の各ルーンに対して操作を実行するか、特定の部分文字列を検索する独自の関数を実装する必要があるでしょう。どちらの場合も、文字列の異なるルーンを反復処理する必要があります。しかし、反復がどのように機能するかについては困惑しやすいです。
+次の例を考えてみましょう。
+s := "hêllo"
+for i := range s {
+ fmt.Printf("position %d: %c\n", i, s[i])
+}
+fmt.Printf("len=%d\n", len(s))
+
混乱を招く可能性のある3つの点を取り上げましょう。
+最後の結果から見てきましょう。len はルーンの数ではなく、文字列内のバイト数を返すことはすでに述べました。文字列リテラルを s
に割り当てているため、s
は UTF-8 文字列です。一方、特殊文字「ê」は 1 バイトでエンコードされません。 2 バイト必要です。したがって、len(s)
を呼び出すと 6 が返されます。
前の例では、各ルーンを反復処理していないことを理解する必要があります。代わりに、ルーンの各開始インデックスを反復処理します。
+ +s[i]
を出力しても i 番目のルーンは出力されません。インデックス i
のバイトの UTF-8 表現を出力します。したがって、 hêllo
の代わりに hÃllo
を出力がされます。
さまざまなルーン文字をすべて出力したい場合は、 range
演算子の value 要素を使用することができます。
または、文字列をルーンのスライスに変換し、それを反復処理することもできます。
+s := "hêllo"
+runes := []rune(s)
+for i, r := range runes {
+ fmt.Printf("position %d: %c\n", i, r)
+}
+
この解決策では、以前の解決策と比較して実行時のオーバーヘッドが発生することに注意してください。実際、文字列をルーンのスライスに変換するには、追加のスライスを割り当て、バイトをルーンに変換する必要があります。文字列のバイト数を n とすると、時間計算量は O(n) になります。したがって、すべてのルーンを反復処理する場合は、最初の解決策を使用するべきです。
+ただし、最初の方法を使用して文字列の i 番目のルーンにアクセスしたい場合は、ルーンインデックスにアクセスできません。代わりに、バイトシーケンス内のルーンの開始インデックスがわかります。
+ + +strings.TrimRight
・ strings.TrimLeft
は、指定されたセットに含まれるすべての末尾・先頭のルーンを削除しますが、 strings.TrimSuffix
・ strings.TrimPrefix
は、指定された接尾辞・接頭辞のない文字列を返します。
たとえば
+ +は 123 を出力します
+ +逆に、 strings.TrimLeft
は、セットに含まれる先頭のルーンをすべて削除します。
一方、strings.TrimSuffix
・ strings.TrimPrefix
は、指定された末尾の接尾辞・接頭辞を除いた文字列を返します。
文字列のリストの連結は、反復ごとに新しい文字列が割り当てられないように、strings.Builder
を使用して行う必要があります。
+=
演算子を用いてスライスのすべての文字列要素を連結する concat
関数を考えてみましょう。
func concat(values []string) string {
+ s := ""
+ for _, value := range values {
+ s += value
+ }
+ return s
+}
+
各反復中に、 +=
演算子は s
と value 文字列を連結します。一見すると、この関数は間違っていないように見えるかもしれません。しかし、この実装は、文字列の核となる特性の1つである不変性を忘れています。したがって、各反復では s
は更新されません。メモリ内に新しい文字列を再割り当てするため、この関数のパフォーマンスに大きな影響を与えます。
幸いなことに、 strings.Builder
を用いることで、この問題に対処する解決策があります。
func concat(values []string) string {
+ sb := strings.Builder{}
+ for _, value := range values {
+ _, _ = sb.WriteString(value)
+ }
+ return sb.String()
+}
+
各反復中に、value の内容を内部バッファに追加する WriteString
メソッドを呼び出して結果の文字列を構築し、メモリのコピーを最小限に抑えることができました。
WriteString
は 2 番目の出力としてエラーを返しますが、意図的に無視しましょう。実際、このメソッドは nil エラー以外を返すことはありません。では、このメソッドがシグネチャの一部としてエラーを返す目的は何でしょうか。strings.Builder
は io.StringWriter
インタフェースを実装しており、これには WriteString(s string) (n int, err error)
という1つのメソッドが含まれています。したがって、このインタフェースに準拠するには、WriteString
はエラーを返さなければならないのです。
内部的には、strings.Builder
はバイトスライスを保持します。 WriteString
を呼び出すたびに、このスライスに追加する呼び出しが行われます。これには2つの影響があります。まず、 append
の呼び出しが衝突状態を引き起こす可能性があるため、この構造体は同時に使用されるべきではありません。2番目の影響は、 非効率なスライスの初期化 (#21) で見たものです。スライスの将来の長さがすでにわかっている場合は、それを事前に割り当てる必要があります。そのために、strings.Builder
は別の n
バイトのためのスペースを保証するメソッド Grow(n int)
を持っています。
func concat(values []string) string {
+ total := 0
+ for i := 0; i < len(values); i++ {
+ total += len(values[i])
+ }
+
+ sb := strings.Builder{}
+ sb.Grow(total) (2)
+ for _, value := range values {
+ _, _ = sb.WriteString(value)
+ }
+ return sb.String()
+}
+
ベンチマークを実行して3つのバージョン(+=
を使用した V1 、事前割り当てなしで strings.Builder{}
を使用した V2 、事前割り当てありの strings.Builder{}
を使用した V3 )を比較してみましょう。入力スライスには 1,000 個の文字列が含まれており、各文字列には 1,000 バイトが含まれています。
BenchmarkConcatV1-4 16 72291485 ns/op
+BenchmarkConcatV2-4 1188 878962 ns/op
+BenchmarkConcatV3-4 5922 190340 ns/op
+
ご覧のとおり、最新バージョンが最も効率的で、V1 より 99% 、V2 より 78% 高速です。
+strings.Builder
は、文字列のリストを連結するための解決策として推奨されます。通常、これはループ内で使用する必要があります。いくつかの文字列 (名前と姓など)を連結するだけの場合、 strings.Builder
の使用は、 +=
演算子や fmt.Sprintf
と比べて可読性が低くなるからです。
bytes
パッケージは strings
パッケージと同じ操作を提供してくれることを覚えておくと、余分なバイト・文字列変換を避けることができます。
文字列または []byte
を扱うことを選択する場合、ほとんどのプログラマーは利便性のために文字列を好む傾向があります。しかし、ほとんどの I/O は実際には []byte
で行われます。たとえば、io.Reader
、io.Writer
、および io.ReadAll
は文字列ではなく []byte
を処理します。
文字列と []byte
のどちらを扱うべきか迷ったとき、[]byte
を扱う方が必ずしも面倒だというわけではないことを思い出してください。strings パッケージからエクスポートされたすべての関数には、bytes
パッケージに代替機能があります。 Split
、Count
、Contains
、Index
などです。したがって、I/O を実行しているかどうかに関係なく、文字列の代わりにバイトを使用してワークフロー全体を実装でき、追加の変換のコストを回避できるかどうかを最初に確認する必要があります。
部分文字列の代わりにコピーを使用すると、部分文字列操作によって返される文字列が同じバイト配列によってサポートされるため、メモリリークを防ぐことができます。
+スライスとメモリリーク (#26) では、スライスまたは配列のスライスがメモリリークの状況を引き起こす可能性があることを確認しました。この原則は、文字列および部分文字列の操作にも当てはまります。
+Go言語で部分文字列操作を使用するときは、2 つのことに留意する必要があります。まず、提供される間隔はルーンの数ではなく、バイト数に基づいています。次に、結果の部分文字列が最初の文字列と同じバッキング配列を共有するため、部分文字列操作によりメモリリークが発生する可能性があります。これを防ぐ方法は、文字列のコピーを手動で実行するか、Go 1.18 から実装されている strings.Clone
を使用することです。
値レシーバーとポインタレシーバーのどちらを使用するかは、どの型なのか、変化させる必要があるかどうか、コピーできないフィールドが含まれているかどうか、オブジェクトはどれくらい大きいのか、などの要素に基づいて決定する必要があります。分からない場合は、ポインタレシーバを使用してください。
+値レシーバーとポインタレシーバーのどちらを選択するかは、必ずしも簡単ではありません。選択に役立ついくつかの条件について説明しましょう。
+ポインタレシーバーでなければならない とき
+ポインタレシーバーであるべき とき
+値レシーバーでなければならない とき
+値レシーバーであるべき とき
+time.Time
などの小さな配列または構造体で、可変フィールドを持たない値型である場合。 int
、float64
、または string
などの基本的な型の場合。 もちろん、特殊なケースは常に存在するため、すべてを網羅することは不可能ですが、このセクションの目標は、ほとんどのケースをカバーするためのガイダンスを提供することです。通常は、そうしない正当な理由がない限り、値レシーバーを使用して間違いありません。分からない場合は、ポインタレシーバを使用する必要があります。
+ +名前付き結果パラメーターの使用は、特に複数の結果パラメーターが同じ型を持つ場合、関数・メソッドの読みやすさを向上させる効率的な方法です。場合によっては、名前付き結果パラメータはゼロ値に初期化されるため、この方法が便利ですらあることもあります。ただし潜在的な副作用には注意してください。
+関数またはメソッドでパラメータを返すとき、これらのパラメータに名前を付けて、通常の変数として使用できます。結果パラメーターに名前を付けると、関数・メソッドの開始時にそのパラメーターはゼロ値に初期化されます。名前付き結果パラメータを使用すると、 むき出しの return 文(引数なし) を呼び出すこともできます。その場合、結果パラメータの現在の値が戻り値として使用されます。
+以下は、名前付き結果パラメータ b
を用いた例です。
この例では、結果パラメータに名前 b
を付けています。引数なしで return を呼び出すと、b
の現在の値が返されます。
場合によっては、名前付きの結果パラメーターによって可読性が向上することもあります。たとえば、2 つのパラメーターが同じ型である場合などです。その他にも、利便性のために用いることができます。ゆえに、明確な利点がある場合は、慎重になりながらも名前付き結果パラメータを使用するべきです。
+ +#43 を参照してください。
+名前付き結果パラメータが状況によっては役立つ理由について説明しました。 ただし、これらはゼロ値に初期化されるため、十分に注意しないと、軽微なバグが発生する可能性があります。たとえば、このコードはどこが間違っているでしょうか。
+func (l loc) getCoordinates(ctx context.Context, address string) (
+ lat, lng float32, err error) {
+ isValid := l.validateAddress(address) (1)
+ if !isValid {
+ return 0, 0, errors.New("invalid address")
+ }
+
+ if ctx.Err() != nil { (2)
+ return 0, 0, err
+ }
+
+ // 座標を取得して返す
+}
+
一瞥しただけではエラーは明らかではないかもしれません。if ctx.Err() != nil
スコープで返されるエラーは err
です。しかし、err
変数には値を割り当てていません。error
型のゼロ値、 nil
に割り当てられたままです。したがって、このコードは常に nil エラーを返します。
名前付き結果パラメータを使用する場合、各パラメータはゼロ値に初期化されることに注意してください。このセクションで説明したように、これにより、見つけるのが必ずしも簡単ではない軽微なバグが発生する可能性があります。ゆえに、潜在的な副作用を避けるために、名前付き結果パラメーターを使用するときは注意してください。
+ +インタフェースを返すときは、nil ポインタを返すのではなく、明示的な nil 値を返すように注意してください。そうしなければ、意図しない結果が発生し、呼び出し元が nil ではない値を受け取る可能性があります。
+ファイル名の代わりに io.Reader
型を受け取るように関数を設計すると、関数の再利用性が向上し、テストが容易になります。
ファイル名をファイルから読み取るための関数入力として受け入れることは、ほとんどの場合、「コードの臭い」とみなされべきです( os.Open
などの特定の関数を除く)。実際、複数のファイルを作成する必要があるかもしれないので、単体テストがより複雑になります。また、関数の再利用性も低下します (ただし、すべての関数が再利用されるわけではありません)。 io.Reader
インタフェースを使用すると、データソースが抽象化されます。入力がファイル、文字列、HTTP リクエスト、gRPC リクエストのいずれであるかに関係なく、実装は再利用でき、簡単にテストできます。
defer
引数とレシーバーがどのように評価されるかを知らない(引数の評価、ポインター、および値レシーバー) (#47)ポインタを defer
関数に渡すことと、呼び出しをクロージャ内にラップすることが、引数とレシーバーの即時評価を克服するために実現可能な解決策です。
defer
関数では、引数は、上位ブロックの関数が戻ってからではなく、すぐに評価されます。たとえば、このコードでは、常に同じステータス――空の文字列――で notify
と incrementCounter
を呼び出します。
const (
+ StatusSuccess = "success"
+ StatusErrorFoo = "error_foo"
+ StatusErrorBar = "error_bar"
+)
+
+func f() error {
+ var status string
+ defer notify(status)
+ defer incrementCounter(status)
+
+ if err := foo(); err != nil {
+ status = StatusErrorFoo
+ return err
+ }
+
+ if err := bar(); err != nil {
+ status = StatusErrorBar
+ return err
+ }
+
+ status = StatusSuccess <5>
+ return nil
+}
+
たしかに、notify(status)
と incrementCounter(status)
を defer
関数として呼び出しています。したがって、Go言語は、defer を使用した段階で f
がステータスの現在の値を返すと、これらの呼び出しの実行を遅らせ、空の文字列を渡します。
defer
を使い続けたい場合の主な方法は 2 つあります。
最初の解決策は文字列ポインタを渡すことです。
+func f() error {
+ var status string
+ defer notify(&status)
+ defer incrementCounter(&status)
+
+ // 関数のそれ以外の部分は変更なし
+}
+
defer
を使用すると、引数(ここではステータスのアドレス)がすぐに評価されます。ステータス自体は関数全体で変更されますが、そのアドレスは割り当てに関係なく一定のままです。よって、notify
または incrementCounter
が文字列ポインタによって参照される値を使用する場合、期待どおりに動作します。ただし、この解決策では 2 つの関数のシグネチャを変更する必要があり、それが常に可能であるとは限りません。
別の解決策があります――クロージャ(本体の外部から変数を参照する匿名関数値)を defer
文として呼び出すことです。
func f() error {
+ var status string
+ defer func() {
+ notify(status)
+ incrementCounter(status)
+ }()
+
+ // 関数のそれ以外の部分は変更なし
+}
+
ここでは、notify
と incrementCounter
の両方の呼び出しをクロージャ内にラップします。このクロージャは、本体の外部からステータス変数を参照します。ゆえに、status
は、defer
を呼び出したときではなく、クロージャが実行されたときに評価されます。この解決策は正しく機能する上に、シグネチャを変更するために notify
や incrementCounter
を必要としません。
この動作はメソッドレシーバーにも適用されることにも注意してください。レシーバーはすぐに評価されます。
+ +panic
の使用は、Go言語でエラーに対処するための手段です。ただし、これは回復不能な状況でのみ使用するようにしてください。たとえば、ヒューマンエラーを通知する場合や、必須の依存関係の読み込みに失敗した場合などです。
Go言語では、panic は通常の流れを停止する組み込み関数です。
+ +このコードは a を出力し、b を出力する前に停止します。
+ +panic の使用は慎重にすべきです。代表的なケースが 2 つあり、1 つはヒューマンエラーを通知する場合(例: sql.Register
ドライバーが nil
または既に登録されている場合に panic を起こします)、もう 1 つはアプリケーションが必須の依存関係の作成に失敗した場合です。結果として、例外的にアプリケーションを停止します。それ以外のほとんどの場合においては、エラー処理は、最後の戻り引数として適切なエラー型を返す関数を通じて行うべきです。
エラーをラップすると、エラーをマークしたり、追加のコンテキストを提供したりできます。ただし、エラーラッピングにより、呼び出し元がソースエラーを利用できるようになるため、潜在的な結合が発生します。それを避けたい場合は、エラーラッピングを使用しないでください。
+Go 1.13 以降、%w ディレクティブを使用すれば簡単にエラーをラップできるようになりました。エラーラッピングとは、ソースエラーも使用できるようにするラッパーコンテナ内でエラーをラップまたはパックすることです。一般に、エラーラッピングの主な使用例は次の 2 つです。
+エラーを処理するとき、エラーをラップするかどうかを決定できます。ラッピングとは、エラーにさらにコンテキストを追加したり、エラーを特定のタイプとしてマークしたりすることです。エラーをマークする必要がある場合は、独自のエラー型を作成する必要があります。ですが、新たにコンテキストを加えたいだけの場合は、新しいエラー型を作成する必要がないため、%w ディレクティブを指定して fmt.Errorf を使用しましょう。ただし、エラーラッピングにより、呼び出し元がソースエラーを利用できるようになるため、潜在的な結合が生じます。それを避けたい場合は、エラーのラッピングではなく、エラーの変換を使用する必要があります。たとえば、%v ディレクティブを指定した fmt.Errorf を使用します。
+ +Go 1.13 のエラーラッピングを %w
ディレクティブと fmt.Errorf
で使用する場合、型に対するエラーの比較は errors.As
を通じて行う必要があります。そうでなければ、返されたエラーがラップされている場合、評価に失敗します。
Go 1.13 のエラーラッピングを %w
ディレクティブと fmt.Errorf
で使用する場合、エラーと値の比較は errors.As
を通じて行う必要があります。そうでなければ、返されたエラーがラップされている場合、評価に失敗します。
センチネルエラーはグローバル変数として定義されたエラーのことです。
+ +一般に、慣例としてErr
で始め、その後にエラー型を続けます。ここでは ErrFoo
です。センチネルエラーは、予期される エラー、つまりクライアントが確認することを期待するエラーを伝えます。一般的なガイドラインとして
+var ErrFoo =errors.New("foo")
。 BarError
は error
インタフェースを実装した上で type BarError struct { ... }
。 アプリケーションで %w
ディレクティブと fmt.Errorf
を使用してエラーラップを使用する場合、特定の値に対するエラーのチェックは ==
の代わりに errors.Is
を使用して行いましょう。それによって、センチネルエラーがラップされている場合でも、errors.Is
はそれを再帰的にアンラップし、チェーン内の各エラーを提供された値と比較できます。
ほとんどの場合、エラーは 1 回で処理されるべきです。エラーをログに記録することは、エラーを処理することです。すなわち、ログに記録するかエラーを返すかを選択する必要があります。多くの場合、エラーラッピングは、エラーに追加のコンテキストを提供し、ソースエラーを返すことができるため、解決策になります。
+エラーを複数回処理することは、特にGo言語に限らず、開発者が頻繁にやってしまうミスです。これにより、同じエラーが複数回ログに記録され、デバッグが困難になる状況が発生する可能性があります。
+エラー処理は 1 度で済ますべきだということを覚えておきましょう。エラーをログに記録することは、エラーを処理することです。つまり、行うべきは、ログに記録するか、エラーを返すかのどちらかだということです。これにより、コードが簡素化され、エラーの状況についてより適切な洞察が得られます。エラーラッピングは、ソースエラーを伝え、エラーにコンテキストを追加できるため、最も使い勝手の良い手段になります。
+ +関数呼び出し中であっても、defer
関数内であっても、エラーを無視するときは、ブランク識別子を使用して明確に行うべきです。そうしないと、将来の読み手がそれが意図的だったのか、それともミスだったのか困惑する可能性があります。
defer
エラーを処理しない (#54)多くの場合、defer
関数によって返されるエラーを無視すべきではありません。状況に応じて、直接処理するか、呼び出し元に伝えましょう。これを無視する場合は、ブランク識別子を使用してください。
次のコードを考えてみましょう。
+ +保守性の観点から、このコードはいくつかの問題を引き起こす可能性があります。ある人がこれを読むことを考えてみます。読み手は、notify がエラーを返すにもかかわらず、そのエラーが親関数によって処理されないことに気づきます。エラー処理が意図的であるかどうかを果たして推測できるでしょうか。以前の開発者がそれを処理するのを忘れたのか、それとも意図的に処理したのかを知ることができるでしょうか。
+これらの理由により、エラーを無視したい場合、ブランク識別子( _
)を使うほかありません。
コンパイルと実行時間の点では、この方法は最初のコード部分と比べて何も変わりません。しかし、この新しいバージョンでは、私たちがエラーに関心がないことを明らかにしています。また、エラーが無視される理由を示すコメントを追加することもできます。
+ + +並行処理と並列処理の基本的な違いを理解することは、 Go 開発者にとって必須です。並行処理は構造に関するものですが、並列処理は実行に関するものです。
+並行処理と並列処理は同じではありません。
+まとめると、並行処理は、並列化できる部分をもつ問題を解決するための構造を提供します。すなわち、並行処理により並列処理が可能になります 。
+ + +熟練した開発者になるには、並行処理が必ずしも高速であるとは限らないことを認識する必要があります。最小限のワークロードの並列処理を伴う解決策は、必ずしも逐次処理より高速であるとは限りません。逐次処理と並行処理のベンチマークは、仮定を検証する方法であるべきです。
+セクション全文はこちら.
+ +ゴルーチンの相互作用を認識していることは、チャネルとミューテックスのどちらを選択するかを決定するときにも役立ちます。一般に、並列ゴルーチンには同期が必要であり、したがってミューテックスが必要です。反対に、並行ゴルーチンは通常、調整とオーケストレーション、つまりチャネルを必要とします。
+並行処理の問題を考慮すると、チャネルまたはミューテックスを使用した解決策を実装できるかどうかが必ずしも明確ではない可能性があります。Go言語は通信によるメモリの共有を促進するため、起こりうる間違いのうちの一つは、ユースケースにかかわらず、チャネルの使用を常に強制することです。しかしながら、2 つの方法は補完的なものであると見なすべきです。
+チャネルまたはミューテックスはどのような場合に使用する必要があるのでしょうか。次の図の例をバックボーンとして使用します。この例には、特定の関係を持つ 3 つの異なるゴルーチンがあります。
+原則として、並列ゴルーチンは、スライスなどの共有リソースにアクセスしたり変更したりする必要がある場合などに、_同期_する必要があります。同期はミューテックスでは強制されますが、どのチャネル型でも強制されません(バッファありチャネルを除く)。したがって、一般に、並列ゴルーチン間の同期はミューテックスを介して達成される必要があります。
+一方、一般に、並行ゴルーチンは 調整およびオーケストレーション をする必要があります。たとえば、G3 が G1 と G2 の両方からの結果を集約する必要がある場合、G1 と G2 は新しい中間結果が利用可能であることを G3 に通知する必要があります。この調整はコミュニケーションの範囲、つまりチャネルに該当します。
+並行ゴルーチンに関しては、リソースの所有権をあるステップ(G1 および G2)から別のステップ(G3)に移管したい場合もあります。たとえば、G1 と G2 によって共有リソースが豊かになっている場合、ある時点でこのジョブは完了したと見なされます。ここでは、チャネルを使用して、特定のリソースの準備ができていることを通知し、所有権の移転を処理する必要があります。
+ミューテックスとチャネルには異なるセマンティクスがあります。状態を共有したいとき、または共有リソースにアクセスしたいときは、ミューテックスによってこのリソースへの排他的アクセスが保証されます。反対に、チャネルはデータの有無(chan struct{}
の有無)に関係なくシグナリングを行う仕組みです。調整や所有権の移転はチャネルを通じて行う必要があります。ゴルーチンが並列か並行かを知ることが重要です。一般に、並列ゴルーチンにはミューテックスが必要で、並行ゴルーチンにはチャネルが必要です。
並行処理に熟達するということは、データ競合と競合状態が異なる概念であることを理解することも意味します。データ競合は、複数のゴルーチンが同じメモリ位置に同時にアクセスし、そのうちの少なくとも 1 つが書き込みを行っている場合に発生します。一方、データ競合がないことが必ずしも決定的実行を意味するわけではありません。動作が制御できないイベントの順序やタイミングに依存している場合、これは競合状態です。
+競合問題は、プログラマーが直面する可能性のあるバグの中で最も困難かつ最も潜伏性の高いバグの 1 つとなります。Go 開発者として、私たちはデータ競合と競合状態、それらが及ぼしうる影響、およびそれらを回避する方法などの重要な側面を理解する必要があります。
+データ競合は、2 つ以上のゴルーチンが同じメモリ位置に同時にアクセスし、少なくとも 1 つが書き込みを行っている場合に発生します。この場合、危険な結果が生じる可能性があります。さらに悪いことに、状況によっては、メモリ位置に無意味なビットの組み合わせを含む値が保持されてしまう可能性があります。
+さまざまな手法を駆使して、データ競合の発生を防ぐことができます。たとえば
+sync/atomic
パッケージを使用する 実行したい操作に応じて、データ競合のないアプリケーションが必ずしも決定的な結果を意味するでしょうか。そうとはいえません。
+競合状態は、動作が制御できないイベントのシーケンスまたはタイミングに依存する場合に発生します。ここでは、イベントのタイミングがゴルーチンの実行順序です。
+まとめると、並行処理のアプリケーションで作業する場合、データ競合は競合状態とは異なることを理解することが不可欠です。データ競合は、複数のゴルーチンが同じメモリ位置に同時にアクセスし、そのうちの少なくとも 1 つが書き込みを行っている場合に発生します。データ競合とは、予期しない動作を意味します。ただし、データ競合のないアプリケーションが必ずしも決定的な結果を意味するわけではありません。データ競合がなくても、アプリケーションは制御されていないイベント(ゴルーチンの実行、チャネルへのメッセージの発信速度、データベースへの呼び出しの継続時間など)に依存する挙動を持つことがあります。その場合は競合状態です。並行処理のアプリケーションの設計に熟練するには、両方の概念を理解することが肝要です。
+ +一定数のゴルーチンを作成するときは、ワークロードのタイプを考慮してください。CPU バウンドのゴルーチンを作成するということは、この数を GOMAXPROCS
変数(デフォルトではホスト上の CPU コアの数に基づく)に近づけることを意味します。 I/O バウンドのゴルーチンの作成は、外部システムなどの他の要因に依存します。
プログラミングでは、ワークロードの実行時間は次のいずれかによって制限されます。
+ここ数十年でメモリが非常に安価になったことを考慮すると、後者は現在では最もまれです。したがって、このセクションでは、最初の 2 つのワークロードタイプ、CPU バウンドと I/O バウンドに焦点を当てます。
+ワーカーによって実行されるワークロードが I/O バウンドである場合、値は主に外部システムに依存します。逆に、ワークロードが CPU に依存している場合、ゴルーチンの最適な数は利用可能な CPU コアの数に近くなります(ベストプラクティスは runtime.GOMAXPROCS
を使用することです)。並行処理のアプリケーションを設計する場合、ワークロードのタイプ( I/O あるいは CPU )を知ることが重要です。
Go Context は、Go言語の並行処理の基礎の一部でもあります。 Context を使用すると、デッドライン、キーと値のリストを保持できます。
+https://pkg.go.dev/context
+Context は、デッドライン、キャンセルシグナル、その他の値を API の境界を越えて伝達します。
+デッドラインとは、次のいずれかで決定される特定の時点を指します。
+time.Duration
(例:250 ms)time.Time
(例:2023-02-07 00:00:00 UTC) デッドラインのセマンティクスは、これを過ぎた場合は進行中のアクティビティを停止する必要があることを伝えます。アクティビティとは、たとえば、チャネルからのメッセージの受信を待機している I/O リクエストやゴルーチンです。
+Go Context のもう 1 つの使用例は、キャンセルシグナルを伝送することです。別のゴルーチン内で CreateFileWatcher(ctx context.Context, filename string)
を呼び出すアプリケーションを作成することを想像してみましょう。この関数は、ファイルから読み取りを続けて更新をキャッチする特定のファイルウォッチャーを作成します。提供された Context が期限切れになるかキャンセルされると、この関数はそれを処理してファイル記述子を閉じます。
Go Context の最後の使用例は、キーと値のリストを運ぶことです。 Context にキーと値のリストを含める意味は何でしょうか。Go Context は汎用的であるため、使用例は無限にあります。
+たとえば、トレースを使用する場合、異なる副次機能間で同じ相関 ID を共有したいことがあるかもしれません。一部の開発者は、この ID を関数シグネチャの一部にするにはあまりに侵略的だと考えるかもしれません。この点に関して、与えられた Context の一部としてそれを含めることを決定することもできます。
+context.Context
タイプは、受信専用の通知チャネル <-chan struct{}
を返す Done
メソッドをエクスポートします。このチャネルは、 Context に関連付けられた作業をキャンセルする必要がある場合に閉じられます。たとえば
context.WithCancel
で作成された Context に関連するDoneチャネルは、cancel関数が呼び出されると閉じられます。 context.WithDeadline
で作成した Context に関連するDoneチャネルは、deadline を過ぎると閉じられます。 注意すべき点の 1 つは、内部チャネルは、特定の値を受け取ったときではなく、 Context がキャンセルされたとき、またはデッドラインに達したときに閉じる必要があるということです。チャネルのクローズは、すべての消費者ゴルーチンが受け取る唯一のチャネルアクションであるためです。このようにして、 Context がキャンセルされるか、デッドラインに達すると、すべての消費者に通知が届きます。
+まとめると、熟練した Go 開発者になるには、 Context とその使用方法について理解する必要があります。原則として、ユーザーが待機させられる関数は Context を取得するべきです。これにより、上流の呼び出し元がこの関数をいつ呼び出すかを決定できるようになるからです。
+ +sync
型のコピー (#74)柔軟性に問題がないようにするために、関数はほとんどの場合、インターフェイスではなく具体的な実装を返す必要があります。逆に、関数は可能な限りインターフェイスを受け入れる必要があります。
+sync
型はコピーされるべきではありません。
ほとんどの場合、インターフェイスではなく具体的な実装を返す必要があります。そうでないとパッケージの依存関係により設計がいっそう複雑になり、すべてのクライアントが同じ抽象化に依存する必要があるため、柔軟性に欠ける可能性があります。結論は前のセクションと似ています。抽象化がクライアントにとって役立つことが (予測ではなく) わかっている場合は、インターフェイスを返すことを検討してもよいでしょう。それ以外の場合は、抽象化を強制すべきではありません。それらはクライアントによって発見される必要があります。何らかの理由でクライアントが実装を抽象化する必要がある場合でも、クライアント側でそれを行うことができます。
+プロファイリングと実行トレーサを利用して、アプリケーションのパフォーマンスと最適化すべき部分について理解しましょう。
セクション全文はこちら。
-GCの調整方法を理解すると、突然の負荷の増加をより効率的に処理できるなど、さまざまな恩恵が得られます。
+GC の調整方法を理解すると、突然の負荷の増加をより効率的に処理できるなど、さまざまな恩恵が得られます。
DockerとKubernetesにデプロイする際のCPUスロットリングを回避するには、Go言語がCFS対応ではないことに留意してください。
+Docker と Kubernetes にデプロイする際のCPUスロットリングを回避するには、Go言語がCFS対応ではないことに留意してください。
This page is a summary of all the mistakes in the 100 Go Mistakes book. Meanwhile, it's also a page open to the community. If you believe that a mistake should be added, please create a community mistake issue.
WarningYou're currently viewing a new version that I'm enriching with significantly more content. Yet, this version is still under development; please be gentle if you find an issue, and feel free to create a PR.
"},{"location":"#code-and-project-organization","title":"Code and Project Organization","text":""},{"location":"#unintended-variable-shadowing-1","title":"Unintended variable shadowing (#1)","text":"TL;DRAvoiding shadowed variables can help prevent mistakes like referencing the wrong variable or confusing readers.
Variable shadowing occurs when a variable name is redeclared in an inner block, but this practice is prone to mistakes. Imposing a rule to forbid shadowed variables depends on personal taste. For example, sometimes it can be convenient to reuse an existing variable name like err
for errors. Yet, in general, we should remain cautious because we now know that we can face a scenario where the code compiles, but the variable that receives the value is not the one expected.
Source code
"},{"location":"#unnecessary-nested-code-2","title":"Unnecessary nested code (#2)","text":"TL;DRAvoiding nested levels and keeping the happy path aligned on the left makes building a mental code model easier.
In general, the more nested levels a function requires, the more complex it is to read and understand. Let\u2019s see some different applications of this rule to optimize our code for readability:
if
block returns, we should omit the else
block in all cases. For example, we shouldn\u2019t write:if foo() {\n// ...\nreturn true\n} else {\n// ...\n}\n
Instead, we omit the else
block like this:
if foo() {\n// ...\nreturn true\n}\n// ...\n
if s != \"\" {\n// ...\n} else {\nreturn errors.New(\"empty string\")\n}\n
Here, an empty s
represents the non-happy path. Hence, we should flip the condition like so:
if s == \"\" {\nreturn errors.New(\"empty string\")\n}\n// ...\n
Writing readable code is an important challenge for every developer. Striving to reduce the number of nested blocks, aligning the happy path on the left, and returning as early as possible are concrete means to improve our code\u2019s readability.
Source code
"},{"location":"#misusing-init-functions-3","title":"Misusing init functions (#3)","text":"TL;DRWhen initializing variables, remember that init functions have limited error handling and make state handling and testing more complex. In most cases, initializations should be handled as specific functions.
An init function is a function used to initialize the state of an application. It takes no arguments and returns no result (a func()
function). When a package is initialized, all the constant and variable declarations in the package are evaluated. Then, the init functions are executed.
Init functions can lead to some issues:
We should be cautious with init functions. They can be helpful in some situations, however, such as defining static configuration. Otherwise, and in most cases, we should handle initializations through ad hoc functions.
Source code
"},{"location":"#overusing-getters-and-setters-4","title":"Overusing getters and setters (#4)","text":"TL;DRForcing the use of getters and setters isn\u2019t idiomatic in Go. Being pragmatic and finding the right balance between efficiency and blindly following certain idioms should be the way to go.
Data encapsulation refers to hiding the values or state of an object. Getters and setters are means to enable encapsulation by providing exported methods on top of unexported object fields.
In Go, there is no automatic support for getters and setters as we see in some languages. It is also considered neither mandatory nor idiomatic to use getters and setters to access struct fields. We shouldn\u2019t overwhelm our code with getters and setters on structs if they don\u2019t bring any value. We should be pragmatic and strive to find the right balance between efficiency and following idioms that are sometimes considered indisputable in other programming paradigms.
Remember that Go is a unique language designed for many characteristics, including simplicity. However, if we find a need for getters and setters or, as mentioned, foresee a future need while guaranteeing forward compatibility, there\u2019s nothing wrong with using them.
"},{"location":"#interface-pollution-5","title":"Interface pollution (#5)","text":"TL;DRAbstractions should be discovered, not created. To prevent unnecessary complexity, create an interface when you need it and not when you foresee needing it, or if you can at least prove the abstraction to be a valid one.
An interface provides a way to specify the behavior of an object. We use interfaces to create common abstractions that multiple objects can implement. What makes Go interfaces so different is that they are satisfied implicitly. There is no explicit keyword like implements
to mark that an object X
implements interface Y
.
In general, we can define three main use cases where interfaces are generally considered as bringing value: factoring out a common behavior, creating some decoupling, and restricting a type to a certain behavior. Yet, this list isn't exhaustive and will also depend on the context we face.
In many occasions, interfaces are made to create abstractions. And the main caveat when programming meets abstractions is remembering that abstractions should be discovered, not created. What does this mean? It means we shouldn\u2019t start creating abstractions in our code if there is no immediate reason to do so. We shouldn\u2019t design with interfaces but wait for a concrete need. Said differently, we should create an interface when we need it, not when we foresee that we could need it. What\u2019s the main problem if we overuse interfaces? The answer is that they make the code flow more complex. Adding a useless level of indirection doesn\u2019t bring any value; it creates a worthless abstraction making the code more difficult to read, understand, and reason about. If we don\u2019t have a strong reason for adding an interface and it\u2019s unclear how an interface makes a code better, we should challenge this interface\u2019s purpose. Why not call the implementation directly?
We should be cautious when creating abstractions in our code (abstractions should be discovered, not created). It\u2019s common for us, software developers, to overengineer our code by trying to guess what the perfect level of abstraction is, based on what we think we might need later. This process should be avoided because, in most cases, it pollutes our code with unnecessary abstractions, making it more complex to read.
Rob Pike
Don\u2019t design with interfaces, discover them.
Let\u2019s not try to solve a problem abstractly but solve what has to be solved now. Last, but not least, if it\u2019s unclear how an interface makes the code better, we should probably consider removing it to make our code simpler.
Source code
"},{"location":"#interface-on-the-producer-side-6","title":"Interface on the producer side (#6)","text":"TL;DRKeeping interfaces on the client side avoids unnecessary abstractions.
Interfaces are satisfied implicitly in Go, which tends to be a gamechanger compared to languages with an explicit implementation. In most cases, the approach to follow is similar to what we described in the previous section: abstractions should be discovered, not created. This means that it\u2019s not up to the producer to force a given abstraction for all the clients. Instead, it\u2019s up to the client to decide whether it needs some form of abstraction and then determine the best abstraction level for its needs.
An interface should live on the consumer side in most cases. However, in particular contexts (for example, when we know\u2014not foresee\u2014that an abstraction will be helpful for consumers), we may want to have it on the producer side. If we do, we should strive to keep it as minimal as possible, increasing its reusability potential and making it more easily composable.
Source code
"},{"location":"#returning-interfaces-7","title":"Returning interfaces (#7)","text":"TL;DRTo prevent being restricted in terms of flexibility, a function shouldn\u2019t return interfaces but concrete implementations in most cases. Conversely, a function should accept interfaces whenever possible.
In most cases, we shouldn\u2019t return interfaces but concrete implementations. Otherwise, it can make our design more complex due to package dependencies and can restrict flexibility because all the clients would have to rely on the same abstraction. Again, the conclusion is similar to the previous sections: if we know (not foresee) that an abstraction will be helpful for clients, we can consider returning an interface. Otherwise, we shouldn\u2019t force abstractions; they should be discovered by clients. If a client needs to abstract an implementation for whatever reason, it can still do that on the client\u2019s side.
"},{"location":"#any-says-nothing-8","title":"any
says nothing (#8)","text":"TL;DR Only use any
if you need to accept or return any possible type, such as json.Marshal
. Otherwise, any
doesn\u2019t provide meaningful information and can lead to compile-time issues by allowing a caller to call methods with any data type.
The any
type can be helpful if there is a genuine need for accepting or returning any possible type (for instance, when it comes to marshaling or formatting). In general, we should avoid overgeneralizing the code we write at all costs. Perhaps a little bit of duplicated code might occasionally be better if it improves other aspects such as code expressiveness.
Source code
"},{"location":"#being-confused-about-when-to-use-generics-9","title":"Being confused about when to use generics (#9)","text":"TL;DRRelying on generics and type parameters can prevent writing boilerplate code to factor out elements or behaviors. However, do not use type parameters prematurely, but only when you see a concrete need for them. Otherwise, they introduce unnecessary abstractions and complexity.
Read the full section here.
Source code
"},{"location":"#not-being-aware-of-the-possible-problems-with-type-embedding-10","title":"Not being aware of the possible problems with type embedding (#10)","text":"TL;DRUsing type embedding can also help avoid boilerplate code; however, ensure that doing so doesn\u2019t lead to visibility issues where some fields should have remained hidden.
When creating a struct, Go offers the option to embed types. But this can sometimes lead to unexpected behaviors if we don\u2019t understand all the implications of type embedding. Throughout this section, we look at how to embed types, what these bring, and the possible issues.
In Go, a struct field is called embedded if it\u2019s declared without a name. For example,
type Foo struct {\nBar // Embedded field\n}\ntype Bar struct {\nBaz int\n}\n
In the Foo
struct, the Bar
type is declared without an associated name; hence, it\u2019s an embedded field.
We use embedding to promote the fields and methods of an embedded type. Because Bar contains a Baz field, this field is promoted to Foo
. Therefore, Baz becomes available from Foo.
What can we say about type embedding? First, let\u2019s note that it\u2019s rarely a necessity, and it means that whatever the use case, we can probably solve it as well without type embedding. Type embedding is mainly used for convenience: in most cases, to promote behaviors.
If we decide to use type embedding, we need to keep two main constraints in mind:
Foo.Baz()
instead of Foo.Bar.Baz()
). If this is the only rationale, let\u2019s not embed the inner type and use a field instead.Using type embedding consciously by keeping these constraints in mind can help avoid boilerplate code with additional forwarding methods. However, let\u2019s make sure we don\u2019t do it solely for cosmetics and not promote elements that should remain hidden.
Source code
"},{"location":"#not-using-the-functional-options-pattern-11","title":"Not using the functional options pattern (#11)","text":"TL;DRTo handle options conveniently and in an API-friendly manner, use the functional options pattern.
Although there are different implementations with minor variations, the main idea is as follows:
type Option func(options *options)
error. For example, WithPort
accepts an int
argument that represents the port and returns an Option
type that represents how to update the options
struct.type options struct {\nport *int\n}\ntype Option func(options *options) error\nfunc WithPort(port int) Option {\nreturn func(options *options) error {\nif port < 0 {\nreturn errors.New(\"port should be positive\")\n}\noptions.port = &port\nreturn nil\n}\n}\nfunc NewServer(addr string, opts ...Option) ( *http.Server, error) { <1>\nvar options options <2>\nfor _, opt := range opts { <3>\nerr := opt(&options) <4>\nif err != nil {\nreturn nil, err\n}\n}\n// At this stage, the options struct is built and contains the config\n// Therefore, we can implement our logic related to port configuration\nvar port int\nif options.port == nil {\nport = defaultHTTPPort\n} else {\nif *options.port == 0 {\nport = randomPort()\n} else {\nport = *options.port\n}\n}\n// ...\n}\n
The functional options pattern provides a handy and API-friendly way to handle options. Although the builder pattern can be a valid option, it has some minor downsides (having to pass a config struct that can be empty or a less handy way to handle error management) that tend to make the functional options pattern the idiomatic way to deal with these kind of problems in Go.
Source code
"},{"location":"#project-misorganization-project-structure-and-package-organization-12","title":"Project misorganization (project structure and package organization) (#12)","text":"Regarding the overall organization, there are different schools of thought. For example, should we organize our application by context or by layer? It depends on our preferences. We may favor grouping code per context (such as the customer context, the contract context, etc.), or we may favor following hexagonal architecture principles and group per technical layer. If the decision we make fits our use case, it cannot be a wrong decision, as long as we remain consistent with it.
Regarding packages, there are multiple best practices that we should follow. First, we should avoid premature packaging because it might cause us to overcomplicate a project. Sometimes, it\u2019s better to use a simple organization and have our project evolve when we understand what it contains rather than forcing ourselves to make the perfect structure up front. Granularity is another essential thing to consider. We should avoid having dozens of nano packages containing only one or two files. If we do, it\u2019s because we have probably missed some logical connections across these packages, making our project harder for readers to understand. Conversely, we should also avoid huge packages that dilute the meaning of a package name.
Package naming should also be considered with care. As we all know (as developers), naming is hard. To help clients understand a Go project, we should name our packages after what they provide, not what they contain. Also, naming should be meaningful. Therefore, a package name should be short, concise, expressive, and, by convention, a single lowercase word.
Regarding what to export, the rule is pretty straightforward. We should minimize what should be exported as much as possible to reduce the coupling between packages and keep unnecessary exported elements hidden. If we are unsure whether to export an element or not, we should default to not exporting it. Later, if we discover that we need to export it, we can adjust our code. Let\u2019s also keep in mind some exceptions, such as making fields exported so that a struct can be unmarshaled with encoding/json.
Organizing a project isn\u2019t straightforward, but following these rules should help make it easier to maintain. However, remember that consistency is also vital to ease maintainability. Therefore, let\u2019s make sure that we keep things as consistent as possible within a codebase.
NoteIn 2023, the Go team has published an official guideline for organizing / structuring a Go project: go.dev/doc/modules/layout
"},{"location":"#creating-utility-packages-13","title":"Creating utility packages (#13)","text":"TL;DRNaming is a critical piece of application design. Creating packages such as common
, util
, and shared
doesn\u2019t bring much value for the reader. Refactor such packages into meaningful and specific package names.
Also, bear in mind that naming a package after what it provides and not what it contains can be an efficient way to increase its expressiveness.
Source code
"},{"location":"#ignoring-package-name-collisions-14","title":"Ignoring package name collisions (#14)","text":"TL;DRTo avoid naming collisions between variables and packages, leading to confusion or perhaps even bugs, use unique names for each one. If this isn\u2019t feasible, use an import alias to change the qualifier to differentiate the package name from the variable name, or think of a better name.
Package collisions occur when a variable name collides with an existing package name, preventing the package from being reused. We should prevent variable name collisions to avoid ambiguity. If we face a collision, we should either find another meaningful name or use an import alias.
"},{"location":"#missing-code-documentation-15","title":"Missing code documentation (#15)","text":"TL;DRTo help clients and maintainers understand your code\u2019s purpose, document exported elements.
Documentation is an important aspect of coding. It simplifies how clients can consume an API but can also help in maintaining a project. In Go, we should follow some rules to make our code idiomatic:
First, every exported element must be documented. Whether it is a structure, an interface, a function, or something else, if it\u2019s exported, it must be documented. The convention is to add comments, starting with the name of the exported element.
As a convention, each comment should be a complete sentence that ends with punctuation. Also bear in mind that when we document a function (or a method), we should highlight what the function intends to do, not how it does it; this belongs to the core of a function and comments, not documentation. Furthermore, the documentation should ideally provide enough information that the consumer does not have to look at our code to understand how to use an exported element.
When it comes to documenting a variable or a constant, we might be interested in conveying two aspects: its purpose and its content. The former should live as code documentation to be useful for external clients. The latter, though, shouldn\u2019t necessarily be public.
To help clients and maintainers understand a package\u2019s scope, we should also document each package. The convention is to start the comment with // Package
followed by the package name. The first line of a package comment should be concise. That\u2019s because it will appear in the package. Then, we can provide all the information we need in the following lines.
Documenting our code shouldn\u2019t be a constraint. We should take the opportunity to make sure it helps clients and maintainers to understand the purpose of our code.
"},{"location":"#not-using-linters-16","title":"Not using linters (#16)","text":"TL;DRTo improve code quality and consistency, use linters and formatters.
A linter is an automatic tool to analyze code and catch errors. The scope of this section isn\u2019t to give an exhaustive list of the existing linters; otherwise, it will become deprecated pretty quickly. But we should understand and remember why linters are essential for most Go projects.
However, if you\u2019re not a regular user of linters, here is a list that you may want to use daily:
Linters and formatters are a powerful way to improve the quality and consistency of our codebase. Let\u2019s take the time to understand which one we should use and make sure we automate their execution (such as a CI or Git precommit hook).
"},{"location":"#data-types","title":"Data Types","text":""},{"location":"#creating-confusion-with-octal-literals-17","title":"Creating confusion with octal literals (#17)","text":"TL;DRWhen reading existing code, bear in mind that integer literals starting with 0
are octal numbers. Also, to improve readability, make octal integers explicit by prefixing them with 0o
.
Octal numbers start with a 0 (e.g., 010
is equal to 8 in base 10). To improve readability and avoid potential mistakes for future code readers, we should make octal numbers explicit using the 0o
prefix (e.g., 0o10
).
We should also note the other integer literal representations:
0b
or 0B
prefix (for example, 0b100
is equal to 4 in base 10)0x
or 0X
prefix (for example, 0xF
is equal to 15 in base 10)i
suffix (for example, 3i
)We can also use an underscore character (_) as a separator for readability. For example, we can write 1 billion this way: 1_000_000_000
. We can also use the underscore character with other representations (for example, 0b00_00_01
).
Source code
"},{"location":"#neglecting-integer-overflows-18","title":"Neglecting integer overflows (#18)","text":"TL;DRBecause integer overflows and underflows are handled silently in Go, you can implement your own functions to catch them.
In Go, an integer overflow that can be detected at compile time generates a compilation error. For example,
var counter int32 = math.MaxInt32 + 1\n
constant 2147483648 overflows int32\n
However, at run time, an integer overflow or underflow is silent; this does not lead to an application panic. It is essential to keep this behavior in mind, because it can lead to sneaky bugs (for example, an integer increment or addition of positive integers that leads to a negative result).
Source code
"},{"location":"#not-understanding-floating-points-19","title":"Not understanding floating-points (#19)","text":"TL;DRMaking floating-point comparisons within a given delta can ensure that your code is portable. When performing addition or subtraction, group the operations with a similar order of magnitude to favor accuracy. Also, perform multiplication and division before addition and subtraction.
In Go, there are two floating-point types (if we omit imaginary numbers): float32 and float64. The concept of a floating point was invented to solve the major problem with integers: their inability to represent fractional values. To avoid bad surprises, we need to know that floating-point arithmetic is an approximation of real arithmetic.
For that, we\u2019ll look at a multiplication example:
var n float32 = 1.0001\nfmt.Println(n * n)\n
We may expect this code to print the result of 1.0001 * 1.0001 = 1.00020001, right? However, running it on most x86 processors prints 1.0002, instead.
Because Go\u2019s float32
and float64
types are approximations, we have to bear a few rules in mind:
Source code
"},{"location":"#not-understanding-slice-length-and-capacity-20","title":"Not understanding slice length and capacity (#20)","text":"TL;DRUnderstanding the difference between slice length and capacity should be part of a Go developer\u2019s core knowledge. The slice length is the number of available elements in the slice, whereas the slice capacity is the number of elements in the backing array.
Read the full section here.
Source code
"},{"location":"#inefficient-slice-initialization-21","title":"Inefficient slice initialization (#21)","text":"TL;DRWhen creating a slice, initialize it with a given length or capacity if its length is already known. This reduces the number of allocations and improves performance.
While initializing a slice using make
, we can provide a length and an optional capacity. Forgetting to pass an appropriate value for both of these parameters when it makes sense is a widespread mistake. Indeed, it can lead to multiple copies and additional effort for the GC to clean the temporary backing arrays. Performance-wise, there\u2019s no good reason not to give the Go runtime a helping hand.
Our options are to allocate a slice with either a given capacity or a given length. Of these two solutions, we have seen that the second tends to be slightly faster. But using a given capacity and append can be easier to implement and read in some contexts.
Source code
"},{"location":"#being-confused-about-nil-vs-empty-slice-22","title":"Being confused about nil vs. empty slice (#22)","text":"TL;DRTo prevent common confusions such as when using the encoding/json
or the reflect
package, you need to understand the difference between nil and empty slices. Both are zero-length, zero-capacity slices, but only a nil slice doesn\u2019t require allocation.
In Go, there is a distinction between nil and empty slices. A nil slice is equals to nil
, whereas an empty slice has a length of zero. A nil slice is empty, but an empty slice isn\u2019t necessarily nil
. Meanwhile, a nil slice doesn\u2019t require any allocation. We have seen throughout this section how to initialize a slice depending on the context by using
var s []string
if we aren\u2019t sure about the final length and the slice can be empty[]string(nil)
as syntactic sugar to create a nil and empty slicemake([]string, length)
if the future length is knownThe last option, []string{}
, should be avoided if we initialize the slice without elements. Finally, let\u2019s check whether the libraries we use make the distinctions between nil and empty slices to prevent unexpected behaviors.
Source code
"},{"location":"#not-properly-checking-if-a-slice-is-empty-23","title":"Not properly checking if a slice is empty (#23)","text":"TL;DRTo check if a slice doesn\u2019t contain any element, check its length. This check works regardless of whether the slice is nil
or empty. The same goes for maps. To design unambiguous APIs, you shouldn\u2019t distinguish between nil and empty slices.
To determine whether a slice has elements, we can either do it by checking if the slice is nil or if its length is equal to 0. Checking the length is the best option to follow as it will cover both if the slice is empty or is the slice is nil.
Meanwhile, when designing interfaces, we should avoid distinguishing nil and empty slices, which leads to subtle programming errors. When returning slices, it should make neither a semantic nor a technical difference if we return a nil or empty slice. Both should mean the same thing for the callers. This principle is the same with maps. To check if a map is empty, check its length, not whether it\u2019s nil.
Source code
"},{"location":"#not-making-slice-copies-correctly-24","title":"Not making slice copies correctly (#24)","text":"TL;DRTo copy one slice to another using the copy
built-in function, remember that the number of copied elements corresponds to the minimum between the two slice\u2019s lengths.
Copying elements from one slice to another is a reasonably frequent operation. When using copy, we must recall that the number of elements copied to the destination corresponds to the minimum between the two slices\u2019 lengths. Also bear in mind that other alternatives exist to copy a slice, so we shouldn\u2019t be surprised if we find them in a codebase.
Source code
"},{"location":"#unexpected-side-effects-using-slice-append-25","title":"Unexpected side effects using slice append (#25)","text":"TL;DRUsing copy or the full slice expression is a way to prevent append
from creating conflicts if two different functions use slices backed by the same array. However, only a slice copy prevents memory leaks if you want to shrink a large slice.
When using slicing, we must remember that we can face a situation leading to unintended side effects. If the resulting slice has a length smaller than its capacity, append can mutate the original slice. If we want to restrict the range of possible side effects, we can use either a slice copy or the full slice expression, which prevents us from doing a copy.
Notes[low:high:max]
(full slice expression): This statement creates a slice similar to the one created with s[low:high]
, except that the resulting slice\u2019s capacity is equal to max - low
.
Source code
"},{"location":"#slices-and-memory-leaks-26","title":"Slices and memory leaks (#26)","text":"TL;DRWorking with a slice of pointers or structs with pointer fields, you can avoid memory leaks by marking as nil the elements excluded by a slicing operation.
"},{"location":"#leaking-capacity","title":"Leaking capacity","text":"Remember that slicing a large slice or array can lead to potential high memory consumption. The remaining space won\u2019t be reclaimed by the GC, and we can keep a large backing array despite using only a few elements. Using a slice copy is the solution to prevent such a case.
Source code
"},{"location":"#slice-and-pointers","title":"Slice and pointers","text":"When we use the slicing operation with pointers or structs with pointer fields, we need to know that the GC won\u2019t reclaim these elements. In that case, the two options are to either perform a copy or explicitly mark the remaining elements or their fields to nil
.
Source code
"},{"location":"#inefficient-map-initialization-27","title":"Inefficient map initialization (#27)","text":"TL;DRWhen creating a map, initialize it with a given length if its length is already known. This reduces the number of allocations and improves performance.
A map provides an unordered collection of key-value pairs in which all the keys are distinct. In Go, a map is based on the hash table data structure. Internally, a hash table is an array of buckets, and each bucket is a pointer to an array of key-value pairs.
If we know up front the number of elements a map will contain, we should create it by providing an initial size. Doing this avoids potential map growth, which is quite heavy computation-wise because it requires reallocating enough space and rebalancing all the elements.
Source code
"},{"location":"#maps-and-memory-leaks-28","title":"Maps and memory leaks (#28)","text":"TL;DRA map can always grow in memory, but it never shrinks. Hence, if it leads to some memory issues, you can try different options, such as forcing Go to recreate the map or using pointers.
Read the full section here.
Source code
"},{"location":"#comparing-values-incorrectly-29","title":"Comparing values incorrectly (#29)","text":"TL;DRTo compare types in Go, you can use the == and != operators if two types are comparable: Booleans, numerals, strings, pointers, channels, and structs are composed entirely of comparable types. Otherwise, you can either use reflect.DeepEqual
and pay the price of reflection or use custom implementations and libraries.
It\u2019s essential to understand how to use ==
and !=
to make comparisons effectively. We can use these operators on operands that are comparable:
We can also use the ?
, >=
, <
, and >
operators with numeric types to compare values and with strings to compare their lexical order.
If operands are not comparable (e.g., slices and maps), we have to use other options such as reflection. Reflection is a form of metaprogramming, and it refers to the ability of an application to introspect and modify its structure and behavior. For example, in Go, we can use reflect.DeepEqual
. This function reports whether two elements are deeply equal by recursively traversing two values. The elements it accepts are basic types plus arrays, structs, slices, maps, pointers, interfaces, and functions. Yet, the main catch is the performance penalty.
If performance is crucial at run time, implementing our custom method might be the best solution. One additional note: we must remember that the standard library has some existing comparison methods. For example, we can use the optimized bytes.Compare
function to compare two slices of bytes. Before implementing a custom method, we need to make sure we don\u2019t reinvent the wheel.
Source code
"},{"location":"#control-structures","title":"Control Structures","text":""},{"location":"#ignoring-that-elements-are-copied-in-range-loops-30","title":"Ignoring that elements are copied inrange
loops (#30)","text":"TL;DR The value element in a range
loop is a copy. Therefore, to mutate a struct, for example, access it via its index or via a classic for
loop (unless the element or the field you want to modify is a pointer).
A range loop allows iterating over different data structures:
Compared to a classic for loop
, a range
loop is a convenient way to iterate over all the elements of one of these data structures, thanks to its concise syntax.
Yet, we should remember that the value element in a range loop is a copy. Therefore, if the value is a struct we need to mutate, we will only update the copy, not the element itself, unless the value or field we modify is a pointer. The favored options are to access the element via the index using a range loop or a classic for loop.
Source code
"},{"location":"#ignoring-how-arguments-are-evaluated-in-range-loops-channels-and-arrays-31","title":"Ignoring how arguments are evaluated inrange
loops (channels and arrays) (#31)","text":"TL;DR Understanding that the expression passed to the range
operator is evaluated only once before the beginning of the loop can help you avoid common mistakes such as inefficient assignment in channel or slice iteration.
The range loop evaluates the provided expression only once, before the beginning of the loop, by doing a copy (regardless of the type). We should remember this behavior to avoid common mistakes that might, for example, lead us to access the wrong element. For example:
a := [3]int{0, 1, 2}\nfor i, v := range a {\na[2] = 10\nif i == 2 {\nfmt.Println(v)\n}\n}\n
This code updates the last index to 10. However, if we run this code, it does not print 10; it prints 2.
Source code
"},{"location":"#ignoring-the-impacts-of-using-pointer-elements-in-range-loops-32","title":"Ignoring the impacts of using pointer elements inrange
loops (#32)","text":"TL;DR Using a local variable or accessing an element using an index, you can prevent mistakes while copying pointers inside a loop.
When iterating over a data structure using a range
loop, we must recall that all the values are assigned to a unique variable with a single unique address. Therefore, if we store a pointer referencing this variable during each iteration, we will end up in a situation where we store the same pointer referencing the same element: the latest one. We can overcome this issue by forcing the creation of a local variable in the loop\u2019s scope or creating a pointer referencing a slice element via its index. Both solutions are fine.
Source code
"},{"location":"#making-wrong-assumptions-during-map-iterations-ordering-and-map-insert-during-iteration-33","title":"Making wrong assumptions during map iterations (ordering and map insert during iteration) (#33)","text":"TL;DRTo ensure predictable outputs when using maps, remember that a map data structure:
Source code
"},{"location":"#ignoring-how-the-break-statement-works-34","title":"Ignoring how thebreak
statement works (#34)","text":"TL;DR Using break
or continue
with a label enforces breaking a specific statement. This can be helpful with switch
or select
statements inside loops.
A break statement is commonly used to terminate the execution of a loop. When loops are used in conjunction with switch or select, developers frequently make the mistake of breaking the wrong statement. For example:
for i := 0; i < 5; i++ {\nfmt.Printf(\"%d \", i)\nswitch i {\ndefault:\ncase 2:\nbreak\n}\n}\n
The break statement doesn\u2019t terminate the for
loop: it terminates the switch
statement, instead. Hence, instead of iterating from 0 to 2, this code iterates from 0 to 4: 0 1 2 3 4
.
One essential rule to keep in mind is that a break
statement terminates the execution of the innermost for
, switch
, or select
statement. In the previous example, it terminates the switch
statement.
To break the loop instead of the switch
statement, the most idiomatic way is to use a label:
loop:\nfor i := 0; i < 5; i++ {\nfmt.Printf(\"%d \", i)\nswitch i {\ndefault:\ncase 2:\nbreak loop\n}\n}\n
Here, we associate the loop
label with the for
loop. Then, because we provide the loop
label to the break
statement, it breaks the loop, not the switch. Therefore, this new version will print 0 1 2
, as we expected.
Source code
"},{"location":"#using-defer-inside-a-loop-35","title":"Usingdefer
inside a loop (#35)","text":"TL;DR Extracting loop logic inside a function leads to executing a defer
statement at the end of each iteration.
The defer
statement delays a call\u2019s execution until the surrounding function returns. It\u2019s mainly used to reduce boilerplate code. For example, if a resource has to be closed eventually, we can use defer
to avoid repeating the closure calls before every single return
.
One common mistake with defer
is to forget that it schedules a function call when the surrounding function returns. For example:
func readFiles(ch <-chan string) error {\nfor path := range ch {\nfile, err := os.Open(path)\nif err != nil {\nreturn err\n}\ndefer file.Close()\n// Do something with file\n}\nreturn nil\n}\n
The defer
calls are executed not during each loop iteration but when the readFiles
function returns. If readFiles
doesn\u2019t return, the file descriptors will be kept open forever, causing leaks.
One common option to fix this problem is to create a surrounding function after defer
, called during each iteration:
func readFiles(ch <-chan string) error {\nfor path := range ch {\nif err := readFile(path); err != nil {\nreturn err\n}\n}\nreturn nil\n}\nfunc readFile(path string) error {\nfile, err := os.Open(path)\nif err != nil {\nreturn err\n}\ndefer file.Close()\n// Do something with file\nreturn nil\n}\n
Another solution is to make the readFile
function a closure but intrinsically, this remains the same solution: adding another surrounding function to execute the defer
calls during each iteration.
Source code
"},{"location":"#strings","title":"Strings","text":""},{"location":"#not-understanding-the-concept-of-rune-36","title":"Not understanding the concept of rune (#36)","text":"TL;DRUnderstanding that a rune corresponds to the concept of a Unicode code point and that it can be composed of multiple bytes should be part of the Go developer\u2019s core knowledge to work accurately with strings.
As runes are everywhere in Go, it's important to understand the following:
rune
corresponds to the concept of a Unicode code point, meaning an item represented by a single value.len()
on a string in Go returns the number of bytes, not the number of runes.Source code
"},{"location":"#inaccurate-string-iteration-37","title":"Inaccurate string iteration (#37)","text":"TL;DRIterating on a string with the range
operator iterates on the runes with the index corresponding to the starting index of the rune\u2019s byte sequence. To access a specific rune index (such as the third rune), convert the string into a []rune
.
Iterating on a string is a common operation for developers. Perhaps we want to perform an operation for each rune in the string or implement a custom function to search for a specific substring. In both cases, we have to iterate on the different runes of a string. But it\u2019s easy to get confused about how iteration works.
For example, consider the following example:
s := \"h\u00eallo\"\nfor i := range s {\nfmt.Printf(\"position %d: %c\\n\", i, s[i])\n}\nfmt.Printf(\"len=%d\\n\", len(s))\n
position 0: h\nposition 1: \u00c3\nposition 3: l\nposition 4: l\nposition 5: o\nlen=6\n
Let's highlight three points that might be confusing:
Let\u2019s start with the last observation. We already mentioned that len returns the number of bytes in a string, not the number of runes. Because we assigned a string literal to s
, s
is a UTF-8 string. Meanwhile, the special character \"\u00ea\" isn\u2019t encoded in a single byte; it requires 2 bytes. Therefore, calling len(s)
returns 6.
Meanwhile, in the previous example, we have to understand that we don't iterate over each rune; instead, we iterate over each starting index of a rune:
Printing s[i]
doesn\u2019t print the ith rune; it prints the UTF-8 representation of the byte at index i
. Hence, we printed \"h\u00c3llo\" instead of \"h\u00eallo\".
If we want to print all the different runes, we can either use the value element of the range
operator:
s := \"h\u00eallo\"\nfor i, r := range s {\nfmt.Printf(\"position %d: %c\\n\", i, r)\n}\n
Or, we can convert the string into a slice of runes and iterate over it:
s := \"h\u00eallo\"\nrunes := []rune(s)\nfor i, r := range runes {\nfmt.Printf(\"position %d: %c\\n\", i, r)\n}\n
Note that this solution introduces a run-time overhead compared to the previous one. Indeed, converting a string into a slice of runes requires allocating an additional slice and converting the bytes into runes: an O(n) time complexity with n the number of bytes in the string. Therefore, if we want to iterate over all the runes, we should use the first solution.
However, if we want to access the ith rune of a string with the first option, we don\u2019t have access to the rune index; rather, we know the starting index of a rune in the byte sequence.
s := \"h\u00eallo\"\nr := []rune(s)[4]\nfmt.Printf(\"%c\\n\", r) // o\n
Source code
"},{"location":"#misusing-trim-functions-38","title":"Misusing trim functions (#38)","text":"TL;DRstrings.TrimRight
/strings.TrimLeft
removes all the trailing/leading runes contained in a given set, whereas strings.TrimSuffix
/strings.TrimPrefix
returns a string without a provided suffix/prefix.
For example:
fmt.Println(strings.TrimRight(\"123oxo\", \"xo\"))\n
The example prints 123:
Conversely, strings.TrimLeft
removes all the leading runes contained in a set.
On the other side, strings.TrimSuffix
/ strings.TrimPrefix
returns a string without the provided trailing suffix / prefix.
Source code
"},{"location":"#under-optimized-strings-concatenation-39","title":"Under-optimized strings concatenation (#39)","text":"TL;DRConcatenating a list of strings should be done with strings.Builder
to prevent allocating a new string during each iteration.
Let\u2019s consider a concat
function that concatenates all the string elements of a slice using the +=
operator:
func concat(values []string) string {\ns := \"\"\nfor _, value := range values {\ns += value\n}\nreturn s\n}\n
During each iteration, the +=
operator concatenates s
with the value string. At first sight, this function may not look wrong. But with this implementation, we forget one of the core characteristics of a string: its immutability. Therefore, each iteration doesn\u2019t update s
; it reallocates a new string in memory, which significantly impacts the performance of this function.
Fortunately, there is a solution to deal with this problem, using strings.Builder
:
func concat(values []string) string {\nsb := strings.Builder{}\nfor _, value := range values {\n_, _ = sb.WriteString(value)\n}\nreturn sb.String()\n}\n
During each iteration, we constructed the resulting string by calling the WriteString
method that appends the content of value to its internal buffer, hence minimizing memory copying.
WriteString
returns an error as the second output, but we purposely ignore it. Indeed, this method will never return a non-nil error. So what\u2019s the purpose of this method returning an error as part of its signature? strings.Builder
implements the io.StringWriter
interface, which contains a single method: WriteString(s string) (n int, err error)
. Hence, to comply with this interface, WriteString
must return an error.
Internally, strings.Builder
holds a byte slice. Each call to WriteString
results in a call to append on this slice. There are two impacts. First, this struct shouldn\u2019t be used concurrently, as the calls to append
would lead to race conditions. The second impact is something that we saw in mistake #21, \"Inefficient slice initialization\": if the future length of a slice is already known, we should preallocate it. For that purpose, strings.Builder
exposes a method Grow(n int)
to guarantee space for another n
bytes:
func concat(values []string) string {\ntotal := 0\nfor i := 0; i < len(values); i++ {\ntotal += len(values[i])\n}\nsb := strings.Builder{}\nsb.Grow(total) (2)\nfor _, value := range values {\n_, _ = sb.WriteString(value)\n}\nreturn sb.String()\n}\n
Let\u2019s run a benchmark to compare the three versions (v1 using +=
; v2 using strings.Builder{}
without preallocation; and v3 using strings.Builder{}
with preallocation). The input slice contains 1,000 strings, and each string contains 1,000 bytes:
BenchmarkConcatV1-4 16 72291485 ns/op\nBenchmarkConcatV2-4 1188 878962 ns/op\nBenchmarkConcatV3-4 5922 190340 ns/op\n
As we can see, the latest version is by far the most efficient: 99% faster than v1 and 78% faster than v2.
strings.Builder
is the recommended solution to concatenate a list of strings. Usually, this solution should be used within a loop. Indeed, if we just have to concatenate a few strings (such as a name and a surname), using strings.Builder
is not recommended as doing so will make the code a bit less readable than using the +=
operator or fmt.Sprintf
.
Source code
"},{"location":"#useless-string-conversions-40","title":"Useless string conversions (#40)","text":"TL;DRRemembering that the bytes
package offers the same operations as the strings
package can help avoid extra byte/string conversions.
When choosing to work with a string or a []byte
, most programmers tend to favor strings for convenience. But most I/O is actually done with []byte
. For example, io.Reader
, io.Writer
, and io.ReadAll
work with []byte
, not strings.
When we\u2019re wondering whether we should work with strings or []byte
, let\u2019s recall that working with []byte
isn\u2019t necessarily less convenient. Indeed, all the exported functions of the strings package also have alternatives in the bytes
package: Split
, Count
, Contains
, Index
, and so on. Hence, whether we\u2019re doing I/O or not, we should first check whether we could implement a whole workflow using bytes instead of strings and avoid the price of additional conversions.
Source code
"},{"location":"#substring-and-memory-leaks-41","title":"Substring and memory leaks (#41)","text":"TL;DRUsing copies instead of substrings can prevent memory leaks, as the string returned by a substring operation will be backed by the same byte array.
In mistake #26, \u201cSlices and memory leaks,\u201d we saw how slicing a slice or array may lead to memory leak situations. This principle also applies to string and substring operations.
We need to keep two things in mind while using the substring operation in Go. First, the interval provided is based on the number of bytes, not the number of runes. Second, a substring operation may lead to a memory leak as the resulting substring will share the same backing array as the initial string. The solutions to prevent this case from happening are to perform a string copy manually or to use strings.Clone
from Go 1.18.
Source code
"},{"location":"#functions-and-methods","title":"Functions and Methods","text":""},{"location":"#not-knowing-which-type-of-receiver-to-use-42","title":"Not knowing which type of receiver to use (#42)","text":"TL;DRThe decision whether to use a value or a pointer receiver should be made based on factors such as the type, whether it has to be mutated, whether it contains a field that can\u2019t be copied, and how large the object is. When in doubt, use a pointer receiver.
Choosing between value and pointer receivers isn\u2019t always straightforward. Let\u2019s discuss some of the conditions to help us choose.
A receiver must be a pointer
type slice []int\nfunc (s *slice) add(element int) {\n*s = append(*s, element)\n}\n
A receiver should be a pointer
A receiver must be a value
A receiver should be a value
time.Time
.int
, float64
, or string
.Of course, it\u2019s impossible to be exhaustive, as there will always be edge cases, but this section\u2019s goal was to provide guidance to cover most cases. By default, we can choose to go with a value receiver unless there\u2019s a good reason not to do so. In doubt, we should use a pointer receiver.
Source code
"},{"location":"#never-using-named-result-parameters-43","title":"Never using named result parameters (#43)","text":"TL;DRUsing named result parameters can be an efficient way to improve the readability of a function/method, especially if multiple result parameters have the same type. In some cases, this approach can also be convenient because named result parameters are initialized to their zero value. But be cautious about potential side effects.
When we return parameters in a function or a method, we can attach names to these parameters and use them as regular variables. When a result parameter is named, it\u2019s initialized to its zero value when the function/method begins. With named result parameters, we can also call a naked return statement (without arguments). In that case, the current values of the result parameters are used as the returned values.
Here\u2019s an example that uses a named result parameter b
:
func f(a int) (b int) {\nb = a\nreturn\n}\n
In this example, we attach a name to the result parameter: b
. When we call return without arguments, it returns the current value of b
.
In some cases, named result parameters can also increase readability: for example, if two parameters have the same type. In other cases, they can also be used for convenience. Therefore, we should use named result parameters sparingly when there\u2019s a clear benefit.
Source code
"},{"location":"#unintended-side-effects-with-named-result-parameters-44","title":"Unintended side effects with named result parameters (#44)","text":"TL;DRSee #43.
We mentioned why named result parameters can be useful in some situations. But as these result parameters are initialized to their zero value, using them can sometimes lead to subtle bugs if we\u2019re not careful enough. For example, can you spot what\u2019s wrong with this code?
func (l loc) getCoordinates(ctx context.Context, address string) (\nlat, lng float32, err error) {\nisValid := l.validateAddress(address) (1)\nif !isValid {\nreturn 0, 0, errors.New(\"invalid address\")\n}\nif ctx.Err() != nil { (2)\nreturn 0, 0, err\n}\n// Get and return coordinates\n}\n
The error might not be obvious at first glance. Here, the error returned in the if ctx.Err() != nil
scope is err
. But we haven\u2019t assigned any value to the err
variable. It\u2019s still assigned to the zero value of an error
type: nil
. Hence, this code will always return a nil error.
When using named result parameters, we must recall that each parameter is initialized to its zero value. As we have seen in this section, this can lead to subtle bugs that aren\u2019t always straightforward to spot while reading code. Therefore, let\u2019s remain cautious when using named result parameters, to avoid potential side effects.
Source code
"},{"location":"#returning-a-nil-receiver-45","title":"Returning a nil receiver (#45)","text":"TL;DRWhen returning an interface, be cautious about not returning a nil pointer but an explicit nil value. Otherwise, unintended consequences may occur and the caller will receive a non-nil value.
Source code
"},{"location":"#using-a-filename-as-a-function-input-46","title":"Using a filename as a function input (#46)","text":"TL;DRDesigning functions to receive io.Reader
types instead of filenames improves the reusability of a function and makes testing easier.
Accepting a filename as a function input to read from a file should, in most cases, be considered a code smell (except in specific functions such as os.Open
). Indeed, it makes unit tests more complex because we may have to create multiple files. It also reduces the reusability of a function (although not all functions are meant to be reused). Using the io.Reader
interface abstracts the data source. Regardless of whether the input is a file, a string, an HTTP request, or a gRPC request, the implementation can be reused and easily tested.
Source code
"},{"location":"#ignoring-how-defer-arguments-and-receivers-are-evaluated-argument-evaluation-pointer-and-value-receivers-47","title":"Ignoring howdefer
arguments and receivers are evaluated (argument evaluation, pointer, and value receivers) (#47)","text":"TL;DR Passing a pointer to a defer
function and wrapping a call inside a closure are two possible solutions to overcome the immediate evaluation of arguments and receivers.
In a defer
function the arguments are evaluated right away, not once the surrounding function returns. For example, in this code, we always call notify
and incrementCounter
with the same status: an empty string.
const (\nStatusSuccess = \"success\"\nStatusErrorFoo = \"error_foo\"\nStatusErrorBar = \"error_bar\"\n)\nfunc f() error {\nvar status string\ndefer notify(status)\ndefer incrementCounter(status)\nif err := foo(); err != nil {\nstatus = StatusErrorFoo\nreturn err\n}\nif err := bar(); err != nil {\nstatus = StatusErrorBar\nreturn err\n}\nstatus = StatusSuccess <5>\nreturn nil\n}\n
Indeed, we call notify(status)
and incrementCounter(status)
as defer
functions. Therefore, Go will delay these calls to be executed once f
returns with the current value of status at the stage we used defer, hence passing an empty string.
Two leading options if we want to keep using defer
.
The first solution is to pass a string pointer:
func f() error {\nvar status string\ndefer notify(&status) defer incrementCounter(&status)\n// The rest of the function unchanged\n}\n
Using defer
evaluates the arguments right away: here, the address of status. Yes, status itself is modified throughout the function, but its address remains constant, regardless of the assignments. Hence, if notify
or incrementCounter
uses the value referenced by the string pointer, it will work as expected. But this solution requires changing the signature of the two functions, which may not always be possible.
There\u2019s another solution: calling a closure (an anonymous function value that references variables from outside its body) as a defer
statement:
func f() error {\nvar status string\ndefer func() {\nnotify(status)\nincrementCounter(status)\n}()\n// The rest of the function unchanged\n}\n
Here, we wrap the calls to both notify
and incrementCounter
within a closure. This closure references the status variable from outside its body. Therefore, status
is evaluated once the closure is executed, not when we call defer
. This solution also works and doesn\u2019t require notify
and incrementCounter
to change their signature.
Let's also note this behavior applies with method receiver: the receiver is evaluated immediately.
Source code
"},{"location":"#error-management","title":"Error Management","text":""},{"location":"#panicking-48","title":"Panicking (#48)","text":"TL;DRUsing panic
is an option to deal with errors in Go. However, it should only be used sparingly in unrecoverable conditions: for example, to signal a programmer error or when you fail to load a mandatory dependency.
In Go, panic is a built-in function that stops the ordinary flow:
func main() {\nfmt.Println(\"a\")\npanic(\"foo\")\nfmt.Println(\"b\")\n}\n
This code prints a and then stops before printing b:
a\npanic: foo\n\ngoroutine 1 [running]:\nmain.main()\n main.go:7 +0xb3\n
Panicking in Go should be used sparingly. There are two prominent cases, one to signal a programmer error (e.g., sql.Register
that panics if the driver is nil
or has already been register) and another where our application fails to create a mandatory dependency. Hence, exceptional conditions that lead us to stop the application. In most other cases, error management should be done with a function that returns a proper error type as the last return argument.
Source code
"},{"location":"#ignoring-when-to-wrap-an-error-49","title":"Ignoring when to wrap an error (#49)","text":"TL;DRWrapping an error allows you to mark an error and/or provide additional context. However, error wrapping creates potential coupling as it makes the source error available for the caller. If you want to prevent that, don\u2019t use error wrapping.
Since Go 1.13, the %w directive allows us to wrap errors conveniently. Error wrapping is about wrapping or packing an error inside a wrapper container that also makes the source error available. In general, the two main use cases for error wrapping are the following:
When handling an error, we can decide to wrap it. Wrapping is about adding additional context to an error and/or marking an error as a specific type. If we need to mark an error, we should create a custom error type. However, if we just want to add extra context, we should use fmt.Errorf with the %w directive as it doesn\u2019t require creating a new error type. Yet, error wrapping creates potential coupling as it makes the source error available for the caller. If we want to prevent it, we shouldn\u2019t use error wrapping but error transformation, for example, using fmt.Errorf with the %v directive.
Source code
"},{"location":"#comparing-an-error-type-inaccurately-50","title":"Comparing an error type inaccurately (#50)","text":"TL;DRIf you use Go 1.13 error wrapping with the %w
directive and fmt.Errorf
, comparing an error against a type has to be done using errors.As
. Otherwise, if the returned error you want to check is wrapped, it will fail the checks.
Source code
"},{"location":"#comparing-an-error-value-inaccurately-51","title":"Comparing an error value inaccurately (#51)","text":"TL;DRIf you use Go 1.13 error wrapping with the %w
directive and fmt.Errorf
, comparing an error against or a value has to be done using errors.As
. Otherwise, if the returned error you want to check is wrapped, it will fail the checks.
A sentinel error is an error defined as a global variable:
import \"errors\"\nvar ErrFoo = errors.New(\"foo\")\n
In general, the convention is to start with Err
followed by the error type: here, ErrFoo
. A sentinel error conveys an expected error, an error that clients will expect to check. As general guidelines:
var ErrFoo = errors.New(\"foo\")
.type BarError struct { ... }
, with BarError
implementing the error
interface.If we use error wrapping in our application with the %w
directive and fmt.Errorf
, checking an error against a specific value should be done using errors.Is
instead of ==
. Thus, even if the sentinel error is wrapped, errors.Is
can recursively unwrap it and compare each error in the chain against the provided value.
Source code
"},{"location":"#handling-an-error-twice-52","title":"Handling an error twice (#52)","text":"TL;DRIn most situations, an error should be handled only once. Logging an error is handling an error. Therefore, you have to choose between logging or returning an error. In many cases, error wrapping is the solution as it allows you to provide additional context to an error and return the source error.
Handling an error multiple times is a mistake made frequently by developers, not specifically in Go. This can cause situations where the same error is logged multiple times make debugging harder.
Let's remind us that handling an error should be done only once. Logging an error is handling an error. Hence, we should either log or return an error. By doing this, we simplify our code and gain better insights into the error situation. Using error wrapping is the most convenient approach as it allows us to propagate the source error and add context to an error.
Source code
"},{"location":"#not-handling-an-error-53","title":"Not handling an error (#53)","text":"TL;DRIgnoring an error, whether during a function call or in a defer
function, should be done explicitly using the blank identifier. Otherwise, future readers may be confused about whether it was intentional or a miss.
Source code
"},{"location":"#not-handling-defer-errors-54","title":"Not handlingdefer
errors (#54)","text":"TL;DR In many cases, you shouldn\u2019t ignore an error returned by a defer
function. Either handle it directly or propagate it to the caller, depending on the context. If you want to ignore it, use the blank identifier.
Consider the following code:
func f() {\n// ...\nnotify() // Error handling is omitted\n}\nfunc notify() error {\n// ...\n}\n
From a maintainability perspective, the code can lead to some issues. Let\u2019s consider a new reader looking at it. This reader notices that notify returns an error but that the error isn\u2019t handled by the parent function. How can they guess whether or not handling the error was intentional? How can they know whether the previous developer forgot to handle it or did it purposely?
For these reasons, when we want to ignore an error, there's only one way to do it, using the blank identifier (_
):
_ = notify\n
In terms of compilation and run time, this approach doesn\u2019t change anything compared to the first piece of code. But this new version makes explicit that we aren\u2019t interested in the error. Also, we can add a comment that indicates the rationale for why an error is ignored:
// At-most once delivery.\n// Hence, it's accepted to miss some of them in case of errors.\n_ = notify()\n
Source code
"},{"location":"#concurrency-foundations","title":"Concurrency: Foundations","text":""},{"location":"#mixing-up-concurrency-and-parallelism-55","title":"Mixing up concurrency and parallelism (#55)","text":"TL;DRUnderstanding the fundamental differences between concurrency and parallelism is a cornerstone of the Go developer\u2019s knowledge. Concurrency is about structure, whereas parallelism is about execution.
Concurrency and parallelism are not the same:
In summary, concurrency provides a structure to solve a problem with parts that may be parallelized. Therefore, concurrency enables parallelism.
"},{"location":"#thinking-concurrency-is-always-faster-56","title":"Thinking concurrency is always faster (#56)","text":"TL;DRTo be a proficient developer, you must acknowledge that concurrency isn\u2019t always faster. Solutions involving parallelization of minimal workloads may not necessarily be faster than a sequential implementation. Benchmarking sequential versus concurrent solutions should be the way to validate assumptions.
Read the full section here.
Source code
"},{"location":"#being-puzzled-about-when-to-use-channels-or-mutexes-57","title":"Being puzzled about when to use channels or mutexes (#57)","text":"TL;DRBeing aware of goroutine interactions can also be helpful when deciding between channels and mutexes. In general, parallel goroutines require synchronization and hence mutexes. Conversely, concurrent goroutines generally require coordination and orchestration and hence channels.
Given a concurrency problem, it may not always be clear whether we can implement a solution using channels or mutexes. Because Go promotes sharing memory by communication, one mistake could be to always force the use of channels, regardless of the use case. However, we should see the two options as complementary.
When should we use channels or mutexes? We will use the example in the next figure as a backbone. Our example has three different goroutines with specific relationships:
In general, parallel goroutines have to synchronize: for example, when they need to access or mutate a shared resource such as a slice. Synchronization is enforced with mutexes but not with any channel types (not with buffered channels). Hence, in general, synchronization between parallel goroutines should be achieved via mutexes.
Conversely, in general, concurrent goroutines have to coordinate and orchestrate. For example, if G3 needs to aggregate results from both G1 and G2, G1 and G2 need to signal to G3 that a new intermediate result is available. This coordination falls under the scope of communication\u2014therefore, channels.
Regarding concurrent goroutines, there\u2019s also the case where we want to transfer the ownership of a resource from one step (G1 and G2) to another (G3); for example, if G1 and G2 are enriching a shared resource and at some point, we consider this job as complete. Here, we should use channels to signal that a specific resource is ready and handle the ownership transfer.
Mutexes and channels have different semantics. Whenever we want to share a state or access a shared resource, mutexes ensure exclusive access to this resource. Conversely, channels are a mechanic for signaling with or without data (chan struct{}
or not). Coordination or ownership transfer should be achieved via channels. It\u2019s important to know whether goroutines are parallel or concurrent because, in general, we need mutexes for parallel goroutines and channels for concurrent ones.
Being proficient in concurrency also means understanding that data races and race conditions are different concepts. Data races occur when multiple goroutines simultaneously access the same memory location and at least one of them is writing. Meanwhile, being data-race-free doesn\u2019t necessarily mean deterministic execution. When a behavior depends on the sequence or the timing of events that can\u2019t be controlled, this is a race condition.
Race problems can be among the hardest and most insidious bugs a programmer can face. As Go developers, we must understand crucial aspects such as data races and race conditions, their possible impacts, and how to avoid them.
"},{"location":"#data-race","title":"Data Race","text":"A data race occurs when two or more goroutines simultaneously access the same memory location and at least one is writing. In this case, the result can be hazardous. Even worse, in some situations, the memory location may end up holding a value containing a meaningless combination of bits.
We can prevent a data race from happening using different techniques. For example:
sync/atomic
packageDepending on the operation we want to perform, does a data-race-free application necessarily mean a deterministic result? Not necessarily.
A race condition occurs when the behavior depends on the sequence or the timing of events that can\u2019t be controlled. Here, the timing of events is the goroutines\u2019 execution order.
In summary, when we work in concurrent applications, it\u2019s essential to understand that a data race is different from a race condition. A data race occurs when multiple goroutines simultaneously access the same memory location and at least one of them is writing. A data race means unexpected behavior. However, a data-race-free application doesn\u2019t necessarily mean deterministic results. An application can be free of data races but still have behavior that depends on uncontrolled events (such as goroutine execution, how fast a message is published to a channel, or how long a call to a database lasts); this is a race condition. Understanding both concepts is crucial to becoming proficient in designing concurrent applications.
Source code
"},{"location":"#not-understanding-the-concurrency-impacts-of-a-workload-type-59","title":"Not understanding the concurrency impacts of a workload type (#59)","text":"TL;DRWhen creating a certain number of goroutines, consider the workload type. Creating CPU-bound goroutines means bounding this number close to the GOMAXPROCS
variable (based by default on the number of CPU cores on the host). Creating I/O-bound goroutines depends on other factors, such as the external system.
In programming, the execution time of a workload is limited by one of the following:
The last is the rarest nowadays, given that memory has become very cheap in recent decades. Hence, this section focuses on the two first workload types: CPU- and I/O-bound.
If the workload executed by the workers is I/O-bound, the value mainly depends on the external system. Conversely, if the workload is CPU-bound, the optimal number of goroutines is close to the number of available CPU cores (a best practice can be to use runtime.GOMAXPROCS
). Knowing the workload type (I/O or CPU) is crucial when designing concurrent applications.
Source code
"},{"location":"#misunderstanding-go-contexts-60","title":"Misunderstanding Go contexts (#60)","text":"TL;DRGo contexts are also one of the cornerstones of concurrency in Go. A context allows you to carry a deadline, a cancellation signal, and/or a list of keys-values.
https://pkg.go.dev/context
A Context carries a deadline, a cancellation signal, and other values across API boundaries.
"},{"location":"#deadline","title":"Deadline","text":"A deadline refers to a specific point in time determined with one of the following:
time.Duration
from now (for example, in 250 ms)time.Time
(for example, 2023-02-07 00:00:00 UTC)The semantics of a deadline convey that an ongoing activity should be stopped if this deadline is met. An activity is, for example, an I/O request or a goroutine waiting to receive a message from a channel.
"},{"location":"#cancellation-signals","title":"Cancellation signals","text":"Another use case for Go contexts is to carry a cancellation signal. Let\u2019s imagine that we want to create an application that calls CreateFileWatcher(ctx context.Context, filename string)
within another goroutine. This function creates a specific file watcher that keeps reading from a file and catches updates. When the provided context expires or is canceled, this function handles it to close the file descriptor.
The last use case for Go contexts is to carry a key-value list. What\u2019s the point of having a context carrying a key-value list? Because Go contexts are generic and mainstream, there are infinite use cases.
For example, if we use tracing, we may want different subfunctions to share the same correlation ID. Some developers may consider this ID too invasive to be part of the function signature. In this regard, we could also decide to include it as part of the provided context.
"},{"location":"#catching-a-context-cancellation","title":"Catching a context cancellation","text":"The context.Context
type exports a Done
method that returns a receive-only notification channel: <-chan struct{}
. This channel is closed when the work associated with the context should be canceled. For example,
context.WithCancel
is closed when the cancel function is called.context.WithDeadline
is closed when the deadline has expired.One thing to note is that the internal channel should be closed when a context is canceled or has met a deadline, instead of when it receives a specific value, because the closure of a channel is the only channel action that all the consumer goroutines will receive. This way, all the consumers will be notified once a context is canceled or a deadline is reached.
In summary, to be a proficient Go developer, we have to understand what a context is and how to use it. In general, a function that users wait for should take a context, as doing so allows upstream callers to decide when calling this function should be aborted.
Source code
"},{"location":"#concurrency-practice","title":"Concurrency: Practice","text":""},{"location":"#propagating-an-inappropriate-context-61","title":"Propagating an inappropriate context (#61)","text":"TL;DRUnderstanding the conditions when a context can be canceled should matter when propagating it: for example, an HTTP handler canceling the context when the response has been sent.
In many situations, it is recommended to propagate Go contexts. However, context propagation can sometimes lead to subtle bugs, preventing subfunctions from being correctly executed.
Let\u2019s consider the following example. We expose an HTTP handler that performs some tasks and returns a response. But just before returning the response, we also want to send it to a Kafka topic. We don\u2019t want to penalize the HTTP consumer latency-wise, so we want the publish action to be handled asynchronously within a new goroutine. We assume that we have at our disposal a publish
function that accepts a context so the action of publishing a message can be interrupted if the context is canceled, for example. Here is a possible implementation:
func handler(w http.ResponseWriter, r *http.Request) {\nresponse, err := doSomeTask(r.Context(), r)\nif err != nil {\nhttp.Error(w, err.Error(), http.StatusInternalServerError)\nreturn\n}\ngo func() {\nerr := publish(r.Context(), response)\n// Do something with err\n}()\nwriteResponse(response)\n}\n
What\u2019s wrong with this piece of code? We have to know that the context attached to an HTTP request can cancel in different conditions:
In the first two cases, we probably handle things correctly. For example, if we get a response from doSomeTask but the client has closed the connection, it\u2019s probably OK to call publish with a context already canceled so the message isn\u2019t published. But what about the last case?
When the response has been written to the client, the context associated with the request will be canceled. Therefore, we are facing a race condition:
In the latter case, calling publish will return an error because we returned the HTTP response quickly.
NoteFrom Go 1.21, there is a way to create a new context without cancel. context.WithoutCancel
returns a copy of parent that is not canceled when parent is canceled.
In summary, propagating a context should be done cautiously.
Source code
"},{"location":"#starting-a-goroutine-without-knowing-when-to-stop-it-62","title":"Starting a goroutine without knowing when to stop it (#62)","text":"TL;DRAvoiding leaks means being mindful that whenever a goroutine is started, you should have a plan to stop it eventually.
Goroutines are easy and cheap to start\u2014so easy and cheap that we may not necessarily have a plan for when to stop a new goroutine, which can lead to leaks. Not knowing when to stop a goroutine is a design issue and a common concurrency mistake in Go.
Let\u2019s discuss a concrete example. We will design an application that needs to watch some external configuration (for example, using a database connection). Here\u2019s a first implementation:
func main() {\nnewWatcher()\n// Run the application\n}\ntype watcher struct { /* Some resources */ }\nfunc newWatcher() {\nw := watcher{}\ngo w.watch() // Creates a goroutine that watches some external configuration\n}\n
The problem with this code is that when the main goroutine exits (perhaps because of an OS signal or because it has a finite workload), the application is stopped. Hence, the resources created by watcher aren\u2019t closed gracefully. How can we prevent this from happening?
One option could be to pass to newWatcher a context that will be canceled when main returns:
func main() {\nctx, cancel := context.WithCancel(context.Background())\ndefer cancel()\nnewWatcher(ctx)\n// Run the application\n}\nfunc newWatcher(ctx context.Context) {\nw := watcher{}\ngo w.watch(ctx)\n}\n
We propagate the context created to the watch method. When the context is canceled, the watcher struct should close its resources. However, can we guarantee that watch will have time to do so? Absolutely not\u2014and that\u2019s a design flaw.
The problem is that we used signaling to convey that a goroutine had to be stopped. We didn\u2019t block the parent goroutine until the resources had been closed. Let\u2019s make sure we do:
func main() {\nw := newWatcher()\ndefer w.close()\n// Run the application\n}\nfunc newWatcher() watcher {\nw := watcher{}\ngo w.watch()\nreturn w\n}\nfunc (w watcher) close() {\n// Close the resources\n}\n
Instead of signaling watcher
that it\u2019s time to close its resources, we now call this close
method, using defer
to guarantee that the resources are closed before the application exits.
In summary, let\u2019s be mindful that a goroutine is a resource like any other that must eventually be closed to free memory or other resources. Starting a goroutine without knowing when to stop it is a design issue. Whenever a goroutine is started, we should have a clear plan about when it will stop. Last but not least, if a goroutine creates resources and its lifetime is bound to the lifetime of the application, it\u2019s probably safer to wait for this goroutine to complete before exiting the application. This way, we can ensure that the resources can be freed.
Source code
"},{"location":"#not-being-careful-with-goroutines-and-loop-variables-63","title":"Not being careful with goroutines and loop variables (#63)","text":"WarningThis mistake isn't relevant anymore from Go 1.22 (source).
"},{"location":"#expecting-a-deterministic-behavior-using-select-and-channels-64","title":"Expecting a deterministic behavior using select and channels (#64)","text":"TL;DRUnderstanding that select
with multiple channels chooses the case randomly if multiple options are possible prevents making wrong assumptions that can lead to subtle concurrency bugs.
One common mistake made by Go developers while working with channels is to make wrong assumptions about how select behaves with multiple channels.
For example, let's consider the following case (disconnectCh
is a unbuffered channel):
go func() {\nfor i := 0; i < 10; i++ {\nmessageCh <- i\n}\ndisconnectCh <- struct{}{}\n}()\nfor {\nselect {\ncase v := <-messageCh:\nfmt.Println(v)\ncase <-disconnectCh:\nfmt.Println(\"disconnection, return\")\nreturn\n}\n}\n
If we run this example multiple times, the result will be random:
0\n1\n2\ndisconnection, return\n\n0\ndisconnection, return\n
Instead of consuming the 10 messages, we only received a few of them. What\u2019s the reason? It lies in the specification of the select statement with multiple channels (https:// go.dev/ref/spec):
Quote
If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection.
Unlike a switch statement, where the first case with a match wins, the select state- ment selects randomly if multiple options are possible.
This behavior might look odd at first, but there\u2019s a good reason for it: to prevent possible starvation. Suppose the first possible communication chosen is based on the source order. In that case, we may fall into a situation where, for example, we only receive from one channel because of a fast sender. To prevent this, the language designers decided to use a random selection.
When using select
with multiple channels, we must remember that if multiple options are possible, the first case in the source order does not automatically win. Instead, Go selects randomly, so there\u2019s no guarantee about which option will be chosen. To overcome this behavior, in the case of a single producer goroutine, we can use either unbuffered channels or a single channel.
Source code
"},{"location":"#not-using-notification-channels-65","title":"Not using notification channels (#65)","text":"TL;DRSend notifications using a chan struct{}
type.
Channels are a mechanism for communicating across goroutines via signaling. A signal can be either with or without data.
Let\u2019s look at a concrete example. We will create a channel that will notify us whenever a certain disconnection occurs. One idea is to handle it as a chan bool
:
disconnectCh := make(chan bool)\n
Now, let\u2019s say we interact with an API that provides us with such a channel. Because it\u2019s a channel of Booleans, we can receive either true
or false
messages. It\u2019s probably clear what true
conveys. But what does false
mean? Does it mean we haven\u2019t been disconnected? And in this case, how frequently will we receive such a signal? Does it mean we have reconnected? Should we even expect to receive false
? Perhaps we should only expect to receive true
messages.
If that\u2019s the case, meaning we don\u2019t need a specific value to convey some information, we need a channel without data. The idiomatic way to handle it is a channel of empty structs: chan struct{}
.
Using nil channels should be part of your concurrency toolset because it allows you to remove cases from select
statements, for example.
What should this code do?
var ch chan int\n<-ch\n
ch
is a chan int
type. The zero value of a channel being nil, ch
is nil
. The goroutine won\u2019t panic; however, it will block forever.
The principle is the same if we send a message to a nil channel. This goroutine blocks forever:
var ch chan int\nch <- 0\n
Then what\u2019s the purpose of Go allowing messages to be received from or sent to a nil channel? For example, we can use nil channels to implement an idiomatic way to merge two channels:
func merge(ch1, ch2 <-chan int) <-chan int {\nch := make(chan int, 1)\ngo func() {\nfor ch1 != nil || ch2 != nil { // Continue if at least one channel isn\u2019t nil\nselect {\ncase v, open := <-ch1:\nif !open {\nch1 = nil // Assign ch1 to a nil channel once closed\nbreak\n}\nch <- v\ncase v, open := <-ch2:\nif !open {\nch2 = nil // Assigns ch2 to a nil channel once closed\nbreak\n}\nch <- v\n}\n}\nclose(ch)\n}()\nreturn ch\n}\n
This elegant solution relies on nil channels to somehow remove one case from the select
statement.
Let\u2019s keep this idea in mind: nil channels are useful in some conditions and should be part of the Go developer\u2019s toolset when dealing with concurrent code.
Source code
"},{"location":"#being-puzzled-about-channel-size-67","title":"Being puzzled about channel size (#67)","text":"TL;DRCarefully decide on the right channel type to use, given a problem. Only unbuffered channels provide strong synchronization guarantees.
You should have a good reason to specify a channel size other than one for buffered channels.
"},{"location":"#forgetting-about-possible-side-effects-with-string-formatting-etcd-data-race-example-and-deadlock-68","title":"Forgetting about possible side effects with string formatting (etcd data race example and deadlock) (#68)","text":"TL;DRBeing aware that string formatting may lead to calling existing functions means watching out for possible deadlocks and other data races.
Source code
"},{"location":"#creating-data-races-with-append-69","title":"Creating data races with append (#69)","text":"TL;DRCalling append
isn\u2019t always data-race-free; hence, it shouldn\u2019t be used concurrently on a shared slice.
Source code
"},{"location":"#using-mutexes-inaccurately-with-slices-and-maps-70","title":"Using mutexes inaccurately with slices and maps (#70)","text":"TL;DRRemembering that slices and maps are pointers can prevent common data races.
Source code
"},{"location":"#misusing-syncwaitgroup-71","title":"Misusingsync.WaitGroup
(#71)","text":"TL;DR To accurately use sync.WaitGroup
, call the Add
method before spinning up goroutines.
Source code
"},{"location":"#forgetting-about-synccond-72","title":"Forgetting aboutsync.Cond
(#72)","text":"TL;DR You can send repeated notifications to multiple goroutines with sync.Cond
.
Source code
"},{"location":"#not-using-errgroup-73","title":"Not usingerrgroup
(#73)","text":"TL;DR You can synchronize a group of goroutines and handle errors and contexts with the errgroup
package.
Source code
"},{"location":"#copying-a-sync-type-74","title":"Copying async
type (#74)","text":"TL;DR sync
types shouldn\u2019t be copied.
Source code
"},{"location":"#standard-library","title":"Standard Library","text":""},{"location":"#providing-a-wrong-time-duration-75","title":"Providing a wrong time duration (#75)","text":"TL;DRRemain cautious with functions accepting a time.Duration
. Even though passing an integer is allowed, strive to use the time API to prevent any possible confusion.
Many common functions in the standard library accept a time.Duration
, which is an alias for the int64
type. However, one time.Duration
unit represents one nanosecond, instead of one millisecond, as commonly seen in other programming languages. As a result, passing numeric types instead of using the time.Duration
API can lead to unexpected behavior.
A developer with experience in other languages might assume that the following code creates a new time.Ticker
that delivers ticks every second, given the value 1000
:
ticker := time.NewTicker(1000)\nfor {\nselect {\ncase <-ticker.C:\n// Do something\n}\n}\n
However, because 1,000 time.Duration
units = 1,000 nanoseconds, ticks are delivered every 1,000 nanoseconds = 1 microsecond, not every second as assumed.
We should always use the time.Duration
API to avoid confusion and unexpected behavior:
ticker = time.NewTicker(time.Microsecond)\n// Or\nticker = time.NewTicker(1000 * time.Nanosecond)\n
Source code
"},{"location":"#timeafter-and-memory-leaks-76","title":"time.After
and memory leaks (#76)","text":"TL;DR Avoiding calls to time.After
in repeated functions (such as loops or HTTP handlers) can avoid peak memory consumption. The resources created by time.After
are released only when the timer expires.
Source code
"},{"location":"#json-handling-common-mistakes-77","title":"JSON handling common mistakes (#77)","text":"Be careful about using embedded fields in Go structs. Doing so may lead to sneaky bugs like an embedded time.Time field implementing the json.Marshaler
interface, hence overriding the default marshaling behavior.
Source code
When comparing two time.Time
structs, recall that time.Time
contains both a wall clock and a monotonic clock, and the comparison using the == operator is done on both clocks.
Source code
any
To avoid wrong assumptions when you provide a map while unmarshaling JSON data, remember that numerics are converted to float64
by default.
Source code
"},{"location":"#common-sql-mistakes-78","title":"Common SQL mistakes (#78)","text":"sql.Open
doesn't necessarily establish connections to a databaseCall the Ping
or PingContext
method if you need to test your configuration and make sure a database is reachable.
Source code
Configure the database connection parameters for production-grade applications.
Using SQL prepared statements makes queries more efficient and more secure.
Source code
Deal with nullable columns in tables using pointers or sql.NullXXX
types.
Source code
Call the Err
method of sql.Rows
after row iterations to ensure that you haven\u2019t missed an error while preparing the next row.
Source code
"},{"location":"#not-closing-transient-resources-http-body-sqlrows-and-osfile-79","title":"Not closing transient resources (HTTP body,sql.Rows
, and os.File
) (#79)","text":"TL;DR Eventually close all structs implementing io.Closer
to avoid possible leaks.
Source code
"},{"location":"#forgetting-the-return-statement-after-replying-to-an-http-request-80","title":"Forgetting the return statement after replying to an HTTP request (#80)","text":"TL;DRTo avoid unexpected behaviors in HTTP handler implementations, make sure you don\u2019t miss the return
statement if you want a handler to stop after http.Error
.
Source code
"},{"location":"#using-the-default-http-client-and-server-81","title":"Using the default HTTP client and server (#81)","text":"TL;DRFor production-grade applications, don\u2019t use the default HTTP client and server implementations. These implementations are missing timeouts and behaviors that should be mandatory in production.
Source code
"},{"location":"#testing","title":"Testing","text":""},{"location":"#not-categorizing-tests-build-tags-environment-variables-and-short-mode-82","title":"Not categorizing tests (build tags, environment variables, and short mode) (#82)","text":"TL;DRCategorizing tests using build flags, environment variables, or short mode makes the testing process more efficient. You can create test categories using build flags or environment variables (for example, unit versus integration tests) and differentiate short from long-running tests to decide which kinds of tests to execute.
Source code
"},{"location":"#not-enabling-the-race-flag-83","title":"Not enabling the race flag (#83)","text":"TL;DREnabling the -race
flag is highly recommended when writing concurrent applications. Doing so allows you to catch potential data races that can lead to software bugs.
Using the -parallel
flag is an efficient way to speed up tests, especially long-running ones. Use the -shuffle
flag to help ensure that a test suite doesn\u2019t rely on wrong assumptions that could hide bugs.
Table-driven tests are an efficient way to group a set of similar tests to prevent code duplication and make future updates easier to handle.
Source code
"},{"location":"#sleeping-in-unit-tests-86","title":"Sleeping in unit tests (#86)","text":"TL;DRAvoid sleeps using synchronization to make a test less flaky and more robust. If synchronization isn\u2019t possible, consider a retry approach.
Source code
"},{"location":"#not-dealing-with-the-time-api-efficiently-87","title":"Not dealing with the time API efficiently (#87)","text":"TL;DRUnderstanding how to deal with functions using the time API is another way to make a test less flaky. You can use standard techniques such as handling the time as part of a hidden dependency or asking clients to provide it.
Source code
"},{"location":"#not-using-testing-utility-packages-httptest-and-iotest-88","title":"Not using testing utility packages (httptest
and iotest
) (#88)","text":"httptest
package is helpful for dealing with HTTP applications. It provides a set of utilities to test both clients and servers.Source code
iotest
package helps write io.Reader and test that an application is tolerant to errors.Source code
"},{"location":"#writing-inaccurate-benchmarks-89","title":"Writing inaccurate benchmarks (#89)","text":"TL;DRRegarding benchmarks:
Read the full section here.
Source code
"},{"location":"#not-exploring-all-the-go-testing-features-90","title":"Not exploring all the Go testing features (#90)","text":"Use code coverage with the -coverprofile
flag to quickly see which part of the code needs more attention.
Place unit tests in a different package to enforce writing tests that focus on an exposed behavior, not internals.
Source code
Handling errors using the *testing.T
variable instead of the classic if err != nil
makes code shorter and easier to read.
Source code
You can use setup and teardown functions to configure a complex environment, such as in the case of integration tests.
Source code
"},{"location":"#not-using-fuzzing-community-mistake","title":"Not using fuzzing (community mistake)","text":"TL;DRFuzzing is an efficient strategy to detect random, unexpected, or malformed inputs to complex functions and methods in order to discover vulnerabilities, bugs, or even potential crashes.
Credits: @jeromedoucet
"},{"location":"#optimizations","title":"Optimizations","text":""},{"location":"#not-understanding-cpu-caches-91","title":"Not understanding CPU caches (#91)","text":"Understanding how to use CPU caches is important for optimizing CPU-bound applications because the L1 cache is about 50 to 100 times faster than the main memory.
Being conscious of the cache line concept is critical to understanding how to organize data in data-intensive applications. A CPU doesn\u2019t fetch memory word by word; instead, it usually copies a memory block to a 64-byte cache line. To get the most out of each individual cache line, enforce spatial locality.
Source code
Source code
Making code predictable for the CPU can also be an efficient way to optimize certain functions. For example, a unit or constant stride is predictable for the CPU, but a non-unit stride (for example, a linked list) isn\u2019t predictable.
Source code
To avoid a critical stride, hence utilizing only a tiny portion of the cache, be aware that caches are partitioned.
"},{"location":"#writing-concurrent-code-that-leads-to-false-sharing-92","title":"Writing concurrent code that leads to false sharing (#92)","text":"TL;DRKnowing that lower levels of CPU caches aren\u2019t shared across all the cores helps avoid performance-degrading patterns such as false sharing while writing concurrency code. Sharing memory is an illusion.
Source code
"},{"location":"#not-taking-into-account-instruction-level-parallelism-93","title":"Not taking into account instruction-level parallelism (#93)","text":"TL;DRUse instruction-level parallelism (ILP) to optimize specific parts of your code to allow a CPU to execute as many parallel instructions as possible. Identifying data hazards is one of the main steps.
Source code
"},{"location":"#not-being-aware-of-data-alignment-94","title":"Not being aware of data alignment (#94)","text":"TL;DRYou can avoid common mistakes by remembering that in Go, basic types are aligned with their own size. For example, keep in mind that reorganizing the fields of a struct by size in descending order can lead to more compact structs (less memory allocation and potentially a better spatial locality).
Source code
"},{"location":"#not-understanding-stack-vs-heap-95","title":"Not understanding stack vs. heap (#95)","text":"TL;DRUnderstanding the fundamental differences between heap and stack should also be part of your core knowledge when optimizing a Go application. Stack allocations are almost free, whereas heap allocations are slower and rely on the GC to clean the memory.
Source code
"},{"location":"#not-knowing-how-to-reduce-allocations-api-change-compiler-optimizations-and-syncpool-96","title":"Not knowing how to reduce allocations (API change, compiler optimizations, andsync.Pool
) (#96)","text":"TL;DR Reducing allocations is also an essential aspect of optimizing a Go application. This can be done in different ways, such as designing the API carefully to prevent sharing up, understanding the common Go compiler optimizations, and using sync.Pool
.
Source code
"},{"location":"#not-relying-on-inlining-97","title":"Not relying on inlining (#97)","text":"TL;DRUse the fast-path inlining technique to efficiently reduce the amortized time to call a function.
"},{"location":"#not-using-go-diagnostics-tooling-98","title":"Not using Go diagnostics tooling (#98)","text":"TL;DRRely on profiling and the execution tracer to understand how an application performs and the parts to optimize.
Read the full section here.
"},{"location":"#not-understanding-how-the-gc-works-99","title":"Not understanding how the GC works (#99)","text":"TL;DRUnderstanding how to tune the GC can lead to multiple benefits such as handling sudden load increases more efficiently.
"},{"location":"#not-understanding-the-impacts-of-running-go-in-docker-and-kubernetes-100","title":"Not understanding the impacts of running Go in Docker and Kubernetes (#100)","text":"TL;DRTo help avoid CPU throttling when deployed in Docker and Kubernetes, keep in mind that Go isn\u2019t CFS-aware.
"},{"location":"20-slice/","title":"Not understanding slice length and capacity","text":"It\u2019s pretty common for Go developers to mix slice length and capacity or not understand them thoroughly. Assimilating these two concepts is essential for efficiently handling core operations such as slice initialization and adding elements with append, copying, or slicing. This misunderstanding can lead to using slices suboptimally or even to memory leaks.
In Go, a slice is backed by an array. That means the slice\u2019s data is stored contiguously in an array data structure. A slice also handles the logic of adding an element if the backing array is full or shrinking the backing array if it\u2019s almost empty.
Internally, a slice holds a pointer to the backing array plus a length and a capacity. The length is the number of elements the slice contains, whereas the capacity is the number of elements in the backing array, counting from the first element in the slice. Let\u2019s go through a few examples to make things clearer. First, let\u2019s initialize a slice with a given length and capacity:
s := make([]int, 3, 6) // Three-length, six-capacity slice\n
The first argument, representing the length, is mandatory. However, the second argument representing the capacity is optional. Figure 1 shows the result of this code in memory.
Figure 1: A three-length, six-capacity slice.
In this case, make
creates an array of six elements (the capacity). But because the length was set to 3, Go initializes only the first three elements. Also, because the slice is an []int
type, the first three elements are initialized to the zeroed value of an int
: 0. The grayed elements are allocated but not yet used.
If we print this slice, we get the elements within the range of the length, [0 0 0]
. If we set s[1]
to 1, the second element of the slice updates without impacting its length or capacity. Figure 2 illustrates this.
Figure 2: Updating the slice\u2019s second element: s[1] = 1.
However, accessing an element outside the length range is forbidden, even though it\u2019s already allocated in memory. For example, s[4]
= 0 would lead to the following panic:
panic: runtime error: index out of range [4] with length 3\n
How can we use the remaining space of the slice? By using the append
built-in function:
s = append(s, 2)\n
This code appends to the existing s
slice a new element. It uses the first grayed element (which was allocated but not yet used) to store element 2, as figure 3 shows.
Figure 3: Appending an element to s.
The length of the slice is updated from 3 to 4 because the slice now contains four elements. Now, what happens if we add three more elements so that the backing array isn\u2019t large enough?
s = append(s, 3, 4, 5)\nfmt.Println(s)\n
If we run this code, we see that the slice was able to cope with our request:
[0 1 0 2 3 4 5]\n
Because an array is a fixed-size structure, it can store the new elements until element 4. When we want to insert element 5, the array is already full: Go internally creates another array by doubling the capacity, copying all the elements, and then inserting element 5. Figure 4 shows this process.
Figure 4: Because the initial backing array is full, Go creates another array and copies all the elements.
The slice now references the new backing array. What will happen to the previous backing array? If it\u2019s no longer referenced, it\u2019s eventually freed by the garbage collector (GC) if allocated on the heap. (We discuss heap memory in mistake #95, \u201cNot understanding stack vs. heap,\u201d and we look at how the GC works in mistake #99, \u201cNot understanding how the GC works.\u201d)
What happens with slicing? Slicing is an operation done on an array or a slice, providing a half-open range; the first index is included, whereas the second is excluded. The following example shows the impact, and figure 5 displays the result in memory:
s1 := make([]int, 3, 6) // Three-length, six-capacity slice\ns2 := s1[1:3] // Slicing from indices 1 to 3\n
Figure 5: The slices s1 and s2 reference the same backing array with different lengths and capacities.
First, s1
is created as a three-length, six-capacity slice. When s2
is created by slicing s1
, both slices reference the same backing array. However, s2
starts from a different index, 1. Therefore, its length and capacity (a two-length, five-capacity slice) differ from s1. If we update s1[1]
or s2[0]
, the change is made to the same array, hence, visible in both slices, as figure 6 shows.
Figure 6: Because s1 and s2 are backed by the same array, updating a common element makes the change visible in both slices.
Now, what happens if we append an element to s2
? Does the following code change s1
as well?
s2 = append(s2, 2)\n
The shared backing array is modified, but only the length of s2
changes. Figure 7 shows the result of appending an element to s2
.
Figure 7: Appending an element to s2.
s1
remains a three-length, six-capacity slice. Therefore, if we print s1
and s2
, the added element is only visible for s2
:
s1=[0 1 0], s2=[1 0 2]\n
It\u2019s important to understand this behavior so that we don\u2019t make wrong assumptions while using append.
NoteIn these examples, the backing array is internal and not available directly to the Go developer. The only exception is when a slice is created from slicing an existing array.
One last thing to note: what if we keep appending elements to s2
until the backing array is full? What will the state be, memory-wise? Let\u2019s add three more elements so that the backing array will not have enough capacity:
s2 = append(s2, 3)\ns2 = append(s2, 4) // At this stage, the backing is already full\ns2 = append(s2, 5)\n
This code leads to creating another backing array. Figure 8 displays the results in memory.
Figure 8: Appending elements to s2 until the backing array is full.
s1
and s2
now reference two different arrays. As s1
is still a three-length, six-capacity slice, it still has some available buffer, so it keeps referencing the initial array. Also, the new backing array was made by copying the initial one from the first index of s2
. That\u2019s why the new array starts with element 1, not 0.
To summarize, the slice length is the number of available elements in the slice, whereas the slice capacity is the number of elements in the backing array. Adding an element to a full slice (length == capacity) leads to creating a new backing array with a new capacity, copying all the elements from the previous array, and updating the slice pointer to the new array.
"},{"location":"28-maps-memory-leaks/","title":"Maps and memory leaks","text":"When working with maps in Go, we need to understand some important characteristics of how a map grows and shrinks. Let\u2019s delve into this to prevent issues that can cause memory leaks.
First, to view a concrete example of this problem, let\u2019s design a scenario where we will work with the following map:
m := make(map[int][128]byte)\n
Each value of m is an array of 128 bytes. We will do the following:
After each step, we want to print the size of the heap (using a printAlloc
utility function). This shows us how this example behaves memory-wise:
func main() {\nn := 1_000_000\nm := make(map[int][128]byte)\nprintAlloc()\nfor i := 0; i < n; i++ { // Adds 1 million elements\nm[i] = [128]byte{}\n}\nprintAlloc()\nfor i := 0; i < n; i++ { // Deletes 1 million elements\ndelete(m, i)\n}\nruntime.GC() // Triggers a manual GC\nprintAlloc()\nruntime.KeepAlive(m) // Keeps a reference to m so that the map isn\u2019t collected\n}\nfunc printAlloc() {\nvar m runtime.MemStats\nruntime.ReadMemStats(&m)\nfmt.Printf(\"%d KB\\n\", m.Alloc/1024)\n}\n
We allocate an empty map, add 1 million elements, remove 1 million elements, and then run a GC. We also make sure to keep a reference to the map using runtime.KeepAlive
so that the map isn\u2019t collected as well. Let\u2019s run this example:
0 MB <-- After m is allocated\n461 MB <-- After we add 1 million elements\n293 MB <-- After we remove 1 million elements\n
What can we observe? At first, the heap size is minimal. Then it grows significantly after having added 1 million elements to the map. But if we expected the heap size to decrease after removing all the elements, this isn\u2019t how maps work in Go. In the end, even though the GC has collected all the elements, the heap size is still 293 MB. So the memory shrunk, but not as we might have expected. What\u2019s the rationale? We need to delve into how a map works in Go.
A map provides an unordered collection of key-value pairs in which all the keys are distinct. In Go, a map is based on the hash table data structure: an array where each element is a pointer to a bucket of key-value pairs, as shown in figure 1.
Figure 1: A hash table example with a focus on bucket 0.
Each bucket is a fixed-size array of eight elements. In the case of an insertion into a bucket that is already full (a bucket overflow), Go creates another bucket of eight elements and links the previous one to it. Figure 2 shows an example:
Figure 2: In case of a bucket overflow, Go allocates a new bucket and links the previous bucket to it.
Under the hood, a Go map is a pointer to a runtime.hmap struct. This struct contains multiple fields, including a B field, giving the number of buckets in the map:
type hmap struct {\nB uint8 // log_2 of # of buckets\n// (can hold up to loadFactor * 2^B items)\n// ...\n}\n
After adding 1 million elements, the value of B equals 18, which means 2\u00b9\u2078 = 262,144 buckets. When we remove 1 million elements, what\u2019s the value of B? Still 18. Hence, the map still contains the same number of buckets.
The reason is that the number of buckets in a map cannot shrink. Therefore, removing elements from a map doesn\u2019t impact the number of existing buckets; it just zeroes the slots in the buckets. A map can only grow and have more buckets; it never shrinks.
In the previous example, we went from 461 MB to 293 MB because the elements were collected, but running the GC didn\u2019t impact the map itself. Even the number of extra buckets (the buckets created because of overflows) remains the same.
Let\u2019s take a step back and discuss when the fact that a map cannot shrink can be a problem. Imagine building a cache using a map[int][128]byte
. This map holds per customer ID (the int
), a sequence of 128 bytes. Now, suppose we want to save the last 1,000 customers. The map size will remain constant, so we shouldn\u2019t worry about the fact that a map cannot shrink.
However, let\u2019s say we want to store one hour of data. Meanwhile, our company has decided to have a big promotion for Black Friday: in one hour, we may have millions of customers connected to our system. But a few days after Black Friday, our map will contain the same number of buckets as during the peak time. This explains why we can experience high memory consumption that doesn\u2019t significantly decrease in such a scenario.
What are the solutions if we don\u2019t want to manually restart our service to clean the amount of memory consumed by the map? One solution could be to re-create a copy of the current map at a regular pace. For example, every hour, we can build a new map, copy all the elements, and release the previous one. The main drawback of this option is that following the copy and until the next garbage collection, we may consume twice the current memory for a short period.
Another solution would be to change the map type to store an array pointer: map[int]*[128]byte
. It doesn\u2019t solve the fact that we will have a significant number of buckets; however, each bucket entry will reserve the size of a pointer for the value instead of 128 bytes (8 bytes on 64-bit systems and 4 bytes on 32-bit systems).
Coming back to the original scenario, let\u2019s compare the memory consumption for each map type following each step. The following table shows the comparison.
Stepmap[int][128]byte
map[int]*[128]byte
Allocate an empty map 0 MB 0 MB Add 1 million elements 461 MB 182 MB Remove all the elements and run a GC 293 MB 38 MB Note If a key or a value is over 128 bytes, Go won\u2019t store it directly in the map bucket. Instead, Go stores a pointer to reference the key or the value.
As we have seen, adding n elements to a map and then deleting all the elements means keeping the same number of buckets in memory. So, we must remember that because a Go map can only grow in size, so does its memory consumption. There is no automated strategy to shrink it. If this leads to high memory consumption, we can try different options such as forcing Go to re-create the map or using pointers to check if it can be optimized.
"},{"location":"56-concurrency-faster/","title":"Thinking concurrency is always faster","text":"A misconception among many developers is believing that a concurrent solution is always faster than a sequential one. This couldn\u2019t be more wrong. The overall performance of a solution depends on many factors, such as the efficiency of our code structure (concurrency), which parts can be tackled in parallel, and the level of contention among the computation units. This post reminds us about some fundamental knowledge of concurrency in Go; then we will see a concrete example where a concurrent solution isn\u2019t necessarily faster.
"},{"location":"56-concurrency-faster/#go-scheduling","title":"Go Scheduling","text":"A thread is the smallest unit of processing that an OS can perform. If a process wants to execute multiple actions simultaneously, it spins up multiple threads. These threads can be:
The OS is responsible for scheduling the thread\u2019s processes optimally so that:
The word thread can also have a different meaning at a CPU level. Each physical core can be composed of multiple logical cores (the concept of hyper-threading), and a logical core is also called a thread. In this post, when we use the word thread, we mean the unit of processing, not a logical core.
A CPU core executes different threads. When it switches from one thread to another, it executes an operation called context switching. The active thread consuming CPU cycles was in an executing state and moves to a runnable state, meaning it\u2019s ready to be executed pending an available core. Context switching is considered an expensive operation because the OS needs to save the current execution state of a thread before the switch (such as the current register values).
As Go developers, we can\u2019t create threads directly, but we can create goroutines, which can be thought of as application-level threads. However, whereas an OS thread is context-switched on and off a CPU core by the OS, a goroutine is context-switched on and off an OS thread by the Go runtime. Also, compared to an OS thread, a goroutine has a smaller memory footprint: 2 KB for goroutines from Go 1.4. An OS thread depends on the OS, but, for example, on Linux/x86\u201332, the default size is 2 MB (see https://man7.org/linux/man-pages/man3/pthread_create.3.html). Having a smaller size makes context switching faster.
NoteContext switching a goroutine versus a thread is about 80% to 90% faster, depending on the architecture.
Let\u2019s now discuss how the Go scheduler works to overview how goroutines are handled. Internally, the Go scheduler uses the following terminology (see proc.go):
Each OS thread (M) is assigned to a CPU core (P) by the OS scheduler. Then, each goroutine (G) runs on an M. The GOMAXPROCS variable defines the limit of Ms in charge of executing user-level code simultaneously. But if a thread is blocked in a system call (for example, I/O), the scheduler can spin up more Ms. As of Go 1.5, GOMAXPROCS is by default equal to the number of available CPU cores.
A goroutine has a simpler lifecycle than an OS thread. It can be doing one of the following:
There\u2019s one last stage to understand about the implementation of Go scheduling: when a goroutine is created but cannot be executed yet; for example, all the other Ms are already executing a G. In this scenario, what will the Go runtime do about it? The answer is queuing. The Go runtime handles two kinds of queues: one local queue per P and a global queue shared among all the Ps.
Figure 1 shows a given scheduling situation on a four-core machine with GOMAXPROCS equal to 4. The parts are the logical cores (Ps), goroutines (Gs), OS threads (Ms), local queues, and global queue:
Figure 1: An example of the current state of a Go application executed on a four-core machine. Goroutines that aren\u2019t in an executing state are either runnable (pending being executed) or waiting (pending a blocking operation)
First, we can see five Ms, whereas GOMAXPROCS is set to 4. But as we mentioned, if needed, the Go runtime can create more OS threads than the GOMAXPROCS value.
P0, P1, and P3 are currently busy executing Go runtime threads. But P2 is presently idle as M3 is switched off P2, and there\u2019s no goroutine to be executed. This isn\u2019t a good situation because six runnable goroutines are pending being executed, some in the global queue and some in other local queues. How will the Go runtime handle this situation? Here\u2019s the scheduling implementation in pseudocode (see proc.go):
runtime.schedule() {\n // Only 1/61 of the time, check the global runnable queue for a G.\n // If not found, check the local queue.\n // If not found,\n // Try to steal from other Ps.\n // If not, check the global runnable queue.\n // If not found, poll network.\n}\n
Every sixty-first execution, the Go scheduler will check whether goroutines from the global queue are available. If not, it will check its local queue. Meanwhile, if both the global and local queues are empty, the Go scheduler can pick up goroutines from other local queues. This principle in scheduling is called work stealing, and it allows an underutilized processor to actively look for another processor\u2019s goroutines and steal some.
One last important thing to mention: prior to Go 1.14, the scheduler was cooperative, which meant a goroutine could be context-switched off a thread only in specific blocking cases (for example, channel send or receive, I/O, waiting to acquire a mutex). Since Go 1.14, the Go scheduler is now preemptive: when a goroutine is running for a specific amount of time (10 ms), it will be marked preemptible and can be context-switched off to be replaced by another goroutine. This allows a long-running job to be forced to share CPU time.
Now that we understand the fundamentals of scheduling in Go, let\u2019s look at a concrete example: implementing a merge sort in a parallel manner.
"},{"location":"56-concurrency-faster/#parallel-merge-sort","title":"Parallel Merge Sort","text":"First, let\u2019s briefly review how the merge sort algorithm works. Then we will implement a parallel version. Note that the objective isn\u2019t to implement the most efficient version but to support a concrete example showing why concurrency isn\u2019t always faster.
The merge sort algorithm works by breaking a list repeatedly into two sublists until each sublist consists of a single element and then merging these sublists so that the result is a sorted list (see figure 2). Each split operation splits the list into two sublists, whereas the merge operation merges two sublists into a sorted list.
Figure 2: Applying the merge sort algorithm repeatedly breaks each list into two sublists. Then the algorithm uses a merge operation such that the resulting list is sorted
Here is the sequential implementation of this algorithm. We don\u2019t include all of the code as it\u2019s not the main point of this section:
func sequentialMergesort(s []int) {\nif len(s) <= 1 {\nreturn\n}\nmiddle := len(s) / 2\nsequentialMergesort(s[:middle]) // First half\nsequentialMergesort(s[middle:]) // Second half\nmerge(s, middle) // Merges the two halves\n}\nfunc merge(s []int, middle int) {\n// ...\n}\n
This algorithm has a structure that makes it open to concurrency. Indeed, as each sequentialMergesort operation works on an independent set of data that doesn\u2019t need to be fully copied (here, an independent view of the underlying array using slicing), we could distribute this workload among the CPU cores by spinning up each sequentialMergesort operation in a different goroutine. Let\u2019s write a first parallel implementation:
func parallelMergesortV1(s []int) {\nif len(s) <= 1 {\nreturn\n}\nmiddle := len(s) / 2\nvar wg sync.WaitGroup\nwg.Add(2)\ngo func() { // Spins up the first half of the work in a goroutine\ndefer wg.Done()\nparallelMergesortV1(s[:middle])\n}()\ngo func() { // Spins up the second half of the work in a goroutine\ndefer wg.Done()\nparallelMergesortV1(s[middle:])\n}()\nwg.Wait()\nmerge(s, middle) // Merges the halves\n}\n
In this version, each half of the workload is handled in a separate goroutine. The parent goroutine waits for both parts by using sync.WaitGroup. Hence, we call the Wait method before the merge operation.
We now have a parallel version of the merge sort algorithm. Therefore, if we run a benchmark to compare this version against the sequential one, the parallel version should be faster, correct? Let\u2019s run it on a four-core machine with 10,000 elements:
Benchmark_sequentialMergesort-4 2278993555 ns/op\nBenchmark_parallelMergesortV1-4 17525998709 ns/op\n
Surprisingly, the parallel version is almost an order of magnitude slower. How can we explain this result? How is it possible that a parallel version that distributes a workload across four cores is slower than a sequential version running on a single machine? Let\u2019s analyze the problem.
If we have a slice of, say, 1,024 elements, the parent goroutine will spin up two goroutines, each in charge of handling a half consisting of 512 elements. Each of these goroutines will spin up two new goroutines in charge of handling 256 elements, then 128, and so on, until we spin up a goroutine to compute a single element.
If the workload that we want to parallelize is too small, meaning we\u2019re going to compute it too fast, the benefit of distributing a job across cores is destroyed: the time it takes to create a goroutine and have the scheduler execute it is much too high compared to directly merging a tiny number of items in the current goroutine. Although goroutines are lightweight and faster to start than threads, we can still face cases where a workload is too small.
So what can we conclude from this result? Does it mean the merge sort algorithm cannot be parallelized? Wait, not so fast.
Let\u2019s try another approach. Because merging a tiny number of elements within a new goroutine isn\u2019t efficient, let\u2019s define a threshold. This threshold will represent how many elements a half should contain in order to be handled in a parallel manner. If the number of elements in the half is fewer than this value, we will handle it sequentially. Here\u2019s a new version:
const max = 2048 // Defines the threshold\nfunc parallelMergesortV2(s []int) {\nif len(s) <= 1 {\nreturn\n}\nif len(s) <= max {\nsequentialMergesort(s) // Calls our initial sequential version\n} else { // If bigger than the threshold, keeps the parallel version\nmiddle := len(s) / 2\nvar wg sync.WaitGroup\nwg.Add(2)\ngo func() {\ndefer wg.Done()\nparallelMergesortV2(s[:middle])\n}()\ngo func() {\ndefer wg.Done()\nparallelMergesortV2(s[middle:])\n}()\nwg.Wait()\nmerge(s, middle)\n}\n}\n
If the number of elements in the s slice is smaller than max, we call the sequential version. Otherwise, we keep calling our parallel implementation. Does this approach impact the result? Yes, it does:
Benchmark_sequentialMergesort-4 2278993555 ns/op\nBenchmark_parallelMergesortV1-4 17525998709 ns/op\nBenchmark_parallelMergesortV2-4 1313010260 ns/op\n
Our v2 parallel implementation is more than 40% faster than the sequential one, thanks to this idea of defining a threshold to indicate when parallel should be more efficient than sequential.
NoteWhy did I set the threshold to 2,048? Because it was the optimal value for this specific workload on my machine. In general, such magic values should be defined carefully with benchmarks (running on an execution environment similar to production). It\u2019s also pretty interesting to note that running the same algorithm in a programming language that doesn\u2019t implement the concept of goroutines has an impact on the value. For example, running the same example in Java using threads means an optimal value closer to 8,192. This tends to illustrate how goroutines are more efficient than threads.
"},{"location":"56-concurrency-faster/#conclusion","title":"Conclusion","text":"We have seen throughout this post the fundamental concepts of scheduling in Go: the differences between a thread and a goroutine and how the Go runtime schedules goroutines. Meanwhile, using the parallel merge sort example, we illustrated that concurrency isn\u2019t always necessarily faster. As we have seen, spinning up goroutines to handle minimal workloads (merging only a small set of elements) demolishes the benefit we could get from parallelism.
So, where should we go from here? We must keep in mind that concurrency isn\u2019t always faster and shouldn\u2019t be considered the default way to go for all problems. First, it makes things more complex. Also, modern CPUs have become incredibly efficient at executing sequential code and predictable code. For example, a superscalar processor can parallelize instruction execution over a single core with high efficiency.
Does this mean we shouldn\u2019t use concurrency? Of course not. However, it\u2019s essential to keep these conclusions in mind. If we\u2019re not sure that a parallel version will be faster, the right approach may be to start with a simple sequential version and build from there using profiling (mistake #98, \u201cNot using Go diagnostics tooling\u201d) and benchmarks (mistake #89, \u201cWriting inaccurate benchmarks\u201d), for example. It can be the only way to ensure that a concurrent implementation is worth it.
"},{"location":"89-benchmarks/","title":"Writing inaccurate benchmarks","text":"In general, we should never guess about performance. When writing optimizations, so many factors may come into play that even if we have a strong opinion about the results, it\u2019s rarely a bad idea to test them. However, writing benchmarks isn\u2019t straightforward. It can be pretty simple to write inaccurate benchmarks and make wrong assumptions based on them. The goal of this post is to examine four common and concrete traps leading to inaccuracy:
Before discussing these traps, let\u2019s briefly review how benchmarks work in Go. The skeleton of a benchmark is as follows:
func BenchmarkFoo(b *testing.B) {\nfor i := 0; i < b.N; i++ {\nfoo()\n}\n}\n
The function name starts with the Benchmark
prefix. The function under test (foo) is called within the for
loop. b.N
represents a variable number of iterations. When running a benchmark, Go tries to make it match the requested benchmark time. The benchmark time is set by default to 1 second and can be changed with the -benchtime
flag. b.N
starts at 1; if the benchmark completes in under 1 second, b.N
is increased, and the benchmark runs again until b.N
roughly matches benchtime:
$ go test -bench=.\ncpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkFoo-4 73 16511228 ns/op\n
Here, the benchmark took about 1 second, and foo
was executed 73 times, for an average execution time of 16,511,228 nanoseconds. We can change the benchmark time using -benchtime
:
$ go test -bench=. -benchtime=2s\nBenchmarkFoo-4 150 15832169 ns/op\n
foo
was executed roughly twice more than during the previous benchmark.
Next, let\u2019s look at some common traps.
"},{"location":"89-benchmarks/#not-resetting-or-pausing-the-timer","title":"Not resetting or pausing the timer","text":"In some cases, we need to perform operations before the benchmark loop. These operations may take quite a while (for example, generating a large slice of data) and may significantly impact the benchmark results:
func BenchmarkFoo(b *testing.B) {\nexpensiveSetup()\nfor i := 0; i < b.N; i++ {\nfunctionUnderTest()\n}\n}\n
In this case, we can use the ResetTimer
method before entering the loop:
func BenchmarkFoo(b *testing.B) {\nexpensiveSetup()\nb.ResetTimer() // Reset the benchmark timer\nfor i := 0; i < b.N; i++ {\nfunctionUnderTest()\n}\n}\n
Calling ResetTimer
zeroes the elapsed benchmark time and memory allocation counters since the beginning of the test. This way, an expensive setup can be discarded from the test results.
What if we have to perform an expensive setup not just once but within each loop iteration?
func BenchmarkFoo(b *testing.B) {\nfor i := 0; i < b.N; i++ {\nexpensiveSetup()\nfunctionUnderTest()\n}\n}\n
We can\u2019t reset the timer, because that would be executed during each loop iteration. But we can stop and resume the benchmark timer, surrounding the call to expensiveSetup
:
func BenchmarkFoo(b *testing.B) {\nfor i := 0; i < b.N; i++ {\nb.StopTimer() // Pause the benchmark timer\nexpensiveSetup()\nb.StartTimer() // Resume the benchmark timer\nfunctionUnderTest()\n}\n}\n
Here, we pause the benchmark timer to perform the expensive setup and then resume the timer.
NoteThere\u2019s one catch to remember about this approach: if the function under test is too fast to execute compared to the setup function, the benchmark may take too long to complete. The reason is that it would take much longer than 1 second to reach benchtime
. Calculating the benchmark time is based solely on the execution time of functionUnderTest
. So, if we wait a significant time in each loop iteration, the benchmark will be much slower than 1 second. If we want to keep the benchmark, one possible mitigation is to decrease benchtime
.
We must be sure to use the timer methods to preserve the accuracy of a benchmark.
"},{"location":"89-benchmarks/#making-wrong-assumptions-about-micro-benchmarks","title":"Making wrong assumptions about micro-benchmarks","text":"A micro-benchmark measures a tiny computation unit, and it can be extremely easy to make wrong assumptions about it. Let\u2019s say, for example, that we aren\u2019t sure whether to use atomic.StoreInt32
or atomic.StoreInt64
(assuming that the values we handle will always fit in 32 bits). We want to write a benchmark to compare both functions:
func BenchmarkAtomicStoreInt32(b *testing.B) {\nvar v int32\nfor i := 0; i < b.N; i++ {\natomic.StoreInt32(&v, 1)\n}\n}\nfunc BenchmarkAtomicStoreInt64(b *testing.B) {\nvar v int64\nfor i := 0; i < b.N; i++ {\natomic.StoreInt64(&v, 1)\n}\n}\n
If we run this benchmark, here\u2019s some example output:
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkAtomicStoreInt32\nBenchmarkAtomicStoreInt32-4 197107742 5.682 ns/op\nBenchmarkAtomicStoreInt64\nBenchmarkAtomicStoreInt64-4 213917528 5.134 ns/op\n
We could easily take this benchmark for granted and decide to use atomic.StoreInt64
because it appears to be faster. Now, for the sake of doing a fair benchmark, we reverse the order and test atomic.StoreInt64
first, followed by atomic.StoreInt32
. Here is some example output:
BenchmarkAtomicStoreInt64\nBenchmarkAtomicStoreInt64-4 224900722 5.434 ns/op\nBenchmarkAtomicStoreInt32\nBenchmarkAtomicStoreInt32-4 230253900 5.159 ns/op\n
This time, atomic.StoreInt32
has better results. What happened?
In the case of micro-benchmarks, many factors can impact the results, such as machine activity while running the benchmarks, power management, thermal scaling, and better cache alignment of a sequence of instructions. We must remember that many factors, even outside the scope of our Go project, can impact the results.
NoteWe should make sure the machine executing the benchmark is idle. However, external processes may run in the background, which may affect benchmark results. For that reason, tools such as perflock
can limit how much CPU a benchmark can consume. For example, we can run a benchmark with 70% of the total available CPU, giving 30% to the OS and other processes and reducing the impact of the machine activity factor on the results.
One option is to increase the benchmark time using the -benchtime
option. Similar to the law of large numbers in probability theory, if we run a benchmark a large number of times, it should tend to approach its expected value (assuming we omit the benefits of instructions caching and similar mechanics).
Another option is to use external tools on top of the classic benchmark tooling. For instance, the benchstat
tool, which is part of the golang.org/x
repository, allows us to compute and compare statistics about benchmark executions.
Let\u2019s run the benchmark 10 times using the -count
option and pipe the output to a specific file:
$ go test -bench=. -count=10 | tee stats.txt\ncpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkAtomicStoreInt32-4 234935682 5.124 ns/op\nBenchmarkAtomicStoreInt32-4 235307204 5.112 ns/op\n// ...\nBenchmarkAtomicStoreInt64-4 235548591 5.107 ns/op\nBenchmarkAtomicStoreInt64-4 235210292 5.090 ns/op\n// ...\n
We can then run benchstat
on this file:
$ benchstat stats.txt\nname time/op\nAtomicStoreInt32-4 5.10ns \u00b1 1%\nAtomicStoreInt64-4 5.10ns \u00b1 1%\n
The results are the same: both functions take on average 5.10 nanoseconds to complete. We also see the percent variation between the executions of a given benchmark: \u00b1 1%. This metric tells us that both benchmarks are stable, giving us more confidence in the computed average results. Therefore, instead of concluding that atomic.StoreInt32
is faster or slower, we can conclude that its execution time is similar to that of atomic.StoreInt64
for the usage we tested (in a specific Go version on a particular machine).
In general, we should be cautious about micro-benchmarks. Many factors can significantly impact the results and potentially lead to wrong assumptions. Increasing the benchmark time or repeating the benchmark executions and computing stats with tools such as benchstat
can be an efficient way to limit external factors and get more accurate results, leading to better conclusions.
Let\u2019s also highlight that we should be careful about using the results of a micro-benchmark executed on a given machine if another system ends up running the application. The production system may act quite differently from the one on which we ran the micro-benchmark.
"},{"location":"89-benchmarks/#not-being-careful-about-compiler-optimizations","title":"Not being careful about compiler optimizations","text":"Another common mistake related to writing benchmarks is being fooled by compiler optimizations, which can also lead to wrong benchmark assumptions. In this section, we look at Go issue 14813 (https://github.com/golang/go/issues/14813, also discussed by Go project member Dave Cheney) with a population count function (a function that counts the number of bits set to 1):
const m1 = 0x5555555555555555\nconst m2 = 0x3333333333333333\nconst m4 = 0x0f0f0f0f0f0f0f0f\nconst h01 = 0x0101010101010101\nfunc popcnt(x uint64) uint64 {\nx -= (x >> 1) & m1\nx = (x & m2) + ((x >> 2) & m2)\nx = (x + (x >> 4)) & m4\nreturn (x * h01) >> 56\n}\n
This function takes and returns a uint64
. To benchmark this function, we can write the following:
func BenchmarkPopcnt1(b *testing.B) {\nfor i := 0; i < b.N; i++ {\npopcnt(uint64(i))\n}\n}\n
However, if we execute this benchmark, we get a surprisingly low result:
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkPopcnt1-4 1000000000 0.2858 ns/op\n
A duration of 0.28 nanoseconds is roughly one clock cycle, so this number is unreasonably low. The problem is that the developer wasn\u2019t careful enough about compiler optimizations. In this case, the function under test is simple enough to be a candidate for inlining: an optimization that replaces a function call with the body of the called function and lets us prevent a function call, which has a small footprint. Once the function is inlined, the compiler notices that the call has no side effects and replaces it with the following benchmark:
func BenchmarkPopcnt1(b *testing.B) {\nfor i := 0; i < b.N; i++ {\n// Empty\n}\n}\n
The benchmark is now empty \u2014 which is why we got a result close to one clock cycle. To prevent this from happening, a best practice is to follow this pattern:
In our case, we write the following benchmark:
var global uint64 // Define a global variable\nfunc BenchmarkPopcnt2(b *testing.B) {\nvar v uint64 // Define a local variable\nfor i := 0; i < b.N; i++ {\nv = popcnt(uint64(i)) // Assign the result to the local variable\n}\nglobal = v // Assign the result to the global variable\n}\n
global
is a global variable, whereas v is a local variable whose scope is the benchmark function. During each loop iteration, we assign the result of popcnt
to the local variable. Then we assign the latest result to the global variable.
Why not assign the result of the popcnt call directly to global to simplify the test? Writing to a global variable is slower than writing to a local variable (these concepts are discussed in 100 Go Mistakes, mistake #95: \u201cNot understanding stack vs. heap\u201d). Therefore, we should write each result to a local variable to limit the footprint during each loop iteration.
If we run these two benchmarks, we now get a significant difference in the results:
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkPopcnt1-4 1000000000 0.2858 ns/op\nBenchmarkPopcnt2-4 606402058 1.993 ns/op\n
BenchmarkPopcnt2
is the accurate version of the benchmark. It guarantees that we avoid the inlining optimizations, which can artificially lower the execution time or even remove the call to the function under test. Relying on the results of BenchmarkPopcnt1
could have led to wrong assumptions.
Let\u2019s remember the pattern to avoid compiler optimizations fooling benchmark results: assign the result of the function under test to a local variable, and then assign the latest result to a global variable. This best practice also prevents us from making incorrect assumptions.
"},{"location":"89-benchmarks/#being-fooled-by-the-observer-effect","title":"Being fooled by the observer effect","text":"In physics, the observer effect is the disturbance of an observed system by the act of observation. This effect can also be seen in benchmarks and can lead to wrong assumptions about results. Let\u2019s look at a concrete example and then try to mitigate it.
We want to implement a function receiving a matrix of int64
elements. This matrix has a fixed number of 512 columns, and we want to compute the total sum of the first eight columns, as shown in figure 1.
Figure 1: Computing the sum of the first eight columns.
For the sake of optimizations, we also want to determine whether varying the number of columns has an impact, so we also implement a second function with 513 columns. The implementation is the following:
func calculateSum512(s [][512]int64) int64 {\nvar sum int64\nfor i := 0; i < len(s); i++ { // Iterate over each row\nfor j := 0; j < 8; j++ { // Iterate over the first eight columns\nsum += s[i][j] // Increment sum\n}\n}\nreturn sum\n}\nfunc calculateSum513(s [][513]int64) int64 {\n// Same implementation as calculateSum512\n}\n
We iterate over each row and then over the first eight columns, and we increment a sum variable that we return. The implementation in calculateSum513
remains the same.
We want to benchmark these functions to decide which one is the most performant given a fixed number of rows:
const rows = 1000\nvar res int64\nfunc BenchmarkCalculateSum512(b *testing.B) {\nvar sum int64\ns := createMatrix512(rows) // Create a matrix of 512 columns\nb.ResetTimer()\nfor i := 0; i < b.N; i++ {\nsum = calculateSum512(s) // Create a matrix of 512 columns\n}\nres = sum\n}\nfunc BenchmarkCalculateSum513(b *testing.B) {\nvar sum int64\ns := createMatrix513(rows) // Create a matrix of 513 columns\nb.ResetTimer()\nfor i := 0; i < b.N; i++ {\nsum = calculateSum513(s) // Calculate the sum\n}\nres = sum\n}\n
We want to create the matrix only once, to limit the footprint on the results. Therefore, we call createMatrix512
and createMatrix513
outside of the loop. We may expect the results to be similar as again we only want to iterate on the first eight columns, but this isn\u2019t the case (on my machine):
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkCalculateSum512-4 81854 15073 ns/op\nBenchmarkCalculateSum513-4 161479 7358 ns/op\n
The second benchmark with 513 columns is about 50% faster. Again, because we iterate only over the first eight columns, this result is quite surprising.
To understand this difference, we need to understand the basics of CPU caches. In a nutshell, a CPU is composed of different caches (usually L1, L2, and L3). These caches reduce the average cost of accessing data from the main memory. In some conditions, the CPU can fetch data from the main memory and copy it to L1. In this case, the CPU tries to fetch into L1 the matrix\u2019s subset that calculateSum
is interested in (the first eight columns of each row). However, the matrix fits in memory in one case (513 columns) but not in the other case (512 columns).
This isn\u2019t in the scope of this post to explain why, but we look at this problem in 100 Go Mistakes, mistake #91: \u201cNot understanding CPU caches.\u201d
Coming back to the benchmark, the main issue is that we keep reusing the same matrix in both cases. Because the function is repeated thousands of times, we don\u2019t measure the function\u2019s execution when it receives a plain new matrix. Instead, we measure a function that gets a matrix that already has a subset of the cells present in the cache. Therefore, because calculateSum513
leads to fewer cache misses, it has a better execution time.
This is an example of the observer effect. Because we keep observing a repeatedly called CPU-bound function, CPU caching may come into play and significantly affect the results. In this example, to prevent this effect, we should create a matrix during each test instead of reusing one:
func BenchmarkCalculateSum512(b *testing.B) {\nvar sum int64\nfor i := 0; i < b.N; i++ {\nb.StopTimer()\ns := createMatrix512(rows) // Create a new matrix during each loop iteration\nb.StartTimer()\nsum = calculateSum512(s)\n}\nres = sum\n}\n
A new matrix is now created during each loop iteration. If we run the benchmark again (and adjust benchtime
\u2014 otherwise, it takes too long to execute), the results are closer to each other:
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkCalculateSum512-4 1116 33547 ns/op\nBenchmarkCalculateSum513-4 998 35507 ns/op\n
Instead of making the incorrect assumption that calculateSum513 is faster, we see that both benchmarks lead to similar results when receiving a new matrix.
As we have seen in this post, because we were reusing the same matrix, CPU caches significantly impacted the results. To prevent this, we had to create a new matrix during each loop iteration. In general, we should remember that observing a function under test may lead to significant differences in results, especially in the context of micro-benchmarks of CPU-bound functions where low-level optimizations matter. Forcing a benchmark to re-create data during each iteration can be a good way to prevent this effect.
"},{"location":"9-generics/","title":"Being confused about when to use generics","text":"Generics is a fresh addition to the language. In a nutshell, it allows writing code with types that can be specified later and instantiated when needed. However, it can be pretty easy to be confused about when to use generics and when not to. Throughout this post, we will describe the concept of generics in Go and then delve into common use and misuses.
"},{"location":"9-generics/#concepts","title":"Concepts","text":"Consider the following function that extracts all the keys from a map[string]int
type:
func getKeys(m map[string]int) []string {\nvar keys []string\nfor k := range m {\nkeys = append(keys, k)\n}\nreturn keys\n}\n
What if we would like to use a similar feature for another map type such as a map[int]string
? Before generics, Go developers had a couple of options: using code generation, reflection, or duplicating code.
For example, we could write two functions, one for each map type, or even try to extend getKeys
to accept different map types:
func getKeys(m any) ([]any, error) {\nswitch t := m.(type) {\ndefault:\nreturn nil, fmt.Errorf(\"unknown type: %T\", t)\ncase map[string]int:\nvar keys []any\nfor k := range t {\nkeys = append(keys, k)\n}\nreturn keys, nil\ncase map[int]string:\n// Copy the extraction logic\n}\n}\n
We can start noticing a couple of issues:
range
loop.int
or string
, we are obliged to return a slice of empty interfaces to factor out key types. This approach increases the effort on the caller-side as the client may also have to perform a type check of the keys or extra conversion.Thanks to generics, we can now refactor this code using type parameters.
Type parameters are generic types we can use with functions and types. For example, the following function accepts a type parameter:
func foo[T any](t T) {\n// ...\n}\n
When calling foo
, we will pass a type argument of any type. Passing a type argument is called instantiation because the work is done at compile time which keeps type safety as part of the core language features and avoids runtime overheads.
Let\u2019s get back to the getKeys
function and use type parameters to write a generic version that would accept any kind of map:
func getKeys[K comparable, V any](m map[K]V) []K { <1>\nvar keys []K <2>\nfor k := range m {\nkeys = append(keys, k)\n}\nreturn keys\n}\n
To handle the map, we defined two kinds of type parameters. First, the values can be of any type: V any
. However, in Go, the map keys can\u2019t be of any type. For example, we cannot use slices:
var m map[[]byte]int\n
This code leads to a compilation error: invalid map key type []byte
. Therefore, instead of accepting any key type, we are obliged to restrict type arguments so that the key type meets specific requirements. Here, being comparable (we can use ==
or !=
). Hence, we defined K
as comparable
instead of any
.
Restricting type arguments to match specific requirements is called a constraint. A constraint is an interface type that can contain:
Let\u2019s see a concrete example for the latter. Imagine we don\u2019t want to accept any comparable
type for map key type. For instance, we would like to restrict it to either int
or string
types. We can define a custom constraint this way:
type customConstraint interface {\n~int | ~string // Define a custom type that will restrict types to int and string\n}\n// Change the type parameter K to be custom\nfunc getKeys[K customConstraint, V any](m map[K]V) []K {\n// Same implementation\n}\n
First, we define a customConstraint
interface to restrict the types to be either int
or string
using the union operator |
(we will discuss the use of ~
a bit later). Then, K
is now a customConstraint
instead of a comparable
as before.
Now, the signature of getKeys
enforces that we can call it with a map of any value type, but the key type has to be an int
or a string
. For example, on the caller-side:
m = map[string]int{\n\"one\": 1,\n\"two\": 2,\n\"three\": 3,\n}\nkeys := getKeys(m)\n
Note that Go can infer that getKeys
is called with a string
type argument. The previous call was similar to this:
keys := getKeys[string](m)\n
Note What\u2019s the difference between a constraint using ~int
or int
? Using int
restricts it to that type, whereas ~int
restricts all the types whose underlying type is an int
.
To illustrate it, let\u2019s imagine a constraint where we would like to restrict a type to any int
type implementing the String() string
method:
type customConstraint interface {\n~int\nString() string\n}\n
Using this constraint will restrict type arguments to custom types like this one:
type customInt int\nfunc (i customInt) String() string {\nreturn strconv.Itoa(int(i))\n}\n
As customInt
is an int
and implements the String() string
method, the customInt
type satisfies the constraint defined.
However, if we change the constraint to contain an int
instead of an ~int
, using customInt
would lead to a compilation error because the int
type doesn\u2019t implement String() string
.
Let\u2019s also note the constraints
package contains a set of common constraints such as Signed
that includes all the signed integer types. Let\u2019s ensure that a constraint doesn\u2019t already exist in this package before creating a new one.
So far, we have discussed examples using generics for functions. However, we can also use generics with data structures.
For example, we will create a linked list containing values of any type. Meanwhile, we will write an Add
method to append a node:
type Node[T any] struct { // Use type parameter\nVal T\nnext *Node[T]\n}\nfunc (n *Node[T]) Add(next *Node[T]) { // Instantiate type receiver\nn.next = next\n}\n
We use type parameters to define T
and use both fields in Node
. Regarding the method, the receiver is instantiated. Indeed, because Node
is generic, it has to follow also the type parameter defined.
One last thing to note about type parameters: they can\u2019t be used on methods, only on functions. For example, the following method wouldn\u2019t compile:
type Foo struct {}\nfunc (Foo) bar[T any](t T) {}\n
./main.go:29:15: methods cannot have type parameters\n
Now, let\u2019s delve into concrete cases where we should and shouldn\u2019t use generics.
"},{"location":"9-generics/#common-uses-and-misuses","title":"Common uses and misuses","text":"So when are generics useful? Let\u2019s discuss a couple of common uses where generics are recommended:
func merge[T any](ch1, ch2 <-chan T) <-chan T {\n// ...\n}\n
sort
package contains functions to sort different slice types such as sort.Ints
or sort.Float64s
. Using type parameters, we can factor out the sorting behaviors that rely on three methods, Len
, Less
, and Swap
:type sliceFn[T any] struct { // Use type parameter\ns []T\ncompare func(T, T) bool // Compare two T elements\n}\nfunc (s sliceFn[T]) Len() int { return len(s.s) }\nfunc (s sliceFn[T]) Less(i, j int) bool { return s.compare(s.s[i], s.s[j]) }\nfunc (s sliceFn[T]) Swap(i, j int) { s.s[i], s.s[j] = s.s[j], s.s[i] }\n
Conversely, when is it recommended not to use generics?
io.Writer
and call the Write
method:func foo[T io.Writer](w T) {\nb := getBytes()\n_, _ = w.Write(b)\n}\n
Though generics can be very helpful in particular conditions, we should be cautious about when to use them and not use them.
In general, when we want to answer when not to use generics, we can find similarities with when not to use interfaces. Indeed, generics introduce a form of abstraction, and we have to remember that unnecessary abstractions introduce complexity.
Let\u2019s not pollute our code with needless abstractions, and let\u2019s focus on solving concrete problems for now. It means that we shouldn\u2019t use type parameters prematurely. Let\u2019s wait until we are about to write boilerplate code to consider using generics.
"},{"location":"98-profiling-execution-tracing/","title":"Not using Go diagnostics tooling","text":"Go offers a few excellent diagnostics tools to help us get insights into how an application performs. This post focuses on the most important ones: profiling and the execution tracer. Both tools are so important that they should be part of the core toolset of any Go developer who is interested in optimization. First, let\u2019s discuss profiling.
"},{"location":"98-profiling-execution-tracing/#profiling","title":"Profiling","text":"Profiling provides insights into the execution of an application. It allows us to resolve performance issues, detect contention, locate memory leaks, and more. These insights can be collected via several profiles:
CPU
\u2014 Determines where an application spends its timeGoroutine
\u2014 Reports the stack traces of the ongoing goroutinesHeap
\u2014 Reports heap memory allocation to monitor current memory usage and check for possible memory leaksMutex
\u2014 Reports lock contentions to see the behaviors of the mutexes used in our code and whether an application spends too much time in locking callsBlock
\u2014 Shows where goroutines block waiting on synchronization primitivesProfiling is achieved via instrumentation using a tool called a profiler, in Go: pprof
. First, let\u2019s understand how and when to enable pprof
; then, we discuss the most critical profile types.
There are several ways to enable pprof
. For example, we can use the net/http/pprof
package to serve the profiling data via HTTP:
package main\nimport (\n\"fmt\"\n\"log\"\n\"net/http\"\n_ \"net/http/pprof\" // Blank import to pprof\n)\nfunc main() {\n// Exposes an HTTP endpoint\nhttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\nfmt.Fprintf(w, \"\")\n})\nlog.Fatal(http.ListenAndServe(\":80\", nil))\n}\n
Importing net/http/pprof
leads to a side effect that allows us to reach the pprof URL: http://host/debug/pprof. Note that enabling pprof
is safe even in production (https://go.dev/doc/diagnostics#profiling). The profiles that impact performance, such as CPU profiling, aren\u2019t enabled by default, nor do they run continuously: they are activated only for a specific period.
Now that we have seen how to expose a pprof
endpoint, let\u2019s discuss the most common profiles.
The CPU profiler relies on the OS and signaling. When it is activated, the application asks the OS to interrupt it every 10 ms by default via a SIGPROF
signal. When the application receives a SIGPROF
, it suspends the current activity and transfers the execution to the profiler. The profiler collects data such as the current goroutine activity and aggregates execution statistics that we can retrieve. Then it stops, and the execution resumes until the next SIGPROF
.
We can access the /debug/pprof/profile endpoint to activate CPU profiling. Accessing this endpoint executes CPU profiling for 30 seconds by default. For 30 seconds, our application is interrupted every 10 ms. Note that we can change these two default values: we can use the seconds
parameter to pass to the endpoint how long the profiling should last (for example, /debug/pprof/profile?seconds=15), and we can change the interruption rate (even to less than 10 ms). But in most cases, 10 ms should be enough, and in decreasing this value (meaning increasing the rate), we should be careful not to harm performance. After 30 seconds, we download the results of the CPU profiler.
We can also enable the CPU profiler using the -cpuprofile
flag, such as when running a benchmark. For example, the following command produces the same type of file that can be downloaded via /debug/ pprof/profile.
$ go test -bench=. -cpuprofile profile.out\n
From this file, we can navigate to the results using go tool
:
$ go tool pprof -http=:8080 <file>\n
This command opens a web UI showing the call graph. The next figure shows an example taken from an application. The larger the arrow, the more it was a hot path. We can then navigate into this graph and get execution insights.
Figure 1: The call graph of an application during 30 seconds.
For example, the graph in the next figure tells us that during 30 seconds, 0.06 seconds were spent in the decode
method (*FetchResponse
receiver). Of these 0.06 seconds, 0.02 were spent in RecordBatch.decode
and 0.01 in makemap
(creating a map).
Figure 2: Example call graph.
We can also access this kind of information from the web UI with different representations. For example, the Top view sorts the functions per execution time, and Flame Graph visualizes the execution time hierarchy. The UI can even display the expensive parts of the source code line by line.
NoteWe can also delve into profiling data via a command line. However, we focus on the web UI in this post.
Thanks to this data, we can get a general idea of how an application behaves:
runtime.mallogc
can mean an excessive number of small heap allocations that we can try to minimize.syscall.Read
or syscall.Write
means the application spends a significant amount of time in Kernel mode. Working on I/O buffering may be an avenue for improvement.These are the kinds of insights we can get from the CPU profiler. It\u2019s valuable to understand the hottest code path and identify bottlenecks. But it won\u2019t determine more than the configured rate because the CPU profiler is executed at a fixed pace (by default, 10 ms). To get finer-grained insights, we should use tracing, which we discuss later in this post.
NoteWe can also attach labels to the different functions. For example, imagine a common function called from different clients. To track the time spent for both clients, we can use pprof.Labels
.
Heap profiling allows us to get statistics about the current heap usage. Like CPU profiling, heap profiling is sample-based. We can change this rate, but we shouldn\u2019t be too granular because the more we decrease the rate, the more effort heap profiling will require to collect data. By default, samples are profiled at one allocation for every 512 KB of heap allocation.
If we reach /debug/pprof/heap/, we get raw data that can be hard to read. However, we can download a heap profile using /debug/pprof/heap/?debug=0 and then open it with go tool
(the same command as in the previous section) to navigate into the data using the web UI.
The next figure shows an example of a heap graph. Calling the MetadataResponse.decode
method leads to allocating 1536 KB of heap data (which represents 6.32% of the total heap). However, 0 out of these 1536 KB were allocated by this function directly, so we need to inspect the second call. The TopicMetadata.decode
method allocated 512 KB out of the 1536 KB; the rest \u2014 1024 KB \u2014 were allocated in another method.
Figure 3: A heap graph.
This is how we can navigate the call chain to understand what part of an application is responsible for most of the heap allocations. We can also look at different sample types:
alloc_objects
\u2014 Total number of objects allocatedalloc_space
\u2014 Total amount of memory allocatedinuse_object
s \u2014 Number of objects allocated and not yet releasedinuse_space
\u2014 Amount of memory allocated and not yet releasedAnother very helpful capability with heap profiling is tracking memory leaks. With a GC-based language, the usual procedure is the following:
Forcing a GC before downloading data is a way to prevent false assumptions. For example, if we see a peak of retained objects without running a GC first, we cannot be sure whether it\u2019s a leak or objects that the next GC will collect.
Using pprof
, we can download a heap profile and force a GC in the meantime. The procedure in Go is the following:
$ go tool pprof -http=:8080 -diff_base <file2> <file1>\n
The next figure shows the kind of data we can access. For example, the amount of heap memory held by the newTopicProducer method (top left) has decreased (\u2013513 KB). In contrast, the amount held by updateMetadata (bottom right) has increased (+512 KB). Slow increases are normal. The second heap profile may have been calculated in the middle of a service call, for example. We can repeat this process or wait longer; the important part is to track steady increases in allocations of a specific object.
Figure 4: The differences between the two heap profiles. Note
Another type of profiling related to the heap is allocs
, which reports allocations. Heap profiling shows the current state of the heap memory. To get insights about past memory allocations since the application started, we can use allocations profiling. As discussed, because stack allocations are cheap, they aren\u2019t part of this profiling, which only focuses on the heap.
The goroutine
profile reports the stack trace of all the current goroutines in an application. We can download a file using /debug/pprof/goroutine/?debug=0 and use go tool again. The next figure shows the kind of information we can get.
Figure 5: Goroutine graph.
We can see the current state of the application and how many goroutines were created per function. In this case, withRecover
has created 296 ongoing goroutines (63%), and 29 were related to a call to responseFeeder
.
This kind of information is also beneficial if we suspect goroutine leaks. We can look at goroutine profiler data to know which part of a system is the suspect.
"},{"location":"98-profiling-execution-tracing/#block-profiling","title":"Block Profiling","text":"The block
profile reports where ongoing goroutines block waiting on synchronization primitives. Possibilities include
Block profiling also records the amount of time a goroutine has been waiting and is accessible via /debug/pprof/block. This profile can be extremely helpful if we suspect that performance is being harmed by blocking calls.
The block
profile isn\u2019t enabled by default: we have to call runtime.SetBlockProfileRate
to enable it. This function controls the fraction of goroutine blocking events that are reported. Once enabled, the profiler will keep collecting data in the background even if we don\u2019t call the /debug/pprof/block endpoint. Let\u2019s be cautious if we want to set a high rate so we don\u2019t harm performance.
If we face a deadlock or suspect that goroutines are in a blocked state, the full goroutine stack dump (/debug/pprof/goroutine/?debug=2) creates a dump of all the current goroutine stack traces. This can be helpful as a first analysis step. For example, the following dump shows a Sarama goroutine blocked for 1,420 minutes on a channel-receive operation:
goroutine 2494290 [chan receive, 1420 minutes]:\ngithub.com/Shopify/sarama.(*syncProducer).SendMessages(0xc00071a090,\n[CA]{0xc0009bb800, 0xfb, 0xfb})\n/app/vendor/github.com/Shopify/sarama/sync_producer.go:117 +0x149\n
"},{"location":"98-profiling-execution-tracing/#mutex-profiling","title":"Mutex Profiling","text":"The last profile type is related to blocking but only regarding mutexes. If we suspect that our application spends significant time waiting for locking mutexes, thus harming execution, we can use mutex profiling. It\u2019s accessible via /debug/pprof/mutex.
This profile works in a manner similar to that for blocking. It\u2019s disabled by default: we have to enable it using runtime.SetMutexProfileFraction
, which controls the fraction of mutex contention events reported.
Following are a few additional notes about profiling:
threadcreate
profile because it\u2019s been broken since 2013 (https://github.com/golang/go/issues/6104).pprof
is extensible, and we can create our own custom profiles using pprof.Profile
.We have seen the most important profiles that we can enable to help us understand how an application performs and possible avenues for optimization. In general, enabling pprof
is recommended, even in production, because in most cases it offers an excellent balance between its footprint and the amount of insight we can get from it. Some profiles, such as the CPU profile, lead to performance penalties but only during the time they are enabled.
Let\u2019s now look at the execution tracer.
"},{"location":"98-profiling-execution-tracing/#execution-tracer","title":"Execution Tracer","text":"The execution tracer is a tool that captures a wide range of runtime events with go tool
to make them available for visualization. It is helpful for the following:
Let\u2019s try it with an example given the Concurrency isn\u2019t Always Faster in Go section. We discussed two parallel versions of the merge sort algorithm. The issue with the first version was poor parallelization, leading to the creation of too many goroutines. Let\u2019s see how the tracer can help us in validating this statement.
We will write a benchmark for the first version and execute it with the -trace flag to enable the execution tracer:
$ go test -bench=. -v -trace=trace.out\n
Note We can also download a remote trace file using the /debug/pprof/ trace?debug=0 pprof endpoint.
This command creates a trace.out file that we can open using go tool:
$ go tool trace trace.out\n2021/11/26 21:36:03 Parsing trace...\n2021/11/26 21:36:31 Splitting trace...\n2021/11/26 21:37:00 Opening browser. Trace viewer is listening on\n http://127.0.0.1:54518\n
The web browser opens, and we can click View Trace to see all the traces during a specific timeframe, as shown in the next figure. This figure represents about 150 ms. We can see multiple helpful metrics, such as the goroutine count and the heap size. The heap size grows steadily until a GC is triggered. We can also observe the activity of the Go application per CPU core. The timeframe starts with user-level code; then a \u201cstop the world\u201d is executed, which occupies the four CPU cores for approximately 40 ms.
Figure 6: Showing goroutine activity and runtime events such as a GC phase.
Regarding concurrency, we can see that this version uses all the available CPU cores on the machine. However, the next figure zooms in on a portion of 1 ms. Each bar corresponds to a single goroutine execution. Having too many small bars doesn\u2019t look right: it means execution that is poorly parallelized.
Figure 7: Too many small bars mean poorly parallelized execution.
The next figure zooms even closer to see how these goroutines are orchestrated. Roughly 50% of the CPU time isn\u2019t spent executing application code. The white spaces represent the time the Go runtime takes to spin up and orchestrate new goroutines.
Figure 8: About 50% of CPU time is spent handling goroutine switches.
Let\u2019s compare this with the second parallel implementation, which was about an order of magnitude faster. The next figure again zooms to a 1 ms timeframe.
Figure 9: The number of white spaces has been significantly reduced, proving that the CPU is more fully occupied.
Each goroutine takes more time to execute, and the number of white spaces has been significantly reduced. Hence, the CPU is much more occupied executing application code than it was in the first version. Each millisecond of CPU time is spent more efficiently, explaining the benchmark differences.
Note that the granularity of the traces is per goroutine, not per function like CPU profiling. However, it\u2019s possible to define user-level tasks to get insights per function or group of functions using the runtime/trace
package.
For example, imagine a function that computes a Fibonacci number and then writes it to a global variable using atomic. We can define two different tasks:
var v int64\n// Creates a fibonacci task\nctx, fibTask := trace.NewTask(context.Background(), \"fibonacci\")\ntrace.WithRegion(ctx, \"main\", func() {\nv = fibonacci(10)\n})\nfibTask.End()\n// Creates a store task\nctx, fibStore := trace.NewTask(ctx, \"store\")\ntrace.WithRegion(ctx, \"main\", func() {\natomic.StoreInt64(&result, v)\n})\nfibStore.End()\n
Using go tool
, we can get more precise information about how these two tasks perform. In the previous trace UI, we can see the boundaries for each task per goroutine. In User-Defined Tasks, we can follow the duration distribution:
Figure 10: Distribution of user-level tasks.
We see that in most cases, the fibonacci
task is executed in less than 15 microseconds, whereas the store
task takes less than 6309 nanoseconds.
In the previous section, we discussed the kinds of information we can get from CPU profiling. What are the main differences compared to the data we can get from user-level traces?
runtime/trace
package)In summary, the execution tracer is a powerful tool for understanding how an application performs. As we have seen with the merge sort example, we can identify poorly parallelized execution. However, the tracer\u2019s granularity remains per goroutine unless we manually use runtime/trace
compared to a CPU profile, for example. We can use both profiling and the execution tracer to get the most out of the standard Go diagnostics tools when optimizing an application.
Community space of \ud83d\udcd6 100 Go Mistakes and How to Avoid Them, published by Manning in 2022.
"},{"location":"book/#description","title":"Description","text":"If you're a Go developer looking to improve your skills, this book is for you. With a focus on practical examples, 100 Go Mistakes and How to Avoid Them covers a wide range of topics from concurrency and error handling to testing and code organization. You'll learn to write more idiomatic, efficient, and maintainable code and become a proficient Go developer.
Read a summary of the 100 mistakes here.
"},{"location":"book/#quotes-and-ratings","title":"Quotes and Ratings","text":"Krystian (Goodreads user)
This is an exceptional book. Usually, if a book contains either high-quality explanations or is written succinctly, I consider myself lucky to have found it. This one combines these two characteristics, which is super rare. It's another Go book for me and I still had quite a lot of \"a-ha!\" moments while reading it, and all of that without the unnecessary fluff, just straight to the point.
Akash Chetty
The book is completely exceptional, especially the examples carved out for each topic are really great. There is one topic that I struggled to understand is Concurrency but the way it is explained in this book is truly an art of genius.
Neeraj Shah
This should be the required reading for all Golang developers before they touch code in Production... It's the Golang equivalent of the legendary 'Effective Java' by Joshua Bloch.
Anupam Sengupta
Not having this will be the 101st mistake a Go programmer could make.
Manning, Goodreads, and Amazon reviews: 4.7/5 avg rating"},{"location":"book/#where-to-buy","title":"Where to Buy?","text":"
100 Go Mistakes and How to Avoid Them (\ud83c\uddec\ud83c\udde7 edition: paper, digital, or audiobook)
au35har
)Go\u8a00\u8a9e100Tips \u958b\u767a\u8005\u306b\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u3078\u306e\u5bfe\u51e6\u6cd5 (\ud83c\uddef\ud83c\uddf5 edition: paper or digital)
English and Japanese covers"},{"location":"book/#about-the-author","title":"About the Author","text":"
Teiva Harsanyi is a senior software engineer at Google. He has worked in various domains, including insurance, transportation, and safety-critical industries like air traffic management. He is passionate about Go and how to design and implement reliable systems.
"},{"location":"chapter-1/","title":"Go: Simple to learn but hard to master","text":"This chapter covers
Making mistakes is part of everyone\u2019s life. As Albert Einstein once said,
Albert Einstein
A person who never made a mistake never tried anything new.
What matters in the end isn\u2019t the number of mistakes we make, but our capacity to learn from them. This assertion also applies to programming. The seniority we acquire in a language isn\u2019t a magical process; it involves making many mistakes and learning from them. The purpose of this book is centered around this idea. It will help you, the reader, become a more proficient Go developer by looking at and learning from 100 common mistakes people make in many areas of the language.
This chapter presents a quick refresher as to why Go has become mainstream over the years. We\u2019ll discuss why, despite Go being considered simple to learn, mastering its nuances can be challenging. Finally, we\u2019ll introduce the concepts this book covers.
"},{"location":"chapter-1/#go-outline","title":"Go outline","text":"If you are reading this book, it\u2019s likely that you\u2019re already sold on Go. Therefore, this section provides a brief reminder about what makes Go such a powerful language.
Software engineering has evolved considerably during the past decades. Most modern systems are no longer written by a single person but by teams consisting of multiple programmers\u2014sometimes even hundreds, if not thousands. Nowadays, code must be readable, expressive, and maintainable to guarantee a system\u2019s durability over the years. Meanwhile, in our fast-moving world, maximizing agility and reducing the time to market is critical for most organizations. Programming should also follow this trend, and companies strive to ensure that software engineers are as productive as possible when reading, writing, and maintaining code.
In response to these challenges, Google created the Go programming language in 2007. Since then, many organizations have adopted the language to support various use cases: APIs, automation, databases, CLIs (command-line interfaces), and so on. Many today consider Go the language of the cloud.
Feature-wise, Go has no type inheritance, no exceptions, no macros, no partial functions, no support for lazy variable evaluation or immutability, no operator overloading, no pattern matching, and on and on. Why are these features missing from the language? The official Go FAQ gives us some insight:
Go FAQ
Why does Go not have feature X? Your favorite feature may be missing because it doesn\u2019t fit, because it affects compilation speed or clarity of design, or because it would make the fundamental system model too difficult.
Judging the quality of a programming language via its number of features is probably not an accurate metric. At least, it\u2019s not an objective of Go. Instead, Go utilizes a few essential characteristics when adopting a language at scale for an organization. These include the following:
Go was built from the ground up with solid features such as outstanding concurrency primitives with goroutines and channels. There\u2019s not a strong need to rely on external libraries to build efficient concurrent applications. Observing how important concurrency is these days also demonstrates why Go is such a suitable language for the present and probably for the foreseeable future.
Some also consider Go a simple language. And, in a sense, this isn\u2019t necessarily wrong. For example, a newcomer can learn the language\u2019s main features in less than a day. So why read a book centered on the concept of mistakes if Go is simple?
"},{"location":"chapter-1/#simple-doesnt-mean-easy","title":"Simple doesn\u2019t mean easy","text":"There is a subtle difference between simple and easy. Simple, applied to a technology, means not complicated to learn or understand. However, easy means that we can achieve anything without much effort. Go is simple to learn but not necessarily easy to master.
Let\u2019s take concurrency, for example. In 2019, a study focusing on concurrency bugs was published: Understanding Real-World Concurrency Bugs in Go. This study was the first systematic analysis of concurrency bugs. It focused on multiple popular Go repositories such as Docker, gRPC, and Kubernetes. One of the most important takeaways from this study is that most of the blocking bugs are caused by inaccurate use of the message-passing paradigm via channels, despite the belief that message passing is easier to handle and less error-prone than sharing memory.
What should be an appropriate reaction to such a takeaway? Should we consider that the language designers were wrong about message passing? Should we reconsider how we deal with concurrency in our project? Of course not.
It\u2019s not a question of confronting message passing versus sharing memory and determining the winner. However, it\u2019s up to us as Go developers to thoroughly understand how to use concurrency, its implications on modern processors, when to favor one approach over the other, and how to avoid common traps. This example highlights that although a concept such as channels and goroutines can be simple to learn, it isn\u2019t an easy topic in practice.
This leitmotif\u2014simple doesn\u2019t mean easy\u2014can be generalized to many aspects of Go, not only concurrency. Hence, to be proficient Go developers, we must have a thorough understanding of many aspects of the language, which requires time, effort, and mistakes.
This book aims to help accelerate our journey toward proficiency by delving into 100 Go mistakes.
"},{"location":"chapter-1/#100-go-mistakes","title":"100 Go mistakes","text":"Why should we read a book about common Go mistakes? Why not deepen our knowledge with an ordinary book that would dig into different topics?
In a 2011 article, neuroscientists proved that the best time for brain growth is when we\u2019re facing mistakes. 1 Haven\u2019t we all experienced the process of learning from a mistake and recalling that occasion after months or even years, when some context related to it? As presented in another article, by Janet Metcalfe, this happens because mistakes have a facilitative effect. 2 The main idea is that we can remember not only the error but also the context surrounding the mistake. This is one of the reasons why learning from mistakes is so efficient.
To strengthen this facilitative effect, this book accompanies each mistake as much as possible with real-world examples. This book isn\u2019t only about theory; it also helps us get better at avoiding mistakes and making more well-informed, conscious decisions because we now understand the rationale behind them.
Unknown
Tell me and I forget. Teach me and I remember. Involve me and I learn.
This book presents seven main categories of mistakes. Overall, the mistakes can be classified as
We introduce each mistake category next.
"},{"location":"chapter-1/#bugs","title":"Bugs","text":"The first type of mistake and probably the most obvious is software bugs. In 2020, a study conducted by Synopsys estimated the cost of software bugs in the U.S. alone to be over $2 trillion. 3
Furthermore, bugs can also lead to tragic impacts. We can, for example, mention cases such as Therac-25, a radiation therapy machine produced by Atomic Energy of Canada Limited (AECL). Because of a race condition, the machine gave its patients radiation doses that were hundreds of times greater than expected, leading to the death of three patients. Hence, software bugs aren\u2019t only about money. As developers, we should remember how impactful our jobs are.
This book covers plenty of cases that could lead to various software bugs, including data races, leaks, logic errors, and other defects. Although accurate tests should be a way to discover such bugs as early as possible, we may sometimes miss cases because of different factors such as time constraints or complexity. Therefore, as a Go developer, it\u2019s essential to make sure we avoid common bugs.
"},{"location":"chapter-1/#needless-complexity","title":"Needless complexity","text":"The next category of mistakes is related to unnecessary complexity. A significant part of software complexity comes from the fact that, as developers, we strive to think about imaginary futures. Instead of solving concrete problems right now, it can be tempting to build evolutionary software that could tackle whatever future use case arises. However, this leads to more drawbacks than benefits in most cases because it can make a codebase more complex to understand and reason about.
Getting back to Go, we can think of plenty of use cases where developers might be tempted to design abstractions for future needs, such as interfaces or generics. This book discusses topics where we should remain careful not to harm a codebase with needless complexity.
"},{"location":"chapter-1/#weaker-readability","title":"Weaker readability","text":"Another kind of mistake is to weaken readability. As Robert C. Martin wrote in his book Clean Code: A Handbook of Agile Software Craftsmanship, the ratio of time spent reading versus writing is well over 10 to 1. Most of us started to program on solo projects where readability wasn\u2019t that important. However, today\u2019s software engineering is programming with a time dimension: making sure we can still work with and maintain an application months, years, or perhaps even decades later.
When programming in Go, we can make many mistakes that can harm readability. These mistakes may include nested code, data type representations, or not using named result parameters in some cases. Throughout this book, we will learn how to write readable code and care for future readers (including our future selves).
"},{"location":"chapter-1/#suboptimal-or-unidiomatic-organization","title":"Suboptimal or unidiomatic organization","text":"Be it while working on a new project or because we acquire inaccurate reflexes, another type of mistake is organizing our code and a project suboptimally and unidiomatically. Such issues can make a project harder to reason about and maintain. This book covers some of these common mistakes in Go. For example, we\u2019ll look at how to structure a project and deal with utility packages or init functions. All in all, looking at these mistakes should help us organize our code and projects more efficiently and idiomatically.
"},{"location":"chapter-1/#lack-of-api-convenience","title":"Lack of API convenience","text":"Making common mistakes that weaken how convenient an API is for our clients is another type of mistake. If an API isn\u2019t user-friendly, it will be less expressive and, hence, harder to understand and more error-prone.
We can think about many situations such as overusing any types, using the wrong creational pattern to deal with options, or blindly applying standard practices from object-oriented programming that affect the usability of our APIs. This book covers common mistakes that prevent us from exposing convenient APIs for our users.
"},{"location":"chapter-1/#under-optimized-code","title":"Under-optimized code","text":"Under-optimized code is another type of mistake made by developers. It can happen for various reasons, such as not understanding language features or even a lack of fundamental knowledge. Performance is one of the most obvious impacts of this mistake, but not the only one.
We can think about optimizing code for other goals, such as accuracy. For example, this book provides some common techniques to ensure that floating-point operations are accurate. Meanwhile, we will cover plenty of cases that can negatively impact performance code because of poorly parallelized executions, not knowing how to reduce allocations, or the impacts of data alignment, for example. We will tackle optimization via different prisms.
"},{"location":"chapter-1/#lack-of-productivity","title":"Lack of productivity","text":"In most cases, what\u2019s the best language we can choose when working on a new project? The one we\u2019re the most productive with. Being comfortable with how a language works and exploiting it to get the best out of it is crucial to reach proficiency.
In this book, we will cover many cases and concrete examples that will help us to be more productive while working in Go. For instance, we\u2019ll look at writing efficient tests to ensure that our code works, relying on the standard library to be more effective, and getting the best out of the profiling tools and linters. Now, it\u2019s time to delve into those 100 common Go mistakes.
"},{"location":"chapter-1/#summary","title":"Summary","text":"J. S. Moser, H. S. Schroder, et al., \u201cMind Your Errors: Evidence for a Neural Mechanism Linking Growth Mindset to Adaptive Posterror Adjustments,\u201d Psychological Science, vol. 22, no. 12, pp. 1484\u20131489, Dec. 2011.\u00a0\u21a9
J. Metcalfe, \u201cLearning from Errors,\u201d Annual Review of Psychology, vol. 68, pp. 465\u2013489, Jan. 2017.\u00a0\u21a9
Synopsys, \u201cThe Cost of Poor Software Quality in the US: A 2020 Report.\u201d 2020. https://news.synopsys.com/2021-01-06-Synopsys-Sponsored-CISQ-Research-Estimates-Cost-of-Poor-Software-Quality-in-the-US-2-08-Trillion-in-2020.\u00a0\u21a9
\u3053\u306e\u30da\u30fc\u30b8\u306f\u300e100 Go Mistakes\u300f\u306e\u5185\u5bb9\u3092\u307e\u3068\u3081\u305f\u3082\u306e\u3067\u3059\u3002\u4e00\u65b9\u3067\u3001\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u306b\u958b\u304b\u308c\u305f\u30da\u30fc\u30b8\u3067\u3082\u3042\u308a\u307e\u3059\u3002\u300c\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u300d\u304c\u65b0\u305f\u306b\u8ffd\u52a0\u3055\u308c\u308b\u3079\u304d\u3060\u3068\u304a\u8003\u3048\u3067\u3057\u305f\u3089 community mistake issue \u3092\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u6ce8\u610f\u73fe\u5728\u3001\u5927\u5e45\u306b\u591a\u304f\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u8ffd\u52a0\u3057\u3066\u5f37\u5316\u3057\u3066\u3044\u308b\u65b0\u3057\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u95b2\u89a7\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306f\u307e\u3060\u958b\u767a\u4e2d\u3067\u3059\u3002\u554f\u984c\u3092\u898b\u3064\u3051\u305f\u5834\u5408\u306f\u3069\u3046\u305e\u6c17\u8efd\u306bPR\u3092\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002
"},{"location":"ja/#_1","title":"\u30b3\u30fc\u30c9\u3068\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u69cb\u6210","text":""},{"location":"ja/#1","title":"\u610f\u56f3\u7684\u3067\u306a\u3044\u5909\u6570\u306e\u30b7\u30e3\u30c9\u30fc\u30a4\u30f3\u30b0 (#1)","text":"\u8981\u7d04\u5909\u6570\u306e\u30b7\u30e3\u30c9\u30fc\u30a4\u30f3\u30b0\u3092\u907f\u3051\u308b\u3053\u3068\u306f\u3001\u8aa4\u3063\u305f\u5909\u6570\u306e\u53c2\u7167\u3084\u8aad\u8005\u306e\u6df7\u4e71\u3092\u9632\u304e\u307e\u3059\u3002
\u5909\u6570\u306e\u30b7\u30e3\u30c9\u30fc\u30a4\u30f3\u30b0\u306f\u3001\u5909\u6570\u540d\u304c\u30d6\u30ed\u30c3\u30af\u5185\u3067\u518d\u5ba3\u8a00\u3055\u308c\u308b\u3053\u3068\u3067\u751f\u3058\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u9593\u9055\u3044\u3092\u5f15\u304d\u8d77\u3053\u3057\u3084\u3059\u304f\u3057\u307e\u3059\u3002\u5909\u6570\u306e\u30b7\u30e3\u30c9\u30fc\u30a4\u30f3\u30b0\u3092\u7981\u6b62\u3059\u308b\u304b\u3069\u3046\u304b\u306f\u500b\u4eba\u306e\u597d\u307f\u306b\u3088\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u30a8\u30e9\u30fc\u306b\u5bfe\u3057\u3066 err
\u306e\u3088\u3046\u306a\u65e2\u5b58\u306e\u5909\u6570\u540d\u3092\u518d\u5229\u7528\u3059\u308b\u3068\u4fbf\u5229\u306a\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3068\u306f\u3044\u3048\u3001\u30b3\u30fc\u30c9\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u305f\u3082\u306e\u306e\u3001\u5024\u3092\u53d7\u3051\u53d6\u3063\u305f\u5909\u6570\u304c\u4e88\u671f\u3057\u305f\u3082\u306e\u3067\u306f\u306a\u3044\u3068\u3044\u3046\u30b7\u30ca\u30ea\u30aa\u306b\u76f4\u9762\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u539f\u5247\u3068\u3057\u3066\u5f15\u304d\u7d9a\u304d\u6ce8\u610f\u3092\u6255\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#2","title":"\u4e0d\u5fc5\u8981\u306b\u30cd\u30b9\u30c8\u3055\u308c\u305f\u30b3\u30fc\u30c9 (#2)","text":"\u8981\u7d04\u30cd\u30b9\u30c8\u304c\u6df1\u304f\u306a\u3089\u306a\u3044\u3088\u3046\u306b\u3057\u3001\u30cf\u30c3\u30d4\u30fc\u30d1\u30b9\u3092\u5de6\u5074\u306b\u63c3\u3048\u308b\u3053\u3068\u3067\u30e1\u30f3\u30bf\u30eb\u30b3\u30fc\u30c9\u30e2\u30c7\u30eb\u3092\u69cb\u7bc9\u3059\u308b\u3053\u3068\u304c\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002
\u4e00\u822c\u7684\u306b\u3001\u95a2\u6570\u304c\u3088\u308a\u6df1\u3044\u30cd\u30b9\u30c8\u3092\u8981\u6c42\u3059\u308b\u307b\u3069\u3001\u8aad\u3093\u3067\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u3088\u308a\u8907\u96d1\u306b\u306a\u308a\u307e\u3059\u3002\u79c1\u305f\u3061\u306e\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3092\u6700\u9069\u5316\u3059\u308b\u305f\u3081\u306b\u3001\u3053\u306e\u30eb\u30fc\u30eb\u306e\u9069\u7528\u65b9\u6cd5\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002
if
\u30d6\u30ed\u30c3\u30af\u304c\u8fd4\u3055\u308c\u308b\u3068\u304d\u3001\u3059\u3079\u3066\u306e\u5834\u5408\u306b\u304a\u3044\u3066 else
\u30d6\u30ed\u30c3\u30af\u3092\u7701\u7565\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 \u305f\u3068\u3048\u3070\u3001\u6b21\u306e\u3088\u3046\u306b\u66f8\u304f\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002if foo() {\n// ...\nreturn true\n} else {\n// ...\n}\n
\u4ee3\u308f\u308a\u306b\u3001\u6b21\u306e\u3088\u3046\u306b else
\u30d6\u30ed\u30c3\u30af\u3092\u7701\u7565\u3057\u307e\u3059\u3002
if foo() {\n// ...\nreturn true\n}\n// ...\n
if s != \"\" {\n// ...\n} else {\nreturn errors.New(\"empty string\")\n}\n
\u3053\u3053\u3067\u306f\u3001\u7a7a\u306e s
\u304c\u30ce\u30f3\u30cf\u30c3\u30d4\u30fc\u30d1\u30b9\u3092\u8868\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6b21\u306e\u3088\u3046\u306b\u6761\u4ef6\u3092\u3072\u3063\u304f\u308a\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
if s == \"\" {\nreturn errors.New(\"empty string\")\n}\n// ...\n
\u8aad\u307f\u3084\u3059\u3044\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u306f\u3001\u3059\u3079\u3066\u306e\u958b\u767a\u8005\u306b\u3068\u3063\u3066\u91cd\u8981\u306a\u8ab2\u984c\u3067\u3059\u3002\u30cd\u30b9\u30c8\u3055\u308c\u305f\u30d6\u30ed\u30c3\u30af\u306e\u6570\u3092\u6e1b\u3089\u3059\u3088\u3046\u52aa\u3081\u3001\u30cf\u30c3\u30d4\u30fc\u30d1\u30b9\u3092\u5de6\u5074\u306b\u63c3\u3048\u3001\u3067\u304d\u308b\u3060\u3051\u65e9\u304f\u623b\u308b\u3053\u3068\u304c\u3001\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u5177\u4f53\u7684\u306a\u624b\u6bb5\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#init-3","title":"init\u95a2\u6570\u306e\u8aa4\u7528 (#3)","text":"\u8981\u7d04\u5909\u6570\u3092\u521d\u671f\u5316\u3059\u308b\u3068\u304d\u306f\u3001init\u95a2\u6570\u306e\u30a8\u30e9\u30fc\u51e6\u7406\u304c\u5236\u9650\u3055\u308c\u3066\u304a\u308a\u3001\u72b6\u614b\u306e\u51e6\u7406\u3068\u30c6\u30b9\u30c8\u304c\u3088\u308a\u8907\u96d1\u306b\u306a\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u521d\u671f\u5316\u306f\u7279\u5b9a\u306e\u95a2\u6570\u3068\u3057\u3066\u51e6\u7406\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002
init\u95a2\u6570\u306f\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u72b6\u614b\u3092\u521d\u671f\u5316\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b\u95a2\u6570\u3067\u3059\u3002\u5f15\u6570\u3092\u53d6\u3089\u305a\u3001\u7d50\u679c\u3082\u8fd4\u3057\u307e\u305b\u3093 ( func()
\u95a2\u6570)\u3002 \u30d1\u30c3\u30b1\u30fc\u30b8\u304c\u521d\u671f\u5316\u3055\u308c\u308b\u3068\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u5185\u306e\u3059\u3079\u3066\u306e\u5b9a\u6570\u304a\u3088\u3073\u5909\u6570\u306e\u5ba3\u8a00\u304c\u8a55\u4fa1\u3055\u308c\u307e\u3059\u3002\u6b21\u306b\u3001init\u95a2\u6570\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002
init\u95a2\u6570\u306f\u3044\u304f\u3064\u304b\u306e\u554f\u984c\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
init\u95a2\u6570\u306b\u306f\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u9759\u7684\u69cb\u6210\u306e\u5b9a\u7fa9\u306a\u3069\u3001\u72b6\u6cc1\u306b\u3088\u3063\u3066\u306f\u5f79\u7acb\u3064\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u308c\u4ee5\u5916\u306e\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u521d\u671f\u5316\u51e6\u7406\u306f\u30a2\u30c9\u30db\u30c3\u30af\u95a2\u6570\u3092\u901a\u3058\u3066\u884c\u308f\u308c\u308b\u3079\u304d\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#4","title":"\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u306e\u4e71\u7528 (#4)","text":"\u8981\u7d04Go\u8a00\u8a9e\u3067\u306f\u3001\u6163\u7528\u7684\u306b\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u306e\u4f7f\u7528\u3092\u5f37\u5236\u3059\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002 \u5b9f\u5229\u3092\u91cd\u8996\u3057\u3001\u52b9\u7387\u6027\u3068\u7279\u5b9a\u306e\u6163\u7fd2\u306b\u5f93\u3046\u3053\u3068\u3068\u306e\u9593\u306e\u9069\u5207\u306a\u30d0\u30e9\u30f3\u30b9\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3001\u9032\u3080\u3079\u304d\u9053\u3067\u3042\u308b\u306f\u305a\u3067\u3059\u3002
\u30c7\u30fc\u30bf\u306e\u30ab\u30d7\u30bb\u30eb\u5316\u3068\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5024\u307e\u305f\u306f\u72b6\u614b\u3092\u96a0\u3059\u3053\u3068\u3092\u6307\u3057\u307e\u3059\u3002\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u306f\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u4e0a\u306b\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3055\u308c\u305f\u30e1\u30bd\u30c3\u30c9\u3092\u63d0\u4f9b\u3059\u308b\u3053\u3068\u3067\u30ab\u30d7\u30bb\u30eb\u5316\u3092\u53ef\u80fd\u306b\u3059\u308b\u624b\u6bb5\u3067\u3059\u3002
Go\u8a00\u8a9e\u3067\u306f\u3001\u4e00\u90e8\u306e\u8a00\u8a9e\u3067\u898b\u3089\u308c\u308b\u3088\u3046\u306a\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u306e\u81ea\u52d5\u30b5\u30dd\u30fc\u30c8\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u307e\u305f\u3001\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u3092\u4f7f\u7528\u3057\u3066\u69cb\u9020\u4f53\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u306f\u5fc5\u9808\u3067\u3082\u6163\u7528\u7684\u3067\u3082\u3042\u308a\u307e\u305b\u3093\u3002\u5024\u3092\u3082\u305f\u3089\u3055\u306a\u3044\u69cb\u9020\u4f53\u306e\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u3067\u30b3\u30fc\u30c9\u3092\u57cb\u3081\u308b\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u5b9f\u5229\u3092\u91cd\u8996\u3057\u3001\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d1\u30e9\u30c0\u30a4\u30e0\u3067\u6642\u306b\u306f\u8b70\u8ad6\u306e\u4f59\u5730\u304c\u306a\u3044\u3068\u8003\u3048\u3089\u308c\u3066\u3044\u308b\u6163\u7fd2\u306b\u5f93\u3046\u3053\u3068\u3068\u3001\u52b9\u7387\u6027\u3068\u306e\u9593\u306e\u9069\u5207\u306a\u30d0\u30e9\u30f3\u30b9\u3092\u898b\u3064\u3051\u308b\u3088\u3046\u52aa\u3081\u308b\u3079\u304d\u3067\u3059\u3002
Go\u8a00\u8a9e\u306f\u3001\u30b7\u30f3\u30d7\u30eb\u3055\u3092\u542b\u3080\u591a\u304f\u306e\u7279\u6027\u3092\u8003\u616e\u3057\u3066\u8a2d\u8a08\u3055\u308c\u305f\u72ec\u81ea\u306e\u8a00\u8a9e\u3067\u3042\u308b\u3053\u3068\u3092\u5fd8\u308c\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u305f\u3060\u3057\u3001\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u306e\u5fc5\u8981\u6027\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u307e\u305f\u306f\u524d\u8ff0\u306e\u3088\u3046\u306b\u3001\u524d\u65b9\u4e92\u63db\u6027\u3092\u4fdd\u8a3c\u3057\u306a\u304c\u3089\u5c06\u6765\u306e\u5fc5\u8981\u6027\u304c\u4e88\u6e2c\u3055\u308c\u308b\u5834\u5408\u306f\u3001\u305d\u308c\u3089\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u306b\u554f\u984c\u306f\u3042\u308a\u307e\u305b\u3093\u3002
"},{"location":"ja/#5","title":"\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u6c5a\u67d3 (#5)","text":"\u8981\u7d04\u62bd\u8c61\u5316\u306f\u4f5c\u6210\u3055\u308c\u308b\u3079\u304d\u3082\u306e\u3067\u306f\u306a\u304f\u3001\u767a\u898b\u3055\u308c\u308b\u3079\u304d\u3082\u306e\u3067\u3059\u3002 \u4e0d\u5fc5\u8981\u306a\u8907\u96d1\u3055\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306f\u3001\u5fc5\u8981\u306b\u306a\u308b\u3068\u4e88\u6e2c\u3057\u305f\u3068\u304d\u3067\u306f\u306a\u304f\u3001\u5fc5\u8981\u306b\u306a\u3063\u305f\u3068\u304d\u306b\u4f5c\u6210\u3059\u308b\u304b\u3001\u5c11\u306a\u304f\u3068\u3082\u62bd\u8c61\u5316\u304c\u6709\u52b9\u3067\u3042\u308b\u3053\u3068\u3092\u8a3c\u660e\u3067\u304d\u308b\u5834\u5408\u306b\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u52d5\u4f5c\u3092\u6307\u5b9a\u3059\u308b\u65b9\u6cd5\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u8907\u6570\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5b9f\u88c5\u3067\u304d\u308b\u5171\u901a\u9805\u3092\u62bd\u51fa\u3059\u308b\u305f\u3081\u306b\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002Go\u8a00\u8a9e\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u304c\u5927\u304d\u304f\u7570\u306a\u308b\u306e\u306f\u3001\u6697\u9ed9\u7684\u306b\u6e80\u305f\u3055\u308c\u308b\u3053\u3068\u3067\u3059\u3002\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 X
\u304c\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9 Y
\u3092\u5b9f\u88c5\u3057\u3066\u3044\u308b\u3053\u3068\u3092\u793a\u3059 implements
\u306e\u3088\u3046\u306a\u660e\u793a\u7684\u306a\u30ad\u30fc\u30ef\u30fc\u30c9\u306f\u3042\u308a\u307e\u305b\u3093\u3002
\u4e00\u822c\u306b\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u304c\u4fa1\u5024\u3092\u3082\u305f\u3089\u3059\u3068\u8003\u3048\u3089\u308c\u308b\u4e3b\u8981\u306a\u4f7f\u7528\u4f8b\u306f3\u3064\u3042\u308a\u307e\u3059\u3002\u305d\u308c\u306f\u3001\u5171\u901a\u306e\u52d5\u4f5c\u3092\u9664\u5916\u3059\u308b\u3001\u4f55\u3089\u304b\u306e\u5206\u96e2\u3092\u4f5c\u6210\u3059\u308b\u3001\u304a\u3088\u3073\u578b\u3092\u7279\u5b9a\u306e\u52d5\u4f5c\u306b\u5236\u9650\u3059\u308b\u3068\u3044\u3046\u3082\u306e\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u3053\u306e\u30ea\u30b9\u30c8\u306f\u3059\u3079\u3066\u3092\u7db2\u7f85\u3057\u3066\u3044\u308b\u308f\u3051\u3067\u306f\u306a\u304f\u3001\u76f4\u9762\u3059\u308b\u72b6\u6cc1\u306b\u3088\u3063\u3066\u3082\u7570\u306a\u308a\u307e\u3059\u3002
\u591a\u304f\u306e\u5834\u5408\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306f\u62bd\u8c61\u5316\u3059\u308b\u305f\u3081\u306b\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u62bd\u8c61\u5316\u3059\u308b\u3068\u304d\u306e\u4e3b\u306a\u6ce8\u610f\u70b9\u306f\u3001\u62bd\u8c61\u5316\u306f\u4f5c\u6210\u3055\u308c\u308b\u3079\u304d\u3067\u306f\u306a\u304f\u3001\u767a\u898b\u3055\u308c\u308b\u3079\u304d\u3067\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304f\u3053\u3068\u3067\u3059\u3002\u3059\u306a\u308f\u3061\u3001\u305d\u3046\u3059\u308b\u76f4\u63a5\u306e\u7406\u7531\u304c\u306a\u3044\u9650\u308a\u3001\u30b3\u30fc\u30c9\u5185\u3067\u62bd\u8c61\u5316\u3059\u3079\u304d\u3067\u306f\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002 \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u4f7f\u3063\u3066\u8a2d\u8a08\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u5177\u4f53\u7684\u306a\u30cb\u30fc\u30ba\u3092\u5f85\u3064\u3079\u304d\u3067\u3059\u3002\u5225\u306e\u8a00\u3044\u65b9\u3092\u3059\u308c\u3070\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306f\u5fc5\u8981\u306b\u306a\u308b\u3068\u4e88\u6e2c\u3057\u305f\u3068\u304d\u3067\u306f\u306a\u304f\u3001\u5fc5\u8981\u306b\u306a\u3063\u305f\u3068\u304d\u306b\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306e\u904e\u5ea6\u306a\u4f7f\u7528\u3092\u3057\u305f\u5834\u5408\u306e\u4e3b\u306a\u554f\u984c\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\u3002\u7b54\u3048\u306f\u3001\u30b3\u30fc\u30c9\u30d5\u30ed\u30fc\u304c\u3088\u308a\u8907\u96d1\u306b\u306a\u308b\u3053\u3068\u3067\u3059\u3002\u5f79\u306b\u7acb\u305f\u306a\u3044\u9593\u63a5\u53c2\u7167\u3092\u8ffd\u52a0\u3057\u3066\u3082\u4f55\u306e\u4fa1\u5024\u3082\u3042\u308a\u307e\u305b\u3093\u3002\u305d\u308c\u306f\u4fa1\u5024\u306e\u306a\u3044\u62bd\u8c61\u5316\u3092\u3059\u308b\u3053\u3068\u3067\u3001\u30b3\u30fc\u30c9\u3092\u8aad\u307f\u3001\u7406\u89e3\u3057\u3001\u63a8\u8ad6\u3059\u308b\u3053\u3068\u3092\u3055\u3089\u306b\u56f0\u96e3\u306b\u3057\u307e\u3059\u3002\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u8ffd\u52a0\u3059\u308b\u660e\u78ba\u306a\u7406\u7531\u304c\u306a\u304f\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306b\u3088\u3063\u3066\u30b3\u30fc\u30c9\u304c\u3069\u306e\u3088\u3046\u306b\u6539\u5584\u3055\u308c\u308b\u304b\u304c\u4e0d\u660e\u77ad\u306a\u5834\u5408\u306f\u3001\u305d\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306e\u76ee\u7684\u306b\u7570\u8b70\u3092\u5531\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5b9f\u88c5\u3092\u76f4\u63a5\u547c\u3073\u51fa\u3059\u306e\u3082\u4e00\u3064\u306e\u624b\u3067\u3059\u3002
\u30b3\u30fc\u30c9\u5185\u3067\u62bd\u8c61\u5316\u3059\u308b\u3068\u304d\u306f\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059 (\u62bd\u8c61\u5316\u306f\u4f5c\u6210\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u767a\u898b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059)\u3002\u5f8c\u3067\u5fc5\u8981\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3082\u306e\u3092\u8003\u616e\u3057\u3001\u5b8c\u74a7\u306a\u62bd\u8c61\u5316\u30ec\u30d9\u30eb\u3092\u63a8\u6e2c\u3057\u3066\u3001\u79c1\u305f\u3061\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u958b\u767a\u8005\u306f\u30b3\u30fc\u30c9\u3092\u30aa\u30fc\u30d0\u30fc\u30a8\u30f3\u30b8\u30cb\u30a2\u30ea\u30f3\u30b0\u3059\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30b3\u30fc\u30c9\u304c\u4e0d\u5fc5\u8981\u306a\u62bd\u8c61\u5316\u3067\u6c5a\u67d3\u3055\u308c\u3001\u8aad\u307f\u306b\u304f\u304f\u306a\u308b\u305f\u3081\u3001\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u907f\u3051\u308b\u3079\u304d\u3067\u3059\u3002
\u30ed\u30d6\u30fb\u30d1\u30a4\u30af
\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3067\u30c7\u30b6\u30a4\u30f3\u3059\u308b\u306a\u3002\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u898b\u3064\u3051\u51fa\u305b\u3002
\u62bd\u8c61\u7684\u306b\u554f\u984c\u3092\u89e3\u6c7a\u3057\u3088\u3046\u3068\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u4eca\u89e3\u6c7a\u3059\u3079\u304d\u3053\u3068\u3092\u89e3\u6c7a\u3057\u307e\u3057\u3087\u3046\u3002\u6700\u5f8c\u306b\u91cd\u8981\u306a\u3053\u3068\u3067\u3059\u304c\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306b\u3088\u3063\u3066\u30b3\u30fc\u30c9\u304c\u3069\u306e\u3088\u3046\u306b\u6539\u5584\u3055\u308c\u308b\u304b\u304c\u4e0d\u660e\u77ad\u306a\u5834\u5408\u306f\u3001\u30b3\u30fc\u30c9\u3092\u7c21\u7d20\u5316\u3059\u308b\u305f\u3081\u306b\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u524a\u9664\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3067\u3057\u3087\u3046\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#6","title":"\u30d7\u30ed\u30c7\u30e5\u30fc\u30b5\u30fc\u5074\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9 (#6)","text":"\u8981\u7d04\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u3067\u4fdd\u6301\u3059\u308b\u3053\u3068\u3067\u4e0d\u5fc5\u8981\u306a\u62bd\u8c61\u5316\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002
Go\u8a00\u8a9e\u3067\u306f\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u304c\u6697\u9ed9\u7684\u306b\u6e80\u305f\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u660e\u793a\u7684\u306a\u5b9f\u88c5\u3092\u6301\u3064\u8a00\u8a9e\u3068\u6bd4\u8f03\u3057\u3066\u5927\u304d\u306a\u5909\u5316\u3092\u3082\u305f\u3089\u3059\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u5f93\u3046\u3079\u304d\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u524d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u8aac\u660e\u3057\u305f\u3082\u306e\u2015\u2015\u62bd\u8c61\u306f\u4f5c\u6210\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u767a\u898b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u2015\u2015\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u3059\u3079\u3066\u306e\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u5bfe\u3057\u3066\u7279\u5b9a\u306e\u62bd\u8c61\u5316\u3092\u5f37\u5236\u3059\u308b\u306e\u306f\u30d7\u30ed\u30c7\u30e5\u30fc\u30b5\u30fc\u306e\u5f79\u5272\u3067\u306f\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u4ee3\u308f\u308a\u306b\u3001\u4f55\u3089\u304b\u306e\u5f62\u5f0f\u306e\u62bd\u8c61\u5316\u304c\u5fc5\u8981\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3057\u3001\u305d\u306e\u30cb\u30fc\u30ba\u306b\u6700\u9069\u306a\u62bd\u8c61\u5316\u30ec\u30d9\u30eb\u3092\u6c7a\u5b9a\u3059\u308b\u306e\u306f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u8cac\u4efb\u3067\u3059\u3002
\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306f\u30b3\u30f3\u30b7\u30e5\u30fc\u30de\u30fc\u5074\u306b\u5b58\u5728\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u7279\u5b9a\u306e\u72b6\u6cc1 (\u305f\u3068\u3048\u3070\u3001\u62bd\u8c61\u5316\u304c\u30b3\u30f3\u30b7\u30e5\u30fc\u30de\u30fc\u306b\u3068\u3063\u3066\u5f79\u7acb\u3064\u3053\u3068\u304c\u308f\u304b\u3063\u3066\u3044\u308b\u2015\u2015\u4e88\u6e2c\u306f\u3057\u3066\u3044\u306a\u3044\u2015\u2015\u5834\u5408) \u3067\u306f\u3001\u305d\u308c\u3092\u30d7\u30ed\u30c7\u30e5\u30fc\u30b5\u30fc\u5074\u3067\u4f7f\u7528\u3057\u305f\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3046\u3057\u305f\u5834\u5408\u3001\u53ef\u80fd\u306a\u9650\u308a\u6700\u5c0f\u9650\u306b\u6291\u3048\u3001\u518d\u5229\u7528\u53ef\u80fd\u6027\u3092\u9ad8\u3081\u3001\u3088\u308a\u7c21\u5358\u306b\u69cb\u6210\u3067\u304d\u308b\u3088\u3046\u306b\u52aa\u3081\u308b\u3079\u304d\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#7","title":"\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u8fd4\u3059 (#7)","text":"\u8981\u7d04\u67d4\u8edf\u6027\u306b\u554f\u984c\u304c\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u95a2\u6570\u306f\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3067\u306f\u306a\u304f\u5177\u4f53\u7684\u200b\u200b\u306a\u5b9f\u88c5\u3092\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u9006\u306b\u3001\u95a2\u6570\u306f\u53ef\u80fd\u306a\u9650\u308a\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u53d7\u3051\u5165\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3067\u306f\u306a\u304f\u5177\u4f53\u7684\u200b\u200b\u306a\u5b9f\u88c5\u3092\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3046\u3067\u306a\u3044\u3068\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u4f9d\u5b58\u95a2\u4fc2\u306b\u3088\u308a\u8a2d\u8a08\u304c\u3044\u3063\u305d\u3046\u8907\u96d1\u306b\u306a\u308a\u3001\u3059\u3079\u3066\u306e\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u540c\u3058\u62bd\u8c61\u5316\u306b\u4f9d\u5b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u67d4\u8edf\u6027\u306b\u6b20\u3051\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u7d50\u8ad6\u306f\u524d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002\u62bd\u8c61\u5316\u304c\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u3068\u3063\u3066\u5f79\u7acb\u3064\u3053\u3068\u304c (\u4e88\u6e2c\u3067\u306f\u306a\u304f) \u308f\u304b\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u8fd4\u3059\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u3082\u3088\u3044\u3067\u3057\u3087\u3046\u3002\u305d\u308c\u4ee5\u5916\u306e\u5834\u5408\u306f\u3001\u62bd\u8c61\u5316\u3092\u5f37\u5236\u3059\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u305d\u308c\u3089\u306f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u3088\u3063\u3066\u767a\u898b\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4f55\u3089\u304b\u306e\u7406\u7531\u3067\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u5b9f\u88c5\u3092\u62bd\u8c61\u5316\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u3067\u3082\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u3067\u305d\u308c\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
"},{"location":"ja/#97","title":"\u30a4\u30f3\u30e9\u30a4\u30f3\u5c55\u958b\u3092\u3057\u3066\u3044\u306a\u3044 (#97)","text":"\u8981\u7d04\u30d5\u30a1\u30b9\u30c8\u30d1\u30b9\u306e\u30a4\u30f3\u30e9\u30a4\u30f3\u5316\u624b\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u3001\u95a2\u6570\u306e\u547c\u3073\u51fa\u3057\u306b\u304b\u304b\u308b\u511f\u5374\u6642\u9593\u3092\u52b9\u7387\u7684\u306b\u524a\u6e1b\u3057\u307e\u3059\u3002
"},{"location":"ja/#go-98","title":"Go\u8a00\u8a9e\u306e\u8a3a\u65ad\u30c4\u30fc\u30eb\u3092\u5229\u7528\u3057\u3066\u3044\u306a\u3044 (#98)","text":"\u8981\u7d04\u30d7\u30ed\u30d5\u30a1\u30a4\u30ea\u30f3\u30b0\u3068\u5b9f\u884c\u30c8\u30ec\u30fc\u30b5\u3092\u5229\u7528\u3057\u3066\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u6700\u9069\u5316\u3059\u3079\u304d\u90e8\u5206\u306b\u3064\u3044\u3066\u7406\u89e3\u3057\u307e\u3057\u3087\u3046\u3002
\u30bb\u30af\u30b7\u30e7\u30f3\u5168\u6587\u306f\u3053\u3061\u3089\u3002
"},{"location":"ja/#gc-99","title":"GC\u306e\u4ed5\u7d44\u307f\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#99)","text":"\u8981\u7d04GC\u306e\u8abf\u6574\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3068\u3001\u7a81\u7136\u306e\u8ca0\u8377\u306e\u5897\u52a0\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u51e6\u7406\u3067\u304d\u308b\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u6069\u6075\u304c\u5f97\u3089\u308c\u307e\u3059\u3002
"},{"location":"ja/#dockerkubernetesgo-100","title":"Docker\u3068Kubernetes\u4e0a\u3067Go\u8a00\u8a9e\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u306e\u5f71\u97ff\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#100)","text":"\u8981\u7d04Docker\u3068Kubernetes\u306b\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u969b\u306eCPU\u30b9\u30ed\u30c3\u30c8\u30ea\u30f3\u30b0\u3092\u56de\u907f\u3059\u308b\u306b\u306f\u3001Go\u8a00\u8a9e\u304cCFS\u5bfe\u5fdc\u3067\u306f\u306a\u3044\u3053\u3068\u306b\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
"},{"location":"jobs/","title":"Go Jobs","text":"Is your company hiring? Sponsor this repository and let a significant audience of Go developers know about your opportunities in this section (book sales: +8k, website traffic: +2000 views and +400 unique visitors per week).
"},{"location":"zh/","title":"100\u4e2aGo\u5e38\u89c1\u9519\u8bef\u53ca\u5982\u4f55\u907f\u514d","text":""},{"location":"zh/#_1","title":"\u4ee3\u7801\u53ca\u5de5\u7a0b\u7ec4\u7ec7","text":""},{"location":"zh/#1","title":"\u610f\u5916\u7684\u53d8\u91cf\u9690\u85cf (#1)","text":"\u907f\u514d\u53d8\u91cf\u9690\u85cf\uff08\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\u88ab\u5185\u90e8\u4f5c\u7528\u57df\u540c\u540d\u53d8\u91cf\u9690\u85cf\uff09\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u53d8\u91cf\u5f15\u7528\u9519\u8bef\uff0c\u6709\u52a9\u4e8e\u4ed6\u4eba\u9605\u8bfb\u7406\u89e3\u3002
"},{"location":"zh/#2","title":"\u4e0d\u5fc5\u8981\u7684\u4ee3\u7801\u5d4c\u5957 (#2)","text":"\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u3001\u8fc7\u591a\u7684\u5d4c\u5957\u5c42\u6b21\uff0c\u5e76\u4e14\u8ba9\u6b63\u5e38\u4ee3\u7801\u8def\u5f84\u5c3d\u91cf\u5de6\u5bf9\u9f50\uff08\u800c\u4e0d\u662f\u653e\u5728\u5206\u652f\u8def\u5f84\u4e2d\uff09\uff0c\u6709\u52a9\u4e8e\u6784\u5efa\u53ef\u8bfb\u6027\u66f4\u597d\u7684\u4ee3\u7801\u3002
"},{"location":"zh/#init-3","title":"\u8bef\u7528init\u51fd\u6570 (#3)","text":"\u521d\u59cb\u5316\u53d8\u91cf\u65f6\uff0c\u8bf7\u8bb0\u4f4f init \u51fd\u6570\u5177\u6709\u6709\u9650\u7684\u9519\u8bef\u5904\u7406\u80fd\u529b\uff0c\u5e76\u4e14\u4f1a\u4f7f\u72b6\u6001\u5904\u7406\u548c\u6d4b\u8bd5\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u521d\u59cb\u5316\u5e94\u8be5\u4f5c\u4e3a\u7279\u5b9a\u51fd\u6570\u6765\u5904\u7406\u3002
"},{"location":"zh/#getterssetters-4","title":"\u6ee5\u7528getters/setters (#4)","text":"\u5728Go\u8bed\u8a00\u4e2d\uff0c\u5f3a\u5236\u4f7f\u7528getter\u548csetter\u65b9\u6cd5\u5e76\u4e0d\u7b26\u5408Go\u60ef\u4f8b\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u5e94\u8be5\u627e\u5230\u6548\u7387\u548c\u76f2\u76ee\u9075\u5faa\u67d0\u4e9b\u60ef\u7528\u6cd5\u4e4b\u95f4\u7684\u5e73\u8861\u70b9\u3002
"},{"location":"zh/#5","title":"\u63a5\u53e3\u6c61\u67d3 (#5)","text":"\u62bd\u8c61\u5e94\u8be5\u88ab\u53d1\u73b0\uff0c\u800c\u4e0d\u662f\u88ab\u521b\u9020\u3002\u4e3a\u4e86\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\uff0c\u9700\u8981\u65f6\u624d\u521b\u5efa\u63a5\u53e3\uff0c\u800c\u4e0d\u662f\u9884\u89c1\u5230\u9700\u8981\u5b83\uff0c\u6216\u8005\u81f3\u5c11\u53ef\u4ee5\u8bc1\u660e\u8fd9\u79cd\u62bd\u8c61\u662f\u6709\u4ef7\u503c\u7684\u3002
"},{"location":"zh/#6","title":"\u5c06\u63a5\u53e3\u5b9a\u4e49\u5728\u5b9e\u73b0\u65b9\u4e00\u4fa7 (#6)","text":"\u5c06\u63a5\u53e3\u4fdd\u7559\u5728\u5f15\u7528\u65b9\u4e00\u4fa7\uff08\u800c\u4e0d\u662f\u5b9e\u73b0\u65b9\u4e00\u4fa7\uff09\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62bd\u8c61\u3002
"},{"location":"zh/#7","title":"\u5c06\u63a5\u53e3\u4f5c\u4e3a\u8fd4\u56de\u503c (#7)","text":"\u4e3a\u4e86\u907f\u514d\u5728\u7075\u6d3b\u6027\u65b9\u9762\u53d7\u5230\u9650\u5236\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u51fd\u6570\u4e0d\u5e94\u8be5\u8fd4\u56de\u63a5\u53e3\uff0c\u800c\u5e94\u8be5\u8fd4\u56de\u5177\u4f53\u7684\u5b9e\u73b0\u3002\u76f8\u53cd\uff0c\u51fd\u6570\u5e94\u8be5\u5c3d\u53ef\u80fd\u5730\u4f7f\u7528\u63a5\u53e3\u4f5c\u4e3a\u53c2\u6570\u3002
"},{"location":"zh/#any-8","title":"any
\u6ca1\u4f20\u9012\u4efb\u4f55\u4fe1\u606f (#8)","text":"\u53ea\u6709\u5728\u9700\u8981\u63a5\u53d7\u6216\u8fd4\u56de\u4efb\u610f\u7c7b\u578b\u65f6\uff0c\u624d\u4f7f\u7528 any
\uff0c\u4f8b\u5982 json.Marshal
\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u56e0\u4e3a any
\u4e0d\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u4fe1\u606f\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u65f6\u95ee\u9898\uff0c\u5982\u5141\u8bb8\u8c03\u7528\u8005\u8c03\u7528\u65b9\u6cd5\u5904\u7406\u4efb\u610f\u7c7b\u578b\u6570\u636e\u3002
\u4f7f\u7528\u6cdb\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u53c2\u6570\u5206\u79bb\u5177\u4f53\u7684\u6570\u636e\u7c7b\u578b\u548c\u884c\u4e3a\uff0c\u907f\u514d\u5199\u5f88\u591a\u91cd\u590d\u5ea6\u5f88\u9ad8\u7684\u4ee3\u7801\u3002\u7136\u800c\uff0c\u4e0d\u8981\u8fc7\u65e9\u5730\u4f7f\u7528\u6cdb\u578b\u3001\u7c7b\u578b\u53c2\u6570\uff0c\u53ea\u6709\u5728\u4f60\u770b\u5230\u771f\u6b63\u9700\u8981\u65f6\u624d\u4f7f\u7528\u3002\u5426\u5219\uff0c\u5b83\u4eec\u4f1a\u5f15\u5165\u4e0d\u5fc5\u8981\u7684\u62bd\u8c61\u548c\u590d\u6742\u6027\u3002
"},{"location":"zh/#10","title":"\u672a\u610f\u8bc6\u5230\u7c7b\u578b\u5d4c\u5957\u7684\u53ef\u80fd\u95ee\u9898 (#10)","text":"\u4f7f\u7528\u7c7b\u578b\u5d4c\u5957\u4e5f\u53ef\u4ee5\u907f\u514d\u5199\u4e00\u4e9b\u91cd\u590d\u4ee3\u7801\uff0c\u7136\u800c\uff0c\u5728\u4f7f\u7528\u65f6\u9700\u8981\u786e\u4fdd\u4e0d\u4f1a\u5bfc\u81f4\u4e0d\u5408\u7406\u7684\u53ef\u89c1\u6027\u95ee\u9898\uff0c\u6bd4\u5982\u6709\u4e9b\u5b57\u6bb5\u5e94\u8be5\u5bf9\u5916\u9690\u85cf\u4e0d\u5e94\u8be5\u88ab\u66b4\u6f0f\u3002
"},{"location":"zh/#function-option-11","title":"\u4e0d\u4f7f\u7528function option\u6a21\u5f0f (#11)","text":"\u4e3a\u4e86\u8bbe\u8ba1\u5e76\u63d0\u4f9b\u66f4\u53cb\u597d\u7684API\uff08\u53ef\u9009\u53c2\u6570\uff09\uff0c\u4e3a\u4e86\u66f4\u597d\u5730\u5904\u7406\u9009\u9879\uff0c\u5e94\u8be5\u4f7f\u7528function option\u6a21\u5f0f\u3002
"},{"location":"zh/#12","title":"\u5de5\u7a0b\u7ec4\u7ec7\u4e0d\u5408\u7406 (\u5de5\u7a0b\u7ed3\u6784\u548c\u5305\u7684\u7ec4\u7ec7) (#12)","text":"\u9075\u5faa\u50cf project-layout \u7684\u5efa\u8bae\u6765\u7ec4\u7ec7Go\u5de5\u7a0b\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u4f60\u6b63\u5728\u5bfb\u627e\u4e00\u4e9b\u7c7b\u4f3c\u7684\u7ecf\u9a8c\u3001\u60ef\u4f8b\u6765\u7ec4\u7ec7\u4e00\u4e2a\u65b0\u7684Go\u5de5\u7a0b\u7684\u65f6\u5019\u3002
"},{"location":"zh/#13","title":"\u521b\u5efa\u5de5\u5177\u5305 (#13)","text":"\u547d\u540d\u662f\u8f6f\u4ef6\u8bbe\u8ba1\u5f00\u53d1\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u90e8\u5206\uff0c\u521b\u5efa\u4e00\u4e9b\u540d\u5982 common
\u3001util
\u3001shared
\u4e4b\u7c7b\u7684\u5305\u540d\u5e76\u4e0d\u4f1a\u7ed9\u8bfb\u8005\u5e26\u6765\u592a\u5927\u4ef7\u503c\uff0c\u5e94\u8be5\u5c06\u8fd9\u4e9b\u5305\u540d\u91cd\u6784\u4e3a\u66f4\u6e05\u6670\u3001\u66f4\u805a\u7126\u7684\u5305\u540d\u3002
\u4e3a\u4e86\u907f\u514d\u53d8\u91cf\u540d\u548c\u5305\u540d\u4e4b\u95f4\u7684\u51b2\u7a81\uff0c\u5bfc\u81f4\u6df7\u6dc6\u6216\u751a\u81f3\u9519\u8bef\uff0c\u5e94\u4e3a\u6bcf\u4e2a\u53d8\u91cf\u548c\u5305\u4f7f\u7528\u552f\u4e00\u7684\u540d\u79f0\u3002\u5982\u679c\u8fd9\u4e0d\u53ef\u884c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5bfc\u5165\u522b\u540d import importAlias 'importPath'
\uff0c\u4ee5\u533a\u5206\u5305\u540d\u548c\u53d8\u91cf\u540d\uff0c\u6216\u8005\u8003\u8651\u4e00\u4e2a\u66f4\u597d\u7684\u53d8\u91cf\u540d\u3002
\u4e3a\u4e86\u8ba9\u4f7f\u7528\u65b9\u3001\u7ef4\u62a4\u4eba\u5458\u80fd\u66f4\u6e05\u6670\u5730\u4e86\u89e3\u4f60\u7684\u4ee3\u7801\u7684\u610f\u56fe\uff0c\u5bfc\u51fa\u7684\u5143\u7d20\uff08\u51fd\u6570\u3001\u7c7b\u578b\u3001\u5b57\u6bb5\uff09\u9700\u8981\u6dfb\u52a0godoc\u6ce8\u91ca\u3002
"},{"location":"zh/#linters-16","title":"\u4e0d\u4f7f\u7528linters\u68c0\u67e5 (#16)","text":"\u4e3a\u4e86\u6539\u5584\u4ee3\u7801\u8d28\u91cf\u3001\u6574\u4f53\u4ee3\u7801\u7684\u4e00\u81f4\u6027\uff0c\u5e94\u8be5\u4f7f\u7528linters\u3001formatters\u3002
"},{"location":"zh/#_2","title":"\u6570\u636e\u7c7b\u578b","text":""},{"location":"zh/#17","title":"\u516b\u8fdb\u5236\u5b57\u9762\u91cf\u5f15\u53d1\u7684\u56f0\u60d1 (#17)","text":"\u5728\u9605\u8bfb\u73b0\u6709\u4ee3\u7801\u65f6\uff0c\u8bf7\u8bb0\u4f4f\u4ee5 0 \u5f00\u5934\u7684\u6574\u6570\u5b57\u9762\u91cf\u662f\u516b\u8fdb\u5236\u6570\u3002\u6b64\u5916\uff0c\u4e3a\u4e86\u63d0\u9ad8\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u524d\u9762\u52a0\u4e0a 0o \u6765\u663e\u5f0f\u5730\u8868\u793a\u516b\u8fdb\u5236\u6574\u6570\u3002
"},{"location":"zh/#18","title":"\u672a\u6ce8\u610f\u53ef\u80fd\u7684\u6574\u6570\u6ea2\u51fa (#18)","text":"\u5728 Go \u4e2d\u6574\u6570\u4e0a\u6ea2\u51fa\u548c\u4e0b\u6ea2\u662f\u9759\u9ed8\u5904\u7406\u7684\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5df1\u7684\u51fd\u6570\u6765\u6355\u83b7\u5b83\u4eec\u3002
"},{"location":"zh/#19","title":"\u6ca1\u6709\u900f\u5f7b\u7406\u89e3\u6d6e\u70b9\u6570 (#19)","text":"\u6bd4\u8f83\u6d6e\u70b9\u6570\u65f6\uff0c\u901a\u8fc7\u6bd4\u8f83\u4e8c\u8005\u7684delta\u503c\u662f\u5426\u4ecb\u4e8e\u4e00\u5b9a\u7684\u8303\u56f4\u5185\uff0c\u80fd\u8ba9\u4f60\u5199\u51fa\u53ef\u79fb\u690d\u6027\u66f4\u597d\u7684\u4ee3\u7801\u3002
\u5728\u8fdb\u884c\u52a0\u6cd5\u6216\u51cf\u6cd5\u65f6\uff0c\u5c06\u5177\u6709\u76f8\u4f3c\u6570\u91cf\u7ea7\u7684\u64cd\u4f5c\u5206\u6210\u540c\u4e00\u7ec4\u4ee5\u63d0\u9ad8\u7cbe\u5ea6 (\u8fc7\u65e9\u6307\u6570\u5bf9\u9f50\u4e22\u5931\u7cbe\u5ea6)\u3002\u6b64\u5916\uff0c\u5728\u8fdb\u884c\u52a0\u6cd5\u548c\u51cf\u6cd5\u4e4b\u524d\uff0c\u5e94\u5148\u8fdb\u884c\u4e58\u6cd5\u548c\u9664\u6cd5 (\u52a0\u51cf\u6cd5\u8bef\u5dee\u4f1a\u88ab\u4e58\u9664\u653e\u5927)\u3002
"},{"location":"zh/#slice-20","title":"\u4e0d\u7406\u89e3slice\u7684\u957f\u5ea6\u548c\u5bb9\u91cf (#20)","text":"\u7406\u89e3slice\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u7684\u533a\u522b\uff0c\u662f\u4e00\u4e2aGo\u5f00\u53d1\u8005\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\u4e4b\u4e00\u3002slice\u7684\u957f\u5ea6\u6307\u7684\u662fslice\u5df2\u7ecf\u5b58\u50a8\u7684\u5143\u7d20\u7684\u6570\u91cf\uff0c\u800c\u5bb9\u91cf\u6307\u7684\u662fslice\u5f53\u524d\u5e95\u5c42\u5f00\u8f9f\u7684\u6570\u7ec4\u6700\u591a\u80fd\u5bb9\u7eb3\u7684\u5143\u7d20\u7684\u6570\u91cf\u3002
"},{"location":"zh/#slice-21","title":"\u4e0d\u9ad8\u6548\u7684slice\u521d\u59cb\u5316 (#21)","text":"\u5f53\u521b\u5efa\u4e00\u4e2aslice\u65f6\uff0c\u5982\u679c\u5176\u957f\u5ea6\u53ef\u4ee5\u9884\u5148\u786e\u5b9a\uff0c\u90a3\u4e48\u53ef\u4ee5\u5728\u5b9a\u4e49\u65f6\u6307\u5b9a\u5b83\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u3002\u8fd9\u53ef\u4ee5\u6539\u5584\u540e\u671fappend\u65f6\u4e00\u6b21\u6216\u8005\u591a\u6b21\u7684\u5185\u5b58\u5206\u914d\u64cd\u4f5c\uff0c\u4ece\u800c\u6539\u5584\u6027\u80fd\u3002\u5bf9\u4e8emap\u7684\u521d\u59cb\u5316\u4e5f\u662f\u8fd9\u6837\u7684\u3002
"},{"location":"zh/#nilslice-22","title":"\u56f0\u60d1\u4e8enil\u548c\u7a7aslice (#22)","text":"\u4e3a\u4e86\u907f\u514d\u5e38\u89c1\u7684\u5bf9nil\u548cempty slice\u5904\u7406\u884c\u4e3a\u7684\u6df7\u6dc6\uff0c\u4f8b\u5982\u5728\u4f7f\u7528 encoding/json \u6216 reflect \u5305\u65f6\uff0c\u4f60\u9700\u8981\u7406\u89e3 nil \u548c empty slice\u7684\u533a\u522b\u3002\u4e24\u8005\u90fd\u662f\u957f\u5ea6\u4e3a\u96f6\u3001\u5bb9\u91cf\u4e3a\u96f6\u7684\u5207\u7247\uff0c\u4f46\u662f nil \u5207\u7247\u4e0d\u9700\u8981\u5206\u914d\u5185\u5b58\u3002
"},{"location":"zh/#slice-23","title":"\u6ca1\u6709\u9002\u5f53\u68c0\u67e5slice\u662f\u5426\u4e3a\u7a7a (#23)","text":"\u68c0\u67e5\u4e00\u4e2aslice\u7684\u662f\u5426\u5305\u542b\u4efb\u4f55\u5143\u7d20\uff0c\u53ef\u4ee5\u68c0\u67e5\u5176\u957f\u5ea6\uff0c\u4e0d\u7ba1slice\u662fnil\u8fd8\u662fempty\uff0c\u68c0\u67e5\u957f\u5ea6\u90fd\u662f\u6709\u6548\u7684\u3002\u8fd9\u4e2a\u68c0\u67e5\u65b9\u6cd5\u4e5f\u9002\u7528\u4e8emap\u3002
\u4e3a\u4e86\u8bbe\u8ba1\u66f4\u660e\u786e\u7684API\uff0cAPI\u4e0d\u5e94\u533a\u5206nil\u548c\u7a7a\u5207\u7247\u3002
"},{"location":"zh/#slice-24","title":"\u6ca1\u6709\u6b63\u786e\u62f7\u8d1dslice (#24)","text":"\u4f7f\u7528 copy
\u62f7\u8d1d\u4e00\u4e2aslice\u5143\u7d20\u5230\u53e6\u4e00\u4e2aslice\u65f6\uff0c\u9700\u8981\u8bb0\u5f97\uff0c\u5b9e\u9645\u62f7\u8d1d\u7684\u5143\u7d20\u6570\u91cf\u662f\u4e8c\u8005slice\u957f\u5ea6\u4e2d\u7684\u8f83\u5c0f\u503c\u3002
\u5982\u679c\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u64cd\u4f5c\u7684slice\u590d\u7528\u4e86\u76f8\u540c\u7684\u5e95\u5c42\u6570\u7ec4\uff0c\u5b83\u4eec\u5bf9slice\u6267\u884cappend\u64cd\u4f5c\u65f6\u53ef\u80fd\u4f1a\u4ea7\u751f\u51b2\u7a81\u3002\u4f7f\u7528copy\u6765\u5b8c\u6574\u590d\u5236\u4e00\u4e2aslice\u6216\u8005\u4f7f\u7528\u5b8c\u6574\u7684slice\u8868\u8fbe\u5f0f[low:high:max]\u9650\u5236\u6700\u5927\u5bb9\u91cf\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u4ea7\u751f\u51b2\u7a81\u3002\u5f53\u60f3\u5bf9\u4e00\u4e2a\u5927slice\u8fdb\u884cshrink\u64cd\u4f5c\u65f6\uff0c\u4e24\u79cd\u65b9\u5f0f\u4e2d\uff0c\u53ea\u6709copy\u624d\u53ef\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002
"},{"location":"zh/#slice-26","title":"slice\u548c\u5185\u5b58\u6cc4\u6f0f (#26)","text":"\u5bf9\u4e8eslice\u5143\u7d20\u4e3a\u6307\u9488\uff0c\u6216\u8005slice\u5143\u7d20\u4e3astruct\u4f46\u662f\u8be5struct\u542b\u6709\u6307\u9488\u5b57\u6bb5\uff0c\u5f53\u901a\u8fc7slice[low:high]\u64cd\u4f5c\u53d6subslice\u65f6\uff0c\u5bf9\u4e8e\u90a3\u4e9b\u4e0d\u53ef\u8bbf\u95ee\u7684\u5143\u7d20\u53ef\u4ee5\u663e\u793a\u8bbe\u7f6e\u4e3anil\u6765\u907f\u514d\u5185\u5b58\u6cc4\u9732\u3002
"},{"location":"zh/#map-27","title":"\u4e0d\u9ad8\u6548\u7684map\u521d\u59cb\u5316 (#27)","text":"\u89c1 #21.
"},{"location":"zh/#map-28","title":"map\u548c\u5185\u5b58\u6cc4\u6f0f (#28)","text":"\u4e00\u4e2amap\u7684buckets\u5360\u7528\u7684\u5185\u5b58\u53ea\u4f1a\u589e\u957f\uff0c\u4e0d\u4f1a\u7f29\u51cf\u3002\u56e0\u6b64\uff0c\u5982\u679c\u5b83\u5bfc\u81f4\u4e86\u4e00\u4e9b\u5185\u5b58\u5360\u7528\u7684\u95ee\u9898\uff0c\u4f60\u9700\u8981\u5c1d\u8bd5\u4e0d\u540c\u7684\u9009\u9879\u6765\u89e3\u51b3\uff0c\u6bd4\u5982\u91cd\u65b0\u521b\u5efa\u4e00\u4e2amap\u4ee3\u66ff\u539f\u6765\u7684\uff08\u539f\u6765\u7684map\u4f1a\u88abGC\u6389\uff09\uff0c\u6216\u8005map[keyType]valueType\u4e2d\u7684valueType\u4f7f\u7528\u6307\u9488\u4ee3\u66ff\u957f\u5ea6\u56fa\u5b9a\u7684\u6570\u7ec4\u6216\u8005sliceHeader\u6765\u7f13\u89e3\u8fc7\u591a\u7684\u5185\u5b58\u5360\u7528\u3002
"},{"location":"zh/#29","title":"\u4e0d\u6b63\u786e\u7684\u503c\u6bd4\u8f83 (#29)","text":"Go\u4e2d\u6bd4\u8f83\u4e24\u4e2a\u7c7b\u578b\u503c\u65f6\uff0c\u5982\u679c\u662f\u53ef\u6bd4\u8f83\u7c7b\u578b\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 ==
\u6216\u8005 !=
\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\uff0c\u6bd4\u5982\uff1abooleans\u3001numerals\u3001strings\u3001pointers\u3001channels\uff0c\u4ee5\u53ca\u5b57\u6bb5\u5168\u90e8\u662f\u53ef\u6bd4\u8f83\u7c7b\u578b\u7684structs\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 reflect.DeepEqual
\u6765\u6bd4\u8f83\uff0c\u7528\u53cd\u5c04\u7684\u8bdd\u4f1a\u727a\u7272\u4e00\u70b9\u6027\u80fd\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u5b9e\u73b0\u548c\u5176\u4ed6\u5e93\u6765\u5b8c\u6210\u3002
range
\u5faa\u73af\u53d8\u91cf\u662f\u4e00\u4e2a\u62f7\u8d1d (#30)","text":"range
\u5faa\u73af\u4e2d\u7684\u5faa\u73af\u53d8\u91cf\u662f\u904d\u5386\u5bb9\u5668\u4e2d\u5143\u7d20\u503c\u7684\u4e00\u4e2a\u62f7\u8d1d\u3002\u56e0\u6b64\uff0c\u5982\u679c\u5143\u7d20\u503c\u662f\u4e00\u4e2astruct\u5e76\u4e14\u60f3\u5728 range
\u4e2d\u4fee\u6539\u5b83\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u503c\u6765\u8bbf\u95ee\u5e76\u4fee\u6539\u5b83\uff0c\u6216\u8005\u4f7f\u7528\u7ecf\u5178\u7684for\u5faa\u73af+\u7d22\u5f15\u503c\u7684\u5199\u6cd5\uff08\u9664\u975e\u904d\u5386\u7684\u5143\u7d20\u662f\u4e00\u4e2a\u6307\u9488\uff09\u3002
range
\u5faa\u73af\u4e2d\u8fed\u4ee3\u76ee\u6807\u503c\u7684\u8ba1\u7b97\u65b9\u5f0f (channels \u548c arrays) (#31)","text":"\u4f20\u9012\u7ed9 range
\u64cd\u4f5c\u7684\u8fed\u4ee3\u76ee\u6807\u5bf9\u5e94\u7684\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u53ea\u4f1a\u5728\u5faa\u73af\u6267\u884c\u524d\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u7406\u89e3\u8fd9\u4e2a\u6709\u52a9\u4e8e\u907f\u514d\u72af\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u4f8b\u5982\u4e0d\u9ad8\u6548\u7684channel\u8d4b\u503c\u64cd\u4f5c\u3001slice\u8fed\u4ee3\u64cd\u4f5c\u3002
range
\u5faa\u73af\u4e2d\u6307\u9488\u5143\u7d20\u7684\u5f71\u54cd range
loops (#32)","text":"\u8fd9\u91cc\u5176\u5b9e\u5f3a\u8c03\u7684\u662f range
\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\uff0c\u8fed\u4ee3\u53d8\u91cf\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u62f7\u8d1d\uff0c\u5047\u8bbe\u7ed9\u53e6\u5916\u4e00\u4e2a\u5bb9\u5668\u5143\u7d20\uff08\u6307\u9488\u7c7b\u578b\uff09\u8d4b\u503c\uff0c\u4e14\u9700\u8981\u5bf9\u8fed\u4ee3\u53d8\u91cf\u53d6\u5730\u5740\u8f6c\u6362\u6210\u6307\u9488\u518d\u8d4b\u503c\u7684\u8bdd\uff0c\u8fd9\u91cc\u6f5c\u85cf\u7740\u4e00\u4e2a\u9519\u8bef\uff0c\u5c31\u662ffor\u5faa\u73af\u8fed\u4ee3\u53d8\u91cf\u662f per-variable-per-loop \u800c\u4e0d\u662f per-variable-per-iteration\u3002\u5982\u679c\u662f\u901a\u8fc7\u5c40\u90e8\u53d8\u91cf\uff08\u7528\u8fed\u4ee3\u53d8\u91cf\u6765\u521d\u59cb\u5316\uff09\u6216\u8005\u4f7f\u7528\u7d22\u5f15\u503c\u6765\u76f4\u63a5\u5f15\u7528\u8fed\u4ee3\u7684\u5143\u7d20\uff0c\u5c06\u6709\u52a9\u4e8e\u907f\u514d\u62f7\u8d1d\u6307\u9488(\u8fed\u4ee3\u53d8\u91cf\u7684\u5730\u5740)\u4e4b\u7c7b\u7684bug\u3002
\u4f7f\u7528map\u65f6\uff0c\u4e3a\u4e86\u80fd\u5f97\u5230\u786e\u5b9a\u4e00\u81f4\u7684\u7ed3\u679c\uff0c\u5e94\u8be5\u8bb0\u4f4fGo\u4e2d\u7684map\u6570\u636e\u7ed3\u6784\uff1a * \u4e0d\u4f1a\u6309\u7167key\u5bf9data\u8fdb\u884c\u6392\u5e8f\uff0c\u904d\u5386\u65f6\u4e0d\u662f\u6309key\u6709\u5e8f\u7684\uff1b * \u904d\u5386\u65f6\u7684\u987a\u5e8f\uff0c\u4e5f\u4e0d\u662f\u6309\u7167\u63d2\u5165\u65f6\u7684\u987a\u5e8f\uff1b * \u6ca1\u6709\u4e00\u4e2a\u786e\u5b9a\u6027\u7684\u904d\u5386\u987a\u5e8f\uff0c\u6bcf\u6b21\u904d\u5386\u987a\u5e8f\u662f\u4e0d\u540c\u7684\uff1b * \u4e0d\u80fd\u4fdd\u8bc1\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u65b0\u63d2\u5165\u7684\u5143\u7d20\uff0c\u5728\u5f53\u524d\u8fed\u4ee3\u4e2d\u80fd\u591f\u88ab\u904d\u5386\u5230\uff1b
"},{"location":"zh/#break-34","title":"\u5ffd\u7565\u4e86break
\u8bed\u53e5\u662f\u5982\u4f55\u5de5\u4f5c\u7684 (#34)","text":"\u914d\u5408label\u4f7f\u7528 break
\u548c continue
\uff0c\u80fd\u591f\u8df3\u8fc7\u4e00\u4e2a\u7279\u5b9a\u7684\u8bed\u53e5\uff0c\u5728\u67d0\u4e9b\u5faa\u73af\u4e2d\u5b58\u5728 switch
\u548cselect
\u8bed\u53e5\u7684\u573a\u666f\u4e2d\u5c31\u6bd4\u8f83\u6709\u5e2e\u52a9\u3002
defer
(#35)","text":"\u5728\u5faa\u73af\u4e2d\u4f7f\u7528defer\u4e0d\u80fd\u5728\u6bcf\u8f6e\u8fed\u4ee3\u7ed3\u675f\u65f6\u6267\u884cdefer\u8bed\u53e5\uff0c\u4f46\u662f\u5c06\u5faa\u73af\u903b\u8f91\u63d0\u53d6\u5230\u51fd\u6570\u5185\u90e8\u4f1a\u5728\u6bcf\u6b21\u8fed\u4ee3\u7ed3\u675f\u65f6\u6267\u884c defer \u8bed\u53e5\u3002
"},{"location":"zh/#_4","title":"\u5b57\u7b26\u4e32","text":""},{"location":"zh/#rune-36","title":"\u6ca1\u6709\u7406\u89e3rune (#36)","text":"\u7406\u89e3rune\u7c7b\u578b\u5bf9\u5e94\u7684\u662f\u4e00\u4e2aunicode\u7801\u70b9\uff0c\u6bcf\u4e00\u4e2aunicode\u7801\u70b9\u5176\u5b9e\u662f\u4e00\u4e2a\u591a\u5b57\u8282\u7684\u5e8f\u5217\uff0c\u4e0d\u662f\u4e00\u4e2abyte\u3002\u8fd9\u5e94\u8be5\u662fGo\u5f00\u53d1\u8005\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\u4e4b\u4e00\uff0c\u7406\u89e3\u4e86\u8fd9\u4e2a\u6709\u52a9\u4e8e\u66f4\u51c6\u786e\u5730\u5904\u7406\u5b57\u7b26\u4e32\u3002
"},{"location":"zh/#37","title":"\u4e0d\u6b63\u786e\u7684\u5b57\u7b26\u4e32\u904d\u5386 (#37)","text":"\u4f7f\u7528 range
\u64cd\u4f5c\u7b26\u5bf9\u4e00\u4e2astring\u8fdb\u884c\u904d\u5386\u5b9e\u9645\u4e0a\u662f\u5bf9string\u5bf9\u5e94\u7684 []rune
\u8fdb\u884c\u904d\u5386\uff0c\u8fed\u4ee3\u53d8\u91cf\u4e2d\u7684\u7d22\u5f15\u503c\uff0c\u8868\u793a\u7684\u5f53\u524drune\u5bf9\u5e94\u7684 []byte
\u5728\u6574\u4e2a []byte(string)
\u4e2d\u7684\u8d77\u59cb\u7d22\u5f15\u3002\u5982\u679c\u8981\u8bbf\u95eestring\u4e2d\u7684\u67d0\u4e00\u4e2arune\uff08\u6bd4\u5982\u7b2c\u4e09\u4e2a\uff09\uff0c\u9996\u5148\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a []rune
\u7136\u540e\u518d\u6309\u7d22\u5f15\u503c\u8bbf\u95ee\u3002
strings.TrimRight
/strings.TrimLeft
\u79fb\u9664\u5728\u5b57\u7b26\u4e32\u5c3e\u90e8\u6216\u8005\u5f00\u5934\u51fa\u73b0\u7684\u4e00\u4e9brunes\uff0c\u51fd\u6570\u4f1a\u6307\u5b9a\u4e00\u4e2arune\u96c6\u5408\uff0c\u51fa\u73b0\u5728\u96c6\u5408\u4e2d\u7684rune\u5c06\u88ab\u4ece\u5b57\u7b26\u4e32\u79fb\u9664\u3002\u800c strings.TrimSuffix
/strings.TrimPrefix
\u662f\u79fb\u9664\u5b57\u7b26\u4e32\u7684\u4e00\u4e2a\u540e\u7f00/\u524d\u7f00\u3002
\u5bf9\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u8fdb\u884c\u904d\u5386\u62fc\u63a5\u64cd\u4f5c\uff0c\u5e94\u8be5\u901a\u8fc7 strings.Builder
\u6765\u5b8c\u6210\uff0c\u4ee5\u907f\u514d\u6bcf\u6b21\u8fed\u4ee3\u62fc\u63a5\u65f6\u90fd\u5206\u914d\u4e00\u4e2a\u65b0\u7684string\u5bf9\u8c61\u51fa\u6765\u3002
bytes
\u5305\u63d0\u4f9b\u4e86\u4e00\u4e9b\u548c strings
\u5305\u76f8\u4f3c\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d []byte/string \u4e4b\u95f4\u7684\u8f6c\u6362\u3002
\u4f7f\u7528\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff0c\u56e0\u4e3a\u5bf9\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684s[low:high]\u64cd\u4f5c\u8fd4\u56de\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5176\u4f7f\u7528\u4e86\u548c\u539f\u5b57\u7b26\u4e32s\u76f8\u540c\u7684\u5e95\u5c42\u6570\u7ec4\u3002
"},{"location":"zh/#_5","title":"\u51fd\u6570\u548c\u65b9\u6cd5","text":""},{"location":"zh/#42","title":"\u4e0d\u77e5\u9053\u4f7f\u7528\u54ea\u79cd\u63a5\u6536\u5668\u7c7b\u578b (#42)","text":"\u5bf9\u4e8e\u63a5\u6536\u5668\u7c7b\u578b\u662f\u91c7\u7528value\u7c7b\u578b\u8fd8\u662fpointer\u7c7b\u578b\uff0c\u5e94\u8be5\u53d6\u51b3\u4e8e\u4e0b\u9762\u8fd9\u51e0\u79cd\u56e0\u7d20\uff0c\u6bd4\u5982\uff1a\u65b9\u6cd5\u5185\u662f\u5426\u4f1a\u5bf9\u5b83\u8fdb\u884c\u4fee\u6539\uff0c\u5b83\u662f\u5426\u5305\u542b\u4e86\u4e00\u4e2a\u4e0d\u80fd\u88ab\u62f7\u8d1d\u7684\u5b57\u6bb5\uff0c\u4ee5\u53ca\u5b83\u8868\u793a\u7684\u5bf9\u8c61\u6709\u591a\u5927\u3002\u5982\u679c\u6709\u7591\u95ee\uff0c\u63a5\u6536\u5668\u53ef\u4ee5\u8003\u8651\u4f7f\u7528pointer\u7c7b\u578b\u3002
"},{"location":"zh/#43","title":"\u4ece\u4e0d\u4f7f\u7528\u547d\u540d\u7684\u8fd4\u56de\u503c (#43)","text":"\u4f7f\u7528\u547d\u540d\u7684\u8fd4\u56de\u503c\uff0c\u662f\u4e00\u79cd\u6709\u6548\u6539\u5584\u51fd\u6570\u3001\u65b9\u6cd5\u53ef\u8bfb\u6027\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u8fd4\u56de\u503c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u7c7b\u578b\u76f8\u540c\u7684\u53c2\u6570\u3002\u53e6\u5916\uff0c\u56e0\u4e3a\u8fd4\u56de\u503c\u5217\u8868\u4e2d\u7684\u53c2\u6570\u662f\u7ecf\u8fc7\u96f6\u503c\u521d\u59cb\u5316\u8fc7\u7684\uff0c\u67d0\u4e9b\u573a\u666f\u4e0b\u4e5f\u4f1a\u7b80\u5316\u51fd\u6570\u3001\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\u5b83\u7684\u4e00\u4e9b\u6f5c\u5728\u526f\u4f5c\u7528\u3002
"},{"location":"zh/#44","title":"\u4f7f\u7528\u547d\u540d\u7684\u8fd4\u56de\u503c\u65f6\u9884\u671f\u5916\u7684\u526f\u4f5c\u7528 (#44)","text":"\u89c1 #43.
\u4f7f\u7528\u547d\u540d\u7684\u8fd4\u56de\u503c\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u521d\u59cb\u5316\u4e86\u96f6\u503c\uff0c\u9700\u8981\u6ce8\u610f\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5f02\u5e38\u8fd4\u56de\u65f6\u662f\u5426\u9700\u8981\u7ed9\u5b83\u8d4b\u4e88\u4e00\u4e2a\u4e0d\u540c\u7684\u503c\uff0c\u6bd4\u5982\u8fd4\u56de\u503c\u5217\u8868\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6709\u540d\u53c2\u6570 err error
\uff0c\u9700\u8981\u6ce8\u610f return err
\u65f6\u662f\u5426\u6b63\u786e\u5730\u5bf9 err
\u8fdb\u884c\u4e86\u8d4b\u503c\u3002
\u5f53\u8fd4\u56de\u4e00\u4e2ainterface\u53c2\u6570\u65f6\uff0c\u9700\u8981\u5c0f\u5fc3\uff0c\u4e0d\u8981\u8fd4\u56de\u4e00\u4e2anil\u6307\u9488\uff0c\u800c\u662f\u5e94\u8be5\u663e\u793a\u8fd4\u56de\u4e00\u4e2anil\u503c\u3002\u5426\u5219\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u4e00\u4e9b\u9884\u671f\u5916\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u8c03\u7528\u65b9\u4f1a\u6536\u5230\u4e00\u4e2a\u975enil\u7684\u503c\u3002
"},{"location":"zh/#46","title":"\u4f7f\u7528\u6587\u4ef6\u540d\u4f5c\u4e3a\u51fd\u6570\u5165\u53c2 (#46)","text":"\u8bbe\u8ba1\u51fd\u6570\u65f6\u4f7f\u7528 io.Reader
\u7c7b\u578b\u4f5c\u4e3a\u5165\u53c2\uff0c\u800c\u4e0d\u662f\u6587\u4ef6\u540d\uff0c\u5c06\u6709\u52a9\u4e8e\u6539\u5584\u51fd\u6570\u7684\u53ef\u590d\u7528\u6027\u3001\u6613\u6d4b\u8bd5\u6027\u3002
defer
\u8bed\u53e5\u4e2d\u53c2\u6570\u3001\u63a5\u6536\u5668\u503c\u7684\u8ba1\u7b97\u65b9\u5f0f (\u53c2\u6570\u503c\u8ba1\u7b97, \u6307\u9488, \u548c value\u7c7b\u578b\u63a5\u6536\u5668) (#47)","text":"\u4e3a\u4e86\u907f\u514d defer
\u8bed\u53e5\u6267\u884c\u65f6\u5c31\u7acb\u5373\u8ba1\u7b97\u5bf9defer\u8981\u6267\u884c\u7684\u51fd\u6570\u7684\u53c2\u6570\u8fdb\u884c\u8ba1\u7b97\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u8981\u6267\u884c\u7684\u51fd\u6570\u653e\u5230\u95ed\u5305\u91cc\u9762\uff0c\u7136\u540e\u901a\u8fc7\u6307\u9488\u4f20\u9012\u53c2\u6570\u7ed9\u95ed\u5305\u5185\u51fd\u6570\uff08\u6216\u8005\u901a\u8fc7\u95ed\u5305\u6355\u83b7\u5916\u90e8\u53d8\u91cf\uff09\uff0c\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002
\u4f7f\u7528 panic
\u662fGo\u4e2d\u4e00\u79cd\u5904\u7406\u9519\u8bef\u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u53ea\u80fd\u5728\u9047\u5230\u4e0d\u53ef\u6062\u590d\u7684\u9519\u8bef\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a\u901a\u77e5\u5f00\u53d1\u4eba\u5458\u4e00\u4e2a\u5f3a\u4f9d\u8d56\u7684\u6a21\u5757\u52a0\u8f7d\u5931\u8d25\u4e86\u3002
Wrapping\uff08\u5305\u88c5\uff09\u9519\u8bef\u5141\u8bb8\u60a8\u6807\u8bb0\u9519\u8bef\u3001\u63d0\u4f9b\u989d\u5916\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u7136\u800c\uff0c\u5305\u88c5\u9519\u8bef\u4f1a\u521b\u5efa\u6f5c\u5728\u7684\u8026\u5408\uff0c\u56e0\u4e3a\u5b83\u4f7f\u5f97\u539f\u6765\u7684\u9519\u8bef\u5bf9\u8c03\u7528\u8005\u53ef\u89c1\u3002\u5982\u679c\u60a8\u60f3\u8981\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\uff0c\u8bf7\u4e0d\u8981\u4f7f\u7528\u5305\u88c5\u9519\u8bef\u7684\u65b9\u5f0f\u3002
"},{"location":"zh/#50","title":"\u4e0d\u6b63\u786e\u7684\u9519\u8bef\u7c7b\u578b\u6bd4\u8f83 (#50)","text":"\u5982\u679c\u4f60\u4f7f\u7528 Go 1.13 \u5f15\u5165\u7684\u7279\u6027 fmt.Errorf
+ %w
\u6765\u5305\u88c5\u4e00\u4e2a\u9519\u8bef\uff0c\u5f53\u8fdb\u884c\u9519\u8bef\u6bd4\u8f83\u65f6\uff0c\u5982\u679c\u60f3\u5224\u65ad\u8be5\u5305\u88c5\u540e\u7684\u9519\u8bef\u662f\u4e0d\u662f\u6307\u5b9a\u7684\u9519\u8bef\u7c7b\u578b\uff0c\u5c31\u9700\u8981\u4f7f\u7528 errors.As
\uff0c\u5982\u679c\u60f3\u5224\u65ad\u662f\u4e0d\u662f\u6307\u5b9a\u7684error\u5bf9\u8c61\u5c31\u9700\u8981\u7528 errors.Is
\u3002
\u89c1 #50.
\u4e3a\u4e86\u8868\u8fbe\u4e00\u4e2a\u9884\u671f\u5185\u7684\u9519\u8bef\uff0c\u8bf7\u4f7f\u7528\u9519\u8bef\u503c\u7684\u65b9\u5f0f\uff0c\u5e76\u901a\u8fc7 ==
\u6216\u8005 errors.Is
\u6765\u6bd4\u8f83\u3002\u800c\u5bf9\u4e8e\u610f\u5916\u9519\u8bef\uff0c\u5219\u5e94\u4f7f\u7528\u7279\u5b9a\u7684\u9519\u8bef\u7c7b\u578b\uff08\u53ef\u4ee5\u901a\u8fc7 errors.As
\u6765\u6bd4\u8f83\uff09\u3002
\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9519\u8bef\u4ec5\u9700\u8981\u5904\u7406\u4e00\u6b21\u3002\u6253\u5370\u9519\u8bef\u65e5\u5fd7\u4e5f\u662f\u4e00\u79cd\u9519\u8bef\u5904\u7406\u3002\u56e0\u6b64\uff0c\u5f53\u51fd\u6570\u5185\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u5e94\u8be5\u5728\u6253\u5370\u65e5\u5fd7\u548c\u8fd4\u56de\u9519\u8bef\u4e2d\u9009\u62e9\u5176\u4e2d\u4e00\u79cd\u3002\u5305\u88c5\u9519\u8bef\u4e5f\u53ef\u4ee5\u63d0\u4f9b\u95ee\u9898\u53d1\u751f\u7684\u989d\u5916\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u4e5f\u5305\u62ec\u4e86\u539f\u6765\u7684\u9519\u8bef\uff08\u53ef\u8003\u8651\u4ea4\u7ed9\u8c03\u7528\u65b9\u8d1f\u8d23\u6253\u65e5\u5fd7\uff09\u3002
"},{"location":"zh/#53","title":"\u4e0d\u5904\u7406\u9519\u8bef (#53)","text":"\u4e0d\u7ba1\u662f\u5728\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u8fd8\u662f\u5728\u4e00\u4e2a defer
\u51fd\u6570\u6267\u884c\u65f6\uff0c\u5982\u679c\u60f3\u8981\u5ffd\u7565\u4e00\u4e2a\u9519\u8bef\uff0c\u5e94\u8be5\u663e\u793a\u5730\u901a\u8fc7 _
\u6765\u5ffd\u7565\uff08\u53ef\u6ce8\u660e\u5ffd\u7565\u7684\u539f\u56e0\uff09\u3002\u5426\u5219\uff0c\u5c06\u6765\u7684\u8bfb\u8005\u5c31\u4f1a\u611f\u89c9\u5230\u56f0\u60d1\uff0c\u5ffd\u7565\u8fd9\u4e2a\u9519\u8bef\u662f\u6709\u610f\u4e3a\u4e4b\u8fd8\u662f\u65e0\u610f\u4e2d\u6f0f\u6389\u4e86\u3002
defer
\u4e2d\u7684\u9519\u8bef (#54)","text":"\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u5e94\u8be5\u5ffd\u7565 defer
\u51fd\u6570\u6267\u884c\u65f6\u8fd4\u56de\u7684\u9519\u8bef\uff0c\u6216\u8005\u663e\u793a\u5904\u7406\u5b83\uff0c\u6216\u8005\u5c06\u5b83\u4f20\u9012\u7ed9\u8c03\u7528\u65b9\u5904\u7406\uff0c\u53ef\u4ee5\u6839\u636e\u60c5\u666f\u8fdb\u884c\u9009\u62e9\u3002\u5982\u679c\u4f60\u786e\u5b9a\u8981\u5ffd\u7565\u8fd9\u4e2a\u9519\u8bef\uff0c\u8bf7\u663e\u793a\u4f7f\u7528 _
\u6765\u5ffd\u7565\u3002
\u7406\u89e3\u5e76\u53d1\uff08concurrency\uff09\u3001\u5e76\u884c\uff08parallelism\uff09\u4e4b\u95f4\u7684\u672c\u8d28\u533a\u522b\u662fGo\u5f00\u53d1\u4eba\u5458\u5fc5\u987b\u8981\u638c\u63e1\u7684\u3002\u5e76\u53d1\u662f\u5173\u4e8e\u7ed3\u6784\u8bbe\u8ba1\u4e0a\u7684\uff0c\u5e76\u884c\u662f\u5173\u4e8e\u5177\u4f53\u6267\u884c\u4e0a\u7684\u3002
"},{"location":"zh/#56","title":"\u8ba4\u4e3a\u5e76\u53d1\u603b\u662f\u66f4\u5feb (#56)","text":"\u8981\u6210\u4e3a\u4e00\u540d\u719f\u7ec3\u7684\u5f00\u53d1\u4eba\u5458\uff0c\u60a8\u5fc5\u987b\u610f\u8bc6\u5230\u5e76\u975e\u6240\u6709\u573a\u666f\u4e0b\u90fd\u662f\u5e76\u53d1\u7684\u65b9\u6848\u66f4\u5feb\u3002\u5bf9\u4e8e\u4efb\u52a1\u4e2d\u7684\u6700\u5c0f\u5de5\u4f5c\u8d1f\u8f7d\u90e8\u5206\uff0c\u5bf9\u5b83\u4eec\u8fdb\u884c\u5e76\u884c\u5316\u5904\u7406\u5e76\u4e0d\u4e00\u5b9a\u5c31\u6709\u660e\u663e\u6536\u76ca\u6216\u8005\u6bd4\u4e32\u884c\u5316\u65b9\u6848\u66f4\u5feb\u3002\u5bf9\u4e32\u884c\u5316\u3001\u5e76\u53d1\u65b9\u6848\u8fdb\u884cbenchmark\u6d4b\u8bd5\uff0c\u662f\u9a8c\u8bc1\u5047\u8bbe\u7684\u597d\u529e\u6cd5\u3002
"},{"location":"zh/#channelsmutexes-57","title":"\u4e0d\u6e05\u695a\u4f55\u65f6\u4f7f\u7528channels\u6216mutexes (#57)","text":"\u4e86\u89e3 goroutine \u4e4b\u95f4\u7684\u4ea4\u4e92\u4e5f\u53ef\u4ee5\u5728\u9009\u62e9\u4f7f\u7528channels\u6216mutexes\u65f6\u6709\u6240\u5e2e\u52a9\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5e76\u884c\u7684 goroutine \u9700\u8981\u540c\u6b65\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528mutexes\u3002\u76f8\u53cd\uff0c\u5e76\u53d1\u7684 goroutine \u901a\u5e38\u9700\u8981\u534f\u8c03\u548c\u7f16\u6392\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528channels\u3002
"},{"location":"zh/#vs-go-58","title":"\u4e0d\u660e\u767d\u7ade\u6001\u95ee\u9898 (\u6570\u636e\u7ade\u6001 vs. \u7ade\u6001\u6761\u4ef6 \u548c Go\u5185\u5b58\u6a21\u578b) (#58)","text":"\u638c\u63e1\u5e76\u53d1\u610f\u5473\u7740\u8981\u8ba4\u8bc6\u5230\u6570\u636e\u7ade\u4e89\uff08data races\uff09\u548c\u7ade\u6001\u6761\u4ef6\uff08race conditions\uff09\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u6982\u5ff5\u3002\u6570\u636e\u7ade\u4e89\uff0c\u6307\u7684\u662f\u6709\u591a\u4e2agoroutines\u540c\u65f6\u8bbf\u95ee\u76f8\u540c\u5185\u5b58\u533a\u57df\u65f6\uff0c\u6ca1\u6709\u5fc5\u8981\u7684\u540c\u6b65\u63a7\u5236\uff0c\u4e14\u5176\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2agoroutine\u662f\u6267\u884c\u7684\u5199\u64cd\u4f5c\u3002\u540c\u65f6\u8981\u8ba4\u8bc6\u5230\uff0c\u6ca1\u6709\u53d1\u751f\u6570\u636e\u7ade\u4e89\u4e0d\u4ee3\u8868\u7a0b\u5e8f\u7684\u6267\u884c\u662f\u786e\u5b9a\u6027\u7684\u3001\u6ca1\u95ee\u9898\u7684\u3002\u5f53\u5728\u67d0\u4e2a\u7279\u5b9a\u7684\u64cd\u4f5c\u987a\u5e8f\u6216\u8005\u7279\u5b9a\u7684\u4e8b\u4ef6\u53d1\u751f\u987a\u5e8f\u4e0b\uff0c\u5982\u679c\u6700\u7ec8\u7684\u884c\u4e3a\u662f\u4e0d\u53ef\u63a7\u7684\uff0c\u8fd9\u5c31\u662f\u7ade\u6001\u6761\u4ef6\u3002
ps\uff1a\u6570\u636e\u7ade\u4e89\u662f\u7ade\u6001\u6761\u4ef6\u7684\u5b50\u96c6\uff0c\u7ade\u6001\u6761\u4ef6\u4e0d\u4ec5\u5c40\u9650\u4e8e\u8bbf\u5b58\u672a\u540c\u6b65\uff0c\u5b83\u53ef\u4ee5\u53d1\u751f\u5728\u66f4\u9ad8\u7684\u5c42\u9762\u3002go test -race
\u68c0\u6d4b\u7684\u662f\u6570\u636e\u7ade\u4e89\uff0c\u9700\u8981\u540c\u6b65\u6765\u89e3\u51b3\uff0c\u800c\u5f00\u53d1\u8005\u8fd8\u9700\u8981\u5173\u6ce8\u9762\u66f4\u5e7f\u7684\u7ade\u6001\u6761\u4ef6\uff0c\u5b83\u9700\u8981\u5bf9\u591a\u4e2agoroutines\u7684\u6267\u884c\u8fdb\u884c\u7f16\u6392\u3002
\u7406\u89e3 Go \u7684\u5185\u5b58\u6a21\u578b\u4ee5\u53ca\u6709\u5173\u987a\u5e8f\u548c\u540c\u6b65\u7684\u5e95\u5c42\u4fdd\u8bc1\u662f\u9632\u6b62\u53ef\u80fd\u7684\u6570\u636e\u7ade\u4e89\u548c\u7ade\u6001\u6761\u4ef6\u7684\u5173\u952e\u3002
"},{"location":"zh/#59","title":"\u4e0d\u7406\u89e3\u4e0d\u540c\u5de5\u4f5c\u8d1f\u8f7d\u7c7b\u578b\u5bf9\u5e76\u53d1\u7684\u5f71\u54cd (#59)","text":"\u5f53\u521b\u5efa\u4e00\u5b9a\u6570\u91cf\u7684goroutines\u662f\uff0c\u9700\u8981\u8003\u8651\u5de5\u4f5c\u8d1f\u8f7d\u7684\u7c7b\u578b\u3002\u5982\u679c\u5de5\u4f5c\u8d1f\u8f7d\u662fCPU\u5bc6\u96c6\u578b\u7684\uff0c\u90a3\u4e48goroutines\u6570\u91cf\u5e94\u8be5\u63a5\u8fd1\u4e8e GOMAXPROCS
\u7684\u503c\uff08\u8be5\u503c\u53d6\u51b3\u4e8e\u4e3b\u673a\u5904\u7406\u5668\u6838\u5fc3\u6570\uff09\u3002\u5982\u679c\u5de5\u4f5c\u8d1f\u8f7d\u662fIO\u5bc6\u96c6\u578b\u7684\uff0cgoroutines\u6570\u91cf\u5c31\u9700\u8981\u8003\u8651\u591a\u79cd\u56e0\u7d20\uff0c\u6bd4\u5982\u5916\u90e8\u7cfb\u7edf\uff08\u8003\u8651\u8bf7\u6c42\u3001\u54cd\u5e94\u901f\u7387\uff09\u3002
Go \u7684\u4e0a\u4e0b\u6587\uff08context\uff09\u4e5f\u662f Go \u5e76\u53d1\u7f16\u7a0b\u7684\u57fa\u77f3\u4e4b\u4e00\u3002\u4e0a\u4e0b\u6587\u5141\u8bb8\u60a8\u643a\u5e26\u622a\u6b62\u65f6\u95f4\u3001\u53d6\u6d88\u4fe1\u53f7\u548c\u952e\u503c\u5217\u8868\u3002
"},{"location":"zh/#_8","title":"\u5e76\u53d1\u7f16\u7a0b: \u5b9e\u8df5","text":""},{"location":"zh/#context-61","title":"\u4f20\u9012\u4e0d\u5408\u9002\u7684context (#61)","text":"\u5f53\u6211\u4eec\u4f20\u9012\u4e86\u4e00\u4e2acontext\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u8fd9\u4e2acontext\u4ec0\u4e48\u65f6\u5019\u53ef\u4ee5\u88ab\u53d6\u6d88\uff0c\u8fd9\u70b9\u5f88\u91cd\u8981\uff0c\u4f8b\u5982\uff1a\u4e00\u4e2aHTTP\u8bf7\u6c42\u5904\u7406\u5668\u5728\u53d1\u9001\u5b8c\u54cd\u5e94\u540e\u53d6\u6d88context\u3002
ps: \u5b9e\u9645\u4e0acontext\u8868\u8fbe\u7684\u662f\u4e00\u4e2a\u52a8\u4f5c\u53ef\u4ee5\u6301\u7eed\u591a\u4e45\u4e4b\u540e\u88ab\u505c\u6b62\u3002
"},{"location":"zh/#goroutine-62","title":"\u542f\u52a8\u4e86\u4e00\u4e2agoroutine\u4f46\u662f\u4e0d\u77e5\u9053\u5b83\u4f55\u65f6\u4f1a\u505c\u6b62 (#62)","text":"\u907f\u514dgoroutine\u6cc4\u6f0f\uff0c\u8981\u6709\u8fd9\u79cd\u610f\u8bc6\uff0c\u5f53\u521b\u5efa\u5e76\u542f\u52a8\u4e00\u4e2agoroutine\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u6709\u5bf9\u5e94\u7684\u8bbe\u8ba1\u8ba9\u5b83\u80fd\u6b63\u5e38\u9000\u51fa\u3002
"},{"location":"zh/#goroutines-63","title":"\u4e0d\u6ce8\u610f\u5904\u7406 goroutines \u548c \u5faa\u73af\u4e2d\u7684\u8fed\u4ee3\u53d8\u91cf (#63)","text":"\u4e3a\u4e86\u907f\u514dgoroutines\u548c\u5faa\u73af\u4e2d\u7684\u8fed\u4ee3\u53d8\u91cf\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u521b\u5efa\u5c40\u90e8\u53d8\u91cf\u5e76\u5c06\u8fed\u4ee3\u53d8\u91cf\u8d4b\u503c\u7ed9\u5c40\u90e8\u53d8\u91cf\uff0c\u6216\u8005goroutine\u8c03\u7528\u5e26\u53c2\u6570\u7684\u51fd\u6570\uff0c\u5c06\u8fed\u4ee3\u53d8\u91cf\u503c\u4f5c\u4e3a\u53c2\u6570\u503c\u4f20\u5165\uff0c\u6765\u4ee3\u66ffgoroutine\u8c03\u7528\u95ed\u5305\u3002
"},{"location":"zh/#select-channels-64","title":"\u4f7f\u7528select + channels \u65f6\u8bef\u4ee5\u4e3a\u5206\u652f\u9009\u62e9\u987a\u5e8f\u662f\u786e\u5b9a\u7684 (#64)","text":"\u8981\u660e\u767d\uff0cselect
\u591a\u4e2achannels\u65f6\uff0c\u5982\u679c\u591a\u4e2achannels\u4e0a\u7684\u64cd\u4f5c\u90fd\u5c31\u7eea\uff0c\u90a3\u4e48\u4f1a\u968f\u673a\u9009\u62e9\u4e00\u4e2a case
\u5206\u652f\u6765\u6267\u884c\uff0c\u56e0\u6b64\u8981\u907f\u514d\u6709\u5206\u652f\u9009\u62e9\u987a\u5e8f\u662f\u4ece\u4e0a\u5230\u4e0b\u7684\u8fd9\u79cd\u9519\u8bef\u9884\u8bbe\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bbe\u8ba1\u4e0a\u7684bug\u3002
\u53d1\u9001\u901a\u77e5\u65f6\u4f7f\u7528 chan struct{}
\u7c7b\u578b\u3002
ps: \u5148\u660e\u767d\u4ec0\u4e48\u662f\u901a\u77e5channels\uff0c\u4e00\u4e2a\u901a\u77e5channels\u6307\u7684\u662f\u53ea\u662f\u7528\u6765\u505a\u901a\u77e5\uff0c\u800c\u5176\u4e2d\u4f20\u9012\u7684\u6570\u636e\u6ca1\u6709\u610f\u4e49\uff0c\u6216\u8005\u7406\u89e3\u6210\u4e0d\u4f20\u9012\u6570\u636e\u7684channels\uff0c\u8fd9\u79cd\u79f0\u4e3a\u901a\u77e5channels\u3002\u5176\u4e2d\u4f20\u9012\u7684\u6570\u636e\u7684\u7c7b\u578bstruct{}\u66f4\u5408\u9002\u3002
"},{"location":"zh/#nil-channels-66","title":"\u4e0d\u4f7f\u7528 nil channels (#66)","text":"\u4f7f\u7528 nil channels \u5e94\u8be5\u662f\u5e76\u53d1\u5904\u7406\u65b9\u5f0f\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982\uff0c\u5b83\u80fd\u591f\u5e2e\u52a9\u7981\u7528 select
\u8bed\u53e5\u4e2d\u7684\u7279\u5b9a\u7684\u5206\u652f\u3002
\u6839\u636e\u6307\u5b9a\u7684\u573a\u666f\u4ed4\u7ec6\u8bc4\u4f30\u5e94\u8be5\u4f7f\u7528\u54ea\u4e00\u79cd channel \u7c7b\u578b\uff08\u5e26\u7f13\u51b2\u7684\uff0c\u4e0d\u5e26\u7f13\u51b2\u7684\uff09\u3002\u53ea\u6709\u4e0d\u5e26\u7f13\u51b2\u7684 channels \u53ef\u4ee5\u63d0\u4f9b\u5f3a\u540c\u6b65\u4fdd\u8bc1\u3002
\u4f7f\u7528\u5e26\u7f13\u51b2\u7684 channels \u65f6\u5982\u679c\u4e0d\u786e\u5b9a size \u8be5\u5982\u4f55\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u5148\u8bbe\u4e3a1\uff0c\u5982\u679c\u6709\u5408\u7406\u7684\u7406\u7531\u518d\u53bb\u6307\u5b9a channels size\u3002
ps: \u6839\u636edisruptor\u8fd9\u4e2a\u9ad8\u6027\u80fd\u5185\u5b58\u6d88\u606f\u961f\u5217\u7684\u5b9e\u8df5\uff0c\u5728\u67d0\u79cd\u8bfb\u5199pacing\u4e0b\uff0c\u961f\u5217\u8981\u4e48\u6ee1\u8981\u4e48\u7a7a\uff0c\u4e0d\u5927\u53ef\u80fd\u5904\u4e8e\u67d0\u79cd\u4ecb\u4e8e\u4e2d\u95f4\u7684\u7a33\u6001\u3002
"},{"location":"zh/#etcd-68","title":"\u5fd8\u8bb0\u4e86\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u53ef\u80fd\u5e26\u6765\u7684\u526f\u4f5c\u7528\uff08\u4f8b\u5982 etcd \u6570\u636e\u7ade\u4e89\u548c\u6b7b\u9501\uff09(#68)","text":"\u610f\u8bc6\u5230\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8c03\u7528\u73b0\u6709\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u9700\u8981\u6ce8\u610f\u53ef\u80fd\u7684\u6b7b\u9501\u548c\u5176\u4ed6\u6570\u636e\u7ade\u4e89\u95ee\u9898\u3002
ps: \u6838\u5fc3\u662f\u8981\u5173\u6ce8 fmt.Sprintf
+ %v
\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65f6 %v
\u5177\u4f53\u5230\u4e0d\u540c\u7684\u7c7b\u578b\u503c\u65f6\uff0c\u5b9e\u9645\u4e0a\u6267\u884c\u7684\u64cd\u4f5c\u662f\u4ec0\u4e48\u3002\u6bd4\u5982 %v
\u8fd9\u4e2aplaceholder\u5bf9\u5e94\u7684\u503c\u65f6\u4e00\u4e2a context.Context
\uff0c\u90a3\u4e48\u4f1a\u5c31\u904d\u5386\u5176\u901a\u8fc7 context.WithValue
\u9644\u52a0\u5728\u5176\u4e2d\u7684 values\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u6d89\u53ca\u5230\u6570\u636e\u7ade\u4e89\u95ee\u9898\u3002\u4e66\u4e2d\u63d0\u53ca\u7684\u53e6\u4e00\u4e2a\u5bfc\u81f4\u6b7b\u9501\u7684\u6848\u4f8b\u672c\u8d28\u4e0a\u4e5f\u662f\u4e00\u6837\u7684\u95ee\u9898\uff0c\u53ea\u4e0d\u8fc7\u53c8\u989d\u5916\u7275\u626f\u5230\u4e86 sync.RWMutex
\u4e0d\u53ef\u91cd\u5165\u7684\u95ee\u9898\u3002
\u8c03\u7528 append
\u4e0d\u603b\u662f\u6ca1\u6709\u6570\u636e\u7ade\u4e89\u7684\uff0c\u56e0\u6b64\u4e0d\u8981\u5728\u4e00\u4e2a\u5171\u4eab\u7684 slice
\u4e0a\u5e76\u53d1\u5730\u6267\u884c append
\u3002
\u8bf7\u8bb0\u4f4f slices \u548c maps \u5f15\u7528\u7c7b\u578b\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u5e38\u89c1\u7684\u6570\u636e\u7ade\u4e89\u95ee\u9898\u3002
ps: \u8fd9\u91cc\u5b9e\u9645\u662f\u56e0\u4e3a\u9519\u8bef\u7406\u89e3\u4e86 slices \u548c maps\uff0c\u5bfc\u81f4\u5199\u51fa\u4e86\u9519\u8bef\u7684\u62f7\u8d1d slices \u548c maps \u7684\u4ee3\u7801\uff0c\u8fdb\u800c\u5bfc\u81f4\u9501\u4fdd\u62a4\u65e0\u6548\u3001\u51fa\u73b0\u6570\u636e\u7ade\u4e89\u95ee\u9898\u3002
"},{"location":"zh/#syncwaitgroup-71","title":"\u8bef\u7528sync.WaitGroup
(#71)","text":"\u6b63\u786e\u5730\u4f7f\u7528 sync.WaitGroup
\u9700\u8981\u5728\u542f\u52a8 goroutines \u4e4b\u524d\u5148\u8c03\u7528 Add
\u65b9\u6cd5\u3002
sync.Cond
(#72)","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 sync.Cond
\u5411\u591a\u4e2a goroutines \u53d1\u9001\u91cd\u590d\u7684\u901a\u77e5\u3002
errgroup
(#73)","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 errgroup
\u5305\u6765\u540c\u6b65\u4e00\u7ec4 goroutines \u5e76\u5904\u7406\u9519\u8bef\u548c\u4e0a\u4e0b\u6587\u3002
sync
\u4e0b\u7684\u7c7b\u578b (#74)","text":"sync
\u5305\u4e0b\u7684\u7c7b\u578b\u4e0d\u5e94\u8be5\u88ab\u62f7\u8d1d\u3002
\u6ce8\u610f\u6709\u4e9b\u51fd\u6570\u63a5\u6536\u4e00\u4e2a time.Duration
\u7c7b\u578b\u7684\u53c2\u6570\u65f6\uff0c\u5c3d\u7ba1\u76f4\u63a5\u4f20\u9012\u4e00\u4e2a\u6574\u6570\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u6700\u597d\u8fd8\u662f\u4f7f\u7528 time API \u4e2d\u7684\u65b9\u6cd5\u6765\u4f20\u9012 duration\uff0c\u4ee5\u907f\u514d\u53ef\u80fd\u9020\u6210\u7684\u56f0\u60d1\u3001bug\u3002
ps: \u91cd\u70b9\u662f\u6ce8\u610f time.Duration \u5b9a\u4e49\u7684\u662f nanoseconds \u6570\u3002
"},{"location":"zh/#timeafter-76","title":"time.After
\u548c\u5185\u5b58\u6cc4\u6f0f (#76)","text":"\u907f\u514d\u5728\u91cd\u590d\u6267\u884c\u5f88\u591a\u6b21\u7684\u51fd\u6570 \uff08\u5982\u5faa\u73af\u4e2d\u6216HTTP\u5904\u7406\u51fd\u6570\uff09\u4e2d\u8c03\u7528 time.After
\uff0c\u8fd9\u53ef\u4ee5\u907f\u514d\u5185\u5b58\u5cf0\u503c\u6d88\u8017\u3002\u7531 time.After
\u521b\u5efa\u7684\u8d44\u6e90\u4ec5\u5728\u8ba1\u65f6\u5668\u8d85\u65f6\u624d\u4f1a\u88ab\u91ca\u653e\u3002
\u8981\u5f53\u5fc3\u5728Go\u7ed3\u6784\u4f53\u4e2d\u5d4c\u5165\u5b57\u6bb5\uff0c\u8fd9\u6837\u505a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bf8\u5982\u5d4c\u5165\u7684 time.Time
\u5b57\u6bb5\u5b9e\u73b0 json.Marshaler
\u63a5\u53e3\uff0c\u4ece\u800c\u8986\u76d6\u9ed8\u8ba4\u7684json\u5e8f\u5217\u3002
\u5f53\u5bf9\u4e24\u4e2a time.Time
\u7c7b\u578b\u503c\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u9700\u8981\u8bb0\u4f4f time.Time
\u5305\u542b\u4e86\u4e00\u4e2a\u5899\u4e0a\u65f6\u949f\uff08wall clock\uff09\u548c\u4e00\u4e2a\u5355\u8c03\u65f6\u949f \uff08monotonic clock\uff09\uff0c\u800c\u4f7f\u7528 ==
\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\u65f6\u4f1a\u540c\u65f6\u6bd4\u8f83\u8fd9\u4e24\u4e2a\u3002
any
\u5f53\u63d0\u4f9b\u4e00\u4e2amap\u7528\u6765unmarshal JSON\u6570\u636e\u65f6\uff0c\u4e3a\u4e86\u907f\u514d\u4e0d\u786e\u5b9a\u7684value\u7ed3\u6784\u6211\u4eec\u4f1a\u4f7f\u7528 any
\u6765\u4f5c\u4e3avalue\u7684\u7c7b\u578b\u800c\u4e0d\u662f\u5b9a\u4e49\u4e00\u4e2astruct\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u9700\u8981\u8bb0\u5f97\u6570\u503c\u9ed8\u8ba4\u4f1a\u88ab\u8f6c\u6362\u4e3a float64
\u3002
sql.Open
\u5e76\u6ca1\u6709\u4e0edb\u670d\u52a1\u5668\u5efa\u7acb\u5b9e\u9645\u8fde\u63a5\u9700\u8981\u8c03\u7528 Ping
\u6216\u8005 PingContext
\u65b9\u6cd5\u6765\u6d4b\u8bd5\u914d\u7f6e\u5e76\u786e\u4fdd\u6570\u636e\u5e93\u662f\u53ef\u8fbe\u7684\u3002
\u4f5c\u4e3a\u751f\u4ea7\u7ea7\u522b\u7684\u5e94\u7528\uff0c\u8bbf\u95ee\u6570\u636e\u5e93\u65f6\u5e94\u8be5\u5173\u6ce8\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u53c2\u6570\u3002
\u4f7f\u7528 SQL prepared \u8bed\u53e5\u80fd\u591f\u8ba9\u67e5\u8be2\u66f4\u52a0\u9ad8\u6548\u548c\u5b89\u5168\u3002
\u4f7f\u7528 sql.NullXXX
\u7c7b\u578b\u5904\u7406\u8868\u4e2d\u7684\u53ef\u7a7a\u5217\u3002
\u8c03\u7528 sql.Rows
\u7684 Err
\u65b9\u6cd5\u6765\u786e\u4fdd\u5728\u51c6\u5907\u4e0b\u4e00\u4e2a\u884c\u65f6\u6ca1\u6709\u9057\u6f0f\u9519\u8bef\u3002
sql.Rows
\u548c os.File
) (#79)","text":"\u6700\u7ec8\u8981\u6ce8\u610f\u5173\u95ed\u6240\u6709\u5b9e\u73b0 io.Closer
\u63a5\u53e3\u7684\u7ed3\u6784\u4f53,\u4ee5\u907f\u514d\u53ef\u80fd\u7684\u6cc4\u6f0f\u3002
\u4e3a\u4e86\u907f\u514d\u5728HTTP\u5904\u7406\u51fd\u6570\u4e2d\u51fa\u73b0\u67d0\u4e9b\u610f\u5916\u7684\u95ee\u9898\uff0c\u5982\u679c\u60f3\u5728\u53d1\u751f http.Error
\u540e\u8ba9HTTP\u5904\u7406\u51fd\u6570\u505c\u6b62\uff0c\u90a3\u4e48\u5c31\u4e0d\u8981\u5fd8\u8bb0\u4f7f\u7528return
\u8bed\u53e5\u6765\u963b\u6b62\u540e\u7eed\u4ee3\u7801\u7684\u6267\u884c\u3002
\u5bf9\u4e8e\u751f\u4ea7\u7ea7\u522b\u7684\u5e94\u7528\uff0c\u4e0d\u8981\u4f7f\u7528\u9ed8\u8ba4\u7684HTTP client\u548cserver\u5b9e\u73b0\u3002\u8fd9\u4e9b\u5b9e\u73b0\u7f3a\u5c11\u8d85\u65f6\u548c\u751f\u4ea7\u73af\u5883\u4e2d\u5e94\u8be5\u5f3a\u5236\u4f7f\u7528\u7684\u884c\u4e3a\u3002
"},{"location":"zh/#_10","title":"\u6d4b\u8bd5","text":""},{"location":"zh/#build-tags-82","title":"\u4e0d\u5bf9\u6d4b\u8bd5\u8fdb\u884c\u5206\u7c7b \uff08build tags, \u73af\u5883\u53d8\u91cf\uff0c\u77ed\u6a21\u5f0f\uff09(#82)","text":"\u5bf9\u6d4b\u8bd5\u8fdb\u884c\u5fc5\u8981\u7684\u5206\u7c7b\uff0c\u53ef\u4ee5\u501f\u52a9 build tags\u3001\u73af\u5883\u53d8\u91cf\u4ee5\u53ca\u77ed\u6a21\u5f0f\uff0c\u6765\u4f7f\u5f97\u6d4b\u8bd5\u8fc7\u7a0b\u66f4\u52a0\u9ad8\u6548\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 build tags \u6216\u73af\u5883\u53d8\u91cf\u6765\u521b\u5efa\u6d4b\u8bd5\u7c7b\u522b\uff08\u4f8b\u5982\u5355\u5143\u6d4b\u8bd5\u4e0e\u96c6\u6210\u6d4b\u8bd5\uff09\uff0c\u5e76\u533a\u5206\u77ed\u6d4b\u8bd5\u4e0e\u957f\u65f6\u95f4\u6d4b\u8bd5\uff0c\u6765\u51b3\u5b9a\u6267\u884c\u54ea\u79cd\u7c7b\u578b\u7684\u3002
ps: \u4e86\u89e3\u4e0bgo build tags\uff0c\u4ee5\u53ca go test -short
\u3002
\u6253\u5f00 -race
\u5f00\u5173\u5728\u7f16\u5199\u5e76\u53d1\u5e94\u7528\u65f6\u975e\u5e38\u91cd\u8981\u3002\u8fd9\u80fd\u5e2e\u52a9\u4f60\u6355\u83b7\u53ef\u80fd\u7684\u6570\u636e\u7ade\u4e89,\u4ece\u800c\u907f\u514d\u8f6f\u4ef6bug\u3002
\u6253\u5f00\u5f00\u5173 -parallel
\u6709\u52a9\u4e8e\u52a0\u901f\u6d4b\u8bd5\u7684\u6267\u884c\uff0c\u7279\u522b\u662f\u6d4b\u8bd5\u4e2d\u5305\u542b\u4e00\u4e9b\u9700\u8981\u957f\u671f\u8fd0\u884c\u7684\u7528\u4f8b\u7684\u65f6\u5019\u3002
\u6253\u5f00\u5f00\u5173 -shuffle
\u80fd\u591f\u6253\u4e71\u6d4b\u8bd5\u7528\u4f8b\u6267\u884c\u7684\u987a\u5e8f\uff0c\u907f\u514d\u4e00\u4e2a\u6d4b\u8bd5\u4f9d\u8d56\u4e8e\u67d0\u4e9b\u4e0d\u7b26\u5408\u771f\u5b9e\u60c5\u51b5\u7684\u9884\u8bbe\uff0c\u6709\u52a9\u4e8e\u53ca\u65e9\u66b4\u6f0fbug\u3002
\u8868\u9a71\u52a8\u7684\u6d4b\u8bd5\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u5f0f,\u53ef\u4ee5\u5c06\u4e00\u7ec4\u76f8\u4f3c\u7684\u6d4b\u8bd5\u5206\u7ec4\u5728\u4e00\u8d77,\u4ee5\u907f\u514d\u4ee3\u7801\u91cd\u590d\u548c\u4f7f\u672a\u6765\u7684\u66f4\u65b0\u66f4\u5bb9\u6613\u5904\u7406\u3002
"},{"location":"zh/#sleep-86","title":"\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u6267\u884csleep\u64cd\u4f5c (#86)","text":"\u4f7f\u7528\u540c\u6b65\u7684\u65b9\u5f0f\u3001\u907f\u514dsleep\uff0c\u6765\u5c3d\u91cf\u51cf\u5c11\u6d4b\u8bd5\u7684\u4e0d\u7a33\u5b9a\u6027\u548c\u63d0\u9ad8\u9c81\u68d2\u6027\u3002\u5982\u679c\u65e0\u6cd5\u4f7f\u7528\u540c\u6b65\u624b\u6bb5,\u53ef\u4ee5\u8003\u8651\u91cd\u8bd5\u7684\u65b9\u5f0f\u3002
"},{"location":"zh/#time-api-87","title":"\u6ca1\u6709\u9ad8\u6548\u5730\u5904\u7406 time API (#87)","text":"\u7406\u89e3\u5982\u4f55\u5904\u7406\u4f7f\u7528 time API \u7684\u51fd\u6570\uff0c\u662f\u4f7f\u6d4b\u8bd5\u66f4\u52a0\u7a33\u5b9a\u7684\u53e6\u4e00\u79cd\u65b9\u5f0f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u6280\u672f\uff0c\u4f8b\u5982\u5c06\u65f6\u95f4\u4f5c\u4e3a\u9690\u85cf\u4f9d\u8d56\u9879\u7684\u4e00\u90e8\u5206\u6765\u5904\u7406\uff0c\u6216\u8005\u8981\u6c42\u5ba2\u6237\u7aef\u63d0\u4f9b\u65f6\u95f4\u3002
"},{"location":"zh/#httptest-iotest-88","title":"\u4e0d\u4f7f\u7528\u6d4b\u8bd5\u76f8\u5173\u7684\u5de5\u5177\u5305 (httptest
\u548c iotest
) (#88)","text":"\u8fd9\u4e2a httptest
\u5305\u5bf9\u5904\u7406HTTP\u5e94\u7528\u7a0b\u5e8f\u5f88\u6709\u5e2e\u52a9\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5b9e\u7528\u7a0b\u5e8f\u6765\u6d4b\u8bd5\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u3002
\u8fd9\u4e2a iotest
\u5305\u6709\u52a9\u4e8e\u7f16\u5199 io.Reader \u5e76\u6d4b\u8bd5\u5e94\u7528\u7a0b\u5e8f\u662f\u5426\u80fd\u591f\u5bb9\u5fcd\u9519\u8bef\u3002
\u4f7f\u7528 time \u65b9\u6cd5\u6765\u4fdd\u6301\u57fa\u51c6\u6d4b\u8bd5\u7684\u51c6\u786e\u6027\u3002
\u589e\u52a0 benchtime
\u6216\u8005\u4f7f\u7528 benchstat
\u7b49\u5de5\u5177\u53ef\u4ee5\u6709\u52a9\u4e8e\u5fae\u57fa\u51c6\u6d4b\u8bd5\u3002
\u5c0f\u5fc3\u5fae\u57fa\u51c6\u6d4b\u8bd5\u7684\u7ed3\u679c,\u5982\u679c\u6700\u7ec8\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u7684\u7cfb\u7edf\u4e0e\u8fd0\u884c\u5fae\u57fa\u51c6\u6d4b\u8bd5\u7684\u7cfb\u7edf\u4e0d\u540c\u3002
\u786e\u4fdd\u6d4b\u8bd5\u51fd\u6570\u662f\u5426\u4f1a\u4ea7\u751f\u4e00\u4e9b\u526f\u4f5c\u7528\uff0c\u9632\u6b62\u7f16\u8bd1\u5668\u4f18\u5316\u6b3a\u9a97\u4f60\u5f97\u5230\u7684\u57fa\u51c6\u6d4b\u8bd5\u7ed3\u679c\u3002
\u4e3a\u4e86\u907f\u514d\u88ab\u89c2\u5bdf\u8005\u6548\u5e94\u6b3a\u9a97,\u5f3a\u5236\u91cd\u65b0\u521b\u5efaCPU\u5bc6\u96c6\u578b\u51fd\u6570\u4f7f\u7528\u7684\u6570\u636e\u3002
"},{"location":"zh/#go-test-90","title":"\u6ca1\u6709\u53bb\u63a2\u7d22go test\u6240\u6709\u7684\u7279\u6027 (#90)","text":"\u4f7f\u7528 -coverprofile
\u53c2\u6570\u53ef\u4ee5\u5feb\u901f\u67e5\u770b\u4ee3\u7801\u7684\u6d4b\u8bd5\u8986\u76d6\u60c5\u51b5\uff0c\u65b9\u4fbf\u5feb\u901f\u67e5\u770b\u54ea\u4e2a\u90e8\u5206\u9700\u8981\u66f4\u591a\u7684\u5173\u6ce8\u3002
\u5355\u5143\u6d4b\u8bd5\u7ec4\u7ec7\u5230\u4e00\u4e2a\u72ec\u7acb\u7684\u5305\u4e2d\uff0c\u5bf9\u4e8e\u5bf9\u5916\u5c42\u66b4\u6f0f\u7684\u63a5\u53e3\uff0c\u9700\u8981\u5199\u4e00\u4e9b\u6d4b\u8bd5\u7528\u4f8b\u3002\u6d4b\u8bd5\u5e94\u8be5\u5173\u6ce8\u516c\u5f00\u7684\u884c\u4e3a\uff0c\u800c\u975e\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\u3002
\u5904\u7406\u9519\u8bef\u65f6,\u4f7f\u7528 *testing.T
\u53d8\u91cf\u800c\u4e0d\u662f\u7ecf\u5178\u7684 if err != nil
\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3002
\u4f60\u53ef\u4ee5\u4f7f\u7528setup\u548cteardown\u51fd\u6570\u6765\u914d\u7f6e\u4e00\u4e2a\u590d\u6742\u7684\u73af\u5883\uff0c\u6bd4\u5982\u5728\u96c6\u6210\u6d4b\u8bd5\u7684\u60c5\u51b5\u4e0b\u3002
"},{"location":"zh/#_11","title":"\u4e0d\u4f7f\u7528\u6a21\u7cca\u6d4b\u8bd5 (\u793e\u533a\u53cd\u9988\u9519\u8bef)","text":"\u6a21\u7cca\u6d4b\u8bd5\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u7b56\u7565\uff0c\u4f7f\u7528\u5b83\u80fd\u68c0\u6d4b\u51fa\u968f\u673a\u3001\u610f\u6599\u5916\u7684\u548c\u4e00\u4e9b\u6076\u610f\u7684\u6570\u636e\u8f93\u5165\uff0c\u6765\u5b8c\u6210\u4e00\u4e9b\u590d\u6742\u7684\u64cd\u4f5c\u3002
\u89c1: @jeromedoucet
"},{"location":"zh/#_12","title":"\u4f18\u5316\u6280\u672f","text":""},{"location":"zh/#cpu-cache-91","title":"\u4e0d\u7406\u89e3CPU cache (#91)","text":"\u7406\u89e3CPU\u7f13\u5b58\u7684\u4f7f\u7528\u5bf9\u4e8e\u4f18\u5316CPU\u5bc6\u96c6\u578b\u5e94\u7528\u5f88\u91cd\u8981\uff0c\u56e0\u4e3aL1\u7f13\u5b58\u6bd4\u4e3b\u5b58\u5feb50\u5230100\u500d\u3002
\u610f\u8bc6\u5230 cache line \u6982\u5ff5\u5bf9\u4e8e\u7406\u89e3\u5982\u4f55\u5728\u6570\u636e\u5bc6\u96c6\u578b\u5e94\u7528\u4e2d\u7ec4\u7ec7\u6570\u636e\u975e\u5e38\u5173\u952e\u3002CPU \u5e76\u4e0d\u662f\u4e00\u4e2a\u4e00\u4e2a\u5b57\u6765\u83b7\u53d6\u5185\u5b58\u3002\u76f8\u53cd\uff0c\u5b83\u901a\u5e38\u590d\u5236\u4e00\u4e2a 64\u5b57\u8282\u957f\u5ea6\u7684 cache line\u3002\u4e3a\u4e86\u83b7\u5f97\u6bcf\u4e2a cache line \u7684\u6700\u5927\u6548\u7528\uff0c\u9700\u8981\u5b9e\u65bd\u7a7a\u95f4\u5c40\u90e8\u6027\u3002
\u4e00\u7cfb\u5217struct\u5143\u7d20\u6784\u6210\u7684slice vs. \u591a\u4e2aslice\u5b57\u6bb5\u6784\u6210\u7684struct
\u6982\u7387\u6027\u7684\u95ee\u9898
\u63d0\u9ad8CPU\u6267\u884c\u4ee3\u7801\u65f6\u7684\u53ef\u9884\u6d4b\u6027\uff0c\u4e5f\u662f\u4f18\u5316\u67d0\u4e9b\u51fd\u6570\u7684\u4e00\u4e2a\u6709\u6548\u65b9\u6cd5\u3002\u6bd4\u5982\uff0c\u56fa\u5b9a\u6b65\u957f\u6216\u8fde\u7eed\u8bbf\u95ee\u5bf9CPU\u6765\u8bf4\u662f\u53ef\u9884\u6d4b\u7684\uff0c\u4f46\u975e\u8fde\u7eed\u8bbf\u95ee\uff08\u4f8b\u5982\u94fe\u8868\uff09\u5c31\u662f\u4e0d\u53ef\u9884\u6d4b\u7684\u3002
\u8981\u6ce8\u610f\u73b0\u4ee3\u7f13\u5b58\u662f\u5206\u533a\u7684\uff08set associative placement\uff0c\u7ec4\u76f8\u8fde\u6620\u5c04\uff09\uff0c\u8981\u6ce8\u610f\u907f\u514d\u4f7f\u7528 critical stride
\uff0c\u8fd9\u79cd\u6b65\u957f\u60c5\u51b5\u4e0b\u53ea\u80fd\u5229\u7528 cache \u7684\u4e00\u5c0f\u90e8\u5206\u3002
critical stride\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u6b65\u957f\uff0c\u6307\u7684\u662f\u5185\u5b58\u8bbf\u95ee\u7684\u6b65\u957f\u521a\u597d\u7b49\u4e8e cache \u5927\u5c0f\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u5c11\u90e8\u5206 cacheline \u88ab\u5229\u7528\u3002
"},{"location":"zh/#false-sharing-92","title":"\u5199\u7684\u5e76\u53d1\u5904\u7406\u903b\u8f91\u4f1a\u5bfc\u81f4false sharing (#92)","text":"\u4e86\u89e3 CPU \u7f13\u5b58\u7684\u8f83\u4f4e\u5c42\u7684 L1\u3001L2 cache \u4e0d\u4f1a\u5728\u6240\u6709\u6838\u95f4\u5171\u4eab\uff0c\u7f16\u5199\u5e76\u53d1\u5904\u7406\u903b\u8f91\u65f6\u80fd\u907f\u514d\u5199\u51fa\u4e00\u4e9b\u964d\u4f4e\u6027\u80fd\u7684\u95ee\u9898\uff0c\u6bd4\u5982\u4f2a\u5171\u4eab\uff08false sharing\uff09\u3002\u5185\u5b58\u5171\u4eab\u53ea\u662f\u4e00\u79cd\u5047\u8c61\u3002
"},{"location":"zh/#93","title":"\u6ca1\u6709\u8003\u8651\u6307\u4ee4\u7ea7\u7684\u5e76\u884c (#93)","text":"\u4f7f\u7528\u6307\u4ee4\u7ea7\u5e76\u884c\uff08ILP\uff09\u4f18\u5316\u4ee3\u7801\u7684\u7279\u5b9a\u90e8\u5206\uff0c\u4ee5\u5141\u8bb8CPU\u5c3d\u53ef\u80fd\u6267\u884c\u66f4\u591a\u53ef\u4ee5\u5e76\u884c\u6267\u884c\u7684\u6307\u4ee4\u3002\u8bc6\u522b\u6307\u4ee4\u7684\u6570\u636e\u4f9d\u8d56\u95ee\u9898\uff08data hazards\uff09\u662f\u4e3b\u8981\u6b65\u9aa4\u4e4b\u4e00\u3002
"},{"location":"zh/#94","title":"\u4e0d\u4e86\u89e3\u6570\u636e\u5bf9\u9f50 (#94)","text":"\u8bb0\u4f4fGo\u4e2d\u57fa\u672c\u7c7b\u578b\u4e0e\u5176\u81ea\u8eab\u5927\u5c0f\u5bf9\u9f50\uff0c\u4f8b\u5982\uff0c\u6309\u964d\u5e8f\u4ece\u5927\u5230\u5c0f\u91cd\u65b0\u7ec4\u7ec7\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u53ef\u4ee5\u5f62\u6210\u66f4\u7d27\u51d1\u7684\u7ed3\u6784\u4f53\uff08\u51cf\u5c11\u5185\u5b58\u5206\u914d\uff0c\u66f4\u597d\u7684\u7a7a\u95f4\u5c40\u90e8\u6027\uff09\uff0c\u8fd9\u6709\u52a9\u4e8e\u907f\u514d\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u3002
"},{"location":"zh/#stack-vs-heap-95","title":"\u4e0d\u4e86\u89e3 stack vs. heap (#95)","text":"\u4e86\u89e3\u5806\u548c\u6808\u4e4b\u95f4\u7684\u533a\u522b\u662f\u5f00\u53d1\u4eba\u5458\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\uff0c\u7279\u522b\u662f\u8981\u53bb\u4f18\u5316\u4e00\u4e2aGo\u7a0b\u5e8f\u65f6\u3002\u6808\u5206\u914d\u7684\u5f00\u9500\u51e0\u4e4e\u4e3a\u96f6\uff0c\u800c\u5806\u5206\u914d\u5219\u8f83\u6162\uff0c\u5e76\u4e14\u4f9d\u8d56GC\u6765\u6e05\u7406\u5185\u5b58\u3002
"},{"location":"zh/#api-compiler-optimizations-and-syncpool-96","title":"\u4e0d\u77e5\u9053\u5982\u4f55\u51cf\u5c11\u5185\u5b58\u5206\u914d\u6b21\u6570 (API\u8c03\u6574, compiler optimizations, andsync.Pool
) (#96)","text":"\u51cf\u5c11\u5185\u5b58\u5206\u914d\u6b21\u6570\u4e5f\u662f\u4f18\u5316Go\u5e94\u7528\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0,\u6bd4\u5982\u4ed4\u7ec6\u8bbe\u8ba1API\u6765\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u4ee5\u53ca\u4f7f\u7528 sync.Pool
\u6765\u5bf9\u5f85\u5206\u914d\u5bf9\u8c61\u8fdb\u884c\u6c60\u5316\u3002
\u4f7f\u7528\u5feb\u901f\u8def\u5f84\u7684\u5185\u8054\u6280\u672f\u6765\u66f4\u52a0\u6709\u6548\u5730\u51cf\u5c11\u8c03\u7528\u51fd\u6570\u7684\u644a\u9500\u65f6\u95f4\u3002
"},{"location":"zh/#go-98","title":"\u4e0d\u4f7f\u7528Go\u95ee\u9898\u8bca\u65ad\u5de5\u5177 (#98)","text":"\u4e86\u89e3Go profilng\u5de5\u5177\u3001\u6267\u884c\u65f6tracer\u6765\u8f85\u52a9\u5224\u65ad\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u662f\u5426\u6b63\u5e38\uff0c\u4ee5\u53ca\u5217\u51fa\u9700\u8981\u4f18\u5316\u7684\u90e8\u5206\u3002
"},{"location":"zh/#gc-99","title":"\u4e0d\u7406\u89e3GC\u662f\u5982\u4f55\u5de5\u4f5c\u7684 (#99)","text":"\u7406\u89e3\u5982\u4f55\u8c03\u4f18GC\u80fd\u591f\u5e26\u6765\u5f88\u591a\u6536\u76ca\uff0c\u4f8b\u5982\u6709\u52a9\u4e8e\u66f4\u9ad8\u6548\u5730\u5904\u7406\u7a81\u589e\u7684\u8d1f\u8f7d\u3002
"},{"location":"zh/#dockerk8sgo-100","title":"\u4e0d\u4e86\u89e3Docker\u6216\u8005K8S\u5bf9\u8fd0\u884c\u7684Go\u5e94\u7528\u7684\u6027\u80fd\u5f71\u54cd (#100)","text":"\u4e3a\u4e86\u907f\u514dCPU throttling\uff08CPU\u9650\u9891\uff09\u95ee\u9898\uff0c\u5f53\u6211\u4eec\u5728Docker\u548cKubernetes\u90e8\u7f72\u5e94\u7528\u65f6\uff0c\u8981\u77e5\u9053Go\u8bed\u8a00\u5bf9CFS(\u5b8c\u5168\u516c\u5e73\u8c03\u5ea6\u5668)\u65e0\u611f\u77e5\u3002
"}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Common Go Mistakes","text":"This page is a summary of all the mistakes in the 100 Go Mistakes book. Meanwhile, it's also a page open to the community. If you believe that a mistake should be added, please create a community mistake issue.
WarningYou're currently viewing a new version that I'm enriching with significantly more content. Yet, this version is still under development; please be gentle if you find an issue, and feel free to create a PR.
"},{"location":"#code-and-project-organization","title":"Code and Project Organization","text":""},{"location":"#unintended-variable-shadowing-1","title":"Unintended variable shadowing (#1)","text":"TL;DRAvoiding shadowed variables can help prevent mistakes like referencing the wrong variable or confusing readers.
Variable shadowing occurs when a variable name is redeclared in an inner block, but this practice is prone to mistakes. Imposing a rule to forbid shadowed variables depends on personal taste. For example, sometimes it can be convenient to reuse an existing variable name like err
for errors. Yet, in general, we should remain cautious because we now know that we can face a scenario where the code compiles, but the variable that receives the value is not the one expected.
Source code
"},{"location":"#unnecessary-nested-code-2","title":"Unnecessary nested code (#2)","text":"TL;DRAvoiding nested levels and keeping the happy path aligned on the left makes building a mental code model easier.
In general, the more nested levels a function requires, the more complex it is to read and understand. Let\u2019s see some different applications of this rule to optimize our code for readability:
if
block returns, we should omit the else
block in all cases. For example, we shouldn\u2019t write:if foo() {\n// ...\nreturn true\n} else {\n// ...\n}\n
Instead, we omit the else
block like this:
if foo() {\n// ...\nreturn true\n}\n// ...\n
if s != \"\" {\n// ...\n} else {\nreturn errors.New(\"empty string\")\n}\n
Here, an empty s
represents the non-happy path. Hence, we should flip the condition like so:
if s == \"\" {\nreturn errors.New(\"empty string\")\n}\n// ...\n
Writing readable code is an important challenge for every developer. Striving to reduce the number of nested blocks, aligning the happy path on the left, and returning as early as possible are concrete means to improve our code\u2019s readability.
Source code
"},{"location":"#misusing-init-functions-3","title":"Misusing init functions (#3)","text":"TL;DRWhen initializing variables, remember that init functions have limited error handling and make state handling and testing more complex. In most cases, initializations should be handled as specific functions.
An init function is a function used to initialize the state of an application. It takes no arguments and returns no result (a func()
function). When a package is initialized, all the constant and variable declarations in the package are evaluated. Then, the init functions are executed.
Init functions can lead to some issues:
We should be cautious with init functions. They can be helpful in some situations, however, such as defining static configuration. Otherwise, and in most cases, we should handle initializations through ad hoc functions.
Source code
"},{"location":"#overusing-getters-and-setters-4","title":"Overusing getters and setters (#4)","text":"TL;DRForcing the use of getters and setters isn\u2019t idiomatic in Go. Being pragmatic and finding the right balance between efficiency and blindly following certain idioms should be the way to go.
Data encapsulation refers to hiding the values or state of an object. Getters and setters are means to enable encapsulation by providing exported methods on top of unexported object fields.
In Go, there is no automatic support for getters and setters as we see in some languages. It is also considered neither mandatory nor idiomatic to use getters and setters to access struct fields. We shouldn\u2019t overwhelm our code with getters and setters on structs if they don\u2019t bring any value. We should be pragmatic and strive to find the right balance between efficiency and following idioms that are sometimes considered indisputable in other programming paradigms.
Remember that Go is a unique language designed for many characteristics, including simplicity. However, if we find a need for getters and setters or, as mentioned, foresee a future need while guaranteeing forward compatibility, there\u2019s nothing wrong with using them.
"},{"location":"#interface-pollution-5","title":"Interface pollution (#5)","text":"TL;DRAbstractions should be discovered, not created. To prevent unnecessary complexity, create an interface when you need it and not when you foresee needing it, or if you can at least prove the abstraction to be a valid one.
An interface provides a way to specify the behavior of an object. We use interfaces to create common abstractions that multiple objects can implement. What makes Go interfaces so different is that they are satisfied implicitly. There is no explicit keyword like implements
to mark that an object X
implements interface Y
.
In general, we can define three main use cases where interfaces are generally considered as bringing value: factoring out a common behavior, creating some decoupling, and restricting a type to a certain behavior. Yet, this list isn't exhaustive and will also depend on the context we face.
In many occasions, interfaces are made to create abstractions. And the main caveat when programming meets abstractions is remembering that abstractions should be discovered, not created. What does this mean? It means we shouldn\u2019t start creating abstractions in our code if there is no immediate reason to do so. We shouldn\u2019t design with interfaces but wait for a concrete need. Said differently, we should create an interface when we need it, not when we foresee that we could need it. What\u2019s the main problem if we overuse interfaces? The answer is that they make the code flow more complex. Adding a useless level of indirection doesn\u2019t bring any value; it creates a worthless abstraction making the code more difficult to read, understand, and reason about. If we don\u2019t have a strong reason for adding an interface and it\u2019s unclear how an interface makes a code better, we should challenge this interface\u2019s purpose. Why not call the implementation directly?
We should be cautious when creating abstractions in our code (abstractions should be discovered, not created). It\u2019s common for us, software developers, to overengineer our code by trying to guess what the perfect level of abstraction is, based on what we think we might need later. This process should be avoided because, in most cases, it pollutes our code with unnecessary abstractions, making it more complex to read.
Rob Pike
Don\u2019t design with interfaces, discover them.
Let\u2019s not try to solve a problem abstractly but solve what has to be solved now. Last, but not least, if it\u2019s unclear how an interface makes the code better, we should probably consider removing it to make our code simpler.
Source code
"},{"location":"#interface-on-the-producer-side-6","title":"Interface on the producer side (#6)","text":"TL;DRKeeping interfaces on the client side avoids unnecessary abstractions.
Interfaces are satisfied implicitly in Go, which tends to be a gamechanger compared to languages with an explicit implementation. In most cases, the approach to follow is similar to what we described in the previous section: abstractions should be discovered, not created. This means that it\u2019s not up to the producer to force a given abstraction for all the clients. Instead, it\u2019s up to the client to decide whether it needs some form of abstraction and then determine the best abstraction level for its needs.
An interface should live on the consumer side in most cases. However, in particular contexts (for example, when we know\u2014not foresee\u2014that an abstraction will be helpful for consumers), we may want to have it on the producer side. If we do, we should strive to keep it as minimal as possible, increasing its reusability potential and making it more easily composable.
Source code
"},{"location":"#returning-interfaces-7","title":"Returning interfaces (#7)","text":"TL;DRTo prevent being restricted in terms of flexibility, a function shouldn\u2019t return interfaces but concrete implementations in most cases. Conversely, a function should accept interfaces whenever possible.
In most cases, we shouldn\u2019t return interfaces but concrete implementations. Otherwise, it can make our design more complex due to package dependencies and can restrict flexibility because all the clients would have to rely on the same abstraction. Again, the conclusion is similar to the previous sections: if we know (not foresee) that an abstraction will be helpful for clients, we can consider returning an interface. Otherwise, we shouldn\u2019t force abstractions; they should be discovered by clients. If a client needs to abstract an implementation for whatever reason, it can still do that on the client\u2019s side.
"},{"location":"#any-says-nothing-8","title":"any
says nothing (#8)","text":"TL;DR Only use any
if you need to accept or return any possible type, such as json.Marshal
. Otherwise, any
doesn\u2019t provide meaningful information and can lead to compile-time issues by allowing a caller to call methods with any data type.
The any
type can be helpful if there is a genuine need for accepting or returning any possible type (for instance, when it comes to marshaling or formatting). In general, we should avoid overgeneralizing the code we write at all costs. Perhaps a little bit of duplicated code might occasionally be better if it improves other aspects such as code expressiveness.
Source code
"},{"location":"#being-confused-about-when-to-use-generics-9","title":"Being confused about when to use generics (#9)","text":"TL;DRRelying on generics and type parameters can prevent writing boilerplate code to factor out elements or behaviors. However, do not use type parameters prematurely, but only when you see a concrete need for them. Otherwise, they introduce unnecessary abstractions and complexity.
Read the full section here.
Source code
"},{"location":"#not-being-aware-of-the-possible-problems-with-type-embedding-10","title":"Not being aware of the possible problems with type embedding (#10)","text":"TL;DRUsing type embedding can also help avoid boilerplate code; however, ensure that doing so doesn\u2019t lead to visibility issues where some fields should have remained hidden.
When creating a struct, Go offers the option to embed types. But this can sometimes lead to unexpected behaviors if we don\u2019t understand all the implications of type embedding. Throughout this section, we look at how to embed types, what these bring, and the possible issues.
In Go, a struct field is called embedded if it\u2019s declared without a name. For example,
type Foo struct {\nBar // Embedded field\n}\ntype Bar struct {\nBaz int\n}\n
In the Foo
struct, the Bar
type is declared without an associated name; hence, it\u2019s an embedded field.
We use embedding to promote the fields and methods of an embedded type. Because Bar contains a Baz field, this field is promoted to Foo
. Therefore, Baz becomes available from Foo.
What can we say about type embedding? First, let\u2019s note that it\u2019s rarely a necessity, and it means that whatever the use case, we can probably solve it as well without type embedding. Type embedding is mainly used for convenience: in most cases, to promote behaviors.
If we decide to use type embedding, we need to keep two main constraints in mind:
Foo.Baz()
instead of Foo.Bar.Baz()
). If this is the only rationale, let\u2019s not embed the inner type and use a field instead.Using type embedding consciously by keeping these constraints in mind can help avoid boilerplate code with additional forwarding methods. However, let\u2019s make sure we don\u2019t do it solely for cosmetics and not promote elements that should remain hidden.
Source code
"},{"location":"#not-using-the-functional-options-pattern-11","title":"Not using the functional options pattern (#11)","text":"TL;DRTo handle options conveniently and in an API-friendly manner, use the functional options pattern.
Although there are different implementations with minor variations, the main idea is as follows:
type Option func(options *options)
error. For example, WithPort
accepts an int
argument that represents the port and returns an Option
type that represents how to update the options
struct.type options struct {\nport *int\n}\ntype Option func(options *options) error\nfunc WithPort(port int) Option {\nreturn func(options *options) error {\nif port < 0 {\nreturn errors.New(\"port should be positive\")\n}\noptions.port = &port\nreturn nil\n}\n}\nfunc NewServer(addr string, opts ...Option) ( *http.Server, error) { <1>\nvar options options <2>\nfor _, opt := range opts { <3>\nerr := opt(&options) <4>\nif err != nil {\nreturn nil, err\n}\n}\n// At this stage, the options struct is built and contains the config\n// Therefore, we can implement our logic related to port configuration\nvar port int\nif options.port == nil {\nport = defaultHTTPPort\n} else {\nif *options.port == 0 {\nport = randomPort()\n} else {\nport = *options.port\n}\n}\n// ...\n}\n
The functional options pattern provides a handy and API-friendly way to handle options. Although the builder pattern can be a valid option, it has some minor downsides (having to pass a config struct that can be empty or a less handy way to handle error management) that tend to make the functional options pattern the idiomatic way to deal with these kind of problems in Go.
Source code
"},{"location":"#project-misorganization-project-structure-and-package-organization-12","title":"Project misorganization (project structure and package organization) (#12)","text":"Regarding the overall organization, there are different schools of thought. For example, should we organize our application by context or by layer? It depends on our preferences. We may favor grouping code per context (such as the customer context, the contract context, etc.), or we may favor following hexagonal architecture principles and group per technical layer. If the decision we make fits our use case, it cannot be a wrong decision, as long as we remain consistent with it.
Regarding packages, there are multiple best practices that we should follow. First, we should avoid premature packaging because it might cause us to overcomplicate a project. Sometimes, it\u2019s better to use a simple organization and have our project evolve when we understand what it contains rather than forcing ourselves to make the perfect structure up front. Granularity is another essential thing to consider. We should avoid having dozens of nano packages containing only one or two files. If we do, it\u2019s because we have probably missed some logical connections across these packages, making our project harder for readers to understand. Conversely, we should also avoid huge packages that dilute the meaning of a package name.
Package naming should also be considered with care. As we all know (as developers), naming is hard. To help clients understand a Go project, we should name our packages after what they provide, not what they contain. Also, naming should be meaningful. Therefore, a package name should be short, concise, expressive, and, by convention, a single lowercase word.
Regarding what to export, the rule is pretty straightforward. We should minimize what should be exported as much as possible to reduce the coupling between packages and keep unnecessary exported elements hidden. If we are unsure whether to export an element or not, we should default to not exporting it. Later, if we discover that we need to export it, we can adjust our code. Let\u2019s also keep in mind some exceptions, such as making fields exported so that a struct can be unmarshaled with encoding/json.
Organizing a project isn\u2019t straightforward, but following these rules should help make it easier to maintain. However, remember that consistency is also vital to ease maintainability. Therefore, let\u2019s make sure that we keep things as consistent as possible within a codebase.
NoteIn 2023, the Go team has published an official guideline for organizing / structuring a Go project: go.dev/doc/modules/layout
"},{"location":"#creating-utility-packages-13","title":"Creating utility packages (#13)","text":"TL;DRNaming is a critical piece of application design. Creating packages such as common
, util
, and shared
doesn\u2019t bring much value for the reader. Refactor such packages into meaningful and specific package names.
Also, bear in mind that naming a package after what it provides and not what it contains can be an efficient way to increase its expressiveness.
Source code
"},{"location":"#ignoring-package-name-collisions-14","title":"Ignoring package name collisions (#14)","text":"TL;DRTo avoid naming collisions between variables and packages, leading to confusion or perhaps even bugs, use unique names for each one. If this isn\u2019t feasible, use an import alias to change the qualifier to differentiate the package name from the variable name, or think of a better name.
Package collisions occur when a variable name collides with an existing package name, preventing the package from being reused. We should prevent variable name collisions to avoid ambiguity. If we face a collision, we should either find another meaningful name or use an import alias.
"},{"location":"#missing-code-documentation-15","title":"Missing code documentation (#15)","text":"TL;DRTo help clients and maintainers understand your code\u2019s purpose, document exported elements.
Documentation is an important aspect of coding. It simplifies how clients can consume an API but can also help in maintaining a project. In Go, we should follow some rules to make our code idiomatic:
First, every exported element must be documented. Whether it is a structure, an interface, a function, or something else, if it\u2019s exported, it must be documented. The convention is to add comments, starting with the name of the exported element.
As a convention, each comment should be a complete sentence that ends with punctuation. Also bear in mind that when we document a function (or a method), we should highlight what the function intends to do, not how it does it; this belongs to the core of a function and comments, not documentation. Furthermore, the documentation should ideally provide enough information that the consumer does not have to look at our code to understand how to use an exported element.
When it comes to documenting a variable or a constant, we might be interested in conveying two aspects: its purpose and its content. The former should live as code documentation to be useful for external clients. The latter, though, shouldn\u2019t necessarily be public.
To help clients and maintainers understand a package\u2019s scope, we should also document each package. The convention is to start the comment with // Package
followed by the package name. The first line of a package comment should be concise. That\u2019s because it will appear in the package. Then, we can provide all the information we need in the following lines.
Documenting our code shouldn\u2019t be a constraint. We should take the opportunity to make sure it helps clients and maintainers to understand the purpose of our code.
"},{"location":"#not-using-linters-16","title":"Not using linters (#16)","text":"TL;DRTo improve code quality and consistency, use linters and formatters.
A linter is an automatic tool to analyze code and catch errors. The scope of this section isn\u2019t to give an exhaustive list of the existing linters; otherwise, it will become deprecated pretty quickly. But we should understand and remember why linters are essential for most Go projects.
However, if you\u2019re not a regular user of linters, here is a list that you may want to use daily:
Linters and formatters are a powerful way to improve the quality and consistency of our codebase. Let\u2019s take the time to understand which one we should use and make sure we automate their execution (such as a CI or Git precommit hook).
"},{"location":"#data-types","title":"Data Types","text":""},{"location":"#creating-confusion-with-octal-literals-17","title":"Creating confusion with octal literals (#17)","text":"TL;DRWhen reading existing code, bear in mind that integer literals starting with 0
are octal numbers. Also, to improve readability, make octal integers explicit by prefixing them with 0o
.
Octal numbers start with a 0 (e.g., 010
is equal to 8 in base 10). To improve readability and avoid potential mistakes for future code readers, we should make octal numbers explicit using the 0o
prefix (e.g., 0o10
).
We should also note the other integer literal representations:
0b
or 0B
prefix (for example, 0b100
is equal to 4 in base 10)0x
or 0X
prefix (for example, 0xF
is equal to 15 in base 10)i
suffix (for example, 3i
)We can also use an underscore character (_) as a separator for readability. For example, we can write 1 billion this way: 1_000_000_000
. We can also use the underscore character with other representations (for example, 0b00_00_01
).
Source code
"},{"location":"#neglecting-integer-overflows-18","title":"Neglecting integer overflows (#18)","text":"TL;DRBecause integer overflows and underflows are handled silently in Go, you can implement your own functions to catch them.
In Go, an integer overflow that can be detected at compile time generates a compilation error. For example,
var counter int32 = math.MaxInt32 + 1\n
constant 2147483648 overflows int32\n
However, at run time, an integer overflow or underflow is silent; this does not lead to an application panic. It is essential to keep this behavior in mind, because it can lead to sneaky bugs (for example, an integer increment or addition of positive integers that leads to a negative result).
Source code
"},{"location":"#not-understanding-floating-points-19","title":"Not understanding floating-points (#19)","text":"TL;DRMaking floating-point comparisons within a given delta can ensure that your code is portable. When performing addition or subtraction, group the operations with a similar order of magnitude to favor accuracy. Also, perform multiplication and division before addition and subtraction.
In Go, there are two floating-point types (if we omit imaginary numbers): float32 and float64. The concept of a floating point was invented to solve the major problem with integers: their inability to represent fractional values. To avoid bad surprises, we need to know that floating-point arithmetic is an approximation of real arithmetic.
For that, we\u2019ll look at a multiplication example:
var n float32 = 1.0001\nfmt.Println(n * n)\n
We may expect this code to print the result of 1.0001 * 1.0001 = 1.00020001, right? However, running it on most x86 processors prints 1.0002, instead.
Because Go\u2019s float32
and float64
types are approximations, we have to bear a few rules in mind:
Source code
"},{"location":"#not-understanding-slice-length-and-capacity-20","title":"Not understanding slice length and capacity (#20)","text":"TL;DRUnderstanding the difference between slice length and capacity should be part of a Go developer\u2019s core knowledge. The slice length is the number of available elements in the slice, whereas the slice capacity is the number of elements in the backing array.
Read the full section here.
Source code
"},{"location":"#inefficient-slice-initialization-21","title":"Inefficient slice initialization (#21)","text":"TL;DRWhen creating a slice, initialize it with a given length or capacity if its length is already known. This reduces the number of allocations and improves performance.
While initializing a slice using make
, we can provide a length and an optional capacity. Forgetting to pass an appropriate value for both of these parameters when it makes sense is a widespread mistake. Indeed, it can lead to multiple copies and additional effort for the GC to clean the temporary backing arrays. Performance-wise, there\u2019s no good reason not to give the Go runtime a helping hand.
Our options are to allocate a slice with either a given capacity or a given length. Of these two solutions, we have seen that the second tends to be slightly faster. But using a given capacity and append can be easier to implement and read in some contexts.
Source code
"},{"location":"#being-confused-about-nil-vs-empty-slice-22","title":"Being confused about nil vs. empty slice (#22)","text":"TL;DRTo prevent common confusions such as when using the encoding/json
or the reflect
package, you need to understand the difference between nil and empty slices. Both are zero-length, zero-capacity slices, but only a nil slice doesn\u2019t require allocation.
In Go, there is a distinction between nil and empty slices. A nil slice is equals to nil
, whereas an empty slice has a length of zero. A nil slice is empty, but an empty slice isn\u2019t necessarily nil
. Meanwhile, a nil slice doesn\u2019t require any allocation. We have seen throughout this section how to initialize a slice depending on the context by using
var s []string
if we aren\u2019t sure about the final length and the slice can be empty[]string(nil)
as syntactic sugar to create a nil and empty slicemake([]string, length)
if the future length is knownThe last option, []string{}
, should be avoided if we initialize the slice without elements. Finally, let\u2019s check whether the libraries we use make the distinctions between nil and empty slices to prevent unexpected behaviors.
Source code
"},{"location":"#not-properly-checking-if-a-slice-is-empty-23","title":"Not properly checking if a slice is empty (#23)","text":"TL;DRTo check if a slice doesn\u2019t contain any element, check its length. This check works regardless of whether the slice is nil
or empty. The same goes for maps. To design unambiguous APIs, you shouldn\u2019t distinguish between nil and empty slices.
To determine whether a slice has elements, we can either do it by checking if the slice is nil or if its length is equal to 0. Checking the length is the best option to follow as it will cover both if the slice is empty or if the slice is nil.
Meanwhile, when designing interfaces, we should avoid distinguishing nil and empty slices, which leads to subtle programming errors. When returning slices, it should make neither a semantic nor a technical difference if we return a nil or empty slice. Both should mean the same thing for the callers. This principle is the same with maps. To check if a map is empty, check its length, not whether it\u2019s nil.
Source code
"},{"location":"#not-making-slice-copies-correctly-24","title":"Not making slice copies correctly (#24)","text":"TL;DRTo copy one slice to another using the copy
built-in function, remember that the number of copied elements corresponds to the minimum between the two slice\u2019s lengths.
Copying elements from one slice to another is a reasonably frequent operation. When using copy, we must recall that the number of elements copied to the destination corresponds to the minimum between the two slices\u2019 lengths. Also bear in mind that other alternatives exist to copy a slice, so we shouldn\u2019t be surprised if we find them in a codebase.
Source code
"},{"location":"#unexpected-side-effects-using-slice-append-25","title":"Unexpected side effects using slice append (#25)","text":"TL;DRUsing copy or the full slice expression is a way to prevent append
from creating conflicts if two different functions use slices backed by the same array. However, only a slice copy prevents memory leaks if you want to shrink a large slice.
When using slicing, we must remember that we can face a situation leading to unintended side effects. If the resulting slice has a length smaller than its capacity, append can mutate the original slice. If we want to restrict the range of possible side effects, we can use either a slice copy or the full slice expression, which prevents us from doing a copy.
Notes[low:high:max]
(full slice expression): This statement creates a slice similar to the one created with s[low:high]
, except that the resulting slice\u2019s capacity is equal to max - low
.
Source code
"},{"location":"#slices-and-memory-leaks-26","title":"Slices and memory leaks (#26)","text":"TL;DRWorking with a slice of pointers or structs with pointer fields, you can avoid memory leaks by marking as nil the elements excluded by a slicing operation.
"},{"location":"#leaking-capacity","title":"Leaking capacity","text":"Remember that slicing a large slice or array can lead to potential high memory consumption. The remaining space won\u2019t be reclaimed by the GC, and we can keep a large backing array despite using only a few elements. Using a slice copy is the solution to prevent such a case.
Source code
"},{"location":"#slice-and-pointers","title":"Slice and pointers","text":"When we use the slicing operation with pointers or structs with pointer fields, we need to know that the GC won\u2019t reclaim these elements. In that case, the two options are to either perform a copy or explicitly mark the remaining elements or their fields to nil
.
Source code
"},{"location":"#inefficient-map-initialization-27","title":"Inefficient map initialization (#27)","text":"TL;DRWhen creating a map, initialize it with a given length if its length is already known. This reduces the number of allocations and improves performance.
A map provides an unordered collection of key-value pairs in which all the keys are distinct. In Go, a map is based on the hash table data structure. Internally, a hash table is an array of buckets, and each bucket is a pointer to an array of key-value pairs.
If we know up front the number of elements a map will contain, we should create it by providing an initial size. Doing this avoids potential map growth, which is quite heavy computation-wise because it requires reallocating enough space and rebalancing all the elements.
Source code
"},{"location":"#maps-and-memory-leaks-28","title":"Maps and memory leaks (#28)","text":"TL;DRA map can always grow in memory, but it never shrinks. Hence, if it leads to some memory issues, you can try different options, such as forcing Go to recreate the map or using pointers.
Read the full section here.
Source code
"},{"location":"#comparing-values-incorrectly-29","title":"Comparing values incorrectly (#29)","text":"TL;DRTo compare types in Go, you can use the == and != operators if two types are comparable: Booleans, numerals, strings, pointers, channels, and structs are composed entirely of comparable types. Otherwise, you can either use reflect.DeepEqual
and pay the price of reflection or use custom implementations and libraries.
It\u2019s essential to understand how to use ==
and !=
to make comparisons effectively. We can use these operators on operands that are comparable:
We can also use the ?
, >=
, <
, and >
operators with numeric types to compare values and with strings to compare their lexical order.
If operands are not comparable (e.g., slices and maps), we have to use other options such as reflection. Reflection is a form of metaprogramming, and it refers to the ability of an application to introspect and modify its structure and behavior. For example, in Go, we can use reflect.DeepEqual
. This function reports whether two elements are deeply equal by recursively traversing two values. The elements it accepts are basic types plus arrays, structs, slices, maps, pointers, interfaces, and functions. Yet, the main catch is the performance penalty.
If performance is crucial at run time, implementing our custom method might be the best solution. One additional note: we must remember that the standard library has some existing comparison methods. For example, we can use the optimized bytes.Compare
function to compare two slices of bytes. Before implementing a custom method, we need to make sure we don\u2019t reinvent the wheel.
Source code
"},{"location":"#control-structures","title":"Control Structures","text":""},{"location":"#ignoring-that-elements-are-copied-in-range-loops-30","title":"Ignoring that elements are copied inrange
loops (#30)","text":"TL;DR The value element in a range
loop is a copy. Therefore, to mutate a struct, for example, access it via its index or via a classic for
loop (unless the element or the field you want to modify is a pointer).
A range loop allows iterating over different data structures:
Compared to a classic for loop
, a range
loop is a convenient way to iterate over all the elements of one of these data structures, thanks to its concise syntax.
Yet, we should remember that the value element in a range loop is a copy. Therefore, if the value is a struct we need to mutate, we will only update the copy, not the element itself, unless the value or field we modify is a pointer. The favored options are to access the element via the index using a range loop or a classic for loop.
Source code
"},{"location":"#ignoring-how-arguments-are-evaluated-in-range-loops-channels-and-arrays-31","title":"Ignoring how arguments are evaluated inrange
loops (channels and arrays) (#31)","text":"TL;DR Understanding that the expression passed to the range
operator is evaluated only once before the beginning of the loop can help you avoid common mistakes such as inefficient assignment in channel or slice iteration.
The range loop evaluates the provided expression only once, before the beginning of the loop, by doing a copy (regardless of the type). We should remember this behavior to avoid common mistakes that might, for example, lead us to access the wrong element. For example:
a := [3]int{0, 1, 2}\nfor i, v := range a {\na[2] = 10\nif i == 2 {\nfmt.Println(v)\n}\n}\n
This code updates the last index to 10. However, if we run this code, it does not print 10; it prints 2.
Source code
"},{"location":"#ignoring-the-impacts-of-using-pointer-elements-in-range-loops-32","title":"Ignoring the impacts of using pointer elements inrange
loops (#32)","text":"TL;DR Using a local variable or accessing an element using an index, you can prevent mistakes while copying pointers inside a loop.
When iterating over a data structure using a range
loop, we must recall that all the values are assigned to a unique variable with a single unique address. Therefore, if we store a pointer referencing this variable during each iteration, we will end up in a situation where we store the same pointer referencing the same element: the latest one. We can overcome this issue by forcing the creation of a local variable in the loop\u2019s scope or creating a pointer referencing a slice element via its index. Both solutions are fine.
Source code
"},{"location":"#making-wrong-assumptions-during-map-iterations-ordering-and-map-insert-during-iteration-33","title":"Making wrong assumptions during map iterations (ordering and map insert during iteration) (#33)","text":"TL;DRTo ensure predictable outputs when using maps, remember that a map data structure:
Source code
"},{"location":"#ignoring-how-the-break-statement-works-34","title":"Ignoring how thebreak
statement works (#34)","text":"TL;DR Using break
or continue
with a label enforces breaking a specific statement. This can be helpful with switch
or select
statements inside loops.
A break statement is commonly used to terminate the execution of a loop. When loops are used in conjunction with switch or select, developers frequently make the mistake of breaking the wrong statement. For example:
for i := 0; i < 5; i++ {\nfmt.Printf(\"%d \", i)\nswitch i {\ndefault:\ncase 2:\nbreak\n}\n}\n
The break statement doesn\u2019t terminate the for
loop: it terminates the switch
statement, instead. Hence, instead of iterating from 0 to 2, this code iterates from 0 to 4: 0 1 2 3 4
.
One essential rule to keep in mind is that a break
statement terminates the execution of the innermost for
, switch
, or select
statement. In the previous example, it terminates the switch
statement.
To break the loop instead of the switch
statement, the most idiomatic way is to use a label:
loop:\nfor i := 0; i < 5; i++ {\nfmt.Printf(\"%d \", i)\nswitch i {\ndefault:\ncase 2:\nbreak loop\n}\n}\n
Here, we associate the loop
label with the for
loop. Then, because we provide the loop
label to the break
statement, it breaks the loop, not the switch. Therefore, this new version will print 0 1 2
, as we expected.
Source code
"},{"location":"#using-defer-inside-a-loop-35","title":"Usingdefer
inside a loop (#35)","text":"TL;DR Extracting loop logic inside a function leads to executing a defer
statement at the end of each iteration.
The defer
statement delays a call\u2019s execution until the surrounding function returns. It\u2019s mainly used to reduce boilerplate code. For example, if a resource has to be closed eventually, we can use defer
to avoid repeating the closure calls before every single return
.
One common mistake with defer
is to forget that it schedules a function call when the surrounding function returns. For example:
func readFiles(ch <-chan string) error {\nfor path := range ch {\nfile, err := os.Open(path)\nif err != nil {\nreturn err\n}\ndefer file.Close()\n// Do something with file\n}\nreturn nil\n}\n
The defer
calls are executed not during each loop iteration but when the readFiles
function returns. If readFiles
doesn\u2019t return, the file descriptors will be kept open forever, causing leaks.
One common option to fix this problem is to create a surrounding function after defer
, called during each iteration:
func readFiles(ch <-chan string) error {\nfor path := range ch {\nif err := readFile(path); err != nil {\nreturn err\n}\n}\nreturn nil\n}\nfunc readFile(path string) error {\nfile, err := os.Open(path)\nif err != nil {\nreturn err\n}\ndefer file.Close()\n// Do something with file\nreturn nil\n}\n
Another solution is to make the readFile
function a closure but intrinsically, this remains the same solution: adding another surrounding function to execute the defer
calls during each iteration.
Source code
"},{"location":"#strings","title":"Strings","text":""},{"location":"#not-understanding-the-concept-of-rune-36","title":"Not understanding the concept of rune (#36)","text":"TL;DRUnderstanding that a rune corresponds to the concept of a Unicode code point and that it can be composed of multiple bytes should be part of the Go developer\u2019s core knowledge to work accurately with strings.
As runes are everywhere in Go, it's important to understand the following:
rune
corresponds to the concept of a Unicode code point, meaning an item represented by a single value.len()
on a string in Go returns the number of bytes, not the number of runes.Source code
"},{"location":"#inaccurate-string-iteration-37","title":"Inaccurate string iteration (#37)","text":"TL;DRIterating on a string with the range
operator iterates on the runes with the index corresponding to the starting index of the rune\u2019s byte sequence. To access a specific rune index (such as the third rune), convert the string into a []rune
.
Iterating on a string is a common operation for developers. Perhaps we want to perform an operation for each rune in the string or implement a custom function to search for a specific substring. In both cases, we have to iterate on the different runes of a string. But it\u2019s easy to get confused about how iteration works.
For example, consider the following example:
s := \"h\u00eallo\"\nfor i := range s {\nfmt.Printf(\"position %d: %c\\n\", i, s[i])\n}\nfmt.Printf(\"len=%d\\n\", len(s))\n
position 0: h\nposition 1: \u00c3\nposition 3: l\nposition 4: l\nposition 5: o\nlen=6\n
Let's highlight three points that might be confusing:
Let\u2019s start with the last observation. We already mentioned that len returns the number of bytes in a string, not the number of runes. Because we assigned a string literal to s
, s
is a UTF-8 string. Meanwhile, the special character \"\u00ea\" isn\u2019t encoded in a single byte; it requires 2 bytes. Therefore, calling len(s)
returns 6.
Meanwhile, in the previous example, we have to understand that we don't iterate over each rune; instead, we iterate over each starting index of a rune:
Printing s[i]
doesn\u2019t print the ith rune; it prints the UTF-8 representation of the byte at index i
. Hence, we printed \"h\u00c3llo\" instead of \"h\u00eallo\".
If we want to print all the different runes, we can either use the value element of the range
operator:
s := \"h\u00eallo\"\nfor i, r := range s {\nfmt.Printf(\"position %d: %c\\n\", i, r)\n}\n
Or, we can convert the string into a slice of runes and iterate over it:
s := \"h\u00eallo\"\nrunes := []rune(s)\nfor i, r := range runes {\nfmt.Printf(\"position %d: %c\\n\", i, r)\n}\n
Note that this solution introduces a run-time overhead compared to the previous one. Indeed, converting a string into a slice of runes requires allocating an additional slice and converting the bytes into runes: an O(n) time complexity with n the number of bytes in the string. Therefore, if we want to iterate over all the runes, we should use the first solution.
However, if we want to access the ith rune of a string with the first option, we don\u2019t have access to the rune index; rather, we know the starting index of a rune in the byte sequence.
s := \"h\u00eallo\"\nr := []rune(s)[4]\nfmt.Printf(\"%c\\n\", r) // o\n
Source code
"},{"location":"#misusing-trim-functions-38","title":"Misusing trim functions (#38)","text":"TL;DRstrings.TrimRight
/strings.TrimLeft
removes all the trailing/leading runes contained in a given set, whereas strings.TrimSuffix
/strings.TrimPrefix
returns a string without a provided suffix/prefix.
For example:
fmt.Println(strings.TrimRight(\"123oxo\", \"xo\"))\n
The example prints 123:
Conversely, strings.TrimLeft
removes all the leading runes contained in a set.
On the other side, strings.TrimSuffix
/ strings.TrimPrefix
returns a string without the provided trailing suffix / prefix.
Source code
"},{"location":"#under-optimized-strings-concatenation-39","title":"Under-optimized strings concatenation (#39)","text":"TL;DRConcatenating a list of strings should be done with strings.Builder
to prevent allocating a new string during each iteration.
Let\u2019s consider a concat
function that concatenates all the string elements of a slice using the +=
operator:
func concat(values []string) string {\ns := \"\"\nfor _, value := range values {\ns += value\n}\nreturn s\n}\n
During each iteration, the +=
operator concatenates s
with the value string. At first sight, this function may not look wrong. But with this implementation, we forget one of the core characteristics of a string: its immutability. Therefore, each iteration doesn\u2019t update s
; it reallocates a new string in memory, which significantly impacts the performance of this function.
Fortunately, there is a solution to deal with this problem, using strings.Builder
:
func concat(values []string) string {\nsb := strings.Builder{}\nfor _, value := range values {\n_, _ = sb.WriteString(value)\n}\nreturn sb.String()\n}\n
During each iteration, we constructed the resulting string by calling the WriteString
method that appends the content of value to its internal buffer, hence minimizing memory copying.
WriteString
returns an error as the second output, but we purposely ignore it. Indeed, this method will never return a non-nil error. So what\u2019s the purpose of this method returning an error as part of its signature? strings.Builder
implements the io.StringWriter
interface, which contains a single method: WriteString(s string) (n int, err error)
. Hence, to comply with this interface, WriteString
must return an error.
Internally, strings.Builder
holds a byte slice. Each call to WriteString
results in a call to append on this slice. There are two impacts. First, this struct shouldn\u2019t be used concurrently, as the calls to append
would lead to race conditions. The second impact is something that we saw in mistake #21, \"Inefficient slice initialization\": if the future length of a slice is already known, we should preallocate it. For that purpose, strings.Builder
exposes a method Grow(n int)
to guarantee space for another n
bytes:
func concat(values []string) string {\ntotal := 0\nfor i := 0; i < len(values); i++ {\ntotal += len(values[i])\n}\nsb := strings.Builder{}\nsb.Grow(total) (2)\nfor _, value := range values {\n_, _ = sb.WriteString(value)\n}\nreturn sb.String()\n}\n
Let\u2019s run a benchmark to compare the three versions (v1 using +=
; v2 using strings.Builder{}
without preallocation; and v3 using strings.Builder{}
with preallocation). The input slice contains 1,000 strings, and each string contains 1,000 bytes:
BenchmarkConcatV1-4 16 72291485 ns/op\nBenchmarkConcatV2-4 1188 878962 ns/op\nBenchmarkConcatV3-4 5922 190340 ns/op\n
As we can see, the latest version is by far the most efficient: 99% faster than v1 and 78% faster than v2.
strings.Builder
is the recommended solution to concatenate a list of strings. Usually, this solution should be used within a loop. Indeed, if we just have to concatenate a few strings (such as a name and a surname), using strings.Builder
is not recommended as doing so will make the code a bit less readable than using the +=
operator or fmt.Sprintf
.
Source code
"},{"location":"#useless-string-conversions-40","title":"Useless string conversions (#40)","text":"TL;DRRemembering that the bytes
package offers the same operations as the strings
package can help avoid extra byte/string conversions.
When choosing to work with a string or a []byte
, most programmers tend to favor strings for convenience. But most I/O is actually done with []byte
. For example, io.Reader
, io.Writer
, and io.ReadAll
work with []byte
, not strings.
When we\u2019re wondering whether we should work with strings or []byte
, let\u2019s recall that working with []byte
isn\u2019t necessarily less convenient. Indeed, all the exported functions of the strings package also have alternatives in the bytes
package: Split
, Count
, Contains
, Index
, and so on. Hence, whether we\u2019re doing I/O or not, we should first check whether we could implement a whole workflow using bytes instead of strings and avoid the price of additional conversions.
Source code
"},{"location":"#substring-and-memory-leaks-41","title":"Substring and memory leaks (#41)","text":"TL;DRUsing copies instead of substrings can prevent memory leaks, as the string returned by a substring operation will be backed by the same byte array.
In mistake #26, \u201cSlices and memory leaks,\u201d we saw how slicing a slice or array may lead to memory leak situations. This principle also applies to string and substring operations.
We need to keep two things in mind while using the substring operation in Go. First, the interval provided is based on the number of bytes, not the number of runes. Second, a substring operation may lead to a memory leak as the resulting substring will share the same backing array as the initial string. The solutions to prevent this case from happening are to perform a string copy manually or to use strings.Clone
from Go 1.18.
Source code
"},{"location":"#functions-and-methods","title":"Functions and Methods","text":""},{"location":"#not-knowing-which-type-of-receiver-to-use-42","title":"Not knowing which type of receiver to use (#42)","text":"TL;DRThe decision whether to use a value or a pointer receiver should be made based on factors such as the type, whether it has to be mutated, whether it contains a field that can\u2019t be copied, and how large the object is. When in doubt, use a pointer receiver.
Choosing between value and pointer receivers isn\u2019t always straightforward. Let\u2019s discuss some of the conditions to help us choose.
A receiver must be a pointer
type slice []int\nfunc (s *slice) add(element int) {\n*s = append(*s, element)\n}\n
A receiver should be a pointer
A receiver must be a value
A receiver should be a value
time.Time
.int
, float64
, or string
.Of course, it\u2019s impossible to be exhaustive, as there will always be edge cases, but this section\u2019s goal was to provide guidance to cover most cases. By default, we can choose to go with a value receiver unless there\u2019s a good reason not to do so. In doubt, we should use a pointer receiver.
Source code
"},{"location":"#never-using-named-result-parameters-43","title":"Never using named result parameters (#43)","text":"TL;DRUsing named result parameters can be an efficient way to improve the readability of a function/method, especially if multiple result parameters have the same type. In some cases, this approach can also be convenient because named result parameters are initialized to their zero value. But be cautious about potential side effects.
When we return parameters in a function or a method, we can attach names to these parameters and use them as regular variables. When a result parameter is named, it\u2019s initialized to its zero value when the function/method begins. With named result parameters, we can also call a naked return statement (without arguments). In that case, the current values of the result parameters are used as the returned values.
Here\u2019s an example that uses a named result parameter b
:
func f(a int) (b int) {\nb = a\nreturn\n}\n
In this example, we attach a name to the result parameter: b
. When we call return without arguments, it returns the current value of b
.
In some cases, named result parameters can also increase readability: for example, if two parameters have the same type. In other cases, they can also be used for convenience. Therefore, we should use named result parameters sparingly when there\u2019s a clear benefit.
Source code
"},{"location":"#unintended-side-effects-with-named-result-parameters-44","title":"Unintended side effects with named result parameters (#44)","text":"TL;DRSee #43.
We mentioned why named result parameters can be useful in some situations. But as these result parameters are initialized to their zero value, using them can sometimes lead to subtle bugs if we\u2019re not careful enough. For example, can you spot what\u2019s wrong with this code?
func (l loc) getCoordinates(ctx context.Context, address string) (\nlat, lng float32, err error) {\nisValid := l.validateAddress(address) (1)\nif !isValid {\nreturn 0, 0, errors.New(\"invalid address\")\n}\nif ctx.Err() != nil { (2)\nreturn 0, 0, err\n}\n// Get and return coordinates\n}\n
The error might not be obvious at first glance. Here, the error returned in the if ctx.Err() != nil
scope is err
. But we haven\u2019t assigned any value to the err
variable. It\u2019s still assigned to the zero value of an error
type: nil
. Hence, this code will always return a nil error.
When using named result parameters, we must recall that each parameter is initialized to its zero value. As we have seen in this section, this can lead to subtle bugs that aren\u2019t always straightforward to spot while reading code. Therefore, let\u2019s remain cautious when using named result parameters, to avoid potential side effects.
Source code
"},{"location":"#returning-a-nil-receiver-45","title":"Returning a nil receiver (#45)","text":"TL;DRWhen returning an interface, be cautious about not returning a nil pointer but an explicit nil value. Otherwise, unintended consequences may occur and the caller will receive a non-nil value.
Source code
"},{"location":"#using-a-filename-as-a-function-input-46","title":"Using a filename as a function input (#46)","text":"TL;DRDesigning functions to receive io.Reader
types instead of filenames improves the reusability of a function and makes testing easier.
Accepting a filename as a function input to read from a file should, in most cases, be considered a code smell (except in specific functions such as os.Open
). Indeed, it makes unit tests more complex because we may have to create multiple files. It also reduces the reusability of a function (although not all functions are meant to be reused). Using the io.Reader
interface abstracts the data source. Regardless of whether the input is a file, a string, an HTTP request, or a gRPC request, the implementation can be reused and easily tested.
Source code
"},{"location":"#ignoring-how-defer-arguments-and-receivers-are-evaluated-argument-evaluation-pointer-and-value-receivers-47","title":"Ignoring howdefer
arguments and receivers are evaluated (argument evaluation, pointer, and value receivers) (#47)","text":"TL;DR Passing a pointer to a defer
function and wrapping a call inside a closure are two possible solutions to overcome the immediate evaluation of arguments and receivers.
In a defer
function the arguments are evaluated right away, not once the surrounding function returns. For example, in this code, we always call notify
and incrementCounter
with the same status: an empty string.
const (\nStatusSuccess = \"success\"\nStatusErrorFoo = \"error_foo\"\nStatusErrorBar = \"error_bar\"\n)\nfunc f() error {\nvar status string\ndefer notify(status)\ndefer incrementCounter(status)\nif err := foo(); err != nil {\nstatus = StatusErrorFoo\nreturn err\n}\nif err := bar(); err != nil {\nstatus = StatusErrorBar\nreturn err\n}\nstatus = StatusSuccess <5>\nreturn nil\n}\n
Indeed, we call notify(status)
and incrementCounter(status)
as defer
functions. Therefore, Go will delay these calls to be executed once f
returns with the current value of status at the stage we used defer, hence passing an empty string.
Two leading options if we want to keep using defer
.
The first solution is to pass a string pointer:
func f() error {\nvar status string\ndefer notify(&status) defer incrementCounter(&status)\n// The rest of the function unchanged\n}\n
Using defer
evaluates the arguments right away: here, the address of status. Yes, status itself is modified throughout the function, but its address remains constant, regardless of the assignments. Hence, if notify
or incrementCounter
uses the value referenced by the string pointer, it will work as expected. But this solution requires changing the signature of the two functions, which may not always be possible.
There\u2019s another solution: calling a closure (an anonymous function value that references variables from outside its body) as a defer
statement:
func f() error {\nvar status string\ndefer func() {\nnotify(status)\nincrementCounter(status)\n}()\n// The rest of the function unchanged\n}\n
Here, we wrap the calls to both notify
and incrementCounter
within a closure. This closure references the status variable from outside its body. Therefore, status
is evaluated once the closure is executed, not when we call defer
. This solution also works and doesn\u2019t require notify
and incrementCounter
to change their signature.
Let's also note this behavior applies with method receiver: the receiver is evaluated immediately.
Source code
"},{"location":"#error-management","title":"Error Management","text":""},{"location":"#panicking-48","title":"Panicking (#48)","text":"TL;DRUsing panic
is an option to deal with errors in Go. However, it should only be used sparingly in unrecoverable conditions: for example, to signal a programmer error or when you fail to load a mandatory dependency.
In Go, panic is a built-in function that stops the ordinary flow:
func main() {\nfmt.Println(\"a\")\npanic(\"foo\")\nfmt.Println(\"b\")\n}\n
This code prints a and then stops before printing b:
a\npanic: foo\n\ngoroutine 1 [running]:\nmain.main()\n main.go:7 +0xb3\n
Panicking in Go should be used sparingly. There are two prominent cases, one to signal a programmer error (e.g., sql.Register
that panics if the driver is nil
or has already been register) and another where our application fails to create a mandatory dependency. Hence, exceptional conditions that lead us to stop the application. In most other cases, error management should be done with a function that returns a proper error type as the last return argument.
Source code
"},{"location":"#ignoring-when-to-wrap-an-error-49","title":"Ignoring when to wrap an error (#49)","text":"TL;DRWrapping an error allows you to mark an error and/or provide additional context. However, error wrapping creates potential coupling as it makes the source error available for the caller. If you want to prevent that, don\u2019t use error wrapping.
Since Go 1.13, the %w directive allows us to wrap errors conveniently. Error wrapping is about wrapping or packing an error inside a wrapper container that also makes the source error available. In general, the two main use cases for error wrapping are the following:
When handling an error, we can decide to wrap it. Wrapping is about adding additional context to an error and/or marking an error as a specific type. If we need to mark an error, we should create a custom error type. However, if we just want to add extra context, we should use fmt.Errorf with the %w directive as it doesn\u2019t require creating a new error type. Yet, error wrapping creates potential coupling as it makes the source error available for the caller. If we want to prevent it, we shouldn\u2019t use error wrapping but error transformation, for example, using fmt.Errorf with the %v directive.
Source code
"},{"location":"#comparing-an-error-type-inaccurately-50","title":"Comparing an error type inaccurately (#50)","text":"TL;DRIf you use Go 1.13 error wrapping with the %w
directive and fmt.Errorf
, comparing an error against a type has to be done using errors.As
. Otherwise, if the returned error you want to check is wrapped, it will fail the checks.
Source code
"},{"location":"#comparing-an-error-value-inaccurately-51","title":"Comparing an error value inaccurately (#51)","text":"TL;DRIf you use Go 1.13 error wrapping with the %w
directive and fmt.Errorf
, comparing an error against or a value has to be done using errors.As
. Otherwise, if the returned error you want to check is wrapped, it will fail the checks.
A sentinel error is an error defined as a global variable:
import \"errors\"\nvar ErrFoo = errors.New(\"foo\")\n
In general, the convention is to start with Err
followed by the error type: here, ErrFoo
. A sentinel error conveys an expected error, an error that clients will expect to check. As general guidelines:
var ErrFoo = errors.New(\"foo\")
.type BarError struct { ... }
, with BarError
implementing the error
interface.If we use error wrapping in our application with the %w
directive and fmt.Errorf
, checking an error against a specific value should be done using errors.Is
instead of ==
. Thus, even if the sentinel error is wrapped, errors.Is
can recursively unwrap it and compare each error in the chain against the provided value.
Source code
"},{"location":"#handling-an-error-twice-52","title":"Handling an error twice (#52)","text":"TL;DRIn most situations, an error should be handled only once. Logging an error is handling an error. Therefore, you have to choose between logging or returning an error. In many cases, error wrapping is the solution as it allows you to provide additional context to an error and return the source error.
Handling an error multiple times is a mistake made frequently by developers, not specifically in Go. This can cause situations where the same error is logged multiple times make debugging harder.
Let's remind us that handling an error should be done only once. Logging an error is handling an error. Hence, we should either log or return an error. By doing this, we simplify our code and gain better insights into the error situation. Using error wrapping is the most convenient approach as it allows us to propagate the source error and add context to an error.
Source code
"},{"location":"#not-handling-an-error-53","title":"Not handling an error (#53)","text":"TL;DRIgnoring an error, whether during a function call or in a defer
function, should be done explicitly using the blank identifier. Otherwise, future readers may be confused about whether it was intentional or a miss.
Source code
"},{"location":"#not-handling-defer-errors-54","title":"Not handlingdefer
errors (#54)","text":"TL;DR In many cases, you shouldn\u2019t ignore an error returned by a defer
function. Either handle it directly or propagate it to the caller, depending on the context. If you want to ignore it, use the blank identifier.
Consider the following code:
func f() {\n// ...\nnotify() // Error handling is omitted\n}\nfunc notify() error {\n// ...\n}\n
From a maintainability perspective, the code can lead to some issues. Let\u2019s consider a new reader looking at it. This reader notices that notify returns an error but that the error isn\u2019t handled by the parent function. How can they guess whether or not handling the error was intentional? How can they know whether the previous developer forgot to handle it or did it purposely?
For these reasons, when we want to ignore an error, there's only one way to do it, using the blank identifier (_
):
_ = notify\n
In terms of compilation and run time, this approach doesn\u2019t change anything compared to the first piece of code. But this new version makes explicit that we aren\u2019t interested in the error. Also, we can add a comment that indicates the rationale for why an error is ignored:
// At-most once delivery.\n// Hence, it's accepted to miss some of them in case of errors.\n_ = notify()\n
Source code
"},{"location":"#concurrency-foundations","title":"Concurrency: Foundations","text":""},{"location":"#mixing-up-concurrency-and-parallelism-55","title":"Mixing up concurrency and parallelism (#55)","text":"TL;DRUnderstanding the fundamental differences between concurrency and parallelism is a cornerstone of the Go developer\u2019s knowledge. Concurrency is about structure, whereas parallelism is about execution.
Concurrency and parallelism are not the same:
In summary, concurrency provides a structure to solve a problem with parts that may be parallelized. Therefore, concurrency enables parallelism.
"},{"location":"#thinking-concurrency-is-always-faster-56","title":"Thinking concurrency is always faster (#56)","text":"TL;DRTo be a proficient developer, you must acknowledge that concurrency isn\u2019t always faster. Solutions involving parallelization of minimal workloads may not necessarily be faster than a sequential implementation. Benchmarking sequential versus concurrent solutions should be the way to validate assumptions.
Read the full section here.
Source code
"},{"location":"#being-puzzled-about-when-to-use-channels-or-mutexes-57","title":"Being puzzled about when to use channels or mutexes (#57)","text":"TL;DRBeing aware of goroutine interactions can also be helpful when deciding between channels and mutexes. In general, parallel goroutines require synchronization and hence mutexes. Conversely, concurrent goroutines generally require coordination and orchestration and hence channels.
Given a concurrency problem, it may not always be clear whether we can implement a solution using channels or mutexes. Because Go promotes sharing memory by communication, one mistake could be to always force the use of channels, regardless of the use case. However, we should see the two options as complementary.
When should we use channels or mutexes? We will use the example in the next figure as a backbone. Our example has three different goroutines with specific relationships:
In general, parallel goroutines have to synchronize: for example, when they need to access or mutate a shared resource such as a slice. Synchronization is enforced with mutexes but not with any channel types (not with buffered channels). Hence, in general, synchronization between parallel goroutines should be achieved via mutexes.
Conversely, in general, concurrent goroutines have to coordinate and orchestrate. For example, if G3 needs to aggregate results from both G1 and G2, G1 and G2 need to signal to G3 that a new intermediate result is available. This coordination falls under the scope of communication\u2014therefore, channels.
Regarding concurrent goroutines, there\u2019s also the case where we want to transfer the ownership of a resource from one step (G1 and G2) to another (G3); for example, if G1 and G2 are enriching a shared resource and at some point, we consider this job as complete. Here, we should use channels to signal that a specific resource is ready and handle the ownership transfer.
Mutexes and channels have different semantics. Whenever we want to share a state or access a shared resource, mutexes ensure exclusive access to this resource. Conversely, channels are a mechanic for signaling with or without data (chan struct{}
or not). Coordination or ownership transfer should be achieved via channels. It\u2019s important to know whether goroutines are parallel or concurrent because, in general, we need mutexes for parallel goroutines and channels for concurrent ones.
Being proficient in concurrency also means understanding that data races and race conditions are different concepts. Data races occur when multiple goroutines simultaneously access the same memory location and at least one of them is writing. Meanwhile, being data-race-free doesn\u2019t necessarily mean deterministic execution. When a behavior depends on the sequence or the timing of events that can\u2019t be controlled, this is a race condition.
Race problems can be among the hardest and most insidious bugs a programmer can face. As Go developers, we must understand crucial aspects such as data races and race conditions, their possible impacts, and how to avoid them.
"},{"location":"#data-race","title":"Data Race","text":"A data race occurs when two or more goroutines simultaneously access the same memory location and at least one is writing. In this case, the result can be hazardous. Even worse, in some situations, the memory location may end up holding a value containing a meaningless combination of bits.
We can prevent a data race from happening using different techniques. For example:
sync/atomic
packageDepending on the operation we want to perform, does a data-race-free application necessarily mean a deterministic result? Not necessarily.
A race condition occurs when the behavior depends on the sequence or the timing of events that can\u2019t be controlled. Here, the timing of events is the goroutines\u2019 execution order.
In summary, when we work in concurrent applications, it\u2019s essential to understand that a data race is different from a race condition. A data race occurs when multiple goroutines simultaneously access the same memory location and at least one of them is writing. A data race means unexpected behavior. However, a data-race-free application doesn\u2019t necessarily mean deterministic results. An application can be free of data races but still have behavior that depends on uncontrolled events (such as goroutine execution, how fast a message is published to a channel, or how long a call to a database lasts); this is a race condition. Understanding both concepts is crucial to becoming proficient in designing concurrent applications.
Source code
"},{"location":"#not-understanding-the-concurrency-impacts-of-a-workload-type-59","title":"Not understanding the concurrency impacts of a workload type (#59)","text":"TL;DRWhen creating a certain number of goroutines, consider the workload type. Creating CPU-bound goroutines means bounding this number close to the GOMAXPROCS
variable (based by default on the number of CPU cores on the host). Creating I/O-bound goroutines depends on other factors, such as the external system.
In programming, the execution time of a workload is limited by one of the following:
The last is the rarest nowadays, given that memory has become very cheap in recent decades. Hence, this section focuses on the two first workload types: CPU- and I/O-bound.
If the workload executed by the workers is I/O-bound, the value mainly depends on the external system. Conversely, if the workload is CPU-bound, the optimal number of goroutines is close to the number of available CPU cores (a best practice can be to use runtime.GOMAXPROCS
). Knowing the workload type (I/O or CPU) is crucial when designing concurrent applications.
Source code
"},{"location":"#misunderstanding-go-contexts-60","title":"Misunderstanding Go contexts (#60)","text":"TL;DRGo contexts are also one of the cornerstones of concurrency in Go. A context allows you to carry a deadline, a cancellation signal, and/or a list of keys-values.
https://pkg.go.dev/context
A Context carries a deadline, a cancellation signal, and other values across API boundaries.
"},{"location":"#deadline","title":"Deadline","text":"A deadline refers to a specific point in time determined with one of the following:
time.Duration
from now (for example, in 250 ms)time.Time
(for example, 2023-02-07 00:00:00 UTC)The semantics of a deadline convey that an ongoing activity should be stopped if this deadline is met. An activity is, for example, an I/O request or a goroutine waiting to receive a message from a channel.
"},{"location":"#cancellation-signals","title":"Cancellation signals","text":"Another use case for Go contexts is to carry a cancellation signal. Let\u2019s imagine that we want to create an application that calls CreateFileWatcher(ctx context.Context, filename string)
within another goroutine. This function creates a specific file watcher that keeps reading from a file and catches updates. When the provided context expires or is canceled, this function handles it to close the file descriptor.
The last use case for Go contexts is to carry a key-value list. What\u2019s the point of having a context carrying a key-value list? Because Go contexts are generic and mainstream, there are infinite use cases.
For example, if we use tracing, we may want different subfunctions to share the same correlation ID. Some developers may consider this ID too invasive to be part of the function signature. In this regard, we could also decide to include it as part of the provided context.
"},{"location":"#catching-a-context-cancellation","title":"Catching a context cancellation","text":"The context.Context
type exports a Done
method that returns a receive-only notification channel: <-chan struct{}
. This channel is closed when the work associated with the context should be canceled. For example,
context.WithCancel
is closed when the cancel function is called.context.WithDeadline
is closed when the deadline has expired.One thing to note is that the internal channel should be closed when a context is canceled or has met a deadline, instead of when it receives a specific value, because the closure of a channel is the only channel action that all the consumer goroutines will receive. This way, all the consumers will be notified once a context is canceled or a deadline is reached.
In summary, to be a proficient Go developer, we have to understand what a context is and how to use it. In general, a function that users wait for should take a context, as doing so allows upstream callers to decide when calling this function should be aborted.
Source code
"},{"location":"#concurrency-practice","title":"Concurrency: Practice","text":""},{"location":"#propagating-an-inappropriate-context-61","title":"Propagating an inappropriate context (#61)","text":"TL;DRUnderstanding the conditions when a context can be canceled should matter when propagating it: for example, an HTTP handler canceling the context when the response has been sent.
In many situations, it is recommended to propagate Go contexts. However, context propagation can sometimes lead to subtle bugs, preventing subfunctions from being correctly executed.
Let\u2019s consider the following example. We expose an HTTP handler that performs some tasks and returns a response. But just before returning the response, we also want to send it to a Kafka topic. We don\u2019t want to penalize the HTTP consumer latency-wise, so we want the publish action to be handled asynchronously within a new goroutine. We assume that we have at our disposal a publish
function that accepts a context so the action of publishing a message can be interrupted if the context is canceled, for example. Here is a possible implementation:
func handler(w http.ResponseWriter, r *http.Request) {\nresponse, err := doSomeTask(r.Context(), r)\nif err != nil {\nhttp.Error(w, err.Error(), http.StatusInternalServerError)\nreturn\n}\ngo func() {\nerr := publish(r.Context(), response)\n// Do something with err\n}()\nwriteResponse(response)\n}\n
What\u2019s wrong with this piece of code? We have to know that the context attached to an HTTP request can cancel in different conditions:
In the first two cases, we probably handle things correctly. For example, if we get a response from doSomeTask but the client has closed the connection, it\u2019s probably OK to call publish with a context already canceled so the message isn\u2019t published. But what about the last case?
When the response has been written to the client, the context associated with the request will be canceled. Therefore, we are facing a race condition:
In the latter case, calling publish will return an error because we returned the HTTP response quickly.
NoteFrom Go 1.21, there is a way to create a new context without cancel. context.WithoutCancel
returns a copy of parent that is not canceled when parent is canceled.
In summary, propagating a context should be done cautiously.
Source code
"},{"location":"#starting-a-goroutine-without-knowing-when-to-stop-it-62","title":"Starting a goroutine without knowing when to stop it (#62)","text":"TL;DRAvoiding leaks means being mindful that whenever a goroutine is started, you should have a plan to stop it eventually.
Goroutines are easy and cheap to start\u2014so easy and cheap that we may not necessarily have a plan for when to stop a new goroutine, which can lead to leaks. Not knowing when to stop a goroutine is a design issue and a common concurrency mistake in Go.
Let\u2019s discuss a concrete example. We will design an application that needs to watch some external configuration (for example, using a database connection). Here\u2019s a first implementation:
func main() {\nnewWatcher()\n// Run the application\n}\ntype watcher struct { /* Some resources */ }\nfunc newWatcher() {\nw := watcher{}\ngo w.watch() // Creates a goroutine that watches some external configuration\n}\n
The problem with this code is that when the main goroutine exits (perhaps because of an OS signal or because it has a finite workload), the application is stopped. Hence, the resources created by watcher aren\u2019t closed gracefully. How can we prevent this from happening?
One option could be to pass to newWatcher a context that will be canceled when main returns:
func main() {\nctx, cancel := context.WithCancel(context.Background())\ndefer cancel()\nnewWatcher(ctx)\n// Run the application\n}\nfunc newWatcher(ctx context.Context) {\nw := watcher{}\ngo w.watch(ctx)\n}\n
We propagate the context created to the watch method. When the context is canceled, the watcher struct should close its resources. However, can we guarantee that watch will have time to do so? Absolutely not\u2014and that\u2019s a design flaw.
The problem is that we used signaling to convey that a goroutine had to be stopped. We didn\u2019t block the parent goroutine until the resources had been closed. Let\u2019s make sure we do:
func main() {\nw := newWatcher()\ndefer w.close()\n// Run the application\n}\nfunc newWatcher() watcher {\nw := watcher{}\ngo w.watch()\nreturn w\n}\nfunc (w watcher) close() {\n// Close the resources\n}\n
Instead of signaling watcher
that it\u2019s time to close its resources, we now call this close
method, using defer
to guarantee that the resources are closed before the application exits.
In summary, let\u2019s be mindful that a goroutine is a resource like any other that must eventually be closed to free memory or other resources. Starting a goroutine without knowing when to stop it is a design issue. Whenever a goroutine is started, we should have a clear plan about when it will stop. Last but not least, if a goroutine creates resources and its lifetime is bound to the lifetime of the application, it\u2019s probably safer to wait for this goroutine to complete before exiting the application. This way, we can ensure that the resources can be freed.
Source code
"},{"location":"#not-being-careful-with-goroutines-and-loop-variables-63","title":"Not being careful with goroutines and loop variables (#63)","text":"WarningThis mistake isn't relevant anymore from Go 1.22 (source).
"},{"location":"#expecting-a-deterministic-behavior-using-select-and-channels-64","title":"Expecting a deterministic behavior using select and channels (#64)","text":"TL;DRUnderstanding that select
with multiple channels chooses the case randomly if multiple options are possible prevents making wrong assumptions that can lead to subtle concurrency bugs.
One common mistake made by Go developers while working with channels is to make wrong assumptions about how select behaves with multiple channels.
For example, let's consider the following case (disconnectCh
is a unbuffered channel):
go func() {\nfor i := 0; i < 10; i++ {\nmessageCh <- i\n}\ndisconnectCh <- struct{}{}\n}()\nfor {\nselect {\ncase v := <-messageCh:\nfmt.Println(v)\ncase <-disconnectCh:\nfmt.Println(\"disconnection, return\")\nreturn\n}\n}\n
If we run this example multiple times, the result will be random:
0\n1\n2\ndisconnection, return\n\n0\ndisconnection, return\n
Instead of consuming the 10 messages, we only received a few of them. What\u2019s the reason? It lies in the specification of the select statement with multiple channels (https:// go.dev/ref/spec):
Quote
If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection.
Unlike a switch statement, where the first case with a match wins, the select state- ment selects randomly if multiple options are possible.
This behavior might look odd at first, but there\u2019s a good reason for it: to prevent possible starvation. Suppose the first possible communication chosen is based on the source order. In that case, we may fall into a situation where, for example, we only receive from one channel because of a fast sender. To prevent this, the language designers decided to use a random selection.
When using select
with multiple channels, we must remember that if multiple options are possible, the first case in the source order does not automatically win. Instead, Go selects randomly, so there\u2019s no guarantee about which option will be chosen. To overcome this behavior, in the case of a single producer goroutine, we can use either unbuffered channels or a single channel.
Source code
"},{"location":"#not-using-notification-channels-65","title":"Not using notification channels (#65)","text":"TL;DRSend notifications using a chan struct{}
type.
Channels are a mechanism for communicating across goroutines via signaling. A signal can be either with or without data.
Let\u2019s look at a concrete example. We will create a channel that will notify us whenever a certain disconnection occurs. One idea is to handle it as a chan bool
:
disconnectCh := make(chan bool)\n
Now, let\u2019s say we interact with an API that provides us with such a channel. Because it\u2019s a channel of Booleans, we can receive either true
or false
messages. It\u2019s probably clear what true
conveys. But what does false
mean? Does it mean we haven\u2019t been disconnected? And in this case, how frequently will we receive such a signal? Does it mean we have reconnected? Should we even expect to receive false
? Perhaps we should only expect to receive true
messages.
If that\u2019s the case, meaning we don\u2019t need a specific value to convey some information, we need a channel without data. The idiomatic way to handle it is a channel of empty structs: chan struct{}
.
Using nil channels should be part of your concurrency toolset because it allows you to remove cases from select
statements, for example.
What should this code do?
var ch chan int\n<-ch\n
ch
is a chan int
type. The zero value of a channel being nil, ch
is nil
. The goroutine won\u2019t panic; however, it will block forever.
The principle is the same if we send a message to a nil channel. This goroutine blocks forever:
var ch chan int\nch <- 0\n
Then what\u2019s the purpose of Go allowing messages to be received from or sent to a nil channel? For example, we can use nil channels to implement an idiomatic way to merge two channels:
func merge(ch1, ch2 <-chan int) <-chan int {\nch := make(chan int, 1)\ngo func() {\nfor ch1 != nil || ch2 != nil { // Continue if at least one channel isn\u2019t nil\nselect {\ncase v, open := <-ch1:\nif !open {\nch1 = nil // Assign ch1 to a nil channel once closed\nbreak\n}\nch <- v\ncase v, open := <-ch2:\nif !open {\nch2 = nil // Assigns ch2 to a nil channel once closed\nbreak\n}\nch <- v\n}\n}\nclose(ch)\n}()\nreturn ch\n}\n
This elegant solution relies on nil channels to somehow remove one case from the select
statement.
Let\u2019s keep this idea in mind: nil channels are useful in some conditions and should be part of the Go developer\u2019s toolset when dealing with concurrent code.
Source code
"},{"location":"#being-puzzled-about-channel-size-67","title":"Being puzzled about channel size (#67)","text":"TL;DRCarefully decide on the right channel type to use, given a problem. Only unbuffered channels provide strong synchronization guarantees.
You should have a good reason to specify a channel size other than one for buffered channels.
"},{"location":"#forgetting-about-possible-side-effects-with-string-formatting-etcd-data-race-example-and-deadlock-68","title":"Forgetting about possible side effects with string formatting (etcd data race example and deadlock) (#68)","text":"TL;DRBeing aware that string formatting may lead to calling existing functions means watching out for possible deadlocks and other data races.
Source code
"},{"location":"#creating-data-races-with-append-69","title":"Creating data races with append (#69)","text":"TL;DRCalling append
isn\u2019t always data-race-free; hence, it shouldn\u2019t be used concurrently on a shared slice.
Source code
"},{"location":"#using-mutexes-inaccurately-with-slices-and-maps-70","title":"Using mutexes inaccurately with slices and maps (#70)","text":"TL;DRRemembering that slices and maps are pointers can prevent common data races.
Source code
"},{"location":"#misusing-syncwaitgroup-71","title":"Misusingsync.WaitGroup
(#71)","text":"TL;DR To accurately use sync.WaitGroup
, call the Add
method before spinning up goroutines.
Source code
"},{"location":"#forgetting-about-synccond-72","title":"Forgetting aboutsync.Cond
(#72)","text":"TL;DR You can send repeated notifications to multiple goroutines with sync.Cond
.
Source code
"},{"location":"#not-using-errgroup-73","title":"Not usingerrgroup
(#73)","text":"TL;DR You can synchronize a group of goroutines and handle errors and contexts with the errgroup
package.
Source code
"},{"location":"#copying-a-sync-type-74","title":"Copying async
type (#74)","text":"TL;DR sync
types shouldn\u2019t be copied.
Source code
"},{"location":"#standard-library","title":"Standard Library","text":""},{"location":"#providing-a-wrong-time-duration-75","title":"Providing a wrong time duration (#75)","text":"TL;DRRemain cautious with functions accepting a time.Duration
. Even though passing an integer is allowed, strive to use the time API to prevent any possible confusion.
Many common functions in the standard library accept a time.Duration
, which is an alias for the int64
type. However, one time.Duration
unit represents one nanosecond, instead of one millisecond, as commonly seen in other programming languages. As a result, passing numeric types instead of using the time.Duration
API can lead to unexpected behavior.
A developer with experience in other languages might assume that the following code creates a new time.Ticker
that delivers ticks every second, given the value 1000
:
ticker := time.NewTicker(1000)\nfor {\nselect {\ncase <-ticker.C:\n// Do something\n}\n}\n
However, because 1,000 time.Duration
units = 1,000 nanoseconds, ticks are delivered every 1,000 nanoseconds = 1 microsecond, not every second as assumed.
We should always use the time.Duration
API to avoid confusion and unexpected behavior:
ticker = time.NewTicker(time.Microsecond)\n// Or\nticker = time.NewTicker(1000 * time.Nanosecond)\n
Source code
"},{"location":"#timeafter-and-memory-leaks-76","title":"time.After
and memory leaks (#76)","text":"TL;DR Avoiding calls to time.After
in repeated functions (such as loops or HTTP handlers) can avoid peak memory consumption. The resources created by time.After
are released only when the timer expires.
Source code
"},{"location":"#json-handling-common-mistakes-77","title":"JSON handling common mistakes (#77)","text":"Be careful about using embedded fields in Go structs. Doing so may lead to sneaky bugs like an embedded time.Time field implementing the json.Marshaler
interface, hence overriding the default marshaling behavior.
Source code
When comparing two time.Time
structs, recall that time.Time
contains both a wall clock and a monotonic clock, and the comparison using the == operator is done on both clocks.
Source code
any
To avoid wrong assumptions when you provide a map while unmarshaling JSON data, remember that numerics are converted to float64
by default.
Source code
"},{"location":"#common-sql-mistakes-78","title":"Common SQL mistakes (#78)","text":"sql.Open
doesn't necessarily establish connections to a databaseCall the Ping
or PingContext
method if you need to test your configuration and make sure a database is reachable.
Source code
Configure the database connection parameters for production-grade applications.
Using SQL prepared statements makes queries more efficient and more secure.
Source code
Deal with nullable columns in tables using pointers or sql.NullXXX
types.
Source code
Call the Err
method of sql.Rows
after row iterations to ensure that you haven\u2019t missed an error while preparing the next row.
Source code
"},{"location":"#not-closing-transient-resources-http-body-sqlrows-and-osfile-79","title":"Not closing transient resources (HTTP body,sql.Rows
, and os.File
) (#79)","text":"TL;DR Eventually close all structs implementing io.Closer
to avoid possible leaks.
Source code
"},{"location":"#forgetting-the-return-statement-after-replying-to-an-http-request-80","title":"Forgetting the return statement after replying to an HTTP request (#80)","text":"TL;DRTo avoid unexpected behaviors in HTTP handler implementations, make sure you don\u2019t miss the return
statement if you want a handler to stop after http.Error
.
Source code
"},{"location":"#using-the-default-http-client-and-server-81","title":"Using the default HTTP client and server (#81)","text":"TL;DRFor production-grade applications, don\u2019t use the default HTTP client and server implementations. These implementations are missing timeouts and behaviors that should be mandatory in production.
Source code
"},{"location":"#testing","title":"Testing","text":""},{"location":"#not-categorizing-tests-build-tags-environment-variables-and-short-mode-82","title":"Not categorizing tests (build tags, environment variables, and short mode) (#82)","text":"TL;DRCategorizing tests using build flags, environment variables, or short mode makes the testing process more efficient. You can create test categories using build flags or environment variables (for example, unit versus integration tests) and differentiate short from long-running tests to decide which kinds of tests to execute.
Source code
"},{"location":"#not-enabling-the-race-flag-83","title":"Not enabling the race flag (#83)","text":"TL;DREnabling the -race
flag is highly recommended when writing concurrent applications. Doing so allows you to catch potential data races that can lead to software bugs.
Using the -parallel
flag is an efficient way to speed up tests, especially long-running ones. Use the -shuffle
flag to help ensure that a test suite doesn\u2019t rely on wrong assumptions that could hide bugs.
Table-driven tests are an efficient way to group a set of similar tests to prevent code duplication and make future updates easier to handle.
Source code
"},{"location":"#sleeping-in-unit-tests-86","title":"Sleeping in unit tests (#86)","text":"TL;DRAvoid sleeps using synchronization to make a test less flaky and more robust. If synchronization isn\u2019t possible, consider a retry approach.
Source code
"},{"location":"#not-dealing-with-the-time-api-efficiently-87","title":"Not dealing with the time API efficiently (#87)","text":"TL;DRUnderstanding how to deal with functions using the time API is another way to make a test less flaky. You can use standard techniques such as handling the time as part of a hidden dependency or asking clients to provide it.
Source code
"},{"location":"#not-using-testing-utility-packages-httptest-and-iotest-88","title":"Not using testing utility packages (httptest
and iotest
) (#88)","text":"httptest
package is helpful for dealing with HTTP applications. It provides a set of utilities to test both clients and servers.Source code
iotest
package helps write io.Reader and test that an application is tolerant to errors.Source code
"},{"location":"#writing-inaccurate-benchmarks-89","title":"Writing inaccurate benchmarks (#89)","text":"TL;DRRegarding benchmarks:
Read the full section here.
Source code
"},{"location":"#not-exploring-all-the-go-testing-features-90","title":"Not exploring all the Go testing features (#90)","text":"Use code coverage with the -coverprofile
flag to quickly see which part of the code needs more attention.
Place unit tests in a different package to enforce writing tests that focus on an exposed behavior, not internals.
Source code
Handling errors using the *testing.T
variable instead of the classic if err != nil
makes code shorter and easier to read.
Source code
You can use setup and teardown functions to configure a complex environment, such as in the case of integration tests.
Source code
"},{"location":"#not-using-fuzzing-community-mistake","title":"Not using fuzzing (community mistake)","text":"TL;DRFuzzing is an efficient strategy to detect random, unexpected, or malformed inputs to complex functions and methods in order to discover vulnerabilities, bugs, or even potential crashes.
Credits: @jeromedoucet
"},{"location":"#optimizations","title":"Optimizations","text":""},{"location":"#not-understanding-cpu-caches-91","title":"Not understanding CPU caches (#91)","text":"Understanding how to use CPU caches is important for optimizing CPU-bound applications because the L1 cache is about 50 to 100 times faster than the main memory.
Being conscious of the cache line concept is critical to understanding how to organize data in data-intensive applications. A CPU doesn\u2019t fetch memory word by word; instead, it usually copies a memory block to a 64-byte cache line. To get the most out of each individual cache line, enforce spatial locality.
Source code
Source code
Making code predictable for the CPU can also be an efficient way to optimize certain functions. For example, a unit or constant stride is predictable for the CPU, but a non-unit stride (for example, a linked list) isn\u2019t predictable.
Source code
To avoid a critical stride, hence utilizing only a tiny portion of the cache, be aware that caches are partitioned.
"},{"location":"#writing-concurrent-code-that-leads-to-false-sharing-92","title":"Writing concurrent code that leads to false sharing (#92)","text":"TL;DRKnowing that lower levels of CPU caches aren\u2019t shared across all the cores helps avoid performance-degrading patterns such as false sharing while writing concurrency code. Sharing memory is an illusion.
Source code
"},{"location":"#not-taking-into-account-instruction-level-parallelism-93","title":"Not taking into account instruction-level parallelism (#93)","text":"TL;DRUse instruction-level parallelism (ILP) to optimize specific parts of your code to allow a CPU to execute as many parallel instructions as possible. Identifying data hazards is one of the main steps.
Source code
"},{"location":"#not-being-aware-of-data-alignment-94","title":"Not being aware of data alignment (#94)","text":"TL;DRYou can avoid common mistakes by remembering that in Go, basic types are aligned with their own size. For example, keep in mind that reorganizing the fields of a struct by size in descending order can lead to more compact structs (less memory allocation and potentially a better spatial locality).
Source code
"},{"location":"#not-understanding-stack-vs-heap-95","title":"Not understanding stack vs. heap (#95)","text":"TL;DRUnderstanding the fundamental differences between heap and stack should also be part of your core knowledge when optimizing a Go application. Stack allocations are almost free, whereas heap allocations are slower and rely on the GC to clean the memory.
Source code
"},{"location":"#not-knowing-how-to-reduce-allocations-api-change-compiler-optimizations-and-syncpool-96","title":"Not knowing how to reduce allocations (API change, compiler optimizations, andsync.Pool
) (#96)","text":"TL;DR Reducing allocations is also an essential aspect of optimizing a Go application. This can be done in different ways, such as designing the API carefully to prevent sharing up, understanding the common Go compiler optimizations, and using sync.Pool
.
Source code
"},{"location":"#not-relying-on-inlining-97","title":"Not relying on inlining (#97)","text":"TL;DRUse the fast-path inlining technique to efficiently reduce the amortized time to call a function.
"},{"location":"#not-using-go-diagnostics-tooling-98","title":"Not using Go diagnostics tooling (#98)","text":"TL;DRRely on profiling and the execution tracer to understand how an application performs and the parts to optimize.
Read the full section here.
"},{"location":"#not-understanding-how-the-gc-works-99","title":"Not understanding how the GC works (#99)","text":"TL;DRUnderstanding how to tune the GC can lead to multiple benefits such as handling sudden load increases more efficiently.
"},{"location":"#not-understanding-the-impacts-of-running-go-in-docker-and-kubernetes-100","title":"Not understanding the impacts of running Go in Docker and Kubernetes (#100)","text":"TL;DRTo help avoid CPU throttling when deployed in Docker and Kubernetes, keep in mind that Go isn\u2019t CFS-aware.
"},{"location":"20-slice/","title":"Not understanding slice length and capacity","text":"It\u2019s pretty common for Go developers to mix slice length and capacity or not understand them thoroughly. Assimilating these two concepts is essential for efficiently handling core operations such as slice initialization and adding elements with append, copying, or slicing. This misunderstanding can lead to using slices suboptimally or even to memory leaks.
In Go, a slice is backed by an array. That means the slice\u2019s data is stored contiguously in an array data structure. A slice also handles the logic of adding an element if the backing array is full or shrinking the backing array if it\u2019s almost empty.
Internally, a slice holds a pointer to the backing array plus a length and a capacity. The length is the number of elements the slice contains, whereas the capacity is the number of elements in the backing array, counting from the first element in the slice. Let\u2019s go through a few examples to make things clearer. First, let\u2019s initialize a slice with a given length and capacity:
s := make([]int, 3, 6) // Three-length, six-capacity slice\n
The first argument, representing the length, is mandatory. However, the second argument representing the capacity is optional. Figure 1 shows the result of this code in memory.
Figure 1: A three-length, six-capacity slice.
In this case, make
creates an array of six elements (the capacity). But because the length was set to 3, Go initializes only the first three elements. Also, because the slice is an []int
type, the first three elements are initialized to the zeroed value of an int
: 0. The grayed elements are allocated but not yet used.
If we print this slice, we get the elements within the range of the length, [0 0 0]
. If we set s[1]
to 1, the second element of the slice updates without impacting its length or capacity. Figure 2 illustrates this.
Figure 2: Updating the slice\u2019s second element: s[1] = 1.
However, accessing an element outside the length range is forbidden, even though it\u2019s already allocated in memory. For example, s[4]
= 0 would lead to the following panic:
panic: runtime error: index out of range [4] with length 3\n
How can we use the remaining space of the slice? By using the append
built-in function:
s = append(s, 2)\n
This code appends to the existing s
slice a new element. It uses the first grayed element (which was allocated but not yet used) to store element 2, as figure 3 shows.
Figure 3: Appending an element to s.
The length of the slice is updated from 3 to 4 because the slice now contains four elements. Now, what happens if we add three more elements so that the backing array isn\u2019t large enough?
s = append(s, 3, 4, 5)\nfmt.Println(s)\n
If we run this code, we see that the slice was able to cope with our request:
[0 1 0 2 3 4 5]\n
Because an array is a fixed-size structure, it can store the new elements until element 4. When we want to insert element 5, the array is already full: Go internally creates another array by doubling the capacity, copying all the elements, and then inserting element 5. Figure 4 shows this process.
Figure 4: Because the initial backing array is full, Go creates another array and copies all the elements.
The slice now references the new backing array. What will happen to the previous backing array? If it\u2019s no longer referenced, it\u2019s eventually freed by the garbage collector (GC) if allocated on the heap. (We discuss heap memory in mistake #95, \u201cNot understanding stack vs. heap,\u201d and we look at how the GC works in mistake #99, \u201cNot understanding how the GC works.\u201d)
What happens with slicing? Slicing is an operation done on an array or a slice, providing a half-open range; the first index is included, whereas the second is excluded. The following example shows the impact, and figure 5 displays the result in memory:
s1 := make([]int, 3, 6) // Three-length, six-capacity slice\ns2 := s1[1:3] // Slicing from indices 1 to 3\n
Figure 5: The slices s1 and s2 reference the same backing array with different lengths and capacities.
First, s1
is created as a three-length, six-capacity slice. When s2
is created by slicing s1
, both slices reference the same backing array. However, s2
starts from a different index, 1. Therefore, its length and capacity (a two-length, five-capacity slice) differ from s1. If we update s1[1]
or s2[0]
, the change is made to the same array, hence, visible in both slices, as figure 6 shows.
Figure 6: Because s1 and s2 are backed by the same array, updating a common element makes the change visible in both slices.
Now, what happens if we append an element to s2
? Does the following code change s1
as well?
s2 = append(s2, 2)\n
The shared backing array is modified, but only the length of s2
changes. Figure 7 shows the result of appending an element to s2
.
Figure 7: Appending an element to s2.
s1
remains a three-length, six-capacity slice. Therefore, if we print s1
and s2
, the added element is only visible for s2
:
s1=[0 1 0], s2=[1 0 2]\n
It\u2019s important to understand this behavior so that we don\u2019t make wrong assumptions while using append.
NoteIn these examples, the backing array is internal and not available directly to the Go developer. The only exception is when a slice is created from slicing an existing array.
One last thing to note: what if we keep appending elements to s2
until the backing array is full? What will the state be, memory-wise? Let\u2019s add three more elements so that the backing array will not have enough capacity:
s2 = append(s2, 3)\ns2 = append(s2, 4) // At this stage, the backing is already full\ns2 = append(s2, 5)\n
This code leads to creating another backing array. Figure 8 displays the results in memory.
Figure 8: Appending elements to s2 until the backing array is full.
s1
and s2
now reference two different arrays. As s1
is still a three-length, six-capacity slice, it still has some available buffer, so it keeps referencing the initial array. Also, the new backing array was made by copying the initial one from the first index of s2
. That\u2019s why the new array starts with element 1, not 0.
To summarize, the slice length is the number of available elements in the slice, whereas the slice capacity is the number of elements in the backing array. Adding an element to a full slice (length == capacity) leads to creating a new backing array with a new capacity, copying all the elements from the previous array, and updating the slice pointer to the new array.
"},{"location":"28-maps-memory-leaks/","title":"Maps and memory leaks","text":"When working with maps in Go, we need to understand some important characteristics of how a map grows and shrinks. Let\u2019s delve into this to prevent issues that can cause memory leaks.
First, to view a concrete example of this problem, let\u2019s design a scenario where we will work with the following map:
m := make(map[int][128]byte)\n
Each value of m is an array of 128 bytes. We will do the following:
After each step, we want to print the size of the heap (using a printAlloc
utility function). This shows us how this example behaves memory-wise:
func main() {\nn := 1_000_000\nm := make(map[int][128]byte)\nprintAlloc()\nfor i := 0; i < n; i++ { // Adds 1 million elements\nm[i] = [128]byte{}\n}\nprintAlloc()\nfor i := 0; i < n; i++ { // Deletes 1 million elements\ndelete(m, i)\n}\nruntime.GC() // Triggers a manual GC\nprintAlloc()\nruntime.KeepAlive(m) // Keeps a reference to m so that the map isn\u2019t collected\n}\nfunc printAlloc() {\nvar m runtime.MemStats\nruntime.ReadMemStats(&m)\nfmt.Printf(\"%d KB\\n\", m.Alloc/1024)\n}\n
We allocate an empty map, add 1 million elements, remove 1 million elements, and then run a GC. We also make sure to keep a reference to the map using runtime.KeepAlive
so that the map isn\u2019t collected as well. Let\u2019s run this example:
0 MB <-- After m is allocated\n461 MB <-- After we add 1 million elements\n293 MB <-- After we remove 1 million elements\n
What can we observe? At first, the heap size is minimal. Then it grows significantly after having added 1 million elements to the map. But if we expected the heap size to decrease after removing all the elements, this isn\u2019t how maps work in Go. In the end, even though the GC has collected all the elements, the heap size is still 293 MB. So the memory shrunk, but not as we might have expected. What\u2019s the rationale? We need to delve into how a map works in Go.
A map provides an unordered collection of key-value pairs in which all the keys are distinct. In Go, a map is based on the hash table data structure: an array where each element is a pointer to a bucket of key-value pairs, as shown in figure 1.
Figure 1: A hash table example with a focus on bucket 0.
Each bucket is a fixed-size array of eight elements. In the case of an insertion into a bucket that is already full (a bucket overflow), Go creates another bucket of eight elements and links the previous one to it. Figure 2 shows an example:
Figure 2: In case of a bucket overflow, Go allocates a new bucket and links the previous bucket to it.
Under the hood, a Go map is a pointer to a runtime.hmap struct. This struct contains multiple fields, including a B field, giving the number of buckets in the map:
type hmap struct {\nB uint8 // log_2 of # of buckets\n// (can hold up to loadFactor * 2^B items)\n// ...\n}\n
After adding 1 million elements, the value of B equals 18, which means 2\u00b9\u2078 = 262,144 buckets. When we remove 1 million elements, what\u2019s the value of B? Still 18. Hence, the map still contains the same number of buckets.
The reason is that the number of buckets in a map cannot shrink. Therefore, removing elements from a map doesn\u2019t impact the number of existing buckets; it just zeroes the slots in the buckets. A map can only grow and have more buckets; it never shrinks.
In the previous example, we went from 461 MB to 293 MB because the elements were collected, but running the GC didn\u2019t impact the map itself. Even the number of extra buckets (the buckets created because of overflows) remains the same.
Let\u2019s take a step back and discuss when the fact that a map cannot shrink can be a problem. Imagine building a cache using a map[int][128]byte
. This map holds per customer ID (the int
), a sequence of 128 bytes. Now, suppose we want to save the last 1,000 customers. The map size will remain constant, so we shouldn\u2019t worry about the fact that a map cannot shrink.
However, let\u2019s say we want to store one hour of data. Meanwhile, our company has decided to have a big promotion for Black Friday: in one hour, we may have millions of customers connected to our system. But a few days after Black Friday, our map will contain the same number of buckets as during the peak time. This explains why we can experience high memory consumption that doesn\u2019t significantly decrease in such a scenario.
What are the solutions if we don\u2019t want to manually restart our service to clean the amount of memory consumed by the map? One solution could be to re-create a copy of the current map at a regular pace. For example, every hour, we can build a new map, copy all the elements, and release the previous one. The main drawback of this option is that following the copy and until the next garbage collection, we may consume twice the current memory for a short period.
Another solution would be to change the map type to store an array pointer: map[int]*[128]byte
. It doesn\u2019t solve the fact that we will have a significant number of buckets; however, each bucket entry will reserve the size of a pointer for the value instead of 128 bytes (8 bytes on 64-bit systems and 4 bytes on 32-bit systems).
Coming back to the original scenario, let\u2019s compare the memory consumption for each map type following each step. The following table shows the comparison.
Stepmap[int][128]byte
map[int]*[128]byte
Allocate an empty map 0 MB 0 MB Add 1 million elements 461 MB 182 MB Remove all the elements and run a GC 293 MB 38 MB Note If a key or a value is over 128 bytes, Go won\u2019t store it directly in the map bucket. Instead, Go stores a pointer to reference the key or the value.
As we have seen, adding n elements to a map and then deleting all the elements means keeping the same number of buckets in memory. So, we must remember that because a Go map can only grow in size, so does its memory consumption. There is no automated strategy to shrink it. If this leads to high memory consumption, we can try different options such as forcing Go to re-create the map or using pointers to check if it can be optimized.
"},{"location":"56-concurrency-faster/","title":"Thinking concurrency is always faster","text":"A misconception among many developers is believing that a concurrent solution is always faster than a sequential one. This couldn\u2019t be more wrong. The overall performance of a solution depends on many factors, such as the efficiency of our code structure (concurrency), which parts can be tackled in parallel, and the level of contention among the computation units. This post reminds us about some fundamental knowledge of concurrency in Go; then we will see a concrete example where a concurrent solution isn\u2019t necessarily faster.
"},{"location":"56-concurrency-faster/#go-scheduling","title":"Go Scheduling","text":"A thread is the smallest unit of processing that an OS can perform. If a process wants to execute multiple actions simultaneously, it spins up multiple threads. These threads can be:
The OS is responsible for scheduling the thread\u2019s processes optimally so that:
The word thread can also have a different meaning at a CPU level. Each physical core can be composed of multiple logical cores (the concept of hyper-threading), and a logical core is also called a thread. In this post, when we use the word thread, we mean the unit of processing, not a logical core.
A CPU core executes different threads. When it switches from one thread to another, it executes an operation called context switching. The active thread consuming CPU cycles was in an executing state and moves to a runnable state, meaning it\u2019s ready to be executed pending an available core. Context switching is considered an expensive operation because the OS needs to save the current execution state of a thread before the switch (such as the current register values).
As Go developers, we can\u2019t create threads directly, but we can create goroutines, which can be thought of as application-level threads. However, whereas an OS thread is context-switched on and off a CPU core by the OS, a goroutine is context-switched on and off an OS thread by the Go runtime. Also, compared to an OS thread, a goroutine has a smaller memory footprint: 2 KB for goroutines from Go 1.4. An OS thread depends on the OS, but, for example, on Linux/x86\u201332, the default size is 2 MB (see https://man7.org/linux/man-pages/man3/pthread_create.3.html). Having a smaller size makes context switching faster.
NoteContext switching a goroutine versus a thread is about 80% to 90% faster, depending on the architecture.
Let\u2019s now discuss how the Go scheduler works to overview how goroutines are handled. Internally, the Go scheduler uses the following terminology (see proc.go):
Each OS thread (M) is assigned to a CPU core (P) by the OS scheduler. Then, each goroutine (G) runs on an M. The GOMAXPROCS variable defines the limit of Ms in charge of executing user-level code simultaneously. But if a thread is blocked in a system call (for example, I/O), the scheduler can spin up more Ms. As of Go 1.5, GOMAXPROCS is by default equal to the number of available CPU cores.
A goroutine has a simpler lifecycle than an OS thread. It can be doing one of the following:
There\u2019s one last stage to understand about the implementation of Go scheduling: when a goroutine is created but cannot be executed yet; for example, all the other Ms are already executing a G. In this scenario, what will the Go runtime do about it? The answer is queuing. The Go runtime handles two kinds of queues: one local queue per P and a global queue shared among all the Ps.
Figure 1 shows a given scheduling situation on a four-core machine with GOMAXPROCS equal to 4. The parts are the logical cores (Ps), goroutines (Gs), OS threads (Ms), local queues, and global queue:
Figure 1: An example of the current state of a Go application executed on a four-core machine. Goroutines that aren\u2019t in an executing state are either runnable (pending being executed) or waiting (pending a blocking operation)
First, we can see five Ms, whereas GOMAXPROCS is set to 4. But as we mentioned, if needed, the Go runtime can create more OS threads than the GOMAXPROCS value.
P0, P1, and P3 are currently busy executing Go runtime threads. But P2 is presently idle as M3 is switched off P2, and there\u2019s no goroutine to be executed. This isn\u2019t a good situation because six runnable goroutines are pending being executed, some in the global queue and some in other local queues. How will the Go runtime handle this situation? Here\u2019s the scheduling implementation in pseudocode (see proc.go):
runtime.schedule() {\n // Only 1/61 of the time, check the global runnable queue for a G.\n // If not found, check the local queue.\n // If not found,\n // Try to steal from other Ps.\n // If not, check the global runnable queue.\n // If not found, poll network.\n}\n
Every sixty-first execution, the Go scheduler will check whether goroutines from the global queue are available. If not, it will check its local queue. Meanwhile, if both the global and local queues are empty, the Go scheduler can pick up goroutines from other local queues. This principle in scheduling is called work stealing, and it allows an underutilized processor to actively look for another processor\u2019s goroutines and steal some.
One last important thing to mention: prior to Go 1.14, the scheduler was cooperative, which meant a goroutine could be context-switched off a thread only in specific blocking cases (for example, channel send or receive, I/O, waiting to acquire a mutex). Since Go 1.14, the Go scheduler is now preemptive: when a goroutine is running for a specific amount of time (10 ms), it will be marked preemptible and can be context-switched off to be replaced by another goroutine. This allows a long-running job to be forced to share CPU time.
Now that we understand the fundamentals of scheduling in Go, let\u2019s look at a concrete example: implementing a merge sort in a parallel manner.
"},{"location":"56-concurrency-faster/#parallel-merge-sort","title":"Parallel Merge Sort","text":"First, let\u2019s briefly review how the merge sort algorithm works. Then we will implement a parallel version. Note that the objective isn\u2019t to implement the most efficient version but to support a concrete example showing why concurrency isn\u2019t always faster.
The merge sort algorithm works by breaking a list repeatedly into two sublists until each sublist consists of a single element and then merging these sublists so that the result is a sorted list (see figure 2). Each split operation splits the list into two sublists, whereas the merge operation merges two sublists into a sorted list.
Figure 2: Applying the merge sort algorithm repeatedly breaks each list into two sublists. Then the algorithm uses a merge operation such that the resulting list is sorted
Here is the sequential implementation of this algorithm. We don\u2019t include all of the code as it\u2019s not the main point of this section:
func sequentialMergesort(s []int) {\nif len(s) <= 1 {\nreturn\n}\nmiddle := len(s) / 2\nsequentialMergesort(s[:middle]) // First half\nsequentialMergesort(s[middle:]) // Second half\nmerge(s, middle) // Merges the two halves\n}\nfunc merge(s []int, middle int) {\n// ...\n}\n
This algorithm has a structure that makes it open to concurrency. Indeed, as each sequentialMergesort operation works on an independent set of data that doesn\u2019t need to be fully copied (here, an independent view of the underlying array using slicing), we could distribute this workload among the CPU cores by spinning up each sequentialMergesort operation in a different goroutine. Let\u2019s write a first parallel implementation:
func parallelMergesortV1(s []int) {\nif len(s) <= 1 {\nreturn\n}\nmiddle := len(s) / 2\nvar wg sync.WaitGroup\nwg.Add(2)\ngo func() { // Spins up the first half of the work in a goroutine\ndefer wg.Done()\nparallelMergesortV1(s[:middle])\n}()\ngo func() { // Spins up the second half of the work in a goroutine\ndefer wg.Done()\nparallelMergesortV1(s[middle:])\n}()\nwg.Wait()\nmerge(s, middle) // Merges the halves\n}\n
In this version, each half of the workload is handled in a separate goroutine. The parent goroutine waits for both parts by using sync.WaitGroup. Hence, we call the Wait method before the merge operation.
We now have a parallel version of the merge sort algorithm. Therefore, if we run a benchmark to compare this version against the sequential one, the parallel version should be faster, correct? Let\u2019s run it on a four-core machine with 10,000 elements:
Benchmark_sequentialMergesort-4 2278993555 ns/op\nBenchmark_parallelMergesortV1-4 17525998709 ns/op\n
Surprisingly, the parallel version is almost an order of magnitude slower. How can we explain this result? How is it possible that a parallel version that distributes a workload across four cores is slower than a sequential version running on a single machine? Let\u2019s analyze the problem.
If we have a slice of, say, 1,024 elements, the parent goroutine will spin up two goroutines, each in charge of handling a half consisting of 512 elements. Each of these goroutines will spin up two new goroutines in charge of handling 256 elements, then 128, and so on, until we spin up a goroutine to compute a single element.
If the workload that we want to parallelize is too small, meaning we\u2019re going to compute it too fast, the benefit of distributing a job across cores is destroyed: the time it takes to create a goroutine and have the scheduler execute it is much too high compared to directly merging a tiny number of items in the current goroutine. Although goroutines are lightweight and faster to start than threads, we can still face cases where a workload is too small.
So what can we conclude from this result? Does it mean the merge sort algorithm cannot be parallelized? Wait, not so fast.
Let\u2019s try another approach. Because merging a tiny number of elements within a new goroutine isn\u2019t efficient, let\u2019s define a threshold. This threshold will represent how many elements a half should contain in order to be handled in a parallel manner. If the number of elements in the half is fewer than this value, we will handle it sequentially. Here\u2019s a new version:
const max = 2048 // Defines the threshold\nfunc parallelMergesortV2(s []int) {\nif len(s) <= 1 {\nreturn\n}\nif len(s) <= max {\nsequentialMergesort(s) // Calls our initial sequential version\n} else { // If bigger than the threshold, keeps the parallel version\nmiddle := len(s) / 2\nvar wg sync.WaitGroup\nwg.Add(2)\ngo func() {\ndefer wg.Done()\nparallelMergesortV2(s[:middle])\n}()\ngo func() {\ndefer wg.Done()\nparallelMergesortV2(s[middle:])\n}()\nwg.Wait()\nmerge(s, middle)\n}\n}\n
If the number of elements in the s slice is smaller than max, we call the sequential version. Otherwise, we keep calling our parallel implementation. Does this approach impact the result? Yes, it does:
Benchmark_sequentialMergesort-4 2278993555 ns/op\nBenchmark_parallelMergesortV1-4 17525998709 ns/op\nBenchmark_parallelMergesortV2-4 1313010260 ns/op\n
Our v2 parallel implementation is more than 40% faster than the sequential one, thanks to this idea of defining a threshold to indicate when parallel should be more efficient than sequential.
NoteWhy did I set the threshold to 2,048? Because it was the optimal value for this specific workload on my machine. In general, such magic values should be defined carefully with benchmarks (running on an execution environment similar to production). It\u2019s also pretty interesting to note that running the same algorithm in a programming language that doesn\u2019t implement the concept of goroutines has an impact on the value. For example, running the same example in Java using threads means an optimal value closer to 8,192. This tends to illustrate how goroutines are more efficient than threads.
"},{"location":"56-concurrency-faster/#conclusion","title":"Conclusion","text":"We have seen throughout this post the fundamental concepts of scheduling in Go: the differences between a thread and a goroutine and how the Go runtime schedules goroutines. Meanwhile, using the parallel merge sort example, we illustrated that concurrency isn\u2019t always necessarily faster. As we have seen, spinning up goroutines to handle minimal workloads (merging only a small set of elements) demolishes the benefit we could get from parallelism.
So, where should we go from here? We must keep in mind that concurrency isn\u2019t always faster and shouldn\u2019t be considered the default way to go for all problems. First, it makes things more complex. Also, modern CPUs have become incredibly efficient at executing sequential code and predictable code. For example, a superscalar processor can parallelize instruction execution over a single core with high efficiency.
Does this mean we shouldn\u2019t use concurrency? Of course not. However, it\u2019s essential to keep these conclusions in mind. If we\u2019re not sure that a parallel version will be faster, the right approach may be to start with a simple sequential version and build from there using profiling (mistake #98, \u201cNot using Go diagnostics tooling\u201d) and benchmarks (mistake #89, \u201cWriting inaccurate benchmarks\u201d), for example. It can be the only way to ensure that a concurrent implementation is worth it.
"},{"location":"89-benchmarks/","title":"Writing inaccurate benchmarks","text":"In general, we should never guess about performance. When writing optimizations, so many factors may come into play that even if we have a strong opinion about the results, it\u2019s rarely a bad idea to test them. However, writing benchmarks isn\u2019t straightforward. It can be pretty simple to write inaccurate benchmarks and make wrong assumptions based on them. The goal of this post is to examine four common and concrete traps leading to inaccuracy:
Before discussing these traps, let\u2019s briefly review how benchmarks work in Go. The skeleton of a benchmark is as follows:
func BenchmarkFoo(b *testing.B) {\nfor i := 0; i < b.N; i++ {\nfoo()\n}\n}\n
The function name starts with the Benchmark
prefix. The function under test (foo) is called within the for
loop. b.N
represents a variable number of iterations. When running a benchmark, Go tries to make it match the requested benchmark time. The benchmark time is set by default to 1 second and can be changed with the -benchtime
flag. b.N
starts at 1; if the benchmark completes in under 1 second, b.N
is increased, and the benchmark runs again until b.N
roughly matches benchtime:
$ go test -bench=.\ncpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkFoo-4 73 16511228 ns/op\n
Here, the benchmark took about 1 second, and foo
was executed 73 times, for an average execution time of 16,511,228 nanoseconds. We can change the benchmark time using -benchtime
:
$ go test -bench=. -benchtime=2s\nBenchmarkFoo-4 150 15832169 ns/op\n
foo
was executed roughly twice more than during the previous benchmark.
Next, let\u2019s look at some common traps.
"},{"location":"89-benchmarks/#not-resetting-or-pausing-the-timer","title":"Not resetting or pausing the timer","text":"In some cases, we need to perform operations before the benchmark loop. These operations may take quite a while (for example, generating a large slice of data) and may significantly impact the benchmark results:
func BenchmarkFoo(b *testing.B) {\nexpensiveSetup()\nfor i := 0; i < b.N; i++ {\nfunctionUnderTest()\n}\n}\n
In this case, we can use the ResetTimer
method before entering the loop:
func BenchmarkFoo(b *testing.B) {\nexpensiveSetup()\nb.ResetTimer() // Reset the benchmark timer\nfor i := 0; i < b.N; i++ {\nfunctionUnderTest()\n}\n}\n
Calling ResetTimer
zeroes the elapsed benchmark time and memory allocation counters since the beginning of the test. This way, an expensive setup can be discarded from the test results.
What if we have to perform an expensive setup not just once but within each loop iteration?
func BenchmarkFoo(b *testing.B) {\nfor i := 0; i < b.N; i++ {\nexpensiveSetup()\nfunctionUnderTest()\n}\n}\n
We can\u2019t reset the timer, because that would be executed during each loop iteration. But we can stop and resume the benchmark timer, surrounding the call to expensiveSetup
:
func BenchmarkFoo(b *testing.B) {\nfor i := 0; i < b.N; i++ {\nb.StopTimer() // Pause the benchmark timer\nexpensiveSetup()\nb.StartTimer() // Resume the benchmark timer\nfunctionUnderTest()\n}\n}\n
Here, we pause the benchmark timer to perform the expensive setup and then resume the timer.
NoteThere\u2019s one catch to remember about this approach: if the function under test is too fast to execute compared to the setup function, the benchmark may take too long to complete. The reason is that it would take much longer than 1 second to reach benchtime
. Calculating the benchmark time is based solely on the execution time of functionUnderTest
. So, if we wait a significant time in each loop iteration, the benchmark will be much slower than 1 second. If we want to keep the benchmark, one possible mitigation is to decrease benchtime
.
We must be sure to use the timer methods to preserve the accuracy of a benchmark.
"},{"location":"89-benchmarks/#making-wrong-assumptions-about-micro-benchmarks","title":"Making wrong assumptions about micro-benchmarks","text":"A micro-benchmark measures a tiny computation unit, and it can be extremely easy to make wrong assumptions about it. Let\u2019s say, for example, that we aren\u2019t sure whether to use atomic.StoreInt32
or atomic.StoreInt64
(assuming that the values we handle will always fit in 32 bits). We want to write a benchmark to compare both functions:
func BenchmarkAtomicStoreInt32(b *testing.B) {\nvar v int32\nfor i := 0; i < b.N; i++ {\natomic.StoreInt32(&v, 1)\n}\n}\nfunc BenchmarkAtomicStoreInt64(b *testing.B) {\nvar v int64\nfor i := 0; i < b.N; i++ {\natomic.StoreInt64(&v, 1)\n}\n}\n
If we run this benchmark, here\u2019s some example output:
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkAtomicStoreInt32\nBenchmarkAtomicStoreInt32-4 197107742 5.682 ns/op\nBenchmarkAtomicStoreInt64\nBenchmarkAtomicStoreInt64-4 213917528 5.134 ns/op\n
We could easily take this benchmark for granted and decide to use atomic.StoreInt64
because it appears to be faster. Now, for the sake of doing a fair benchmark, we reverse the order and test atomic.StoreInt64
first, followed by atomic.StoreInt32
. Here is some example output:
BenchmarkAtomicStoreInt64\nBenchmarkAtomicStoreInt64-4 224900722 5.434 ns/op\nBenchmarkAtomicStoreInt32\nBenchmarkAtomicStoreInt32-4 230253900 5.159 ns/op\n
This time, atomic.StoreInt32
has better results. What happened?
In the case of micro-benchmarks, many factors can impact the results, such as machine activity while running the benchmarks, power management, thermal scaling, and better cache alignment of a sequence of instructions. We must remember that many factors, even outside the scope of our Go project, can impact the results.
NoteWe should make sure the machine executing the benchmark is idle. However, external processes may run in the background, which may affect benchmark results. For that reason, tools such as perflock
can limit how much CPU a benchmark can consume. For example, we can run a benchmark with 70% of the total available CPU, giving 30% to the OS and other processes and reducing the impact of the machine activity factor on the results.
One option is to increase the benchmark time using the -benchtime
option. Similar to the law of large numbers in probability theory, if we run a benchmark a large number of times, it should tend to approach its expected value (assuming we omit the benefits of instructions caching and similar mechanics).
Another option is to use external tools on top of the classic benchmark tooling. For instance, the benchstat
tool, which is part of the golang.org/x
repository, allows us to compute and compare statistics about benchmark executions.
Let\u2019s run the benchmark 10 times using the -count
option and pipe the output to a specific file:
$ go test -bench=. -count=10 | tee stats.txt\ncpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkAtomicStoreInt32-4 234935682 5.124 ns/op\nBenchmarkAtomicStoreInt32-4 235307204 5.112 ns/op\n// ...\nBenchmarkAtomicStoreInt64-4 235548591 5.107 ns/op\nBenchmarkAtomicStoreInt64-4 235210292 5.090 ns/op\n// ...\n
We can then run benchstat
on this file:
$ benchstat stats.txt\nname time/op\nAtomicStoreInt32-4 5.10ns \u00b1 1%\nAtomicStoreInt64-4 5.10ns \u00b1 1%\n
The results are the same: both functions take on average 5.10 nanoseconds to complete. We also see the percent variation between the executions of a given benchmark: \u00b1 1%. This metric tells us that both benchmarks are stable, giving us more confidence in the computed average results. Therefore, instead of concluding that atomic.StoreInt32
is faster or slower, we can conclude that its execution time is similar to that of atomic.StoreInt64
for the usage we tested (in a specific Go version on a particular machine).
In general, we should be cautious about micro-benchmarks. Many factors can significantly impact the results and potentially lead to wrong assumptions. Increasing the benchmark time or repeating the benchmark executions and computing stats with tools such as benchstat
can be an efficient way to limit external factors and get more accurate results, leading to better conclusions.
Let\u2019s also highlight that we should be careful about using the results of a micro-benchmark executed on a given machine if another system ends up running the application. The production system may act quite differently from the one on which we ran the micro-benchmark.
"},{"location":"89-benchmarks/#not-being-careful-about-compiler-optimizations","title":"Not being careful about compiler optimizations","text":"Another common mistake related to writing benchmarks is being fooled by compiler optimizations, which can also lead to wrong benchmark assumptions. In this section, we look at Go issue 14813 (https://github.com/golang/go/issues/14813, also discussed by Go project member Dave Cheney) with a population count function (a function that counts the number of bits set to 1):
const m1 = 0x5555555555555555\nconst m2 = 0x3333333333333333\nconst m4 = 0x0f0f0f0f0f0f0f0f\nconst h01 = 0x0101010101010101\nfunc popcnt(x uint64) uint64 {\nx -= (x >> 1) & m1\nx = (x & m2) + ((x >> 2) & m2)\nx = (x + (x >> 4)) & m4\nreturn (x * h01) >> 56\n}\n
This function takes and returns a uint64
. To benchmark this function, we can write the following:
func BenchmarkPopcnt1(b *testing.B) {\nfor i := 0; i < b.N; i++ {\npopcnt(uint64(i))\n}\n}\n
However, if we execute this benchmark, we get a surprisingly low result:
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkPopcnt1-4 1000000000 0.2858 ns/op\n
A duration of 0.28 nanoseconds is roughly one clock cycle, so this number is unreasonably low. The problem is that the developer wasn\u2019t careful enough about compiler optimizations. In this case, the function under test is simple enough to be a candidate for inlining: an optimization that replaces a function call with the body of the called function and lets us prevent a function call, which has a small footprint. Once the function is inlined, the compiler notices that the call has no side effects and replaces it with the following benchmark:
func BenchmarkPopcnt1(b *testing.B) {\nfor i := 0; i < b.N; i++ {\n// Empty\n}\n}\n
The benchmark is now empty \u2014 which is why we got a result close to one clock cycle. To prevent this from happening, a best practice is to follow this pattern:
In our case, we write the following benchmark:
var global uint64 // Define a global variable\nfunc BenchmarkPopcnt2(b *testing.B) {\nvar v uint64 // Define a local variable\nfor i := 0; i < b.N; i++ {\nv = popcnt(uint64(i)) // Assign the result to the local variable\n}\nglobal = v // Assign the result to the global variable\n}\n
global
is a global variable, whereas v is a local variable whose scope is the benchmark function. During each loop iteration, we assign the result of popcnt
to the local variable. Then we assign the latest result to the global variable.
Why not assign the result of the popcnt call directly to global to simplify the test? Writing to a global variable is slower than writing to a local variable (these concepts are discussed in 100 Go Mistakes, mistake #95: \u201cNot understanding stack vs. heap\u201d). Therefore, we should write each result to a local variable to limit the footprint during each loop iteration.
If we run these two benchmarks, we now get a significant difference in the results:
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkPopcnt1-4 1000000000 0.2858 ns/op\nBenchmarkPopcnt2-4 606402058 1.993 ns/op\n
BenchmarkPopcnt2
is the accurate version of the benchmark. It guarantees that we avoid the inlining optimizations, which can artificially lower the execution time or even remove the call to the function under test. Relying on the results of BenchmarkPopcnt1
could have led to wrong assumptions.
Let\u2019s remember the pattern to avoid compiler optimizations fooling benchmark results: assign the result of the function under test to a local variable, and then assign the latest result to a global variable. This best practice also prevents us from making incorrect assumptions.
"},{"location":"89-benchmarks/#being-fooled-by-the-observer-effect","title":"Being fooled by the observer effect","text":"In physics, the observer effect is the disturbance of an observed system by the act of observation. This effect can also be seen in benchmarks and can lead to wrong assumptions about results. Let\u2019s look at a concrete example and then try to mitigate it.
We want to implement a function receiving a matrix of int64
elements. This matrix has a fixed number of 512 columns, and we want to compute the total sum of the first eight columns, as shown in figure 1.
Figure 1: Computing the sum of the first eight columns.
For the sake of optimizations, we also want to determine whether varying the number of columns has an impact, so we also implement a second function with 513 columns. The implementation is the following:
func calculateSum512(s [][512]int64) int64 {\nvar sum int64\nfor i := 0; i < len(s); i++ { // Iterate over each row\nfor j := 0; j < 8; j++ { // Iterate over the first eight columns\nsum += s[i][j] // Increment sum\n}\n}\nreturn sum\n}\nfunc calculateSum513(s [][513]int64) int64 {\n// Same implementation as calculateSum512\n}\n
We iterate over each row and then over the first eight columns, and we increment a sum variable that we return. The implementation in calculateSum513
remains the same.
We want to benchmark these functions to decide which one is the most performant given a fixed number of rows:
const rows = 1000\nvar res int64\nfunc BenchmarkCalculateSum512(b *testing.B) {\nvar sum int64\ns := createMatrix512(rows) // Create a matrix of 512 columns\nb.ResetTimer()\nfor i := 0; i < b.N; i++ {\nsum = calculateSum512(s) // Create a matrix of 512 columns\n}\nres = sum\n}\nfunc BenchmarkCalculateSum513(b *testing.B) {\nvar sum int64\ns := createMatrix513(rows) // Create a matrix of 513 columns\nb.ResetTimer()\nfor i := 0; i < b.N; i++ {\nsum = calculateSum513(s) // Calculate the sum\n}\nres = sum\n}\n
We want to create the matrix only once, to limit the footprint on the results. Therefore, we call createMatrix512
and createMatrix513
outside of the loop. We may expect the results to be similar as again we only want to iterate on the first eight columns, but this isn\u2019t the case (on my machine):
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkCalculateSum512-4 81854 15073 ns/op\nBenchmarkCalculateSum513-4 161479 7358 ns/op\n
The second benchmark with 513 columns is about 50% faster. Again, because we iterate only over the first eight columns, this result is quite surprising.
To understand this difference, we need to understand the basics of CPU caches. In a nutshell, a CPU is composed of different caches (usually L1, L2, and L3). These caches reduce the average cost of accessing data from the main memory. In some conditions, the CPU can fetch data from the main memory and copy it to L1. In this case, the CPU tries to fetch into L1 the matrix\u2019s subset that calculateSum
is interested in (the first eight columns of each row). However, the matrix fits in memory in one case (513 columns) but not in the other case (512 columns).
This isn\u2019t in the scope of this post to explain why, but we look at this problem in 100 Go Mistakes, mistake #91: \u201cNot understanding CPU caches.\u201d
Coming back to the benchmark, the main issue is that we keep reusing the same matrix in both cases. Because the function is repeated thousands of times, we don\u2019t measure the function\u2019s execution when it receives a plain new matrix. Instead, we measure a function that gets a matrix that already has a subset of the cells present in the cache. Therefore, because calculateSum513
leads to fewer cache misses, it has a better execution time.
This is an example of the observer effect. Because we keep observing a repeatedly called CPU-bound function, CPU caching may come into play and significantly affect the results. In this example, to prevent this effect, we should create a matrix during each test instead of reusing one:
func BenchmarkCalculateSum512(b *testing.B) {\nvar sum int64\nfor i := 0; i < b.N; i++ {\nb.StopTimer()\ns := createMatrix512(rows) // Create a new matrix during each loop iteration\nb.StartTimer()\nsum = calculateSum512(s)\n}\nres = sum\n}\n
A new matrix is now created during each loop iteration. If we run the benchmark again (and adjust benchtime
\u2014 otherwise, it takes too long to execute), the results are closer to each other:
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz\nBenchmarkCalculateSum512-4 1116 33547 ns/op\nBenchmarkCalculateSum513-4 998 35507 ns/op\n
Instead of making the incorrect assumption that calculateSum513 is faster, we see that both benchmarks lead to similar results when receiving a new matrix.
As we have seen in this post, because we were reusing the same matrix, CPU caches significantly impacted the results. To prevent this, we had to create a new matrix during each loop iteration. In general, we should remember that observing a function under test may lead to significant differences in results, especially in the context of micro-benchmarks of CPU-bound functions where low-level optimizations matter. Forcing a benchmark to re-create data during each iteration can be a good way to prevent this effect.
"},{"location":"9-generics/","title":"Being confused about when to use generics","text":"Generics is a fresh addition to the language. In a nutshell, it allows writing code with types that can be specified later and instantiated when needed. However, it can be pretty easy to be confused about when to use generics and when not to. Throughout this post, we will describe the concept of generics in Go and then delve into common use and misuses.
"},{"location":"9-generics/#concepts","title":"Concepts","text":"Consider the following function that extracts all the keys from a map[string]int
type:
func getKeys(m map[string]int) []string {\nvar keys []string\nfor k := range m {\nkeys = append(keys, k)\n}\nreturn keys\n}\n
What if we would like to use a similar feature for another map type such as a map[int]string
? Before generics, Go developers had a couple of options: using code generation, reflection, or duplicating code.
For example, we could write two functions, one for each map type, or even try to extend getKeys
to accept different map types:
func getKeys(m any) ([]any, error) {\nswitch t := m.(type) {\ndefault:\nreturn nil, fmt.Errorf(\"unknown type: %T\", t)\ncase map[string]int:\nvar keys []any\nfor k := range t {\nkeys = append(keys, k)\n}\nreturn keys, nil\ncase map[int]string:\n// Copy the extraction logic\n}\n}\n
We can start noticing a couple of issues:
range
loop.int
or string
, we are obliged to return a slice of empty interfaces to factor out key types. This approach increases the effort on the caller-side as the client may also have to perform a type check of the keys or extra conversion.Thanks to generics, we can now refactor this code using type parameters.
Type parameters are generic types we can use with functions and types. For example, the following function accepts a type parameter:
func foo[T any](t T) {\n// ...\n}\n
When calling foo
, we will pass a type argument of any type. Passing a type argument is called instantiation because the work is done at compile time which keeps type safety as part of the core language features and avoids runtime overheads.
Let\u2019s get back to the getKeys
function and use type parameters to write a generic version that would accept any kind of map:
func getKeys[K comparable, V any](m map[K]V) []K { <1>\nvar keys []K <2>\nfor k := range m {\nkeys = append(keys, k)\n}\nreturn keys\n}\n
To handle the map, we defined two kinds of type parameters. First, the values can be of any type: V any
. However, in Go, the map keys can\u2019t be of any type. For example, we cannot use slices:
var m map[[]byte]int\n
This code leads to a compilation error: invalid map key type []byte
. Therefore, instead of accepting any key type, we are obliged to restrict type arguments so that the key type meets specific requirements. Here, being comparable (we can use ==
or !=
). Hence, we defined K
as comparable
instead of any
.
Restricting type arguments to match specific requirements is called a constraint. A constraint is an interface type that can contain:
Let\u2019s see a concrete example for the latter. Imagine we don\u2019t want to accept any comparable
type for map key type. For instance, we would like to restrict it to either int
or string
types. We can define a custom constraint this way:
type customConstraint interface {\n~int | ~string // Define a custom type that will restrict types to int and string\n}\n// Change the type parameter K to be custom\nfunc getKeys[K customConstraint, V any](m map[K]V) []K {\n// Same implementation\n}\n
First, we define a customConstraint
interface to restrict the types to be either int
or string
using the union operator |
(we will discuss the use of ~
a bit later). Then, K
is now a customConstraint
instead of a comparable
as before.
Now, the signature of getKeys
enforces that we can call it with a map of any value type, but the key type has to be an int
or a string
. For example, on the caller-side:
m = map[string]int{\n\"one\": 1,\n\"two\": 2,\n\"three\": 3,\n}\nkeys := getKeys(m)\n
Note that Go can infer that getKeys
is called with a string
type argument. The previous call was similar to this:
keys := getKeys[string](m)\n
Note What\u2019s the difference between a constraint using ~int
or int
? Using int
restricts it to that type, whereas ~int
restricts all the types whose underlying type is an int
.
To illustrate it, let\u2019s imagine a constraint where we would like to restrict a type to any int
type implementing the String() string
method:
type customConstraint interface {\n~int\nString() string\n}\n
Using this constraint will restrict type arguments to custom types like this one:
type customInt int\nfunc (i customInt) String() string {\nreturn strconv.Itoa(int(i))\n}\n
As customInt
is an int
and implements the String() string
method, the customInt
type satisfies the constraint defined.
However, if we change the constraint to contain an int
instead of an ~int
, using customInt
would lead to a compilation error because the int
type doesn\u2019t implement String() string
.
Let\u2019s also note the constraints
package contains a set of common constraints such as Signed
that includes all the signed integer types. Let\u2019s ensure that a constraint doesn\u2019t already exist in this package before creating a new one.
So far, we have discussed examples using generics for functions. However, we can also use generics with data structures.
For example, we will create a linked list containing values of any type. Meanwhile, we will write an Add
method to append a node:
type Node[T any] struct { // Use type parameter\nVal T\nnext *Node[T]\n}\nfunc (n *Node[T]) Add(next *Node[T]) { // Instantiate type receiver\nn.next = next\n}\n
We use type parameters to define T
and use both fields in Node
. Regarding the method, the receiver is instantiated. Indeed, because Node
is generic, it has to follow also the type parameter defined.
One last thing to note about type parameters: they can\u2019t be used on methods, only on functions. For example, the following method wouldn\u2019t compile:
type Foo struct {}\nfunc (Foo) bar[T any](t T) {}\n
./main.go:29:15: methods cannot have type parameters\n
Now, let\u2019s delve into concrete cases where we should and shouldn\u2019t use generics.
"},{"location":"9-generics/#common-uses-and-misuses","title":"Common uses and misuses","text":"So when are generics useful? Let\u2019s discuss a couple of common uses where generics are recommended:
func merge[T any](ch1, ch2 <-chan T) <-chan T {\n// ...\n}\n
sort
package contains functions to sort different slice types such as sort.Ints
or sort.Float64s
. Using type parameters, we can factor out the sorting behaviors that rely on three methods, Len
, Less
, and Swap
:type sliceFn[T any] struct { // Use type parameter\ns []T\ncompare func(T, T) bool // Compare two T elements\n}\nfunc (s sliceFn[T]) Len() int { return len(s.s) }\nfunc (s sliceFn[T]) Less(i, j int) bool { return s.compare(s.s[i], s.s[j]) }\nfunc (s sliceFn[T]) Swap(i, j int) { s.s[i], s.s[j] = s.s[j], s.s[i] }\n
Conversely, when is it recommended not to use generics?
io.Writer
and call the Write
method:func foo[T io.Writer](w T) {\nb := getBytes()\n_, _ = w.Write(b)\n}\n
Though generics can be very helpful in particular conditions, we should be cautious about when to use them and not use them.
In general, when we want to answer when not to use generics, we can find similarities with when not to use interfaces. Indeed, generics introduce a form of abstraction, and we have to remember that unnecessary abstractions introduce complexity.
Let\u2019s not pollute our code with needless abstractions, and let\u2019s focus on solving concrete problems for now. It means that we shouldn\u2019t use type parameters prematurely. Let\u2019s wait until we are about to write boilerplate code to consider using generics.
"},{"location":"98-profiling-execution-tracing/","title":"Not using Go diagnostics tooling","text":"Go offers a few excellent diagnostics tools to help us get insights into how an application performs. This post focuses on the most important ones: profiling and the execution tracer. Both tools are so important that they should be part of the core toolset of any Go developer who is interested in optimization. First, let\u2019s discuss profiling.
"},{"location":"98-profiling-execution-tracing/#profiling","title":"Profiling","text":"Profiling provides insights into the execution of an application. It allows us to resolve performance issues, detect contention, locate memory leaks, and more. These insights can be collected via several profiles:
CPU
\u2014 Determines where an application spends its timeGoroutine
\u2014 Reports the stack traces of the ongoing goroutinesHeap
\u2014 Reports heap memory allocation to monitor current memory usage and check for possible memory leaksMutex
\u2014 Reports lock contentions to see the behaviors of the mutexes used in our code and whether an application spends too much time in locking callsBlock
\u2014 Shows where goroutines block waiting on synchronization primitivesProfiling is achieved via instrumentation using a tool called a profiler, in Go: pprof
. First, let\u2019s understand how and when to enable pprof
; then, we discuss the most critical profile types.
There are several ways to enable pprof
. For example, we can use the net/http/pprof
package to serve the profiling data via HTTP:
package main\nimport (\n\"fmt\"\n\"log\"\n\"net/http\"\n_ \"net/http/pprof\" // Blank import to pprof\n)\nfunc main() {\n// Exposes an HTTP endpoint\nhttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\nfmt.Fprintf(w, \"\")\n})\nlog.Fatal(http.ListenAndServe(\":80\", nil))\n}\n
Importing net/http/pprof
leads to a side effect that allows us to reach the pprof URL: http://host/debug/pprof. Note that enabling pprof
is safe even in production (https://go.dev/doc/diagnostics#profiling). The profiles that impact performance, such as CPU profiling, aren\u2019t enabled by default, nor do they run continuously: they are activated only for a specific period.
Now that we have seen how to expose a pprof
endpoint, let\u2019s discuss the most common profiles.
The CPU profiler relies on the OS and signaling. When it is activated, the application asks the OS to interrupt it every 10 ms by default via a SIGPROF
signal. When the application receives a SIGPROF
, it suspends the current activity and transfers the execution to the profiler. The profiler collects data such as the current goroutine activity and aggregates execution statistics that we can retrieve. Then it stops, and the execution resumes until the next SIGPROF
.
We can access the /debug/pprof/profile endpoint to activate CPU profiling. Accessing this endpoint executes CPU profiling for 30 seconds by default. For 30 seconds, our application is interrupted every 10 ms. Note that we can change these two default values: we can use the seconds
parameter to pass to the endpoint how long the profiling should last (for example, /debug/pprof/profile?seconds=15), and we can change the interruption rate (even to less than 10 ms). But in most cases, 10 ms should be enough, and in decreasing this value (meaning increasing the rate), we should be careful not to harm performance. After 30 seconds, we download the results of the CPU profiler.
We can also enable the CPU profiler using the -cpuprofile
flag, such as when running a benchmark. For example, the following command produces the same type of file that can be downloaded via /debug/ pprof/profile.
$ go test -bench=. -cpuprofile profile.out\n
From this file, we can navigate to the results using go tool
:
$ go tool pprof -http=:8080 <file>\n
This command opens a web UI showing the call graph. The next figure shows an example taken from an application. The larger the arrow, the more it was a hot path. We can then navigate into this graph and get execution insights.
Figure 1: The call graph of an application during 30 seconds.
For example, the graph in the next figure tells us that during 30 seconds, 0.06 seconds were spent in the decode
method (*FetchResponse
receiver). Of these 0.06 seconds, 0.02 were spent in RecordBatch.decode
and 0.01 in makemap
(creating a map).
Figure 2: Example call graph.
We can also access this kind of information from the web UI with different representations. For example, the Top view sorts the functions per execution time, and Flame Graph visualizes the execution time hierarchy. The UI can even display the expensive parts of the source code line by line.
NoteWe can also delve into profiling data via a command line. However, we focus on the web UI in this post.
Thanks to this data, we can get a general idea of how an application behaves:
runtime.mallogc
can mean an excessive number of small heap allocations that we can try to minimize.syscall.Read
or syscall.Write
means the application spends a significant amount of time in Kernel mode. Working on I/O buffering may be an avenue for improvement.These are the kinds of insights we can get from the CPU profiler. It\u2019s valuable to understand the hottest code path and identify bottlenecks. But it won\u2019t determine more than the configured rate because the CPU profiler is executed at a fixed pace (by default, 10 ms). To get finer-grained insights, we should use tracing, which we discuss later in this post.
NoteWe can also attach labels to the different functions. For example, imagine a common function called from different clients. To track the time spent for both clients, we can use pprof.Labels
.
Heap profiling allows us to get statistics about the current heap usage. Like CPU profiling, heap profiling is sample-based. We can change this rate, but we shouldn\u2019t be too granular because the more we decrease the rate, the more effort heap profiling will require to collect data. By default, samples are profiled at one allocation for every 512 KB of heap allocation.
If we reach /debug/pprof/heap/, we get raw data that can be hard to read. However, we can download a heap profile using /debug/pprof/heap/?debug=0 and then open it with go tool
(the same command as in the previous section) to navigate into the data using the web UI.
The next figure shows an example of a heap graph. Calling the MetadataResponse.decode
method leads to allocating 1536 KB of heap data (which represents 6.32% of the total heap). However, 0 out of these 1536 KB were allocated by this function directly, so we need to inspect the second call. The TopicMetadata.decode
method allocated 512 KB out of the 1536 KB; the rest \u2014 1024 KB \u2014 were allocated in another method.
Figure 3: A heap graph.
This is how we can navigate the call chain to understand what part of an application is responsible for most of the heap allocations. We can also look at different sample types:
alloc_objects
\u2014 Total number of objects allocatedalloc_space
\u2014 Total amount of memory allocatedinuse_object
s \u2014 Number of objects allocated and not yet releasedinuse_space
\u2014 Amount of memory allocated and not yet releasedAnother very helpful capability with heap profiling is tracking memory leaks. With a GC-based language, the usual procedure is the following:
Forcing a GC before downloading data is a way to prevent false assumptions. For example, if we see a peak of retained objects without running a GC first, we cannot be sure whether it\u2019s a leak or objects that the next GC will collect.
Using pprof
, we can download a heap profile and force a GC in the meantime. The procedure in Go is the following:
$ go tool pprof -http=:8080 -diff_base <file2> <file1>\n
The next figure shows the kind of data we can access. For example, the amount of heap memory held by the newTopicProducer method (top left) has decreased (\u2013513 KB). In contrast, the amount held by updateMetadata (bottom right) has increased (+512 KB). Slow increases are normal. The second heap profile may have been calculated in the middle of a service call, for example. We can repeat this process or wait longer; the important part is to track steady increases in allocations of a specific object.
Figure 4: The differences between the two heap profiles. Note
Another type of profiling related to the heap is allocs
, which reports allocations. Heap profiling shows the current state of the heap memory. To get insights about past memory allocations since the application started, we can use allocations profiling. As discussed, because stack allocations are cheap, they aren\u2019t part of this profiling, which only focuses on the heap.
The goroutine
profile reports the stack trace of all the current goroutines in an application. We can download a file using /debug/pprof/goroutine/?debug=0 and use go tool again. The next figure shows the kind of information we can get.
Figure 5: Goroutine graph.
We can see the current state of the application and how many goroutines were created per function. In this case, withRecover
has created 296 ongoing goroutines (63%), and 29 were related to a call to responseFeeder
.
This kind of information is also beneficial if we suspect goroutine leaks. We can look at goroutine profiler data to know which part of a system is the suspect.
"},{"location":"98-profiling-execution-tracing/#block-profiling","title":"Block Profiling","text":"The block
profile reports where ongoing goroutines block waiting on synchronization primitives. Possibilities include
Block profiling also records the amount of time a goroutine has been waiting and is accessible via /debug/pprof/block. This profile can be extremely helpful if we suspect that performance is being harmed by blocking calls.
The block
profile isn\u2019t enabled by default: we have to call runtime.SetBlockProfileRate
to enable it. This function controls the fraction of goroutine blocking events that are reported. Once enabled, the profiler will keep collecting data in the background even if we don\u2019t call the /debug/pprof/block endpoint. Let\u2019s be cautious if we want to set a high rate so we don\u2019t harm performance.
If we face a deadlock or suspect that goroutines are in a blocked state, the full goroutine stack dump (/debug/pprof/goroutine/?debug=2) creates a dump of all the current goroutine stack traces. This can be helpful as a first analysis step. For example, the following dump shows a Sarama goroutine blocked for 1,420 minutes on a channel-receive operation:
goroutine 2494290 [chan receive, 1420 minutes]:\ngithub.com/Shopify/sarama.(*syncProducer).SendMessages(0xc00071a090,\n[CA]{0xc0009bb800, 0xfb, 0xfb})\n/app/vendor/github.com/Shopify/sarama/sync_producer.go:117 +0x149\n
"},{"location":"98-profiling-execution-tracing/#mutex-profiling","title":"Mutex Profiling","text":"The last profile type is related to blocking but only regarding mutexes. If we suspect that our application spends significant time waiting for locking mutexes, thus harming execution, we can use mutex profiling. It\u2019s accessible via /debug/pprof/mutex.
This profile works in a manner similar to that for blocking. It\u2019s disabled by default: we have to enable it using runtime.SetMutexProfileFraction
, which controls the fraction of mutex contention events reported.
Following are a few additional notes about profiling:
threadcreate
profile because it\u2019s been broken since 2013 (https://github.com/golang/go/issues/6104).pprof
is extensible, and we can create our own custom profiles using pprof.Profile
.We have seen the most important profiles that we can enable to help us understand how an application performs and possible avenues for optimization. In general, enabling pprof
is recommended, even in production, because in most cases it offers an excellent balance between its footprint and the amount of insight we can get from it. Some profiles, such as the CPU profile, lead to performance penalties but only during the time they are enabled.
Let\u2019s now look at the execution tracer.
"},{"location":"98-profiling-execution-tracing/#execution-tracer","title":"Execution Tracer","text":"The execution tracer is a tool that captures a wide range of runtime events with go tool
to make them available for visualization. It is helpful for the following:
Let\u2019s try it with an example given the Concurrency isn\u2019t Always Faster in Go section. We discussed two parallel versions of the merge sort algorithm. The issue with the first version was poor parallelization, leading to the creation of too many goroutines. Let\u2019s see how the tracer can help us in validating this statement.
We will write a benchmark for the first version and execute it with the -trace flag to enable the execution tracer:
$ go test -bench=. -v -trace=trace.out\n
Note We can also download a remote trace file using the /debug/pprof/ trace?debug=0 pprof endpoint.
This command creates a trace.out file that we can open using go tool:
$ go tool trace trace.out\n2021/11/26 21:36:03 Parsing trace...\n2021/11/26 21:36:31 Splitting trace...\n2021/11/26 21:37:00 Opening browser. Trace viewer is listening on\n http://127.0.0.1:54518\n
The web browser opens, and we can click View Trace to see all the traces during a specific timeframe, as shown in the next figure. This figure represents about 150 ms. We can see multiple helpful metrics, such as the goroutine count and the heap size. The heap size grows steadily until a GC is triggered. We can also observe the activity of the Go application per CPU core. The timeframe starts with user-level code; then a \u201cstop the world\u201d is executed, which occupies the four CPU cores for approximately 40 ms.
Figure 6: Showing goroutine activity and runtime events such as a GC phase.
Regarding concurrency, we can see that this version uses all the available CPU cores on the machine. However, the next figure zooms in on a portion of 1 ms. Each bar corresponds to a single goroutine execution. Having too many small bars doesn\u2019t look right: it means execution that is poorly parallelized.
Figure 7: Too many small bars mean poorly parallelized execution.
The next figure zooms even closer to see how these goroutines are orchestrated. Roughly 50% of the CPU time isn\u2019t spent executing application code. The white spaces represent the time the Go runtime takes to spin up and orchestrate new goroutines.
Figure 8: About 50% of CPU time is spent handling goroutine switches.
Let\u2019s compare this with the second parallel implementation, which was about an order of magnitude faster. The next figure again zooms to a 1 ms timeframe.
Figure 9: The number of white spaces has been significantly reduced, proving that the CPU is more fully occupied.
Each goroutine takes more time to execute, and the number of white spaces has been significantly reduced. Hence, the CPU is much more occupied executing application code than it was in the first version. Each millisecond of CPU time is spent more efficiently, explaining the benchmark differences.
Note that the granularity of the traces is per goroutine, not per function like CPU profiling. However, it\u2019s possible to define user-level tasks to get insights per function or group of functions using the runtime/trace
package.
For example, imagine a function that computes a Fibonacci number and then writes it to a global variable using atomic. We can define two different tasks:
var v int64\n// Creates a fibonacci task\nctx, fibTask := trace.NewTask(context.Background(), \"fibonacci\")\ntrace.WithRegion(ctx, \"main\", func() {\nv = fibonacci(10)\n})\nfibTask.End()\n// Creates a store task\nctx, fibStore := trace.NewTask(ctx, \"store\")\ntrace.WithRegion(ctx, \"main\", func() {\natomic.StoreInt64(&result, v)\n})\nfibStore.End()\n
Using go tool
, we can get more precise information about how these two tasks perform. In the previous trace UI, we can see the boundaries for each task per goroutine. In User-Defined Tasks, we can follow the duration distribution:
Figure 10: Distribution of user-level tasks.
We see that in most cases, the fibonacci
task is executed in less than 15 microseconds, whereas the store
task takes less than 6309 nanoseconds.
In the previous section, we discussed the kinds of information we can get from CPU profiling. What are the main differences compared to the data we can get from user-level traces?
runtime/trace
package)In summary, the execution tracer is a powerful tool for understanding how an application performs. As we have seen with the merge sort example, we can identify poorly parallelized execution. However, the tracer\u2019s granularity remains per goroutine unless we manually use runtime/trace
compared to a CPU profile, for example. We can use both profiling and the execution tracer to get the most out of the standard Go diagnostics tools when optimizing an application.
Community space of \ud83d\udcd6 100 Go Mistakes and How to Avoid Them, published by Manning in 2022.
"},{"location":"book/#description","title":"Description","text":"If you're a Go developer looking to improve your skills, this book is for you. With a focus on practical examples, 100 Go Mistakes and How to Avoid Them covers a wide range of topics from concurrency and error handling to testing and code organization. You'll learn to write more idiomatic, efficient, and maintainable code and become a proficient Go developer.
Read a summary of the 100 mistakes here.
"},{"location":"book/#quotes-and-ratings","title":"Quotes and Ratings","text":"Krystian (Goodreads user)
This is an exceptional book. Usually, if a book contains either high-quality explanations or is written succinctly, I consider myself lucky to have found it. This one combines these two characteristics, which is super rare. It's another Go book for me and I still had quite a lot of \"a-ha!\" moments while reading it, and all of that without the unnecessary fluff, just straight to the point.
Akash Chetty
The book is completely exceptional, especially the examples carved out for each topic are really great. There is one topic that I struggled to understand is Concurrency but the way it is explained in this book is truly an art of genius.
Neeraj Shah
This should be the required reading for all Golang developers before they touch code in Production... It's the Golang equivalent of the legendary 'Effective Java' by Joshua Bloch.
Anupam Sengupta
Not having this will be the 101st mistake a Go programmer could make.
Manning, Goodreads, and Amazon reviews: 4.7/5 avg rating"},{"location":"book/#where-to-buy","title":"Where to Buy?","text":"
100 Go Mistakes and How to Avoid Them (\ud83c\uddec\ud83c\udde7 edition: paper, digital, or audiobook)
au35har
)Go\u8a00\u8a9e100Tips \u958b\u767a\u8005\u306b\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u3078\u306e\u5bfe\u51e6\u6cd5 (\ud83c\uddef\ud83c\uddf5 edition: paper or digital)
English and Japanese covers"},{"location":"book/#about-the-author","title":"About the Author","text":"
Teiva Harsanyi is a senior software engineer at Google. He has worked in various domains, including insurance, transportation, and safety-critical industries like air traffic management. He is passionate about Go and how to design and implement reliable systems.
"},{"location":"chapter-1/","title":"Go: Simple to learn but hard to master","text":"This chapter covers
Making mistakes is part of everyone\u2019s life. As Albert Einstein once said,
Albert Einstein
A person who never made a mistake never tried anything new.
What matters in the end isn\u2019t the number of mistakes we make, but our capacity to learn from them. This assertion also applies to programming. The seniority we acquire in a language isn\u2019t a magical process; it involves making many mistakes and learning from them. The purpose of this book is centered around this idea. It will help you, the reader, become a more proficient Go developer by looking at and learning from 100 common mistakes people make in many areas of the language.
This chapter presents a quick refresher as to why Go has become mainstream over the years. We\u2019ll discuss why, despite Go being considered simple to learn, mastering its nuances can be challenging. Finally, we\u2019ll introduce the concepts this book covers.
"},{"location":"chapter-1/#go-outline","title":"Go outline","text":"If you are reading this book, it\u2019s likely that you\u2019re already sold on Go. Therefore, this section provides a brief reminder about what makes Go such a powerful language.
Software engineering has evolved considerably during the past decades. Most modern systems are no longer written by a single person but by teams consisting of multiple programmers\u2014sometimes even hundreds, if not thousands. Nowadays, code must be readable, expressive, and maintainable to guarantee a system\u2019s durability over the years. Meanwhile, in our fast-moving world, maximizing agility and reducing the time to market is critical for most organizations. Programming should also follow this trend, and companies strive to ensure that software engineers are as productive as possible when reading, writing, and maintaining code.
In response to these challenges, Google created the Go programming language in 2007. Since then, many organizations have adopted the language to support various use cases: APIs, automation, databases, CLIs (command-line interfaces), and so on. Many today consider Go the language of the cloud.
Feature-wise, Go has no type inheritance, no exceptions, no macros, no partial functions, no support for lazy variable evaluation or immutability, no operator overloading, no pattern matching, and on and on. Why are these features missing from the language? The official Go FAQ gives us some insight:
Go FAQ
Why does Go not have feature X? Your favorite feature may be missing because it doesn\u2019t fit, because it affects compilation speed or clarity of design, or because it would make the fundamental system model too difficult.
Judging the quality of a programming language via its number of features is probably not an accurate metric. At least, it\u2019s not an objective of Go. Instead, Go utilizes a few essential characteristics when adopting a language at scale for an organization. These include the following:
Go was built from the ground up with solid features such as outstanding concurrency primitives with goroutines and channels. There\u2019s not a strong need to rely on external libraries to build efficient concurrent applications. Observing how important concurrency is these days also demonstrates why Go is such a suitable language for the present and probably for the foreseeable future.
Some also consider Go a simple language. And, in a sense, this isn\u2019t necessarily wrong. For example, a newcomer can learn the language\u2019s main features in less than a day. So why read a book centered on the concept of mistakes if Go is simple?
"},{"location":"chapter-1/#simple-doesnt-mean-easy","title":"Simple doesn\u2019t mean easy","text":"There is a subtle difference between simple and easy. Simple, applied to a technology, means not complicated to learn or understand. However, easy means that we can achieve anything without much effort. Go is simple to learn but not necessarily easy to master.
Let\u2019s take concurrency, for example. In 2019, a study focusing on concurrency bugs was published: Understanding Real-World Concurrency Bugs in Go. This study was the first systematic analysis of concurrency bugs. It focused on multiple popular Go repositories such as Docker, gRPC, and Kubernetes. One of the most important takeaways from this study is that most of the blocking bugs are caused by inaccurate use of the message-passing paradigm via channels, despite the belief that message passing is easier to handle and less error-prone than sharing memory.
What should be an appropriate reaction to such a takeaway? Should we consider that the language designers were wrong about message passing? Should we reconsider how we deal with concurrency in our project? Of course not.
It\u2019s not a question of confronting message passing versus sharing memory and determining the winner. However, it\u2019s up to us as Go developers to thoroughly understand how to use concurrency, its implications on modern processors, when to favor one approach over the other, and how to avoid common traps. This example highlights that although a concept such as channels and goroutines can be simple to learn, it isn\u2019t an easy topic in practice.
This leitmotif\u2014simple doesn\u2019t mean easy\u2014can be generalized to many aspects of Go, not only concurrency. Hence, to be proficient Go developers, we must have a thorough understanding of many aspects of the language, which requires time, effort, and mistakes.
This book aims to help accelerate our journey toward proficiency by delving into 100 Go mistakes.
"},{"location":"chapter-1/#100-go-mistakes","title":"100 Go mistakes","text":"Why should we read a book about common Go mistakes? Why not deepen our knowledge with an ordinary book that would dig into different topics?
In a 2011 article, neuroscientists proved that the best time for brain growth is when we\u2019re facing mistakes. 1 Haven\u2019t we all experienced the process of learning from a mistake and recalling that occasion after months or even years, when some context related to it? As presented in another article, by Janet Metcalfe, this happens because mistakes have a facilitative effect. 2 The main idea is that we can remember not only the error but also the context surrounding the mistake. This is one of the reasons why learning from mistakes is so efficient.
To strengthen this facilitative effect, this book accompanies each mistake as much as possible with real-world examples. This book isn\u2019t only about theory; it also helps us get better at avoiding mistakes and making more well-informed, conscious decisions because we now understand the rationale behind them.
Unknown
Tell me and I forget. Teach me and I remember. Involve me and I learn.
This book presents seven main categories of mistakes. Overall, the mistakes can be classified as
We introduce each mistake category next.
"},{"location":"chapter-1/#bugs","title":"Bugs","text":"The first type of mistake and probably the most obvious is software bugs. In 2020, a study conducted by Synopsys estimated the cost of software bugs in the U.S. alone to be over $2 trillion. 3
Furthermore, bugs can also lead to tragic impacts. We can, for example, mention cases such as Therac-25, a radiation therapy machine produced by Atomic Energy of Canada Limited (AECL). Because of a race condition, the machine gave its patients radiation doses that were hundreds of times greater than expected, leading to the death of three patients. Hence, software bugs aren\u2019t only about money. As developers, we should remember how impactful our jobs are.
This book covers plenty of cases that could lead to various software bugs, including data races, leaks, logic errors, and other defects. Although accurate tests should be a way to discover such bugs as early as possible, we may sometimes miss cases because of different factors such as time constraints or complexity. Therefore, as a Go developer, it\u2019s essential to make sure we avoid common bugs.
"},{"location":"chapter-1/#needless-complexity","title":"Needless complexity","text":"The next category of mistakes is related to unnecessary complexity. A significant part of software complexity comes from the fact that, as developers, we strive to think about imaginary futures. Instead of solving concrete problems right now, it can be tempting to build evolutionary software that could tackle whatever future use case arises. However, this leads to more drawbacks than benefits in most cases because it can make a codebase more complex to understand and reason about.
Getting back to Go, we can think of plenty of use cases where developers might be tempted to design abstractions for future needs, such as interfaces or generics. This book discusses topics where we should remain careful not to harm a codebase with needless complexity.
"},{"location":"chapter-1/#weaker-readability","title":"Weaker readability","text":"Another kind of mistake is to weaken readability. As Robert C. Martin wrote in his book Clean Code: A Handbook of Agile Software Craftsmanship, the ratio of time spent reading versus writing is well over 10 to 1. Most of us started to program on solo projects where readability wasn\u2019t that important. However, today\u2019s software engineering is programming with a time dimension: making sure we can still work with and maintain an application months, years, or perhaps even decades later.
When programming in Go, we can make many mistakes that can harm readability. These mistakes may include nested code, data type representations, or not using named result parameters in some cases. Throughout this book, we will learn how to write readable code and care for future readers (including our future selves).
"},{"location":"chapter-1/#suboptimal-or-unidiomatic-organization","title":"Suboptimal or unidiomatic organization","text":"Be it while working on a new project or because we acquire inaccurate reflexes, another type of mistake is organizing our code and a project suboptimally and unidiomatically. Such issues can make a project harder to reason about and maintain. This book covers some of these common mistakes in Go. For example, we\u2019ll look at how to structure a project and deal with utility packages or init functions. All in all, looking at these mistakes should help us organize our code and projects more efficiently and idiomatically.
"},{"location":"chapter-1/#lack-of-api-convenience","title":"Lack of API convenience","text":"Making common mistakes that weaken how convenient an API is for our clients is another type of mistake. If an API isn\u2019t user-friendly, it will be less expressive and, hence, harder to understand and more error-prone.
We can think about many situations such as overusing any types, using the wrong creational pattern to deal with options, or blindly applying standard practices from object-oriented programming that affect the usability of our APIs. This book covers common mistakes that prevent us from exposing convenient APIs for our users.
"},{"location":"chapter-1/#under-optimized-code","title":"Under-optimized code","text":"Under-optimized code is another type of mistake made by developers. It can happen for various reasons, such as not understanding language features or even a lack of fundamental knowledge. Performance is one of the most obvious impacts of this mistake, but not the only one.
We can think about optimizing code for other goals, such as accuracy. For example, this book provides some common techniques to ensure that floating-point operations are accurate. Meanwhile, we will cover plenty of cases that can negatively impact performance code because of poorly parallelized executions, not knowing how to reduce allocations, or the impacts of data alignment, for example. We will tackle optimization via different prisms.
"},{"location":"chapter-1/#lack-of-productivity","title":"Lack of productivity","text":"In most cases, what\u2019s the best language we can choose when working on a new project? The one we\u2019re the most productive with. Being comfortable with how a language works and exploiting it to get the best out of it is crucial to reach proficiency.
In this book, we will cover many cases and concrete examples that will help us to be more productive while working in Go. For instance, we\u2019ll look at writing efficient tests to ensure that our code works, relying on the standard library to be more effective, and getting the best out of the profiling tools and linters. Now, it\u2019s time to delve into those 100 common Go mistakes.
"},{"location":"chapter-1/#summary","title":"Summary","text":"J. S. Moser, H. S. Schroder, et al., \u201cMind Your Errors: Evidence for a Neural Mechanism Linking Growth Mindset to Adaptive Posterror Adjustments,\u201d Psychological Science, vol. 22, no. 12, pp. 1484\u20131489, Dec. 2011.\u00a0\u21a9
J. Metcalfe, \u201cLearning from Errors,\u201d Annual Review of Psychology, vol. 68, pp. 465\u2013489, Jan. 2017.\u00a0\u21a9
Synopsys, \u201cThe Cost of Poor Software Quality in the US: A 2020 Report.\u201d 2020. https://news.synopsys.com/2021-01-06-Synopsys-Sponsored-CISQ-Research-Estimates-Cost-of-Poor-Software-Quality-in-the-US-2-08-Trillion-in-2020.\u00a0\u21a9
\u3053\u306e\u30da\u30fc\u30b8\u306f\u300e100 Go Mistakes\u300f\u306e\u5185\u5bb9\u3092\u307e\u3068\u3081\u305f\u3082\u306e\u3067\u3059\u3002\u4e00\u65b9\u3067\u3001\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u306b\u958b\u304b\u308c\u305f\u30da\u30fc\u30b8\u3067\u3082\u3042\u308a\u307e\u3059\u3002\u300c\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u300d\u304c\u65b0\u305f\u306b\u8ffd\u52a0\u3055\u308c\u308b\u3079\u304d\u3060\u3068\u304a\u8003\u3048\u3067\u3057\u305f\u3089 community mistake issue \u3092\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u6ce8\u610f\u73fe\u5728\u3001\u5927\u5e45\u306b\u591a\u304f\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u8ffd\u52a0\u3057\u3066\u5f37\u5316\u3057\u3066\u3044\u308b\u65b0\u3057\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u95b2\u89a7\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306f\u307e\u3060\u958b\u767a\u4e2d\u3067\u3059\u3002\u554f\u984c\u3092\u898b\u3064\u3051\u305f\u5834\u5408\u306f\u3069\u3046\u305e\u6c17\u8efd\u306bPR\u3092\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002
"},{"location":"ja/#_1","title":"\u30b3\u30fc\u30c9\u3068\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u69cb\u6210","text":""},{"location":"ja/#1","title":"\u610f\u56f3\u7684\u3067\u306a\u3044\u5909\u6570\u306e\u30b7\u30e3\u30c9\u30fc\u30a4\u30f3\u30b0 (#1)","text":"\u8981\u7d04\u5909\u6570\u306e\u30b7\u30e3\u30c9\u30fc\u30a4\u30f3\u30b0\u3092\u907f\u3051\u308b\u3053\u3068\u306f\u3001\u8aa4\u3063\u305f\u5909\u6570\u306e\u53c2\u7167\u3084\u8aad\u307f\u624b\u306e\u6df7\u4e71\u3092\u9632\u304e\u307e\u3059\u3002
\u5909\u6570\u306e\u30b7\u30e3\u30c9\u30fc\u30a4\u30f3\u30b0\u306f\u3001\u5909\u6570\u540d\u304c\u30d6\u30ed\u30c3\u30af\u5185\u3067\u518d\u5ba3\u8a00\u3055\u308c\u308b\u3053\u3068\u3067\u751f\u3058\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u9593\u9055\u3044\u3092\u5f15\u304d\u8d77\u3053\u3057\u3084\u3059\u304f\u3057\u307e\u3059\u3002\u5909\u6570\u306e\u30b7\u30e3\u30c9\u30fc\u30a4\u30f3\u30b0\u3092\u7981\u6b62\u3059\u308b\u304b\u3069\u3046\u304b\u306f\u500b\u4eba\u306e\u597d\u307f\u306b\u3088\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u30a8\u30e9\u30fc\u306b\u5bfe\u3057\u3066 err
\u306e\u3088\u3046\u306a\u65e2\u5b58\u306e\u5909\u6570\u540d\u3092\u518d\u5229\u7528\u3059\u308b\u3068\u4fbf\u5229\u306a\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3068\u306f\u3044\u3048\u3001\u30b3\u30fc\u30c9\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u305f\u3082\u306e\u306e\u3001\u5024\u3092\u53d7\u3051\u53d6\u3063\u305f\u5909\u6570\u304c\u4e88\u671f\u3057\u305f\u3082\u306e\u3067\u306f\u306a\u3044\u3068\u3044\u3046\u30b7\u30ca\u30ea\u30aa\u306b\u76f4\u9762\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u539f\u5247\u3068\u3057\u3066\u5f15\u304d\u7d9a\u304d\u6ce8\u610f\u3092\u6255\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#2","title":"\u4e0d\u5fc5\u8981\u306b\u30cd\u30b9\u30c8\u3055\u308c\u305f\u30b3\u30fc\u30c9 (#2)","text":"\u8981\u7d04\u30cd\u30b9\u30c8\u304c\u6df1\u304f\u306a\u3089\u306a\u3044\u3088\u3046\u306b\u3057\u3001\u30cf\u30c3\u30d4\u30fc\u30d1\u30b9\u3092\u5de6\u5074\u306b\u63c3\u3048\u308b\u3053\u3068\u3067\u30e1\u30f3\u30bf\u30eb\u30b3\u30fc\u30c9\u30e2\u30c7\u30eb\u3092\u69cb\u7bc9\u3059\u308b\u3053\u3068\u304c\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002
\u4e00\u822c\u7684\u306b\u3001\u95a2\u6570\u304c\u3088\u308a\u6df1\u3044\u30cd\u30b9\u30c8\u3092\u8981\u6c42\u3059\u308b\u307b\u3069\u3001\u8aad\u3093\u3067\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u3088\u308a\u8907\u96d1\u306b\u306a\u308a\u307e\u3059\u3002\u79c1\u305f\u3061\u306e\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3092\u6700\u9069\u5316\u3059\u308b\u305f\u3081\u306b\u3001\u3053\u306e\u30eb\u30fc\u30eb\u306e\u9069\u7528\u65b9\u6cd5\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002
if
\u30d6\u30ed\u30c3\u30af\u304c\u8fd4\u3055\u308c\u308b\u3068\u304d\u3001\u3059\u3079\u3066\u306e\u5834\u5408\u306b\u304a\u3044\u3066 else
\u30d6\u30ed\u30c3\u30af\u3092\u7701\u7565\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 \u305f\u3068\u3048\u3070\u3001\u6b21\u306e\u3088\u3046\u306b\u66f8\u304f\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002if foo() {\n// ...\nreturn true\n} else {\n// ...\n}\n
\u4ee3\u308f\u308a\u306b\u3001\u6b21\u306e\u3088\u3046\u306b else
\u30d6\u30ed\u30c3\u30af\u3092\u7701\u7565\u3057\u307e\u3059\u3002
if foo() {\n// ...\nreturn true\n}\n// ...\n
if s != \"\" {\n// ...\n} else {\nreturn errors.New(\"empty string\")\n}\n
\u3053\u3053\u3067\u306f\u3001\u7a7a\u306e s
\u304c\u30ce\u30f3\u30cf\u30c3\u30d4\u30fc\u30d1\u30b9\u3092\u8868\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6b21\u306e\u3088\u3046\u306b\u6761\u4ef6\u3092\u3072\u3063\u304f\u308a\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
if s == \"\" {\nreturn errors.New(\"empty string\")\n}\n// ...\n
\u8aad\u307f\u3084\u3059\u3044\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u306f\u3001\u3059\u3079\u3066\u306e\u958b\u767a\u8005\u306b\u3068\u3063\u3066\u91cd\u8981\u306a\u8ab2\u984c\u3067\u3059\u3002\u30cd\u30b9\u30c8\u3055\u308c\u305f\u30d6\u30ed\u30c3\u30af\u306e\u6570\u3092\u6e1b\u3089\u3059\u3088\u3046\u52aa\u3081\u3001\u30cf\u30c3\u30d4\u30fc\u30d1\u30b9\u3092\u5de6\u5074\u306b\u63c3\u3048\u3001\u3067\u304d\u308b\u3060\u3051\u65e9\u304f\u623b\u308b\u3053\u3068\u304c\u3001\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u5177\u4f53\u7684\u306a\u624b\u6bb5\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#init-3","title":"init\u95a2\u6570\u306e\u8aa4\u7528 (#3)","text":"\u8981\u7d04\u5909\u6570\u3092\u521d\u671f\u5316\u3059\u308b\u3068\u304d\u306f\u3001init\u95a2\u6570\u306e\u30a8\u30e9\u30fc\u51e6\u7406\u304c\u5236\u9650\u3055\u308c\u3066\u304a\u308a\u3001\u72b6\u614b\u306e\u51e6\u7406\u3068\u30c6\u30b9\u30c8\u304c\u3088\u308a\u8907\u96d1\u306b\u306a\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u521d\u671f\u5316\u306f\u7279\u5b9a\u306e\u95a2\u6570\u3068\u3057\u3066\u51e6\u7406\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002
init\u95a2\u6570\u306f\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u72b6\u614b\u3092\u521d\u671f\u5316\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b\u95a2\u6570\u3067\u3059\u3002\u5f15\u6570\u3092\u53d6\u3089\u305a\u3001\u7d50\u679c\u3082\u8fd4\u3057\u307e\u305b\u3093\uff08 func()
\u95a2\u6570\uff09\u3002\u30d1\u30c3\u30b1\u30fc\u30b8\u304c\u521d\u671f\u5316\u3055\u308c\u308b\u3068\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u5185\u306e\u3059\u3079\u3066\u306e\u5b9a\u6570\u304a\u3088\u3073\u5909\u6570\u306e\u5ba3\u8a00\u304c\u8a55\u4fa1\u3055\u308c\u307e\u3059\u3002\u6b21\u306b\u3001init\u95a2\u6570\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002
init\u95a2\u6570\u306f\u3044\u304f\u3064\u304b\u306e\u554f\u984c\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
init\u95a2\u6570\u306b\u306f\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u9759\u7684\u69cb\u6210\u306e\u5b9a\u7fa9\u306a\u3069\u3001\u72b6\u6cc1\u306b\u3088\u3063\u3066\u306f\u5f79\u7acb\u3064\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u308c\u4ee5\u5916\u306e\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u521d\u671f\u5316\u51e6\u7406\u306f\u305d\u306e\u305f\u3081\u3060\u3051\u306b\u5b58\u5728\u3059\u308b\u95a2\u6570\u3092\u901a\u3058\u3066\u884c\u308f\u308c\u308b\u3079\u304d\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#4","title":"\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u306e\u4e71\u7528 (#4)","text":"\u8981\u7d04Go\u8a00\u8a9e\u3067\u306f\u3001\u6163\u7528\u7684\u306b\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u306e\u4f7f\u7528\u3092\u5f37\u5236\u3059\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u5b9f\u5229\u3092\u91cd\u8996\u3057\u3001\u52b9\u7387\u6027\u3068\u7279\u5b9a\u306e\u6163\u7fd2\u306b\u5f93\u3046\u3053\u3068\u3068\u306e\u9593\u306e\u9069\u5207\u306a\u30d0\u30e9\u30f3\u30b9\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3001\u9032\u3080\u3079\u304d\u9053\u3067\u3042\u308b\u306f\u305a\u3067\u3059\u3002
\u30c7\u30fc\u30bf\u306e\u30ab\u30d7\u30bb\u30eb\u5316\u3068\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5024\u307e\u305f\u306f\u72b6\u614b\u3092\u96a0\u3059\u3053\u3068\u3092\u6307\u3057\u307e\u3059\u3002\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u306f\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u4e0a\u306b\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3055\u308c\u305f\u30e1\u30bd\u30c3\u30c9\u3092\u63d0\u4f9b\u3059\u308b\u3053\u3068\u3067\u30ab\u30d7\u30bb\u30eb\u5316\u3092\u53ef\u80fd\u306b\u3059\u308b\u624b\u6bb5\u3067\u3059\u3002
Go\u8a00\u8a9e\u3067\u306f\u3001\u4e00\u90e8\u306e\u8a00\u8a9e\u3067\u898b\u3089\u308c\u308b\u3088\u3046\u306a\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u306e\u81ea\u52d5\u30b5\u30dd\u30fc\u30c8\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u307e\u305f\u3001\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u3092\u4f7f\u7528\u3057\u3066\u69cb\u9020\u4f53\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u306f\u5fc5\u9808\u3067\u3082\u6163\u7528\u7684\u3067\u3082\u3042\u308a\u307e\u305b\u3093\u3002\u5024\u3092\u3082\u305f\u3089\u3055\u306a\u3044\u69cb\u9020\u4f53\u306e\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u3067\u30b3\u30fc\u30c9\u3092\u57cb\u3081\u308b\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u5b9f\u5229\u3092\u91cd\u8996\u3057\u3001\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30d1\u30e9\u30c0\u30a4\u30e0\u3067\u6642\u306b\u306f\u8b70\u8ad6\u306e\u4f59\u5730\u304c\u306a\u3044\u3068\u8003\u3048\u3089\u308c\u3066\u3044\u308b\u6163\u7fd2\u306b\u5f93\u3046\u3053\u3068\u3068\u3001\u52b9\u7387\u6027\u3068\u306e\u9593\u306e\u9069\u5207\u306a\u30d0\u30e9\u30f3\u30b9\u3092\u898b\u3064\u3051\u308b\u3088\u3046\u52aa\u3081\u308b\u3079\u304d\u3067\u3059\u3002
Go\u8a00\u8a9e\u306f\u3001\u30b7\u30f3\u30d7\u30eb\u3055\u3092\u542b\u3080\u591a\u304f\u306e\u7279\u6027\u3092\u8003\u616e\u3057\u3066\u8a2d\u8a08\u3055\u308c\u305f\u72ec\u81ea\u306e\u8a00\u8a9e\u3067\u3042\u308b\u3053\u3068\u3092\u5fd8\u308c\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u305f\u3060\u3057\u3001\u30b2\u30c3\u30bf\u30fc\u3068\u30bb\u30c3\u30bf\u30fc\u306e\u5fc5\u8981\u6027\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u3001\u307e\u305f\u306f\u524d\u8ff0\u306e\u3088\u3046\u306b\u3001\u524d\u65b9\u4e92\u63db\u6027\u3092\u4fdd\u8a3c\u3057\u306a\u304c\u3089\u5c06\u6765\u306e\u5fc5\u8981\u6027\u304c\u4e88\u6e2c\u3055\u308c\u308b\u5834\u5408\u306f\u3001\u305d\u308c\u3089\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u306b\u554f\u984c\u306f\u3042\u308a\u307e\u305b\u3093\u3002
"},{"location":"ja/#5","title":"\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u6c5a\u67d3 (#5)","text":"\u8981\u7d04\u62bd\u8c61\u5316\u306f\u4f5c\u6210\u3055\u308c\u308b\u3079\u304d\u3082\u306e\u3067\u306f\u306a\u304f\u3001\u767a\u898b\u3055\u308c\u308b\u3079\u304d\u3082\u306e\u3067\u3059\u3002\u4e0d\u5fc5\u8981\u306a\u8907\u96d1\u3055\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306f\u3001\u5fc5\u8981\u306b\u306a\u308b\u3068\u4e88\u6e2c\u3057\u305f\u3068\u304d\u3067\u306f\u306a\u304f\u3001\u5fc5\u8981\u306b\u306a\u3063\u305f\u3068\u304d\u306b\u4f5c\u6210\u3059\u308b\u304b\u3001\u5c11\u306a\u304f\u3068\u3082\u62bd\u8c61\u5316\u304c\u6709\u52b9\u3067\u3042\u308b\u3053\u3068\u3092\u8a3c\u660e\u3067\u304d\u308b\u5834\u5408\u306b\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u52d5\u4f5c\u3092\u6307\u5b9a\u3059\u308b\u65b9\u6cd5\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u8907\u6570\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5b9f\u88c5\u3067\u304d\u308b\u5171\u901a\u9805\u3092\u62bd\u51fa\u3059\u308b\u305f\u3081\u306b\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002Go\u8a00\u8a9e\u306e\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u304c\u5927\u304d\u304f\u7570\u306a\u308b\u306e\u306f\u3001\u6697\u9ed9\u7684\u306b\u6e80\u305f\u3055\u308c\u308b\u3053\u3068\u3067\u3059\u3002\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 X
\u304c\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9 Y
\u3092\u5b9f\u88c5\u3057\u3066\u3044\u308b\u3053\u3068\u3092\u793a\u3059 implements
\u306e\u3088\u3046\u306a\u660e\u793a\u7684\u306a\u30ad\u30fc\u30ef\u30fc\u30c9\u306f\u3042\u308a\u307e\u305b\u3093\u3002
\u4e00\u822c\u306b\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u304c\u4fa1\u5024\u3092\u3082\u305f\u3089\u3059\u3068\u8003\u3048\u3089\u308c\u308b\u4e3b\u8981\u306a\u4f7f\u7528\u4f8b\u306f\uff13\u3064\u3042\u308a\u307e\u3059\u3002\u305d\u308c\u306f\u3001\u5171\u901a\u306e\u52d5\u4f5c\u3092\u9664\u5916\u3059\u308b\u3001\u4f55\u3089\u304b\u306e\u5206\u96e2\u3092\u4f5c\u6210\u3059\u308b\u3001\u304a\u3088\u3073\u578b\u3092\u7279\u5b9a\u306e\u52d5\u4f5c\u306b\u5236\u9650\u3059\u308b\u3068\u3044\u3046\u3082\u306e\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u3053\u306e\u30ea\u30b9\u30c8\u306f\u3059\u3079\u3066\u3092\u7db2\u7f85\u3057\u3066\u3044\u308b\u308f\u3051\u3067\u306f\u306a\u304f\u3001\u76f4\u9762\u3059\u308b\u72b6\u6cc1\u306b\u3088\u3063\u3066\u3082\u7570\u306a\u308a\u307e\u3059\u3002
\u591a\u304f\u306e\u5834\u5408\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306f\u62bd\u8c61\u5316\u3059\u308b\u305f\u3081\u306b\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u62bd\u8c61\u5316\u3059\u308b\u3068\u304d\u306e\u4e3b\u306a\u6ce8\u610f\u70b9\u306f\u3001\u62bd\u8c61\u5316\u306f\u4f5c\u6210\u3055\u308c\u308b\u3079\u304d\u3067\u306f\u306a\u304f\u3001\u767a\u898b\u3055\u308c\u308b\u3079\u304d\u3067\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304f\u3053\u3068\u3067\u3059\u3002\u3059\u306a\u308f\u3061\u3001\u305d\u3046\u3059\u308b\u76f4\u63a5\u306e\u7406\u7531\u304c\u306a\u3044\u9650\u308a\u3001\u30b3\u30fc\u30c9\u5185\u3067\u62bd\u8c61\u5316\u3059\u3079\u304d\u3067\u306f\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u4f7f\u3063\u3066\u8a2d\u8a08\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u5177\u4f53\u7684\u306a\u30cb\u30fc\u30ba\u3092\u5f85\u3064\u3079\u304d\u3067\u3059\u3002\u5225\u306e\u8a00\u3044\u65b9\u3092\u3059\u308c\u3070\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306f\u5fc5\u8981\u306b\u306a\u308b\u3068\u4e88\u6e2c\u3057\u305f\u3068\u304d\u3067\u306f\u306a\u304f\u3001\u5fc5\u8981\u306b\u306a\u3063\u305f\u3068\u304d\u306b\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 \u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306e\u904e\u5ea6\u306a\u4f7f\u7528\u3092\u3057\u305f\u5834\u5408\u306e\u4e3b\u306a\u554f\u984c\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\u3002\u7b54\u3048\u306f\u3001\u30b3\u30fc\u30c9\u30d5\u30ed\u30fc\u304c\u3088\u308a\u8907\u96d1\u306b\u306a\u308b\u3053\u3068\u3067\u3059\u3002\u5f79\u306b\u7acb\u305f\u306a\u3044\u9593\u63a5\u53c2\u7167\u3092\u8ffd\u52a0\u3057\u3066\u3082\u4f55\u306e\u4fa1\u5024\u3082\u3042\u308a\u307e\u305b\u3093\u3002\u305d\u308c\u306f\u4fa1\u5024\u306e\u306a\u3044\u62bd\u8c61\u5316\u3092\u3059\u308b\u3053\u3068\u3067\u3001\u30b3\u30fc\u30c9\u3092\u8aad\u307f\u3001\u7406\u89e3\u3057\u3001\u63a8\u8ad6\u3059\u308b\u3053\u3068\u3092\u3055\u3089\u306b\u56f0\u96e3\u306b\u3057\u307e\u3059\u3002\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u8ffd\u52a0\u3059\u308b\u660e\u78ba\u306a\u7406\u7531\u304c\u306a\u304f\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306b\u3088\u3063\u3066\u30b3\u30fc\u30c9\u304c\u3069\u306e\u3088\u3046\u306b\u6539\u5584\u3055\u308c\u308b\u304b\u304c\u4e0d\u660e\u77ad\u306a\u5834\u5408\u306f\u3001\u305d\u306e\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306e\u76ee\u7684\u306b\u7570\u8b70\u3092\u5531\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5b9f\u88c5\u3092\u76f4\u63a5\u547c\u3073\u51fa\u3059\u306e\u3082\u4e00\u3064\u306e\u624b\u3067\u3059\u3002
\u30b3\u30fc\u30c9\u5185\u3067\u62bd\u8c61\u5316\u3059\u308b\u3068\u304d\u306f\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\uff08\u62bd\u8c61\u5316\u306f\u4f5c\u6210\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u767a\u898b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff09\u3002\u5f8c\u3067\u5fc5\u8981\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3082\u306e\u3092\u8003\u616e\u3057\u3001\u5b8c\u74a7\u306a\u62bd\u8c61\u5316\u30ec\u30d9\u30eb\u3092\u63a8\u6e2c\u3057\u3066\u3001\u79c1\u305f\u3061\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u958b\u767a\u8005\u306f\u30b3\u30fc\u30c9\u3092\u30aa\u30fc\u30d0\u30fc\u30a8\u30f3\u30b8\u30cb\u30a2\u30ea\u30f3\u30b0\u3059\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30b3\u30fc\u30c9\u304c\u4e0d\u5fc5\u8981\u306a\u62bd\u8c61\u5316\u3067\u6c5a\u67d3\u3055\u308c\u3001\u8aad\u307f\u306b\u304f\u304f\u306a\u308b\u305f\u3081\u3001\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u907f\u3051\u308b\u3079\u304d\u3067\u3059\u3002
\u30ed\u30d6\u30fb\u30d1\u30a4\u30af
\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3067\u30c7\u30b6\u30a4\u30f3\u3059\u308b\u306a\u3002\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u898b\u3064\u3051\u51fa\u305b\u3002
\u62bd\u8c61\u7684\u306b\u554f\u984c\u3092\u89e3\u6c7a\u3057\u3088\u3046\u3068\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u4eca\u89e3\u6c7a\u3059\u3079\u304d\u3053\u3068\u3092\u89e3\u6c7a\u3057\u307e\u3057\u3087\u3046\u3002\u6700\u5f8c\u306b\u91cd\u8981\u306a\u3053\u3068\u3067\u3059\u304c\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306b\u3088\u3063\u3066\u30b3\u30fc\u30c9\u304c\u3069\u306e\u3088\u3046\u306b\u6539\u5584\u3055\u308c\u308b\u304b\u304c\u4e0d\u660e\u77ad\u306a\u5834\u5408\u306f\u3001\u30b3\u30fc\u30c9\u3092\u7c21\u7d20\u5316\u3059\u308b\u305f\u3081\u306b\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u524a\u9664\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3067\u3057\u3087\u3046\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#6","title":"\u751f\u7523\u8005\u5074\u306e\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9 (#6)","text":"\u8981\u7d04\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u3067\u4fdd\u6301\u3059\u308b\u3053\u3068\u3067\u4e0d\u5fc5\u8981\u306a\u62bd\u8c61\u5316\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002
Go\u8a00\u8a9e\u3067\u306f\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u304c\u6697\u9ed9\u7684\u306b\u6e80\u305f\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u660e\u793a\u7684\u306a\u5b9f\u88c5\u3092\u6301\u3064\u8a00\u8a9e\u3068\u6bd4\u8f03\u3057\u3066\u5927\u304d\u306a\u5909\u5316\u3092\u3082\u305f\u3089\u3059\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u5f93\u3046\u3079\u304d\u30a2\u30d7\u30ed\u30fc\u30c1\u306f\u524d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u8aac\u660e\u3057\u305f\u3082\u306e\u2015\u2015\u62bd\u8c61\u306f\u4f5c\u6210\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u767a\u898b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u2015\u2015\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u3059\u3079\u3066\u306e\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u5bfe\u3057\u3066\u7279\u5b9a\u306e\u62bd\u8c61\u5316\u3092\u5f37\u5236\u3059\u308b\u306e\u306f\u751f\u7523\u8005\u306e\u5f79\u5272\u3067\u306f\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u4ee3\u308f\u308a\u306b\u3001\u4f55\u3089\u304b\u306e\u5f62\u5f0f\u306e\u62bd\u8c61\u5316\u304c\u5fc5\u8981\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3057\u3001\u305d\u306e\u30cb\u30fc\u30ba\u306b\u6700\u9069\u306a\u62bd\u8c61\u5316\u30ec\u30d9\u30eb\u3092\u6c7a\u5b9a\u3059\u308b\u306e\u306f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u8cac\u4efb\u3067\u3059\u3002
\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306f\u6d88\u8cbb\u8005\u5074\u306b\u5b58\u5728\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u7279\u5b9a\u306e\u72b6\u6cc1\uff08\u305f\u3068\u3048\u3070\u3001\u62bd\u8c61\u5316\u304c\u6d88\u8cbb\u8005\u306b\u3068\u3063\u3066\u5f79\u7acb\u3064\u3053\u3068\u304c\u308f\u304b\u3063\u3066\u3044\u308b\u2015\u2015\u4e88\u6e2c\u306f\u3057\u3066\u3044\u306a\u3044\u2015\u2015\u5834\u5408\uff09\u3067\u306f\u3001\u305d\u308c\u3092\u751f\u7523\u8005\u5074\u3067\u4f7f\u7528\u3057\u305f\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3046\u3057\u305f\u5834\u5408\u3001\u53ef\u80fd\u306a\u9650\u308a\u6700\u5c0f\u9650\u306b\u6291\u3048\u3001\u518d\u5229\u7528\u53ef\u80fd\u6027\u3092\u9ad8\u3081\u3001\u3088\u308a\u7c21\u5358\u306b\u69cb\u6210\u3067\u304d\u308b\u3088\u3046\u306b\u52aa\u3081\u308b\u3079\u304d\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#7","title":"\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u8fd4\u3059 (#7)","text":"\u8981\u7d04\u67d4\u8edf\u6027\u306b\u554f\u984c\u304c\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u95a2\u6570\u306f\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3067\u306f\u306a\u304f\u5177\u4f53\u7684\u200b\u200b\u306a\u5b9f\u88c5\u3092\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u9006\u306b\u3001\u95a2\u6570\u306f\u53ef\u80fd\u306a\u9650\u308a\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u53d7\u3051\u5165\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3067\u306f\u306a\u304f\u5177\u4f53\u7684\u306a\u5b9f\u88c5\u3092\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3046\u3067\u306a\u3044\u3068\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u4f9d\u5b58\u95a2\u4fc2\u306b\u3088\u308a\u8a2d\u8a08\u304c\u3044\u3063\u305d\u3046\u8907\u96d1\u306b\u306a\u308a\u3001\u3059\u3079\u3066\u306e\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u540c\u3058\u62bd\u8c61\u5316\u306b\u4f9d\u5b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u67d4\u8edf\u6027\u306b\u6b20\u3051\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u7d50\u8ad6\u306f\u524d\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3068\u4f3c\u3066\u3044\u307e\u3059\u3002\u62bd\u8c61\u5316\u304c\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u3068\u3063\u3066\u5f79\u7acb\u3064\u3053\u3068\u304c\uff08\u4e88\u6e2c\u3055\u308c\u308b\u3067\u306f\u306a\u304f\uff09\u308f\u304b\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u8fd4\u3059\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u3082\u3088\u3044\u3067\u3057\u3087\u3046\u3002\u305d\u308c\u4ee5\u5916\u306e\u5834\u5408\u306f\u3001\u62bd\u8c61\u5316\u3092\u5f37\u5236\u3059\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u305d\u308c\u3089\u306f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u3088\u3063\u3066\u767a\u898b\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4f55\u3089\u304b\u306e\u7406\u7531\u3067\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u5b9f\u88c5\u3092\u62bd\u8c61\u5316\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u3067\u3082\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u3067\u305d\u308c\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
"},{"location":"ja/#any-8","title":"any
\u306f\u4f55\u3082\u8a00\u308f\u306a\u3044 (#8)","text":"\u8981\u7d04 json.Marshal
\u306a\u3069\u8003\u3048\u3046\u308b\u3059\u3079\u3066\u306e\u578b\u3092\u53d7\u3051\u5165\u308c\u308b\u304b\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306b\u306e\u307f any
\u3092\u4f7f\u7528\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u305d\u308c\u4ee5\u5916\u306e\u5834\u5408\u3001any
\u306f\u610f\u5473\u306e\u3042\u308b\u60c5\u5831\u3092\u63d0\u4f9b\u305b\u305a\u3001\u547c\u3073\u51fa\u3057\u5143\u304c\u4efb\u610f\u306e\u30c7\u30fc\u30bf\u578b\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3092\u8a31\u53ef\u3059\u308b\u305f\u3081\u3001\u30b3\u30f3\u30d1\u30a4\u30eb\u6642\u306b\u554f\u984c\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
any
\u578b\u306f\u3001\u8003\u3048\u3046\u308b\u3059\u3079\u3066\u306e\u578b\u3092\u53d7\u3051\u5165\u308c\u308b\u304b\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\uff08\u305f\u3068\u3048\u3070\u3001\u30de\u30fc\u30b7\u30e3\u30ea\u30f3\u30b0\u3084\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306e\u5834\u5408\uff09\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002\u539f\u5247\u3068\u3057\u3066\u30b3\u30fc\u30c9\u3092\u904e\u5ea6\u306b\u4e00\u822c\u5316\u3059\u308b\u3053\u3068\u306f\u4f55\u3068\u3057\u3066\u3082\u907f\u3051\u308b\u3079\u304d\u3067\u3059\u3002\u30b3\u30fc\u30c9\u306e\u8868\u73fe\u529b\u306a\u3069\u306e\u4ed6\u306e\u5074\u9762\u304c\u5411\u4e0a\u3059\u308b\u5834\u5408\u306f\u3001\u30b3\u30fc\u30c9\u3092\u5c11\u3057\u91cd\u8907\u3055\u305b\u305f\u307b\u3046\u304c\u826f\u3044\u3053\u3068\u3082\u3042\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#9","title":"\u30b8\u30a7\u30cd\u30ea\u30c3\u30af\u30b9\u3092\u3044\u3064\u4f7f\u7528\u3059\u308b\u3079\u304d\u304b\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#9)","text":"\u8981\u7d04\u30b8\u30a7\u30cd\u30ea\u30c3\u30af\u30b9\u3068\u578b\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u8981\u7d20\u3084\u52d5\u4f5c\u3092\u9664\u5916\u3059\u308b\u305f\u3081\u306e\u30dc\u30a4\u30e9\u30fc\u30d7\u30ec\u30fc\u30c8\u30b3\u30fc\u30c9\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u578b\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u6642\u671f\u5c1a\u65e9\u306b\u4f7f\u7528\u305b\u305a\u3001\u5177\u4f53\u7684\u306a\u5fc5\u8981\u6027\u304c\u308f\u304b\u3063\u305f\u5834\u5408\u306b\u306e\u307f\u4f7f\u7528\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u3001\u4e0d\u5fc5\u8981\u306a\u62bd\u8c61\u5316\u3068\u8907\u96d1\u3055\u304c\u751f\u3058\u307e\u3059\u3002
\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u5168\u6587\u306f\u3053\u3061\u3089\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#10","title":"\u578b\u306e\u57cb\u3081\u8fbc\u307f\u3067\u8d77\u3053\u308a\u3046\u308b\u554f\u984c\u3092\u628a\u63e1\u3057\u3066\u3044\u306a\u3044 (#10)","text":"\u8981\u7d04\u578b\u57cb\u3081\u8fbc\u307f\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30dc\u30a4\u30e9\u30fc\u30d7\u30ec\u30fc\u30c8\u30b3\u30fc\u30c9\u3092\u56de\u907f\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u305d\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u4e00\u90e8\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u975e\u8868\u793a\u306b\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306b\u554f\u984c\u304c\u767a\u751f\u3057\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u69cb\u9020\u4f53\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u3001Go\u8a00\u8a9e\u306f\u578b\u3092\u57cb\u3081\u8fbc\u3080\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u578b\u57cb\u3081\u8fbc\u307f\u306e\u610f\u5473\u3092\u3059\u3079\u3066\u7406\u89e3\u3057\u3066\u3044\u306a\u3044\u3068\u3001\u4e88\u671f\u3057\u306a\u3044\u52d5\u4f5c\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u578b\u3092\u57cb\u3081\u8fbc\u3080\u65b9\u6cd5\u3001\u305d\u308c\u304c\u3082\u305f\u3089\u3059\u3082\u306e\u3001\u304a\u3088\u3073\u8003\u3048\u3089\u308c\u308b\u554f\u984c\u306b\u3064\u3044\u3066\u898b\u3066\u3044\u304d\u307e\u3059\u3002
Go\u8a00\u8a9e\u3067\u306f\u3001\u540d\u524d\u306a\u3057\u3067\u5ba3\u8a00\u3055\u308c\u305f\u69cb\u9020\u4f53\u30d5\u30a3\u30fc\u30eb\u30c9\u306f\u3001\u57cb\u3081\u8fbc\u307f\u3068\u547c\u3070\u308c\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u6b21\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002
type Foo struct {\nBar // \u57cb\u3081\u8fbc\u307f\u30d5\u30a3\u30fc\u30eb\u30c9\n}\ntype Bar struct {\nBaz int\n}\n
Foo
\u69cb\u9020\u4f53\u3067\u306f\u3001Bar
\u578b\u304c\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u540d\u524d\u306a\u3057\u3067\u5ba3\u8a00\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u308c\u306f\u57cb\u3081\u8fbc\u307f\u30d5\u30a3\u30fc\u30eb\u30c9\u3067\u3059\u3002
\u57cb\u3081\u8fbc\u307f\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u57cb\u3081\u8fbc\u307f\u578b\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3068\u30e1\u30bd\u30c3\u30c9\u306f\u6607\u683c\u3057\u307e\u3059\u3002Bar \u306b\u306f Baz \u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u3053\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306f Foo
\u306b\u6607\u683c\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001Foo \u304b\u3089 Baz \u3092\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002
\u578b\u306e\u57cb\u3081\u8fbc\u307f\u306b\u3064\u3044\u3066\u4f55\u304c\u8a00\u3048\u308b\u3067\u3057\u3087\u3046\u304b\u3002\u307e\u305a\u3001\u3053\u308c\u304c\u5fc5\u8981\u306b\u306a\u308b\u3053\u3068\u306f\u307b\u3068\u3093\u3069\u306a\u304f\u3001\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u304c\u4f55\u3067\u3042\u308c\u3001\u304a\u305d\u3089\u304f\u578b\u57cb\u3081\u8fbc\u307f\u306a\u3057\u3067\u3082\u540c\u69d8\u306b\u89e3\u6c7a\u3067\u304d\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u578b\u306e\u57cb\u3081\u8fbc\u307f\u306f\u4e3b\u306b\u5229\u4fbf\u6027\u3092\u76ee\u7684\u3068\u3057\u3066\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u305d\u308c\u306f\u52d5\u4f5c\u3092\u6607\u683c\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
\u578b\u57cb\u3081\u8fbc\u307f\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u6b21\u306e 2 \u3064\u306e\u4e3b\u306a\u5236\u7d04\u3092\u5ff5\u982d\u306b\u7f6e\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
Foo.Bar.Baz()
\u306e\u4ee3\u308f\u308a\u306b Foo.Baz()
\u306a\u3069\uff09\u3002 \u3053\u308c\u304c\u552f\u4e00\u306e\u6839\u62e0\u3067\u3042\u308b\u5834\u5408\u306f\u3001\u5185\u90e8\u578b\u3092\u57cb\u3081\u8fbc\u307e\u305a\u3001\u4ee3\u308f\u308a\u306b\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u3089\u306e\u5236\u7d04\u3092\u5ff5\u982d\u306b\u7f6e\u3044\u3066\u578b\u57cb\u3081\u8fbc\u307f\u3092\u610f\u8b58\u7684\u306b\u4f7f\u7528\u3059\u308b\u3068\u3001\u8ffd\u52a0\u306e\u8ee2\u9001\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308b\u30dc\u30a4\u30e9\u30fc\u30d7\u30ec\u30fc\u30c8\u30b3\u30fc\u30c9\u3092\u56de\u907f\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u898b\u305f\u76ee\u3060\u3051\u3092\u76ee\u7684\u3068\u3057\u305f\u308a\u3001\u96a0\u3059\u3079\u304d\u8981\u7d20\u3092\u6607\u683c\u3057\u305f\u308a\u3057\u306a\u3044\u3088\u3046\u306b\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#functional-options-11","title":"Functional Options \u30d1\u30bf\u30fc\u30f3\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#11)","text":"\u8981\u7d04API \u306b\u9069\u3057\u305f\u65b9\u6cd5\u3067\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4fbf\u5229\u306b\u51e6\u7406\u3059\u308b\u306b\u306f\u3001Functional Options \u30d1\u30bf\u30fc\u30f3\u3092\u4f7f\u7528\u3057\u307e\u3057\u3087\u3046\u3002
\u3055\u307e\u3056\u307e\u306a\u5b9f\u88c5\u65b9\u6cd5\u304c\u5b58\u5728\u3057\u3001\u591a\u5c11\u306e\u9055\u3044\u306f\u3042\u308a\u307e\u3059\u304c\u3001\u4e3b\u306a\u8003\u3048\u65b9\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002
type Option func(options *options)
\u30a8\u30e9\u30fc\u3092\u8fd4\u3059\u95a2\u6570\u3067\u3059\u3002\u305f\u3068\u3048\u3070\u3001WithPort
\u306f\u30dd\u30fc\u30c8\u3092\u8868\u3059 int
\u5f15\u6570\u3092\u53d7\u3051\u53d6\u308a\u3001options
\u69cb\u9020\u4f53\u306e\u66f4\u65b0\u65b9\u6cd5\u3092\u8868\u3059 Option
\u578b\u3092\u8fd4\u3057\u307e\u3059\u3002type options struct {\nport *int\n}\ntype Option func(options *options) error\nfunc WithPort(port int) Option {\nreturn func(options *options) error {\nif port < 0 {\nreturn errors.New(\"port should be positive\")\n}\noptions.port = &port\nreturn nil\n}\n}\nfunc NewServer(addr string, opts ...Option) ( *http.Server, error) { <1>\nvar options options <2>\nfor _, opt := range opts { <3>\nerr := opt(&options) <4>\nif err != nil {\nreturn nil, err\n}\n}\n// \u3053\u306e\u6bb5\u968e\u3067\u3001\u30aa\u30d7\u30b7\u30e7\u30f3\u69cb\u9020\u4f53\u304c\u69cb\u7bc9\u3055\u308c\u3001\u69cb\u6210\u304c\u542b\u307e\u308c\u307e\u3059\u3002\n// \u3057\u305f\u304c\u3063\u3066\u3001\u30dd\u30fc\u30c8\u8a2d\u5b9a\u306b\u95a2\u9023\u3059\u308b\u30ed\u30b8\u30c3\u30af\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002\nvar port int\nif options.port == nil {\nport = defaultHTTPPort\n} else {\nif *options.port == 0 {\nport = randomPort()\n} else {\nport = *options.port\n}\n}\n// ...\n}\n
Functional Options \u30d1\u30bf\u30fc\u30f3\u306f\u3001\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u624b\u8efd\u3067 API \u30d5\u30ec\u30f3\u30c9\u30ea\u30fc\u306a\u65b9\u6cd5\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002 Builder \u30d1\u30bf\u30fc\u30f3\u306f\u6709\u52b9\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u304c\u3001\u3044\u304f\u3064\u304b\u306e\u5c0f\u3055\u306a\u6b20\u70b9\uff08\u7a7a\u306e\u53ef\u80fd\u6027\u304c\u3042\u308b\u69cb\u6210\u69cb\u9020\u4f53\u3092\u6e21\u3055\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3001\u307e\u305f\u306f\u30a8\u30e9\u30fc\u3092\u51e6\u7406\u3059\u308b\u65b9\u6cd5\u304c\u3042\u307e\u308a\u4fbf\u5229\u3067\u306f\u306a\u3044\uff09\u304c\u3042\u308a\u3001\u3053\u306e\u7a2e\u306e\u554f\u984c\u306b\u304a\u3044\u3066 Functional Options \u30d1\u30bf\u30fc\u30f3\u304cGo\u8a00\u8a9e\u306b\u304a\u3051\u308b\u6163\u7528\u7684\u306a\u5bfe\u51e6\u65b9\u6cd5\u306b\u306a\u308b\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#12","title":"\u8aa4\u3063\u305f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u69cb\u6210 (\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u69cb\u9020\u3068\u30d1\u30c3\u30b1\u30fc\u30b8\u69cb\u6210) (#12)","text":"\u5168\u4f53\u7684\u306a\u69cb\u6210\u306b\u95a2\u3057\u3066\u306f\u3001\u3055\u307e\u3056\u307e\u306a\u8003\u3048\u65b9\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3054\u3068\u306b\u6574\u7406\u3059\u3079\u304d\u304b\u3001\u305d\u308c\u3068\u3082\u30ec\u30a4\u30e4\u30fc\u3054\u3068\u306b\u6574\u7406\u3059\u3079\u304d\u304b\u3001\u305d\u308c\u306f\u597d\u307f\u306b\u3088\u3063\u3066\u7570\u306a\u308a\u307e\u3059\u3002\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\uff08\u9867\u5ba2\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3001\u5951\u7d04\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306a\u3069\uff09\u3054\u3068\u306b\u30b3\u30fc\u30c9\u3092\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b\u3053\u3068\u3092\u9078\u3076\u5834\u5408\u3082\u3042\u308c\u3070\u3001\u516d\u89d2\u5f62\u306e\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u539f\u5247\u306b\u5f93\u3046\u3053\u3068\u3068\u3001\u6280\u8853\u5c64\u3054\u3068\u306b\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b\u3053\u3068\u3092\u9078\u3076\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u3002\u79c1\u305f\u3061\u304c\u884c\u3046\u6c7a\u5b9a\u304c\u4e00\u8cab\u3057\u3066\u3044\u308b\u9650\u308a\u3001\u305d\u308c\u304c\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u306b\u9069\u5408\u3059\u308b\u306a\u3089\u3001\u305d\u308c\u304c\u9593\u9055\u3063\u3066\u3044\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002
\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u95a2\u3057\u3066\u306f\u3001\u5f93\u3046\u3079\u304d\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u304c\u8907\u6570\u3042\u308a\u307e\u3059\u3002\u307e\u305a\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304c\u904e\u5ea6\u306b\u8907\u96d1\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u6642\u671f\u5c1a\u65e9\u306a\u30d1\u30c3\u30b1\u30fc\u30b8\u5316\u306f\u907f\u3051\u308b\u3079\u304d\u3067\u3059\u3002\u5834\u5408\u306b\u3088\u3063\u3066\u306f\u3001\u5b8c\u74a7\u306a\u69cb\u9020\u3092\u6700\u521d\u304b\u3089\u7121\u7406\u306b\u4f5c\u308d\u3046\u3068\u3059\u308b\u3088\u308a\u3082\u3001\u5358\u7d14\u306a\u69cb\u6210\u3092\u4f7f\u7528\u3057\u3001\u305d\u306e\u5185\u5bb9\u3092\u7406\u89e3\u3057\u305f\u4e0a\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u767a\u5c55\u3055\u305b\u308b\u307b\u3046\u304c\u826f\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002 \u7c92\u5ea6\u3082\u8003\u616e\u3059\u3079\u304d\u91cd\u8981\u306a\u70b9\u3067\u3059\u3002 1 \u3064\u307e\u305f\u306f 2 \u3064\u306e\u30d5\u30a1\u30a4\u30eb\u3060\u3051\u3092\u542b\u3080\u6570\u5341\u306e\u30ca\u30ce\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u306f\u907f\u3051\u308b\u3079\u304d\u3067\u3059\u3002\u305d\u306e\u5834\u5408\u3001\u304a\u305d\u3089\u304f\u3053\u308c\u3089\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u9593\u306e\u8ad6\u7406\u7684\u306a\u63a5\u7d9a\u306e\u4e00\u90e8\u304c\u629c\u3051\u843d\u3061\u3001\u8aad\u307f\u624b\u306b\u3068\u3063\u3066\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304c\u7406\u89e3\u3057\u306b\u304f\u304f\u306a\u308b\u304b\u3089\u3067\u3059\u3002\u9006\u306b\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u540d\u306e\u610f\u5473\u3092\u8584\u3081\u308b\u3088\u3046\u306a\u5de8\u5927\u306a\u30d1\u30c3\u30b1\u30fc\u30b8\u3082\u907f\u3051\u308b\u3079\u304d\u3067\u3059\u3002
\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u540d\u524d\u4ed8\u3051\u3082\u6ce8\u610f\u3057\u3066\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\uff08\u958b\u767a\u8005\u306a\u3089\uff09\u8ab0\u3082\u304c\u77e5\u3063\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u540d\u524d\u3092\u4ed8\u3051\u308b\u306e\u306f\u96e3\u3057\u3044\u3067\u3059\u3002\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c Go \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u7406\u89e3\u3057\u3084\u3059\u3044\u3088\u3046\u306b\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u542b\u307e\u308c\u308b\u3082\u306e\u3067\u306f\u306a\u304f\u3001\u63d0\u4f9b\u3059\u308b\u3082\u306e\u306b\u57fa\u3065\u3044\u3066\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u540d\u524d\u3092\u4ed8\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u307e\u305f\u3001\u30cd\u30fc\u30df\u30f3\u30b0\u306b\u306f\u610f\u5473\u306e\u3042\u308b\u3082\u306e\u3092\u4ed8\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u540d\u306f\u77ed\u304f\u3001\u7c21\u6f54\u3067\u3001\u8868\u73fe\u529b\u8c4a\u304b\u3067\u3001\u6163\u4f8b\u306b\u3088\u308a\u5358\u4e00\u306e\u5c0f\u6587\u5b57\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u4f55\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u306e\u30eb\u30fc\u30eb\u306f\u975e\u5e38\u306b\u7c21\u5358\u3067\u3059\u3002\u30d1\u30c3\u30b1\u30fc\u30b8\u9593\u306e\u7d50\u5408\u3092\u6e1b\u3089\u3057\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3055\u308c\u308b\u4e0d\u8981\u306a\u8981\u7d20\u3092\u975e\u8868\u793a\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3082\u306e\u3092\u3067\u304d\u308b\u9650\u308a\u6700\u5c0f\u9650\u306b\u6291\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u8981\u7d20\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u304b\u3069\u3046\u304b\u4e0d\u660e\u306a\u5834\u5408\u306f\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5f8c\u3067\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u304c\u5224\u660e\u3057\u305f\u5834\u5408\u306f\u3001\u30b3\u30fc\u30c9\u3092\u8abf\u6574\u3067\u304d\u307e\u3059\u3002\u307e\u305f\u3001\u69cb\u9020\u4f53\u3092 encoding/json \u3067\u30a2\u30f3\u30de\u30fc\u30b7\u30e3\u30ea\u30f3\u30b0\u3067\u304d\u308b\u3088\u3046\u306b\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u306a\u3069\u3001\u3044\u304f\u3064\u304b\u306e\u4f8b\u5916\u306b\u3082\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u69cb\u6210\u3059\u308b\u306e\u306f\u7c21\u5358\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u3053\u308c\u3089\u306e\u30eb\u30fc\u30eb\u306b\u5f93\u3046\u3053\u3068\u3067\u7dad\u6301\u304c\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u4fdd\u5b88\u6027\u3092\u5bb9\u6613\u306b\u3059\u308b\u305f\u3081\u306b\u306f\u4e00\u8cab\u6027\u3082\u91cd\u8981\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30b3\u30fc\u30c9\u30d9\u30fc\u30b9\u5185\u3067\u53ef\u80fd\u306a\u9650\u308a\u4e00\u8cab\u6027\u3092\u4fdd\u3064\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002
\u88dc\u8db3Go \u30c1\u30fc\u30e0\u306f Go \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u7d44\u7e54\u5316/\u69cb\u9020\u5316\u306b\u95a2\u3059\u308b\u516c\u5f0f\u30ac\u30a4\u30c9\u30e9\u30a4\u30f3\u30922023\u5e74\u306b\u767a\u884c\u3057\u307e\u3057\u305f\uff1a go.dev/doc/modules/layout
"},{"location":"ja/#13","title":"\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u4f5c\u6210 (#13)","text":"\u8981\u7d04\u540d\u524d\u4ed8\u3051\u306f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u8a2d\u8a08\u306e\u91cd\u8981\u306a\u90e8\u5206\u3067\u3059\u3002common
\u3001util
\u3001shared
\u306e\u3088\u3046\u306a\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f5c\u6210\u3057\u3066\u3082\u3001\u8aad\u307f\u624b\u306b\u305d\u308c\u307b\u3069\u306e\u4fa1\u5024\u3092\u3082\u305f\u3089\u3057\u307e\u305b\u3093\u3002\u3053\u306e\u3088\u3046\u306a\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u610f\u5473\u306e\u3042\u308b\u5177\u4f53\u7684\u306a\u30d1\u30c3\u30b1\u30fc\u30b8\u540d\u306b\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3057\u307e\u3057\u3087\u3046\u3002
\u307e\u305f\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u542b\u307e\u308c\u308b\u3082\u306e\u3067\u306f\u306a\u304f\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u304c\u63d0\u4f9b\u3059\u308b\u3082\u306e\u306b\u57fa\u3065\u3044\u3066\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u540d\u524d\u3092\u4ed8\u3051\u308b\u3068\u3001\u305d\u306e\u8868\u73fe\u529b\u3092\u9ad8\u3081\u308b\u52b9\u7387\u7684\u306a\u65b9\u6cd5\u306b\u306a\u308b\u3053\u3068\u306b\u3082\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#14","title":"\u30d1\u30c3\u30b1\u30fc\u30b8\u540d\u306e\u885d\u7a81\u3092\u7121\u8996\u3059\u308b (#14)","text":"\u8981\u7d04\u6df7\u4e71\u3001\u3055\u3089\u306b\u306f\u30d0\u30b0\u306b\u3064\u306a\u304c\u308a\u304b\u306d\u306a\u3044\u3001\u5909\u6570\u3068\u30d1\u30c3\u30b1\u30fc\u30b8\u9593\u306e\u540d\u524d\u306e\u885d\u7a81\u3092\u56de\u907f\u3059\u308b\u305f\u3081\u306b\u3001\u305d\u308c\u305e\u308c\u306b\u4e00\u610f\u306e\u540d\u524d\u3092\u4f7f\u7528\u3057\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u304c\u4e0d\u53ef\u80fd\u306a\u5834\u5408\u306f\u3001\u30a4\u30f3\u30dd\u30fc\u30c8\u30a8\u30a4\u30ea\u30a2\u30b9\u3092\u4f7f\u7528\u3057\u3066\u4fee\u98fe\u5b50\u3092\u5909\u66f4\u3057\u3066\u30d1\u30c3\u30b1\u30fc\u30b8\u540d\u3068\u5909\u6570\u540d\u3092\u533a\u5225\u3059\u308b\u304b\u3001\u3088\u308a\u826f\u3044\u540d\u524d\u3092\u8003\u3048\u3066\u304f\u3060\u3055\u3044\u3002
\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u885d\u7a81\u306f\u3001\u5909\u6570\u540d\u304c\u65e2\u5b58\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u540d\u3068\u885d\u7a81\u3059\u308b\u5834\u5408\u306b\u767a\u751f\u3057\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u518d\u5229\u7528\u304c\u59a8\u3052\u3089\u308c\u307e\u3059\u3002\u66d6\u6627\u3055\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u5909\u6570\u540d\u306e\u885d\u7a81\u3092\u9632\u3050\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u885d\u7a81\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306f\u3001\u5225\u306e\u610f\u5473\u306e\u3042\u308b\u540d\u524d\u3092\u898b\u3064\u3051\u308b\u304b\u3001\u30a4\u30f3\u30dd\u30fc\u30c8\u30a8\u30a4\u30ea\u30a2\u30b9\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
"},{"location":"ja/#15","title":"\u30b3\u30fc\u30c9\u306e\u6587\u7ae0\u5316\u304c\u884c\u308f\u308c\u3066\u3044\u306a\u3044 (#15)","text":"\u8981\u7d04\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3068\u30e1\u30f3\u30c6\u30ca\u304c\u30b3\u30fc\u30c9\u306e\u610f\u56f3\u3092\u7406\u89e3\u3067\u304d\u308b\u3088\u3046\u306b\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3055\u308c\u305f\u8981\u7d20\u3092\u6587\u7ae0\u5316\u3057\u307e\u3057\u3087\u3046\u3002
\u6587\u7ae0\u5316\u306f\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u91cd\u8981\u306a\u5074\u9762\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c API \u3092\u3088\u308a\u7c21\u5358\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u7dad\u6301\u306b\u3082\u5f79\u7acb\u3061\u307e\u3059\u3002Go\u8a00\u8a9e\u3067\u306f\u3001\u30b3\u30fc\u30c9\u3092\u6163\u7528\u7684\u306a\u3082\u306e\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u3044\u304f\u3064\u304b\u306e\u30eb\u30fc\u30eb\u306b\u5f93\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u307e\u305a\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u8981\u7d20\u3092\u6587\u7ae0\u5316\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u69cb\u9020\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3001\u95a2\u6570\u306a\u3069\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u5834\u5408\u306f\u6587\u7ae0\u5316\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6163\u4f8b\u3068\u3057\u3066\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3055\u308c\u305f\u8981\u7d20\u306e\u540d\u524d\u304b\u3089\u59cb\u307e\u308b\u30b3\u30e1\u30f3\u30c8\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002
\u6163\u4f8b\u3068\u3057\u3066\u3001\u5404\u30b3\u30e1\u30f3\u30c8\u306f\u53e5\u8aad\u70b9\u3067\u7d42\u308f\u308b\u5b8c\u5168\u306a\u6587\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u307e\u305f\u3001\u95a2\u6570\uff08\u307e\u305f\u306f\u30e1\u30bd\u30c3\u30c9\uff09\u3092\u6587\u7ae0\u5316\u3059\u308b\u3068\u304d\u306f\u3001\u95a2\u6570\u304c\u3069\u306e\u3088\u3046\u306b\u5b9f\u884c\u3059\u308b\u304b\u3067\u306f\u306a\u304f\u3001\u305d\u306e\u95a2\u6570\u304c\u4f55\u3092\u5b9f\u884c\u3059\u308b\u3064\u3082\u308a\u3067\u3042\u308b\u304b\u3092\u5f37\u8abf\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u306b\u3082\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3067\u306f\u306a\u304f\u3001\u95a2\u6570\u3068\u30b3\u30e1\u30f3\u30c8\u306b\u3064\u3044\u3066\u3067\u3059\u3002\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f\u7406\u60f3\u7684\u306b\u306f\u3001\u6d88\u8cbb\u8005\u304c\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3055\u308c\u305f\u8981\u7d20\u306e\u4f7f\u7528\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u30b3\u30fc\u30c9\u3092\u898b\u308b\u5fc5\u8981\u304c\u306a\u3044\u307b\u3069\u5341\u5206\u306a\u60c5\u5831\u3092\u63d0\u4f9b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u5909\u6570\u307e\u305f\u306f\u5b9a\u6570\u3092\u6587\u7ae0\u5316\u3059\u308b\u5834\u5408\u3001\u305d\u306e\u76ee\u7684\u3068\u5185\u5bb9\u3068\u3044\u30462\u3064\u306e\u5074\u9762\u3092\u4f1d\u3048\u308b\u3053\u3068\u304c\u91cd\u8981\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u524d\u8005\u306f\u3001\u5916\u90e8\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u3068\u3063\u3066\u5f79\u7acb\u3064\u3088\u3046\u306b\u3001\u30b3\u30fc\u30c9\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3068\u3057\u3066\u5b58\u5728\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u5f8c\u8005\u306f\u5fc5\u305a\u3057\u3082\u516c\u958b\u3055\u308c\u308b\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002
\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3068\u30e1\u30f3\u30c6\u30ca\u304c\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u76ee\u7684\u3092\u7406\u89e3\u3067\u304d\u308b\u3088\u3046\u306b\u3001\u5404\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3059\u308b\u5fc5\u8981\u3082\u3042\u308a\u307e\u3059\u3002\u6163\u4f8b\u3068\u3057\u3066\u3001\u30b3\u30e1\u30f3\u30c8\u306f //Package
\u3067\u59cb\u307e\u308a\u3001\u305d\u306e\u5f8c\u306b\u30d1\u30c3\u30b1\u30fc\u30b8\u540d\u304c\u7d9a\u304d\u307e\u3059\u3002\u30d1\u30c3\u30b1\u30fc\u30b8\u30b3\u30e1\u30f3\u30c8\u306e\u6700\u521d\u306e\u884c\u306f\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u8868\u793a\u3055\u308c\u308b\u305f\u3081\u7c21\u6f54\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u6b21\u306e\u884c\u306b\u5fc5\u8981\u306a\u60c5\u5831\u3092\u3059\u3079\u3066\u5165\u529b\u3057\u307e\u3059\u3002
\u30b3\u30fc\u30c9\u3092\u6587\u7ae0\u5316\u3059\u308b\u3053\u3068\u304c\u5236\u7d04\u306b\u306a\u308b\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3084\u30e1\u30f3\u30c6\u30ca\u304c\u30b3\u30fc\u30c9\u306e\u610f\u56f3\u3092\u7406\u89e3\u3059\u308b\u306e\u306b\u5f79\u7acb\u3064\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
"},{"location":"ja/#16","title":"\u30ea\u30f3\u30bf\u30fc\u3092\u4f7f\u7528\u3057\u3066\u306a\u3044 (#16)","text":"\u8981\u7d04\u30b3\u30fc\u30c9\u306e\u54c1\u8cea\u3068\u4e00\u8cab\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u306b\u306f\u3001\u30ea\u30f3\u30bf\u30fc\u3068\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u30fc\u3092\u4f7f\u7528\u3057\u307e\u3057\u3087\u3046
\u30ea\u30f3\u30bf\u30fc\u306f\u3001\u30b3\u30fc\u30c9\u3092\u5206\u6790\u3057\u3066\u30a8\u30e9\u30fc\u3092\u691c\u51fa\u3059\u308b\u81ea\u52d5\u30c4\u30fc\u30eb\u3067\u3059\u3002\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u76ee\u7684\u306f\u3001\u65e2\u5b58\u306e\u30ea\u30f3\u30bf\u30fc\u306e\u5b8c\u5168\u306a\u30ea\u30b9\u30c8\u3092\u63d0\u4f9b\u3059\u308b\u3053\u3068\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u305d\u3046\u3057\u305f\u5834\u5408\u3001\u3059\u3050\u306b\u4f7f\u3044\u7269\u306b\u306a\u3089\u306a\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u304b\u3089\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u307b\u3068\u3093\u3069\u306e Go \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u30ea\u30f3\u30bf\u30fc\u304c\u4e0d\u53ef\u6b20\u3067\u3042\u308b\u7406\u7531\u3092\u7406\u89e3\u3057\u3001\u899a\u3048\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002
\u30ea\u30f3\u30bf\u30fc\u3068\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u30fc\u306f\u3001\u30b3\u30fc\u30c9\u30d9\u30fc\u30b9\u306e\u54c1\u8cea\u3068\u4e00\u8cab\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u5f37\u529b\u306a\u65b9\u6cd5\u3067\u3059\u3002\u6642\u9593\u3092\u304b\u3051\u3066\u3069\u308c\u3092\u4f7f\u7528\u3059\u3079\u304d\u304b\u3092\u7406\u89e3\u3057\u3001\u305d\u308c\u3089\u306e\u5b9f\u884c\uff08 CI \u3084 Git \u30d7\u30ea\u30b3\u30df\u30c3\u30c8\u30d5\u30c3\u30af\u306a\u3069\uff09\u3092\u81ea\u52d5\u5316\u3057\u307e\u3057\u3087\u3046\u3002
"},{"location":"ja/#_2","title":"\u30c7\u30fc\u30bf\u578b","text":""},{"location":"ja/#8-17","title":"8\u9032\u6570\u30ea\u30c6\u30e9\u30eb\u3067\u6df7\u4e71\u3092\u62db\u304f (#17)","text":"\u8981\u7d04\u65e2\u5b58\u306e\u30b3\u30fc\u30c9\u3092\u8aad\u3080\u3068\u304d\u306f\u3001 0
\u3067\u59cb\u307e\u308b\u6574\u6570\u30ea\u30c6\u30e9\u30eb\u304c8\u9032\u6570\u3067\u3042\u308b\u3053\u3068\u306b\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u307e\u305f\u3001\u63a5\u982d\u8f9e 0o
\u3092\u4ed8\u3051\u308b\u3053\u3068\u30678\u9032\u6574\u6570\u3067\u3042\u308b\u3053\u3068\u3092\u660e\u78ba\u306b\u3057\u3001\u8aad\u307f\u3084\u3059\u3055\u3092\u5411\u4e0a\u3055\u305b\u307e\u3057\u3087\u3046\u3002
8 \u9032\u6570\u306f 0 \u3067\u59cb\u307e\u308a\u307e\u3059\uff08\u305f\u3068\u3048\u3070\u3001010
\u306f 10 \u9032\u6570\u306e 8 \u306b\u76f8\u5f53\u3057\u307e\u3059\uff09\u3002\u53ef\u8aad\u6027\u3092\u5411\u4e0a\u3055\u305b\u3001\u5c06\u6765\u306e\u30b3\u30fc\u30c9\u30ea\u30fc\u30c0\u30fc\u306e\u6f5c\u5728\u7684\u306a\u9593\u9055\u3044\u3092\u56de\u907f\u3059\u308b\u306b\u306f\u3001 0o
\u63a5\u982d\u8f9e\u3092\u4f7f\u7528\u3057\u3066 8 \u9032\u6570\u3067\u3042\u308b\u3053\u3068\u3092\u660e\u3089\u304b\u306b\u3057\u307e\u3057\u3087\u3046\uff08\u4f8b: 0o10
\uff09\u3002
\u4ed6\u306e\u6574\u6570\u30ea\u30c6\u30e9\u30eb\u8868\u73fe\u306b\u3082\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
0b
\u3042\u308b\u3044\u306f 0B
\u3092\u4f7f\u7528\u3057\u307e\u3059\uff08\u305f\u3068\u3048\u3070\u3001 0b
\u306f10\u9032\u6570\u306e 4 \u306b\u76f8\u5f53\u3057\u307e\u3059\uff090x
\u3042\u308b\u3044\u306f 0X
\u3092\u4f7f\u7528\u3057\u307e\u3059\uff08\u305f\u3068\u3048\u3070\u3001 0xF
\u306f10\u9032\u6570\u306e 15 \u306b\u76f8\u5f53\u3057\u307e\u3059\uff09\u3002 i
\u3092\u4f7f\u7528\u3057\u307e\u3059\uff08\u305f\u3068\u3048\u3070\u3001 3i
\uff09\u8aad\u307f\u3084\u3059\u304f\u3059\u308b\u305f\u3081\u306b\u3001\u533a\u5207\u308a\u6587\u5b57\u3068\u3057\u3066\u30a2\u30f3\u30c0\u30fc\u30b9\u30b3\u30a2\uff08 _ \uff09\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001 10 \u5104\u306f 1_000_000_000
\u306e\u3088\u3046\u306b\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30a2\u30f3\u30c0\u30fc\u30b9\u30b3\u30a2\u306f 0b)00_00_01
\u306e\u3088\u3046\u306b\u4ed6\u306e\u8868\u73fe\u3068\u4f75\u7528\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#18","title":"\u6574\u6570\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u306e\u7121\u8996 (#18)","text":"\u8981\u7d04Go\u8a00\u8a9e\u3067\u306f\u6574\u6570\u306e\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u3068\u30a2\u30f3\u30c0\u30fc\u30d5\u30ed\u30fc\u304c\u88cf\u5074\u3067\u51e6\u7406\u3055\u308c\u308b\u305f\u3081\u3001\u305d\u308c\u3089\u3092\u30ad\u30e3\u30c3\u30c1\u3059\u308b\u72ec\u81ea\u306e\u95a2\u6570\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002
Go\u8a00\u8a9e\u3067\u306f\u3001\u30b3\u30f3\u30d1\u30a4\u30eb\u6642\u306b\u691c\u51fa\u3067\u304d\u308b\u6574\u6570\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u306b\u3088\u3063\u3066\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002
var counter int32 = math.MaxInt32 + 1\n
constant 2147483648 overflows int32\n
\u305f\u3060\u3057\u3001\u5b9f\u884c\u6642\u306b\u306f\u3001\u6574\u6570\u306e\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u307e\u305f\u306f\u30a2\u30f3\u30c0\u30fc\u30d5\u30ed\u30fc\u306f\u767a\u751f\u3057\u307e\u305b\u3093\u3002\u3053\u308c\u306b\u3088\u3063\u3066\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30cb\u30c3\u30af\u304c\u767a\u751f\u3059\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u52d5\u4f5c\u306f\u3084\u3063\u304b\u3044\u306a\u30d0\u30b0\uff08\u305f\u3068\u3048\u3070\u3001\u8ca0\u306e\u7d50\u679c\u306b\u3064\u306a\u304c\u308b\u6574\u6570\u306e\u5897\u5206\u3084\u6b63\u306e\u6574\u6570\u306e\u52a0\u7b97\u306a\u3069\uff09\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u982d\u306b\u5165\u308c\u3066\u304a\u304f\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#19","title":"\u6d6e\u52d5\u5c0f\u6570\u70b9\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#19)","text":"\u8981\u7d04\u7279\u5b9a\u306e\u30c7\u30eb\u30bf\u5185\u3067\u6d6e\u52d5\u5c0f\u6570\u70b9\u6bd4\u8f03\u3092\u884c\u3046\u3068\u3001\u30b3\u30fc\u30c9\u306e\u79fb\u690d\u6027\u3092\u78ba\u4fdd\u3067\u304d\u307e\u3059\u3002\u52a0\u7b97\u307e\u305f\u306f\u6e1b\u7b97\u3092\u5b9f\u884c\u3059\u308b\u3068\u304d\u306f\u3001\u7cbe\u5ea6\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306b\u3001\u540c\u7a0b\u5ea6\u306e\u5927\u304d\u3055\u306e\u6f14\u7b97\u3092\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u307e\u305f\u3001\u4e57\u7b97\u3068\u9664\u7b97\u306f\u52a0\u7b97\u3068\u6e1b\u7b97\u306e\u524d\u306b\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002
Go\u8a00\u8a9e\u306b\u306f\u3001\uff08\u865a\u6570\u3092\u7701\u7565\u3057\u305f\u5834\u5408\uff09 float32
\u3068 float64
\u3068\u3044\u30462\u3064\u306e\u6d6e\u52d5\u5c0f\u6570\u70b9\u578b\u304c\u3042\u308a\u307e\u3059\u3002\u6d6e\u52d5\u5c0f\u6570\u70b9\u306e\u6982\u5ff5\u306f\u3001\u5c0f\u6570\u5024\u3092\u8868\u73fe\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u6574\u6570\u306e\u5927\u304d\u306a\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u767a\u660e\u3055\u308c\u307e\u3057\u305f\u3002\u4e88\u671f\u305b\u306c\u4e8b\u614b\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6f14\u7b97\u306f\u5b9f\u969b\u306e\u6f14\u7b97\u306e\u8fd1\u4f3c\u3067\u3042\u308b\u3053\u3068\u3092\u77e5\u3063\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u305d\u306e\u305f\u3081\u306b\u3001\u4e57\u7b97\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002
var n float32 = 1.0001\nfmt.Println(n * n)\n
\u3053\u306e\u30b3\u30fc\u30c9\u306b\u304a\u3044\u3066\u306f 1.0001 * 1.0001 = 1.00020001 \u3068\u3044\u3046\u7d50\u679c\u304c\u51fa\u529b\u3055\u308c\u308b\u3053\u3068\u3092\u671f\u5f85\u3059\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u306a\u304c\u3089\u3001\u307b\u3068\u3093\u3069\u306e x86 \u30d7\u30ed\u30bb\u30c3\u30b5\u3067\u306f\u3001\u4ee3\u308f\u308a\u306b 1.0002 \u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002
Go\u8a00\u8a9e\u306e float32
\u304a\u3088\u3073 float64
\u578b\u306f\u8fd1\u4f3c\u5024\u3067\u3042\u308b\u305f\u3081\u3001\u3044\u304f\u3064\u304b\u306e\u30eb\u30fc\u30eb\u3092\u5ff5\u982d\u306b\u7f6e\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#20","title":"\u30b9\u30e9\u30a4\u30b9\u306e\u9577\u3055\u3068\u5bb9\u91cf\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#20)","text":"\u8981\u7d04Go \u958b\u767a\u8005\u306a\u3089\u3070\u3001\u30b9\u30e9\u30a4\u30b9\u306e\u9577\u3055\u3068\u5bb9\u91cf\u306e\u9055\u3044\u3092\u7406\u89e3\u3059\u308b\u3079\u304d\u3067\u3059\u3002\u30b9\u30e9\u30a4\u30b9\u306e\u9577\u3055\u306f\u30b9\u30e9\u30a4\u30b9\u5185\u306e\u4f7f\u7528\u53ef\u80fd\u306a\u8981\u7d20\u306e\u6570\u3067\u3042\u308a\u3001\u30b9\u30e9\u30a4\u30b9\u306e\u5bb9\u91cf\u306f\u30d0\u30c3\u30ad\u30f3\u30b0\u914d\u5217\u5185\u306e\u8981\u7d20\u306e\u6570\u3067\u3059\u3002
\u30bb\u30af\u30b7\u30e7\u30f3\u5168\u6587\u306f\u3053\u3061\u3089.
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#21","title":"\u975e\u52b9\u7387\u306a\u30b9\u30e9\u30a4\u30b9\u306e\u521d\u671f\u5316 (#21)","text":"\u8981\u7d04\u30b9\u30e9\u30a4\u30b9\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u3001\u9577\u3055\u304c\u3059\u3067\u306b\u308f\u304b\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u9577\u3055\u307e\u305f\u306f\u5bb9\u91cf\u3067\u30b9\u30e9\u30a4\u30b9\u3092\u521d\u671f\u5316\u3057\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u5272\u308a\u5f53\u3066\u306e\u6570\u304c\u6e1b\u308a\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002
make
\u3092\u4f7f\u7528\u3057\u3066\u30b9\u30e9\u30a4\u30b9\u3092\u521d\u671f\u5316\u3059\u308b\u3068\u304d\u306b\u3001\u9577\u3055\u3068\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u5bb9\u91cf\u3092\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u4e21\u65b9\u306b\u9069\u5207\u306a\u5024\u3092\u6e21\u3059\u3053\u3068\u304c\u9069\u5f53\u3067\u3042\u308b\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u305d\u308c\u3092\u5fd8\u308c\u308b\u306e\u306f\u3088\u304f\u3042\u308b\u9593\u9055\u3044\u3067\u3059\u3002\u5b9f\u969b\u3001\u8907\u6570\u306e\u30b3\u30d4\u30fc\u304c\u5fc5\u8981\u306b\u306a\u308a\u3001\u4e00\u6642\u7684\u306a\u30d0\u30c3\u30ad\u30f3\u30b0\u914d\u5217\u3092\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u3059\u308b\u305f\u3081\u306b GC \u306b\u8ffd\u52a0\u306e\u52b4\u529b\u304c\u304b\u304b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u89b3\u70b9\u304b\u3089\u8a00\u3048\u3070\u3001Go \u30e9\u30f3\u30bf\u30a4\u30e0\u306b\u624b\u3092\u5dee\u3057\u4f38\u3079\u306a\u3044\u7406\u7531\u306f\u3042\u308a\u307e\u305b\u3093\u3002
\u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u5bb9\u91cf\u307e\u305f\u306f\u6307\u5b9a\u3055\u308c\u305f\u9577\u3055\u306e\u30b9\u30e9\u30a4\u30b9\u3092\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u3067\u3059\u3002 \u3053\u308c\u3089 2 \u3064\u306e\u89e3\u6c7a\u7b56\u306e\u3046\u3061\u30012 \u756a\u76ee\u306e\u89e3\u6c7a\u7b56\u306e\u65b9\u304c\u308f\u305a\u304b\u306b\u9ad8\u901f\u3067\u3042\u308b\u50be\u5411\u304c\u3042\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3057\u305f\u3002\u305f\u3060\u3057\u3001\u7279\u5b9a\u306e\u5bb9\u91cf\u3068\u8ffd\u52a0\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u5834\u5408\u306b\u3088\u3063\u3066\u306f\u5b9f\u88c5\u3068\u8aad\u307f\u53d6\u308a\u304c\u5bb9\u6613\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#nil-22","title":"nil \u3068\u7a7a\u306e\u30b9\u30e9\u30a4\u30b9\u3092\u6df7\u540c\u3057\u3066\u3044\u308b (#22)","text":"\u8981\u7d04encoding/json
\u3084 reflect
\u30d1\u30c3\u30b1\u30fc\u30b8\u306a\u3069\u3092\u4f7f\u7528\u3059\u308b\u3068\u304d\u306b\u3088\u304f\u3042\u308b\u6df7\u4e71\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u306f\u3001nil \u30b9\u30e9\u30a4\u30b9\u3068\u7a7a\u306e\u30b9\u30e9\u30a4\u30b9\u306e\u9055\u3044\u3092\u7406\u89e3\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3069\u3061\u3089\u3082\u9577\u3055\u30bc\u30ed\u3001\u5bb9\u91cf\u30bc\u30ed\u306e\u30b9\u30e9\u30a4\u30b9\u3067\u3059\u304c\u3001\u5272\u308a\u5f53\u3066\u3092\u5fc5\u8981\u3068\u3057\u306a\u3044\u306e\u306f nil \u30b9\u30e9\u30a4\u30b9\u3060\u3051\u3067\u3059\u3002
Go\u8a00\u8a9e\u3067\u306f\u3001nil \u3068\u7a7a\u306e\u30b9\u30e9\u30a4\u30b9\u306f\u533a\u5225\u3055\u308c\u307e\u3059\u3002nil \u30b9\u30e9\u30a4\u30b9\u306f nil
\u306b\u7b49\u3057\u3044\u306e\u306b\u5bfe\u3057\u3001\u7a7a\u306e\u30b9\u30e9\u30a4\u30b9\u306e\u9577\u3055\u306f\u30bc\u30ed\u3067\u3059\u3002nil \u30b9\u30e9\u30a4\u30b9\u306f\u7a7a\u3067\u3059\u304c\u3001\u7a7a\u306e\u30b9\u30e9\u30a4\u30b9\u306f\u5fc5\u305a\u3057\u3082nil
\u3067\u3042\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002\u4e00\u65b9\u3001nil \u30b9\u30e9\u30a4\u30b9\u306b\u306f\u5272\u308a\u5f53\u3066\u306f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u5168\u4f53\u3092\u901a\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u65b9\u6cd5\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u72b6\u6cc1\u306b\u5fdc\u3058\u3066\u30b9\u30e9\u30a4\u30b9\u3092\u521d\u671f\u5316\u3059\u308b\u3053\u3068\u3092\u898b\u3066\u304d\u307e\u3057\u305f\u3002
var s []string
[]string(nil)
make([]string, length)
\u8981\u7d20\u306a\u3057\u3067\u30b9\u30e9\u30a4\u30b9\u3092\u521d\u671f\u5316\u3059\u308b\u5834\u5408\u3001\u6700\u5f8c\u306e\u30aa\u30d7\u30b7\u30e7\u30f3 []string{}
\u306f\u907f\u3051\u308b\u3079\u304d\u3067\u3059\u3002\u6700\u5f8c\u306b\u3001\u4e88\u671f\u3057\u306a\u3044\u52d5\u4f5c\u3092\u9632\u3050\u305f\u3081\u306b\u3001\u4f7f\u7528\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u304c nil \u3068\u7a7a\u306e\u30b9\u30e9\u30a4\u30b9\u3092\u533a\u5225\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#23","title":"\u30b9\u30e9\u30a4\u30b9\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u9069\u5207\u306b\u78ba\u8a8d\u3057\u306a\u3044 (#23)","text":"\u8981\u7d04\u30b9\u30e9\u30a4\u30b9\u306b\u8981\u7d20\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308b\u306b\u306f\u3001\u305d\u306e\u9577\u3055\u3092\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u306f\u3001\u30b9\u30e9\u30a4\u30b9\u304c nil
\u3067\u3042\u308b\u304b\u7a7a\u3067\u3042\u308b\u304b\u306b\u95a2\u4fc2\u306a\u304f\u6a5f\u80fd\u3057\u307e\u3059\u3002\u30de\u30c3\u30d7\u306b\u3064\u3044\u3066\u3082\u540c\u69d8\u3067\u3059\u3002\u660e\u78ba\u306a API \u3092\u8a2d\u8a08\u3059\u308b\u306b\u306f\u3001nil \u30b9\u30e9\u30a4\u30b9\u3068\u7a7a\u306e\u30b9\u30e9\u30a4\u30b9\u3092\u533a\u5225\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002
\u30b9\u30e9\u30a4\u30b9\u306b\u8981\u7d20\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3059\u308b\u306b\u306f\u3001\u30b9\u30e9\u30a4\u30b9\u304c nil \u304b\u3069\u3046\u304b\u3001\u307e\u305f\u306f\u305d\u306e\u9577\u3055\u304c 0 \u306b\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3059\u308b\u3053\u3068\u3067\u5224\u65ad\u3067\u304d\u307e\u3059\u3002\u30b9\u30e9\u30a4\u30b9\u304c\u7a7a\u3067\u3042\u308b\u5834\u5408\u3068\u30b9\u30e9\u30a4\u30b9\u304c nil \u3067\u3042\u308b\u5834\u5408\u306e\u4e21\u65b9\u3092\u30ab\u30d0\u30fc\u3067\u304d\u308b\u305f\u3081\u3001\u9577\u3055\u3092\u78ba\u304b\u3081\u308b\u3053\u3068\u304c\u6700\u826f\u306e\u65b9\u6cd5\u3067\u3059\u3002
\u4e00\u65b9\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u8a2d\u8a08\u3059\u308b\u3068\u304d\u306f\u3001\u8efd\u5fae\u306a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30a8\u30e9\u30fc\u3092\u8d77\u3053\u3055\u306a\u3044\u3088\u3046 nil \u30b9\u30e9\u30a4\u30b9\u3068\u7a7a\u306e\u30b9\u30e9\u30a4\u30b9\u3092\u533a\u5225\u3057\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30b9\u30e9\u30a4\u30b9\u3092\u8fd4\u3059\u3068\u304d\u306b\u3001nil \u307e\u305f\u306f\u7a7a\u306e\u30b9\u30e9\u30a4\u30b9\u3092\u8fd4\u3059\u304b\u3069\u3046\u304b\u306f\u3001\u610f\u5473\u7684\u306b\u3082\u6280\u8853\u7684\u306b\u3082\u9055\u3044\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u30b3\u30fc\u30e9\u30fc\u306b\u3068\u3063\u3066\u306f\u3069\u3061\u3089\u3082\u540c\u3058\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u306f\u305a\u3067\u3059\u3002\u3053\u306e\u539f\u7406\u306f\u9023\u60f3\u914d\u5217\u3067\u3082\u540c\u3058\u3067\u3059\u3002\u9023\u60f3\u914d\u5217\u304c\u7a7a\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3059\u308b\u306b\u306f\u3001\u305d\u308c\u304c nil \u304b\u3069\u3046\u304b\u3067\u306f\u306a\u304f\u3001\u305d\u306e\u9577\u3055\u3092\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#24","title":"\u30b9\u30e9\u30a4\u30b9\u30b3\u30d4\u30fc\u3092\u6b63\u3057\u304f\u4f5c\u6210\u3057\u3066\u3044\u306a\u3044 (#24)","text":"\u8981\u7d04\u7d44\u307f\u8fbc\u307f\u95a2\u6570 copy
\u3092\u4f7f\u7528\u3057\u3066\u3042\u308b\u30b9\u30e9\u30a4\u30b9\u3092\u5225\u306e\u30b9\u30e9\u30a4\u30b9\u306b\u30b3\u30d4\u30fc\u3059\u308b\u306b\u306f\u3001\u30b3\u30d4\u30fc\u3055\u308c\u308b\u8981\u7d20\u306e\u6570\u304c2\u3064\u306e\u30b9\u30e9\u30a4\u30b9\u306e\u9577\u3055\u306e\u9593\u306e\u6700\u5c0f\u5024\u306b\u76f8\u5f53\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u8981\u7d20\u3092\u3042\u308b\u30b9\u30e9\u30a4\u30b9\u304b\u3089\u5225\u306e\u30b9\u30e9\u30a4\u30b9\u306b\u30b3\u30d4\u30fc\u3059\u308b\u64cd\u4f5c\u306f\u3001\u304b\u306a\u308a\u983b\u7e41\u306b\u884c\u308f\u308c\u307e\u3059\u3002\u30b3\u30d4\u30fc\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u30b3\u30d4\u30fc\u5148\u306b\u30b3\u30d4\u30fc\u3055\u308c\u308b\u8981\u7d20\u306e\u6570\u306f 2 \u3064\u306e\u30b9\u30e9\u30a4\u30b9\u306e\u9577\u3055\u306e\u9593\u306e\u6700\u5c0f\u5024\u306b\u76f8\u5f53\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u307e\u305f\u3001\u30b9\u30e9\u30a4\u30b9\u3092\u30b3\u30d4\u30fc\u3059\u308b\u305f\u3081\u306e\u4ed6\u306e\u4ee3\u66ff\u624b\u6bb5\u304c\u5b58\u5728\u3059\u308b\u3053\u3068\u306b\u3082\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u305d\u306e\u305f\u3081\u3001\u30b3\u30fc\u30c9\u30d9\u30fc\u30b9\u3067\u305d\u308c\u3089\u3092\u898b\u3064\u3051\u3066\u3082\u9a5a\u304f\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#25","title":"\u30b9\u30e9\u30a4\u30b9\u8ffd\u52a0\u306e\u4f7f\u7528\u306b\u3088\u308b\u4e88\u671f\u305b\u306c\u526f\u4f5c\u7528 (#25)","text":"\u8981\u7d042\u3064\u306e\u7570\u306a\u308b\u95a2\u6570\u304c\u540c\u3058\u914d\u5217\u306b\u57fa\u3065\u304f\u30b9\u30e9\u30a4\u30b9\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306b\u3001copy \u307e\u305f\u306f\u5b8c\u5168\u306a\u30b9\u30e9\u30a4\u30b9\u5f0f\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067 append
\u306b\u3088\u308b\u885d\u7a81\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u5927\u304d\u306a\u30b9\u30e9\u30a4\u30b9\u3092\u7e2e\u5c0f\u3059\u308b\u5834\u5408\u3001\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u306f\u30b9\u30e9\u30a4\u30b9\u30b3\u30d4\u30fc\u3060\u3051\u3067\u3059\u3002
\u30b9\u30e9\u30a4\u30b9\u3092\u4f7f\u7528\u3059\u308b\u3068\u304d\u306f\u3001\u4e88\u671f\u305b\u306c\u526f\u4f5c\u7528\u306b\u3064\u306a\u304c\u308b\u72b6\u6cc1\u306b\u76f4\u9762\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u7d50\u679c\u306e\u30b9\u30e9\u30a4\u30b9\u306e\u9577\u3055\u304c\u305d\u306e\u5bb9\u91cf\u3088\u308a\u5c0f\u3055\u3044\u5834\u5408\u3001\u8ffd\u52a0\u306b\u3088\u3063\u3066\u5143\u306e\u30b9\u30e9\u30a4\u30b9\u304c\u5909\u66f4\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u8d77\u3053\u308a\u5f97\u308b\u526f\u4f5c\u7528\u306e\u7bc4\u56f2\u3092\u5236\u9650\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u30b9\u30e9\u30a4\u30b9\u30b3\u30d4\u30fc\u307e\u305f\u306f\u5b8c\u5168\u306a\u30b9\u30e9\u30a4\u30b9\u5f0f\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30b3\u30d4\u30fc\u3092\u5b9f\u884c\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002
???+ note\u3000\"\u88dc\u8db3\"
`s[low:high:max]`\uff08\u5b8c\u5168\u306a\u30b9\u30e9\u30a4\u30b9\u5f0f\uff09\uff1a\u3053\u306e\u547d\u4ee4\u6587\u306f\u3001\u5bb9\u91cf\u304c `max - low` \u306b\u7b49\u3057\u3044\u3053\u3068\u3092\u9664\u3051\u3070\u3001`s[low:high]` \u3067\u4f5c\u6210\u3055\u308c\u305f\u30b9\u30e9\u30a4\u30b9\u3068\u540c\u69d8\u306e\u30b9\u30e9\u30a4\u30b9\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\n
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#26","title":"\u30b9\u30e9\u30a4\u30b9\u3068\u30e1\u30e2\u30ea\u30ea\u30fc\u30af (#26)","text":"\u8981\u7d04\u30dd\u30a4\u30f3\u30bf\u306e\u30b9\u30e9\u30a4\u30b9\u307e\u305f\u306f\u30dd\u30a4\u30f3\u30bf\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u6301\u3064\u69cb\u9020\u4f53\u3092\u64cd\u4f5c\u3059\u308b\u5834\u5408\u3001\u30b9\u30e9\u30a4\u30b9\u64cd\u4f5c\u306b\u3088\u3063\u3066\u9664\u5916\u3055\u308c\u305f\u8981\u7d20\u3092 nil \u3068\u3059\u308b\u3053\u3068\u3067\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002
"},{"location":"ja/#_3","title":"\u5bb9\u91cf\u6f0f\u308c","text":"\u5927\u304d\u306a\u30b9\u30e9\u30a4\u30b9\u307e\u305f\u306f\u914d\u5217\u3092\u30b9\u30e9\u30a4\u30b9\u3059\u308b\u3068\u3001\u30e1\u30e2\u30ea\u6d88\u8cbb\u304c\u9ad8\u304f\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u6b8b\u308a\u306e\u30b9\u30da\u30fc\u30b9\u306f GC \u306b\u3088\u3063\u3066\u518d\u5229\u7528\u3055\u308c\u305a\u3001\u5c11\u6570\u306e\u8981\u7d20\u3057\u304b\u4f7f\u7528\u3057\u306a\u3044\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u5927\u304d\u306a\u30d0\u30c3\u30ad\u30f3\u30b0\u914d\u5217\u304c\u4fdd\u6301\u3055\u308c\u307e\u3059\u3002\u30b9\u30e9\u30a4\u30b9\u30b3\u30d4\u30fc\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u3053\u306e\u3088\u3046\u306a\u4e8b\u614b\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#_4","title":"\u30b9\u30e9\u30a4\u30b9\u3068\u30dd\u30a4\u30f3\u30bf\u30fc","text":"\u30dd\u30a4\u30f3\u30bf\u307e\u305f\u306f\u30dd\u30a4\u30f3\u30bf\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u542b\u3080\u69cb\u9020\u4f53\u3092\u4f7f\u7528\u3057\u3066\u30b9\u30e9\u30a4\u30b9\u64cd\u4f5c\u3092\u3059\u308b\u5834\u5408\u3001GC \u304c\u3053\u308c\u3089\u306e\u8981\u7d20\u3092\u518d\u5229\u7528\u3057\u306a\u3044\u3053\u3068\u3092\u77e5\u3063\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u306e\u5834\u5408\u306e\u9078\u629e\u80a2\u306f\u3001\u30b3\u30d4\u30fc\u3092\u5b9f\u884c\u3059\u308b\u304b\u3001\u6b8b\u308a\u306e\u8981\u7d20\u307e\u305f\u306f\u305d\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u660e\u793a\u7684\u306b nil
\u3068\u3059\u308b\u3053\u3068\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#27","title":"\u975e\u52b9\u7387\u306a\u9023\u60f3\u914d\u5217\u306e\u521d\u671f\u5316 (#27)","text":"\u8981\u7d04\u9023\u60f3\u914d\u5217\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u3001\u305d\u306e\u9577\u3055\u304c\u3059\u3067\u306b\u308f\u304b\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u9577\u3055\u3067\u521d\u671f\u5316\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u5272\u308a\u5f53\u3066\u306e\u6570\u304c\u6e1b\u308a\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002
\u9023\u60f3\u914d\u5217\u306f\u3001\u30ad\u30fc\u30fb\u5024\u30da\u30a2\u306e\u9806\u5e8f\u306a\u3057\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u306a\u304a\u3001\u305d\u308c\u305e\u308c\u306e\u30da\u30a2\u306f\u56fa\u6709\u306e\u30ad\u30fc\u3092\u6301\u3061\u307e\u3059\u3002Go\u8a00\u8a9e\u3067\u306f\u3001\u9023\u60f3\u914d\u5217\u306f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u57fa\u3065\u3044\u3066\u3044\u307e\u3059\u3002\u5185\u90e8\u7684\u306b\u306f\u3001\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u306f\u30d0\u30b1\u30c3\u30c8\u306e\u914d\u5217\u3067\u3042\u308a\u3001\u5404\u30d0\u30b1\u30c3\u30c8\u306f\u30ad\u30fc\u30fb\u5024\u30da\u30a2\u306e\u914d\u5217\u3078\u306e\u30dd\u30a4\u30f3\u30bf\u3067\u3059\u3002
\u9023\u60f3\u914d\u5217\u306b\u542b\u307e\u308c\u308b\u8981\u7d20\u306e\u6570\u304c\u4e8b\u524d\u306b\u308f\u304b\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u305d\u306e\u521d\u671f\u30b5\u30a4\u30ba\u3092\u6307\u5b9a\u3057\u3066\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u9023\u60f3\u914d\u5217\u306e\u5897\u5927\u306f\u3001\u5341\u5206\u306a\u30b9\u30da\u30fc\u30b9\u3092\u518d\u5272\u308a\u5f53\u3066\u3057\u3001\u3059\u3079\u3066\u306e\u8981\u7d20\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u518d\u8abf\u6574\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u8a08\u7b97\u91cf\u304c\u975e\u5e38\u306b\u591a\u304f\u306a\u308a\u307e\u3059\u304c\u3001\u3053\u308c\u306b\u3088\u308a\u305d\u308c\u3092\u56de\u907f\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#28","title":"\u9023\u60f3\u914d\u5217\u3068\u30e1\u30e2\u30ea\u30ea\u30fc\u30af (#28)","text":"\u8981\u7d04\u9023\u60f3\u914d\u5217\u306f\u30e1\u30e2\u30ea\u5185\u3067\u5e38\u306b\u5897\u5927\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u7e2e\u5c0f\u3059\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002 \u3057\u305f\u304c\u3063\u3066\u3001\u30e1\u30e2\u30ea\u306e\u554f\u984c\u304c\u767a\u751f\u3059\u308b\u5834\u5408\u306f\u3001\u9023\u60f3\u914d\u5217\u3092\u5f37\u5236\u7684\u306b\u518d\u4f5c\u6210\u3057\u305f\u308a\u3001\u30dd\u30a4\u30f3\u30bf\u3092\u4f7f\u7528\u3057\u305f\u308a\u3059\u308b\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u624b\u6bb5\u3092\u8a66\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\u30bb\u30af\u30b7\u30e7\u30f3\u5168\u6587\u306f\u3053\u3061\u3089.
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#29","title":"\u8aa4\u3063\u305f\u65b9\u6cd5\u306b\u3088\u308b\u5024\u306e\u6bd4\u8f03 (#29)","text":"\u8981\u7d04Go\u8a00\u8a9e\u3067\u578b\u3092\u6bd4\u8f03\u3059\u200b\u200b\u308b\u306b\u306f\u30012 \u3064\u306e\u578b\u304c\u6bd4\u8f03\u53ef\u80fd\u306a\u3089\u3070\u3001== \u6f14\u7b97\u5b50\u3068 != \u6f14\u7b97\u5b50\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u771f\u7406\u5024\u3001\u6570\u5024\u3001\u6587\u5b57\u5217\u3001\u30dd\u30a4\u30f3\u30bf\u3001\u30c1\u30e3\u30cd\u30eb\u3001\u304a\u3088\u3073\u69cb\u9020\u4f53\u304c\u5b8c\u5168\u306b\u6bd4\u8f03\u53ef\u80fd\u306a\u578b\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u305d\u308c\u4ee5\u5916\u306f\u3001 reflect.DeepEqual
\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30d5\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u4ee3\u511f\u3092\u652f\u6255\u3046\u304b\u3001\u72ec\u81ea\u306e\u5b9f\u88c5\u3068\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\u52b9\u679c\u7684\u306b\u6bd4\u8f03\u3059\u308b\u306b\u306f\u3001 ==
\u3068 !=
\u306e\u4f7f\u7528\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u4e0d\u53ef\u6b20\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u6f14\u7b97\u5b50\u306f\u3001\u6bd4\u8f03\u53ef\u80fd\u306a\u88ab\u6f14\u7b97\u5b50\u3067\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002
?
\u3001 >=
\u3001 <
\u3001\u304a\u3088\u3073 >
\u6f14\u7b97\u5b50\u3092\u6570\u5024\u578b\u3067\u4f7f\u7528\u3057\u3066\u5024\u3092\u6bd4\u8f03\u3057\u305f\u308a\u3001\u6587\u5b57\u5217\u3067\u5b57\u53e5\u9806\u5e8f\u3092\u6bd4\u8f03\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002
\u88ab\u6f14\u7b97\u5b50\u304c\u6bd4\u8f03\u3067\u304d\u306a\u3044\u5834\u5408\uff08\u30b9\u30e9\u30a4\u30b9\u3068\u9023\u60f3\u914d\u5217\u306a\u3069\uff09\u3001\u30ea\u30d5\u30ec\u30af\u30b7\u30e7\u30f3\u306a\u3069\u306e\u4ed6\u306e\u65b9\u6cd5\u3092\u5229\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30ea\u30d5\u30ec\u30af\u30b7\u30e7\u30f3\u306f\u30e1\u30bf\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u4e00\u7a2e\u3067\u3042\u308a\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u305d\u306e\u69cb\u9020\u3068\u52d5\u4f5c\u3092\u5185\u7701\u3057\u3066\u5909\u66f4\u3059\u308b\u6a5f\u80fd\u3092\u6307\u3057\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001Go\u8a00\u8a9e\u3067\u306f reflect.DeepEqual
\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u95a2\u6570\u306f\u30012\u3064\u306e\u5024\u3092\u518d\u5e30\u7684\u306b\u8abf\u3079\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u30012\u3064\u306e\u8981\u7d20\u304c\u5b8c\u5168\u306b\u7b49\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u5831\u544a\u3057\u307e\u3059\u3002\u53d7\u3051\u5165\u308c\u3089\u308c\u308b\u8981\u7d20\u306f\u3001\u57fa\u672c\u7684\u306a\u578b\u306b\u52a0\u3048\u3066\u3001\u914d\u5217\u3001\u69cb\u9020\u4f53\u3001\u30b9\u30e9\u30a4\u30b9\u3001\u9023\u60f3\u914d\u5217\u3001\u30dd\u30a4\u30f3\u30bf\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3001\u95a2\u6570\u3067\u3059\u3002\u3057\u304b\u3057\u3001\u6700\u5927\u306e\u843d\u3068\u3057\u7a74\u306f\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u4e0a\u306e\u30da\u30ca\u30eb\u30c6\u30a3\u3067\u3059\u3002
\u5b9f\u884c\u6642\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u91cd\u8981\u306a\u5834\u5408\u306f\u3001\u72ec\u81ea\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u6700\u5584\u306e\u89e3\u6c7a\u7b56\u3068\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002 \u8ffd\u8a18\uff1a\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u306f\u65e2\u306b\u6bd4\u8f03\u30e1\u30bd\u30c3\u30c9\u304c\u3044\u304f\u3064\u304b\u3042\u308b\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u6700\u9069\u5316\u3055\u308c\u305f bytes.Compare
\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u30012\u3064\u306e\u30d0\u30a4\u30c8\u30b9\u30e9\u30a4\u30b9\u3092\u6bd4\u8f03\u3067\u304d\u307e\u3059\u3002\u72ec\u81ea\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3059\u308b\u524d\u306b\u3001\u8eca\u8f2a\u306e\u518d\u767a\u660e\u3092\u3057\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#_5","title":"\u69cb\u9020\u306e\u5236\u5fa1","text":""},{"location":"ja/#range-30","title":"\u8981\u7d20\u304crange
\u30eb\u30fc\u30d7\u5185\u3067\u30b3\u30d4\u30fc\u3055\u308c\u308b\u3053\u3068\u3092\u77e5\u3089\u306a\u3044 (#30)","text":"\u8981\u7d04 range
\u30eb\u30fc\u30d7\u5185\u306e value \u8981\u7d20\u306f\u30b3\u30d4\u30fc\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u305f\u3068\u3048\u3070\u69cb\u9020\u4f53\u3092\u5909\u66f4\u3059\u308b\u306b\u306f\u3001\u305d\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4ecb\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3059\u308b\u304b\u3001\u5f93\u6765\u306e for
\u30eb\u30fc\u30d7\u3092\u4ecb\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3057\u3087\u3046\uff08\u5909\u66f4\u3059\u308b\u8981\u7d20\u307e\u305f\u306f\u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u30dd\u30a4\u30f3\u30bf\u3067\u3042\u308b\u5834\u5408\u3092\u9664\u304f\uff09\u3002
range \u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u3055\u307e\u3056\u307e\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u53cd\u5fa9\u51e6\u7406\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\u53e4\u5178\u7684\u306a for
\u30eb\u30fc\u30d7\u3068\u6bd4\u8f03\u3059\u308b\u3068\u3001range
\u30eb\u30fc\u30d7\u306f\u305d\u306e\u7c21\u6f54\u306a\u69cb\u6587\u306e\u304a\u304b\u3052\u3067\u3001\u3053\u308c\u3089\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u3059\u3079\u3066\u306e\u8981\u7d20\u306b\u53cd\u5fa9\u51e6\u7406\u3092\u3059\u308b\u306e\u306b\u4fbf\u5229\u3067\u3059\u3002
\u305f\u3060\u3057\u3001range \u30eb\u30fc\u30d7\u5185\u306e\u5024\u8981\u7d20\u306f\u30b3\u30d4\u30fc\u3067\u3042\u308b\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5024\u3092\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u69cb\u9020\u4f53\u306e\u5834\u5408\u3001\u5909\u66f4\u3059\u308b\u5024\u307e\u305f\u306f\u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u30dd\u30a4\u30f3\u30bf\u3067\u306a\u3044\u9650\u308a\u3001\u8981\u7d20\u81ea\u4f53\u3067\u306f\u306a\u304f\u30b3\u30d4\u30fc\u306e\u307f\u3092\u66f4\u65b0\u3057\u307e\u3059\u3002range \u30eb\u30fc\u30d7\u307e\u305f\u306f\u5f93\u6765\u306e for \u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u7d4c\u7531\u3067\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u63a8\u5968\u3055\u308c\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#range-31","title":"range
\u30eb\u30fc\u30d7\uff08\u30c1\u30e3\u30cd\u30eb\u3068\u914d\u5217\uff09\u3067\u306e\u5f15\u6570\u306e\u8a55\u4fa1\u65b9\u6cd5\u3092\u77e5\u3089\u306a\u3044 (#31)","text":"\u8981\u7d04 range
\u6f14\u7b97\u5b50\u306b\u6e21\u3055\u308c\u308b\u5f0f\u306f\u30eb\u30fc\u30d7\u306e\u958b\u59cb\u524d\u306b 1 \u56de\u3060\u3051\u8a55\u4fa1\u3055\u308c\u308b\u3053\u3068\u3092\u7406\u89e3\u3059\u308b\u3068\u3001\u30c1\u30e3\u30cd\u30eb\u307e\u305f\u306f\u30b9\u30e9\u30a4\u30b9\u306e\u53cd\u5fa9\u51e6\u7406\u306b\u304a\u3051\u308b\u975e\u52b9\u7387\u306a\u5272\u308a\u5f53\u3066\u306a\u3069\u306e\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002
range \u30eb\u30fc\u30d7\u306f\u3001\uff08\u30bf\u30a4\u30d7\u306b\u95a2\u4fc2\u306a\u304f\uff09\u30b3\u30d4\u30fc\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u306b\u3088\u308a\u3001\u30eb\u30fc\u30d7\u306e\u958b\u59cb\u524d\u306b\u3001\u6307\u5b9a\u3055\u308c\u305f\u5f0f\u3092 1 \u56de\u3060\u3051\u8a55\u4fa1\u3057\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u8aa4\u3063\u305f\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3057\u307e\u3046\u3001\u3068\u3044\u3046\u3088\u3046\u306a\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u3053\u306e\u52d5\u4f5c\u3092\u899a\u3048\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070
a := [3]int{0, 1, 2}\nfor i, v := range a {\na[2] = 10\nif i == 2 {\nfmt.Println(v)\n}\n}\n
\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001\u6700\u5f8c\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092 10 \u306b\u66f4\u65b0\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068\u300110 \u306f\u51fa\u529b\u3055\u308c\u307e\u305b\u3093\u3002 2 \u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#range-32","title":"range
\u30eb\u30fc\u30d7\u5185\u306b\u304a\u3051\u308b\u30dd\u30a4\u30f3\u30bf\u8981\u7d20\u306e\u4f7f\u7528\u304c\u53ca\u307c\u3059\u5f71\u97ff\u3092\u5206\u304b\u3063\u3066\u3044\u306a\u3044 (#32)","text":"\u8981\u7d04 \u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3092\u4f7f\u7528\u3059\u308b\u304b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3057\u3066\u8981\u7d20\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001\u30eb\u30fc\u30d7\u5185\u3067\u30dd\u30a4\u30f3\u30bf\u3092\u30b3\u30d4\u30fc\u3059\u308b\u969b\u306e\u9593\u9055\u3044\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
range
\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u53cd\u5fa9\u51e6\u7406\u3092\u65bd\u3059\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u5024\u304c\u5358\u4e00\u306e\u4e00\u610f\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u6301\u3064\u4e00\u610f\u306e\u5909\u6570\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u308b\u3053\u3068\u3092\u601d\u3044\u51fa\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3086\u3048\u306b\u3001\u5404\u53cd\u5fa9\u51e6\u7406\u4e2d\u306b\u3053\u306e\u5909\u6570\u3092\u53c2\u7167\u3059\u308b\u30dd\u30a4\u30f3\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u3068\u3001\u540c\u3058\u8981\u7d20\u3001\u3064\u307e\u308a\u6700\u65b0\u306e\u8981\u7d20\u3092\u53c2\u7167\u3059\u308b\u540c\u3058\u30dd\u30a4\u30f3\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u306e\u554f\u984c\u306f\u3001\u30eb\u30fc\u30d7\u306e\u30b9\u30b3\u30fc\u30d7\u5185\u306b\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3092\u5f37\u5236\u7684\u306b\u4f5c\u6210\u3059\u308b\u304b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4ecb\u3057\u3066\u30b9\u30e9\u30a4\u30b9\u8981\u7d20\u3092\u53c2\u7167\u3059\u308b\u30dd\u30a4\u30f3\u30bf\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u3067\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002\u3069\u3061\u3089\u306e\u89e3\u6c7a\u7b56\u3067\u3082\u554f\u984c\u3042\u308a\u307e\u305b\u3093\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#33","title":"\u9023\u60f3\u914d\u5217\u306e\u53cd\u5fa9\u51e6\u7406\u4e2d\u306b\u8aa4\u3063\u305f\u4eee\u5b9a\u3092\u3059\u308b\uff08\u53cd\u5fa9\u51e6\u7406\u4e2d\u306e\u9806\u5e8f\u4ed8\u3051\u3068\u9023\u60f3\u914d\u5217\u306e\u633f\u5165\uff09 (#33)","text":"\u8981\u7d04\u9023\u60f3\u914d\u5217\u3092\u4f7f\u7528\u3059\u308b\u3068\u304d\u306b\u4e88\u6e2c\u53ef\u80fd\u306a\u51fa\u529b\u3092\u4fdd\u8a3c\u3059\u308b\u306b\u306f\u3001\u9023\u60f3\u914d\u5217\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u304c\u6b21\u306e\u3068\u304a\u308a\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#break-34","title":"break
\u6587\u304c\u3069\u306e\u3088\u3046\u306b\u6a5f\u80fd\u3059\u308b\u304b\u3092\u5206\u304b\u3063\u3066\u3044\u306a\u3044 (#34)","text":"\u8981\u7d04 \u30e9\u30d9\u30eb\u3068 break
\u307e\u305f\u306f continue
\u306e\u4f75\u7528\u306f\u3001\u7279\u5b9a\u306e\u547d\u4ee4\u6587\u3092\u5f37\u5236\u7684\u306b\u4e2d\u65ad\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u30eb\u30fc\u30d7\u5185\u306e switch
\u307e\u305f\u306f select
\u6587\u3067\u5f79\u7acb\u3061\u307e\u3059\u3002
\u901a\u5e38\u3001break \u6587\u306f\u30eb\u30fc\u30d7\u306e\u5b9f\u884c\u3092\u7d42\u4e86\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u30eb\u30fc\u30d7\u304c switch \u307e\u305f\u306f select \u3068\u7d44\u307f\u5408\u308f\u305b\u3066\u4f7f\u7528\u200b\u200b\u3055\u308c\u308b\u5834\u5408\u3001\u76ee\u7684\u306e\u547d\u4ee4\u6587\u3067\u306f\u306a\u3044\u306e\u306b\u4e2d\u65ad\u3055\u305b\u3066\u3057\u307e\u3046\u3001\u3068\u3044\u3046\u30df\u30b9\u3092\u3059\u308b\u3053\u3068\u304c\u958b\u767a\u8005\u306b\u306f\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070
for i := 0; i < 5; i++ {\nfmt.Printf(\"%d \", i)\nswitch i {\ndefault:\ncase 2:\nbreak\n}\n}\n
break \u6587\u306f for
\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\u3055\u305b\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u4ee3\u308f\u308a\u306b switch
\u6587\u3092\u7d42\u4e86\u3055\u305b\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u30b3\u30fc\u30c9\u306f 0 \u304b\u3089 2 \u307e\u3067\u3092\u53cd\u5fa9\u3059\u308b\u4ee3\u308f\u308a\u306b\u30010 \u304b\u3089 4 \u307e\u3067\u3092\u53cd\u5fa9\u3057\u307e\u3059\uff080 1 2 3 4
\uff09\u3002
\u899a\u3048\u3066\u304a\u304f\u3079\u304d\u91cd\u8981\u306a\u30eb\u30fc\u30eb\u306e1\u3064\u306f\u3001 break
\u6587\u306f\u6700\u3082\u5185\u5074\u306e for
\u3001switch
\u3001\u307e\u305f\u306f select
\u6587\u306e\u5b9f\u884c\u3092\u7d42\u4e86\u3059\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u524d\u306e\u4f8b\u3067\u306f\u3001switch
\u6587\u3092\u7d42\u4e86\u3057\u307e\u3059\u3002
switch
\u6587\u306e\u4ee3\u308f\u308a\u306b\u30eb\u30fc\u30d7\u3092\u4e2d\u65ad\u3059\u308b\u6700\u3082\u6163\u7528\u7684\u306a\u65b9\u6cd5\u306f\u30e9\u30d9\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3059\u3002
loop:\nfor i := 0; i < 5; i++ {\nfmt.Printf(\"%d \", i)\nswitch i {\ndefault:\ncase 2:\nbreak loop\n}\n}\n
\u3053\u3053\u3067\u306f\u3001loop
\u30e9\u30d9\u30eb\u3092 for
\u30eb\u30fc\u30d7\u306b\u95a2\u9023\u4ed8\u3051\u307e\u3059\u3002 \u6b21\u306b\u3001break
\u6587\u306b loop
\u30e9\u30d9\u30eb\u3092\u6307\u5b9a\u3059\u308b\u306e\u3067\u3001switch \u3067\u306f\u306a\u304f loop \u304c\u4e2d\u65ad\u3055\u308c\u307e\u3059\u3002\u3088\u3063\u3066\u3001\u3053\u306e\u65b0\u3057\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u306f\u4e88\u60f3\u3069\u304a\u308a 0 1 2
\u3092\u51fa\u529b\u3057\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#defer-35","title":"\u30eb\u30fc\u30d7\u5185\u3067defer
\u3092\u4f7f\u7528\u3059\u308b (#35)","text":"\u8981\u7d04 \u95a2\u6570\u5185\u306e\u30eb\u30fc\u30d7\u30ed\u30b8\u30c3\u30af\u3092\u62bd\u51fa\u3059\u308b\u3068\u3001\u5404\u53cd\u5fa9\u306e\u6700\u5f8c\u306b defer
\u6587\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002
defer
\u6587\u306f\u3001\u4e0a\u4f4d\u30d6\u30ed\u30c3\u30af\u306e\u95a2\u6570\u304c\u623b\u308b\u307e\u3067\u547c\u3073\u51fa\u3057\u306e\u5b9f\u884c\u3092\u9045\u3089\u305b\u307e\u3059\u3002\u3053\u308c\u306f\u4e3b\u306b\u5b9a\u578b\u30b3\u30fc\u30c9\u3092\u524a\u6e1b\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u30ea\u30bd\u30fc\u30b9\u3092\u6700\u7d42\u7684\u306b\u9589\u3058\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u3001defer
\u3092\u4f7f\u7528\u3057\u3066\u3001return
\u3092\u5b9f\u884c\u3059\u308b\u524d\u306b\u30af\u30ed\u30fc\u30b8\u30e3\u547c\u3073\u51fa\u3057\u3092\u7e70\u308a\u8fd4\u3059\u3053\u3068\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
defer
\u3067\u3088\u304f\u3042\u308b\u30df\u30b9\u306e1\u3064\u306f\u3001\u4e0a\u4f4d\u30d6\u30ed\u30c3\u30af \u306e\u95a2\u6570\u304c\u623b\u3063\u305f\u3068\u304d\u306b\u95a2\u6570\u547c\u3073\u51fa\u3057\u304c\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\u3055\u308c\u308b\u3053\u3068\u3092\u5fd8\u308c\u308b\u3053\u3068\u3067\u3059\u3002\u305f\u3068\u3048\u3070
func readFiles(ch <-chan string) error {\nfor path := range ch {\nfile, err := os.Open(path)\nif err != nil {\nreturn err\n}\ndefer file.Close()\n// \u30d5\u30a1\u30a4\u30eb\u306b\u4f55\u3089\u304b\u306e\u51e6\u7406\u3092\u3059\u308b\n}\nreturn nil\n}\n
defer
\u547c\u3073\u51fa\u3057\u306f\u3001\u5404\u30eb\u30fc\u30d7\u53cd\u5fa9\u4e2d\u3067\u306f\u306a\u304f\u3001readFiles
\u95a2\u6570\u304c\u8fd4\u3055\u308c\u305f\u3068\u304d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002 readFiles
\u304c\u8fd4\u3089\u306a\u3044\u5834\u5408\u3001\u30d5\u30a1\u30a4\u30eb\u8a18\u8ff0\u5b50\u306f\u6c38\u4e45\u306b\u958b\u3044\u305f\u307e\u307e\u306b\u306a\u308a\u3001\u30ea\u30fc\u30af\u304c\u767a\u751f\u3057\u307e\u3059\u3002
\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u4e00\u822c\u7684\u306a\u624b\u6bb5\u306e1\u3064\u306f\u3001 defer
\u306e\u5f8c\u306b\u3001\u5404\u53cd\u5fa9\u4e2d\u306b\u547c\u3073\u51fa\u3055\u308c\u308b\u4e0a\u4f4d\u30d6\u30ed\u30c3\u30af\u306e\u95a2\u6570\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u3067\u3059\u3002
func readFiles(ch <-chan string) error {\nfor path := range ch {\nif err := readFile(path); err != nil {\nreturn err\n}\n}\nreturn nil\n}\nfunc readFile(path string) error {\nfile, err := os.Open(path)\nif err != nil {\nreturn err\n}\ndefer file.Close()\n// \u30d5\u30a1\u30a4\u30eb\u306b\u4f55\u3089\u304b\u306e\u51e6\u7406\u3092\u3059\u308b\nreturn nil\n}\n
\u5225\u306e\u89e3\u6c7a\u7b56\u306f\u3001readFile
\u95a2\u6570\u3092\u30af\u30ed\u30fc\u30b8\u30e3\u306b\u3059\u308b\u3053\u3068\u3067\u3059\u304c\u3001\u672c\u8cea\u7684\u306b\u306f\u540c\u3058\u3067\u3059\u3002\u5225\u306e\u4e0a\u4f4d\u30d6\u30ed\u30c3\u30af\u306e\u95a2\u6570\u3092\u8ffd\u52a0\u3057\u3066\u3001\u5404\u53cd\u5fa9\u4e2d\u306b defer
\u547c\u3073\u51fa\u3057\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#_6","title":"\u6587\u5b57\u5217","text":""},{"location":"ja/#36","title":"\u30eb\u30fc\u30f3\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#36)","text":"\u8981\u7d04\u30eb\u30fc\u30f3\u304c Unicode \u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u306e\u6982\u5ff5\u306b\u5bfe\u5fdc\u3057\u3001\u8907\u6570\u306e\u30d0\u30a4\u30c8\u3067\u69cb\u6210\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u306f\u3001 Go \u958b\u767a\u8005\u304c\u6587\u5b57\u5217\u3092\u6b63\u78ba\u306b\u64cd\u4f5c\u3059\u308b\u305f\u3081\u306b\u4e0d\u53ef\u6b20\u3067\u3059\u3002
Go\u8a00\u8a9e\u3067\u306f\u30eb\u30fc\u30f3\u304c\u3042\u3089\u3086\u308b\u5834\u6240\u306b\u4f7f\u7528\u3055\u308c\u308b\u305f\u3081\u3001\u6b21\u306e\u70b9\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002
rune
\u306f Unicode \u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u306e\u6982\u5ff5\u306b\u5bfe\u5fdc\u3057\u3001\u5358\u4e00\u306e\u5024\u3067\u8868\u3055\u308c\u308b\u30a2\u30a4\u30c6\u30e0\u3092\u610f\u5473\u3057\u307e\u3059\u3002len()
\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30eb\u30fc\u30f3\u6570\u3067\u306f\u306a\u304f\u30d0\u30a4\u30c8\u6570\u304c\u8fd4\u3055\u308c\u307e\u3059\u3002\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#37","title":"\u6587\u5b57\u5217\u306b\u5bfe\u3059\u308b\u4e0d\u6b63\u78ba\u306a\u53cd\u5fa9\u51e6\u7406 (#37)","text":"\u8981\u7d04range
\u6f14\u7b97\u5b50\u3092\u4f7f\u7528\u3057\u3066\u6587\u5b57\u5217\u3092\u53cd\u5fa9\u51e6\u7406\u3059\u308b\u3068\u3001\u30eb\u30fc\u30f3\u306e\u30d0\u30a4\u30c8\u30b7\u30fc\u30b1\u30f3\u30b9\u306e\u958b\u59cb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5bfe\u5fdc\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3057\u3066\u30eb\u30fc\u30f3\u304c\u53cd\u5fa9\u51e6\u7406\u3055\u308c\u307e\u3059\u3002\u7279\u5b9a\u306e\u30eb\u30fc\u30f3\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\uff08 3 \u756a\u76ee\u306e\u30eb\u30fc\u30f3\u306a\u3069\uff09\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u306b\u306f\u3001\u6587\u5b57\u5217\u3092 []rune
\u306b\u5909\u63db\u3057\u307e\u3059\u3002
\u6587\u5b57\u5217\u306e\u53cd\u5fa9\u51e6\u7406\u306f\u3001\u958b\u767a\u8005\u306b\u3068\u3063\u3066\u4e00\u822c\u7684\u306a\u64cd\u4f5c\u3067\u3059\u3002\u304a\u305d\u3089\u304f\u3001\u6587\u5b57\u5217\u5185\u306e\u5404\u30eb\u30fc\u30f3\u306b\u5bfe\u3057\u3066\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u304b\u3001\u7279\u5b9a\u306e\u90e8\u5206\u6587\u5b57\u5217\u3092\u691c\u7d22\u3059\u308b\u72ec\u81ea\u306e\u95a2\u6570\u3092\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3067\u3057\u3087\u3046\u3002\u3069\u3061\u3089\u306e\u5834\u5408\u3082\u3001\u6587\u5b57\u5217\u306e\u7570\u306a\u308b\u30eb\u30fc\u30f3\u3092\u53cd\u5fa9\u51e6\u7406\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u53cd\u5fa9\u304c\u3069\u306e\u3088\u3046\u306b\u6a5f\u80fd\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u306f\u56f0\u60d1\u3057\u3084\u3059\u3044\u3067\u3059\u3002
\u6b21\u306e\u4f8b\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002
s := \"h\u00eallo\"\nfor i := range s {\nfmt.Printf(\"position %d: %c\\n\", i, s[i])\n}\nfmt.Printf(\"len=%d\\n\", len(s))\n
position 0: h\nposition 1: \u00c3\nposition 3: l\nposition 4: l\nposition 5: o\nlen=6\n
\u6df7\u4e71\u3092\u62db\u304f\u53ef\u80fd\u6027\u306e\u3042\u308b3\u3064\u306e\u70b9\u3092\u53d6\u308a\u4e0a\u3052\u307e\u3057\u3087\u3046\u3002
\u6700\u5f8c\u306e\u7d50\u679c\u304b\u3089\u898b\u3066\u304d\u307e\u3057\u3087\u3046\u3002len \u306f\u30eb\u30fc\u30f3\u306e\u6570\u3067\u306f\u306a\u304f\u3001\u6587\u5b57\u5217\u5185\u306e\u30d0\u30a4\u30c8\u6570\u3092\u8fd4\u3059\u3053\u3068\u306f\u3059\u3067\u306b\u8ff0\u3079\u307e\u3057\u305f\u3002\u6587\u5b57\u5217\u30ea\u30c6\u30e9\u30eb\u3092 s
\u306b\u5272\u308a\u5f53\u3066\u3066\u3044\u308b\u305f\u3081\u3001s
\u306f UTF-8 \u6587\u5b57\u5217\u3067\u3059\u3002\u4e00\u65b9\u3001\u7279\u6b8a\u6587\u5b57\u300c\u00ea\u300d\u306f 1 \u30d0\u30a4\u30c8\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u307e\u305b\u3093\u3002 2 \u30d0\u30a4\u30c8\u5fc5\u8981\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001len(s)
\u3092\u547c\u3073\u51fa\u3059\u3068 6 \u304c\u8fd4\u3055\u308c\u307e\u3059\u3002
\u524d\u306e\u4f8b\u3067\u306f\u3001\u5404\u30eb\u30fc\u30f3\u3092\u53cd\u5fa9\u51e6\u7406\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u3092\u7406\u89e3\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee3\u308f\u308a\u306b\u3001\u30eb\u30fc\u30f3\u306e\u5404\u958b\u59cb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53cd\u5fa9\u51e6\u7406\u3057\u307e\u3059\u3002
s[i]
\u3092\u51fa\u529b\u3057\u3066\u3082 i \u756a\u76ee\u306e\u30eb\u30fc\u30f3\u306f\u51fa\u529b\u3055\u308c\u307e\u305b\u3093\u3002\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 i
\u306e\u30d0\u30a4\u30c8\u306e UTF-8 \u8868\u73fe\u3092\u51fa\u529b\u3057\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001 h\u00eallo
\u306e\u4ee3\u308f\u308a\u306b h\u00c3llo
\u3092\u51fa\u529b\u304c\u3055\u308c\u307e\u3059\u3002
\u3055\u307e\u3056\u307e\u306a\u30eb\u30fc\u30f3\u6587\u5b57\u3092\u3059\u3079\u3066\u51fa\u529b\u3057\u305f\u3044\u5834\u5408\u306f\u3001 range
\u6f14\u7b97\u5b50\u306e value \u8981\u7d20\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
s := \"h\u00eallo\"\nfor i, r := range s {\nfmt.Printf(\"position %d: %c\\n\", i, r)\n}\n
\u307e\u305f\u306f\u3001\u6587\u5b57\u5217\u3092\u30eb\u30fc\u30f3\u306e\u30b9\u30e9\u30a4\u30b9\u306b\u5909\u63db\u3057\u3001\u305d\u308c\u3092\u53cd\u5fa9\u51e6\u7406\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002
s := \"h\u00eallo\"\nrunes := []rune(s)\nfor i, r := range runes {\nfmt.Printf(\"position %d: %c\\n\", i, r)\n}\n
\u3053\u306e\u89e3\u6c7a\u7b56\u3067\u306f\u3001\u4ee5\u524d\u306e\u89e3\u6c7a\u7b56\u3068\u6bd4\u8f03\u3057\u3066\u5b9f\u884c\u6642\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u767a\u751f\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5b9f\u969b\u3001\u6587\u5b57\u5217\u3092\u30eb\u30fc\u30f3\u306e\u30b9\u30e9\u30a4\u30b9\u306b\u5909\u63db\u3059\u308b\u306b\u306f\u3001\u8ffd\u52a0\u306e\u30b9\u30e9\u30a4\u30b9\u3092\u5272\u308a\u5f53\u3066\u3001\u30d0\u30a4\u30c8\u3092\u30eb\u30fc\u30f3\u306b\u5909\u63db\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6587\u5b57\u5217\u306e\u30d0\u30a4\u30c8\u6570\u3092 n \u3068\u3059\u308b\u3068\u3001\u6642\u9593\u8a08\u7b97\u91cf\u306f O(n) \u306b\u306a\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3059\u3079\u3066\u306e\u30eb\u30fc\u30f3\u3092\u53cd\u5fa9\u51e6\u7406\u3059\u308b\u5834\u5408\u306f\u3001\u6700\u521d\u306e\u89e3\u6c7a\u7b56\u3092\u4f7f\u7528\u3059\u308b\u3079\u304d\u3067\u3059\u3002
\u305f\u3060\u3057\u3001\u6700\u521d\u306e\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u6587\u5b57\u5217\u306e i \u756a\u76ee\u306e\u30eb\u30fc\u30f3\u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u30eb\u30fc\u30f3\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u305b\u3093\u3002\u4ee3\u308f\u308a\u306b\u3001\u30d0\u30a4\u30c8\u30b7\u30fc\u30b1\u30f3\u30b9\u5185\u306e\u30eb\u30fc\u30f3\u306e\u958b\u59cb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u308f\u304b\u308a\u307e\u3059\u3002
s := \"h\u00eallo\"\nr := []rune(s)[4]\nfmt.Printf(\"%c\\n\", r) // o\n
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#trim-38","title":"trim\u95a2\u6570\u306e\u8aa4\u7528 (#38)","text":"\u8981\u7d04strings.TrimRight
\u30fb strings.TrimLeft
\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u30bb\u30c3\u30c8\u306b\u542b\u307e\u308c\u308b\u3059\u3079\u3066\u306e\u672b\u5c3e\u30fb\u5148\u982d\u306e\u30eb\u30fc\u30f3\u3092\u524a\u9664\u3057\u307e\u3059\u304c\u3001 strings.TrimSuffix
\u30fb strings.TrimPrefix
\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u63a5\u5c3e\u8f9e\u30fb\u63a5\u982d\u8f9e\u306e\u306a\u3044\u6587\u5b57\u5217\u3092\u8fd4\u3057\u307e\u3059\u3002
\u305f\u3068\u3048\u3070
fmt.Println(strings.TrimRight(\"123oxo\", \"xo\"))\n
\u306f 123 \u3092\u51fa\u529b\u3057\u307e\u3059
\u9006\u306b\u3001 strings.TrimLeft
\u306f\u3001\u30bb\u30c3\u30c8\u306b\u542b\u307e\u308c\u308b\u5148\u982d\u306e\u30eb\u30fc\u30f3\u3092\u3059\u3079\u3066\u524a\u9664\u3057\u307e\u3059\u3002
\u4e00\u65b9\u3001strings.TrimSuffix
\u30fb strings.TrimPrefix
\u306f\u3001\u6307\u5b9a\u3055\u308c\u305f\u672b\u5c3e\u306e\u63a5\u5c3e\u8f9e\u30fb\u63a5\u982d\u8f9e\u3092\u9664\u3044\u305f\u6587\u5b57\u5217\u3092\u8fd4\u3057\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#39","title":"\u6700\u9069\u5316\u304c\u4e0d\u5341\u5206\u306a\u6587\u5b57\u5217\u306e\u9023\u7d50 (#39)","text":"\u8981\u7d04\u6587\u5b57\u5217\u306e\u30ea\u30b9\u30c8\u306e\u9023\u7d50\u306f\u3001\u53cd\u5fa9\u3054\u3068\u306b\u65b0\u3057\u3044\u6587\u5b57\u5217\u304c\u5272\u308a\u5f53\u3066\u3089\u308c\u306a\u3044\u3088\u3046\u306b\u3001strings.Builder
\u3092\u4f7f\u7528\u3057\u3066\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
+=
\u6f14\u7b97\u5b50\u3092\u7528\u3044\u3066\u30b9\u30e9\u30a4\u30b9\u306e\u3059\u3079\u3066\u306e\u6587\u5b57\u5217\u8981\u7d20\u3092\u9023\u7d50\u3059\u308b concat
\u95a2\u6570\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002
func concat(values []string) string {\ns := \"\"\nfor _, value := range values {\ns += value\n}\nreturn s\n}\n
\u5404\u53cd\u5fa9\u4e2d\u306b\u3001 +=
\u6f14\u7b97\u5b50\u306f s
\u3068 value \u6587\u5b57\u5217\u3092\u9023\u7d50\u3057\u307e\u3059\u3002\u4e00\u898b\u3059\u308b\u3068\u3001\u3053\u306e\u95a2\u6570\u306f\u9593\u9055\u3063\u3066\u3044\u306a\u3044\u3088\u3046\u306b\u898b\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u5b9f\u88c5\u306f\u3001\u6587\u5b57\u5217\u306e\u6838\u3068\u306a\u308b\u7279\u6027\u306e1\u3064\u3067\u3042\u308b\u4e0d\u5909\u6027\u3092\u5fd8\u308c\u3066\u3044\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u5404\u53cd\u5fa9\u3067\u306f s
\u306f\u66f4\u65b0\u3055\u308c\u307e\u305b\u3093\u3002\u30e1\u30e2\u30ea\u5185\u306b\u65b0\u3057\u3044\u6587\u5b57\u5217\u3092\u518d\u5272\u308a\u5f53\u3066\u3059\u308b\u305f\u3081\u3001\u3053\u306e\u95a2\u6570\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u5927\u304d\u306a\u5f71\u97ff\u3092\u4e0e\u3048\u307e\u3059\u3002
\u5e78\u3044\u306a\u3053\u3068\u306b\u3001 strings.Builder
\u3092\u7528\u3044\u308b\u3053\u3068\u3067\u3001\u3053\u306e\u554f\u984c\u306b\u5bfe\u51e6\u3059\u308b\u89e3\u6c7a\u7b56\u304c\u3042\u308a\u307e\u3059\u3002
func concat(values []string) string {\nsb := strings.Builder{}\nfor _, value := range values {\n_, _ = sb.WriteString(value)\n}\nreturn sb.String()\n}\n
\u5404\u53cd\u5fa9\u4e2d\u306b\u3001value \u306e\u5185\u5bb9\u3092\u5185\u90e8\u30d0\u30c3\u30d5\u30a1\u306b\u8ffd\u52a0\u3059\u308b WriteString
\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u7d50\u679c\u306e\u6587\u5b57\u5217\u3092\u69cb\u7bc9\u3057\u3001\u30e1\u30e2\u30ea\u306e\u30b3\u30d4\u30fc\u3092\u6700\u5c0f\u9650\u306b\u6291\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002
WriteString
\u306f 2 \u756a\u76ee\u306e\u51fa\u529b\u3068\u3057\u3066\u30a8\u30e9\u30fc\u3092\u8fd4\u3057\u307e\u3059\u304c\u3001\u610f\u56f3\u7684\u306b\u7121\u8996\u3057\u307e\u3057\u3087\u3046\u3002\u5b9f\u969b\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f nil \u30a8\u30e9\u30fc\u4ee5\u5916\u3092\u8fd4\u3059\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3067\u306f\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u30b7\u30b0\u30cd\u30c1\u30e3\u306e\u4e00\u90e8\u3068\u3057\u3066\u30a8\u30e9\u30fc\u3092\u8fd4\u3059\u76ee\u7684\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\u3002strings.Builder
\u306f io.StringWriter
\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u5b9f\u88c5\u3057\u3066\u304a\u308a\u3001\u3053\u308c\u306b\u306f WriteString(s string) (n int, err error)
\u3068\u3044\u30461\u3064\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306b\u6e96\u62e0\u3059\u308b\u306b\u306f\u3001WriteString
\u306f\u30a8\u30e9\u30fc\u3092\u8fd4\u3055\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u306e\u3067\u3059\u3002
\u5185\u90e8\u7684\u306b\u306f\u3001strings.Builder
\u306f\u30d0\u30a4\u30c8\u30b9\u30e9\u30a4\u30b9\u3092\u4fdd\u6301\u3057\u307e\u3059\u3002 WriteString
\u3092\u547c\u3073\u51fa\u3059\u305f\u3073\u306b\u3001\u3053\u306e\u30b9\u30e9\u30a4\u30b9\u306b\u8ffd\u52a0\u3059\u308b\u547c\u3073\u51fa\u3057\u304c\u884c\u308f\u308c\u307e\u3059\u3002\u3053\u308c\u306b\u306f2\u3064\u306e\u5f71\u97ff\u304c\u3042\u308a\u307e\u3059\u3002\u307e\u305a\u3001 append
\u306e\u547c\u3073\u51fa\u3057\u304c\u885d\u7a81\u72b6\u614b\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u3053\u306e\u69cb\u9020\u4f53\u306f\u540c\u6642\u306b\u4f7f\u7528\u3055\u308c\u308b\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u30022\u756a\u76ee\u306e\u5f71\u97ff\u306f\u3001 \u975e\u52b9\u7387\u306a\u30b9\u30e9\u30a4\u30b9\u306e\u521d\u671f\u5316 (#21) \u3067\u898b\u305f\u3082\u306e\u3067\u3059\u3002\u30b9\u30e9\u30a4\u30b9\u306e\u5c06\u6765\u306e\u9577\u3055\u304c\u3059\u3067\u306b\u308f\u304b\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u305d\u308c\u3092\u4e8b\u524d\u306b\u5272\u308a\u5f53\u3066\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u306b\u3001strings.Builder
\u306f\u5225\u306e n
\u30d0\u30a4\u30c8\u306e\u305f\u3081\u306e\u30b9\u30da\u30fc\u30b9\u3092\u4fdd\u8a3c\u3059\u308b\u30e1\u30bd\u30c3\u30c9 Grow(n int)
\u3092\u6301\u3063\u3066\u3044\u307e\u3059\u3002
func concat(values []string) string {\ntotal := 0\nfor i := 0; i < len(values); i++ {\ntotal += len(values[i])\n}\nsb := strings.Builder{}\nsb.Grow(total) (2)\nfor _, value := range values {\n_, _ = sb.WriteString(value)\n}\nreturn sb.String()\n}\n
\u30d9\u30f3\u30c1\u30de\u30fc\u30af\u3092\u5b9f\u884c\u3057\u30663\u3064\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\uff08+=
\u3092\u4f7f\u7528\u3057\u305f V1 \u3001\u4e8b\u524d\u5272\u308a\u5f53\u3066\u306a\u3057\u3067 strings.Builder{}
\u3092\u4f7f\u7528\u3057\u305f V2 \u3001\u4e8b\u524d\u5272\u308a\u5f53\u3066\u3042\u308a\u306e strings.Builder{}
\u3092\u4f7f\u7528\u3057\u305f V3 \uff09\u3092\u6bd4\u8f03\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u5165\u529b\u30b9\u30e9\u30a4\u30b9\u306b\u306f 1,000 \u500b\u306e\u6587\u5b57\u5217\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u3001\u5404\u6587\u5b57\u5217\u306b\u306f 1,000 \u30d0\u30a4\u30c8\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002
BenchmarkConcatV1-4 16 72291485 ns/op\nBenchmarkConcatV2-4 1188 878962 ns/op\nBenchmarkConcatV3-4 5922 190340 ns/op\n
\u3054\u89a7\u306e\u3068\u304a\u308a\u3001\u6700\u65b0\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u6700\u3082\u52b9\u7387\u7684\u3067\u3001V1 \u3088\u308a 99% \u3001V2 \u3088\u308a 78% \u9ad8\u901f\u3067\u3059\u3002
strings.Builder
\u306f\u3001\u6587\u5b57\u5217\u306e\u30ea\u30b9\u30c8\u3092\u9023\u7d50\u3059\u308b\u305f\u3081\u306e\u89e3\u6c7a\u7b56\u3068\u3057\u3066\u63a8\u5968\u3055\u308c\u307e\u3059\u3002\u901a\u5e38\u3001\u3053\u308c\u306f\u30eb\u30fc\u30d7\u5185\u3067\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3044\u304f\u3064\u304b\u306e\u6587\u5b57\u5217 \uff08\u540d\u524d\u3068\u59d3\u306a\u3069\uff09\u3092\u9023\u7d50\u3059\u308b\u3060\u3051\u306e\u5834\u5408\u3001 strings.Builder
\u306e\u4f7f\u7528\u306f\u3001 +=
\u6f14\u7b97\u5b50\u3084 fmt.Sprintf
\u3068\u6bd4\u3079\u3066\u53ef\u8aad\u6027\u304c\u4f4e\u304f\u306a\u308b\u304b\u3089\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#40","title":"\u7121\u99c4\u306a\u6587\u5b57\u5217\u5909\u63db (#40)","text":"\u8981\u7d04bytes
\u30d1\u30c3\u30b1\u30fc\u30b8\u306f strings
\u30d1\u30c3\u30b1\u30fc\u30b8\u3068\u540c\u3058\u64cd\u4f5c\u3092\u63d0\u4f9b\u3057\u3066\u304f\u308c\u308b\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304f\u3068\u3001\u4f59\u5206\u306a\u30d0\u30a4\u30c8\u30fb\u6587\u5b57\u5217\u5909\u63db\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\u6587\u5b57\u5217\u307e\u305f\u306f []byte
\u3092\u6271\u3046\u3053\u3068\u3092\u9078\u629e\u3059\u308b\u5834\u5408\u3001\u307b\u3068\u3093\u3069\u306e\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u306f\u5229\u4fbf\u6027\u306e\u305f\u3081\u306b\u6587\u5b57\u5217\u3092\u597d\u3080\u50be\u5411\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u307b\u3068\u3093\u3069\u306e I/O \u306f\u5b9f\u969b\u306b\u306f []byte
\u3067\u884c\u308f\u308c\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001io.Reader
\u3001io.Writer
\u3001\u304a\u3088\u3073 io.ReadAll
\u306f\u6587\u5b57\u5217\u3067\u306f\u306a\u304f []byte
\u3092\u51e6\u7406\u3057\u307e\u3059\u3002
\u6587\u5b57\u5217\u3068 []byte
\u306e\u3069\u3061\u3089\u3092\u6271\u3046\u3079\u304d\u304b\u8ff7\u3063\u305f\u3068\u304d\u3001[]byte
\u3092\u6271\u3046\u65b9\u304c\u5fc5\u305a\u3057\u3082\u9762\u5012\u3060\u3068\u3044\u3046\u308f\u3051\u3067\u306f\u306a\u3044\u3053\u3068\u3092\u601d\u3044\u51fa\u3057\u3066\u304f\u3060\u3055\u3044\u3002strings \u30d1\u30c3\u30b1\u30fc\u30b8\u304b\u3089\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u95a2\u6570\u306b\u306f\u3001bytes
\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u4ee3\u66ff\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002 Split
\u3001Count
\u3001Contains
\u3001Index
\u306a\u3069\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001I/O \u3092\u5b9f\u884c\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u306b\u95a2\u4fc2\u306a\u304f\u3001\u6587\u5b57\u5217\u306e\u4ee3\u308f\u308a\u306b\u30d0\u30a4\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30ef\u30fc\u30af\u30d5\u30ed\u30fc\u5168\u4f53\u3092\u5b9f\u88c5\u3067\u304d\u3001\u8ffd\u52a0\u306e\u5909\u63db\u306e\u30b3\u30b9\u30c8\u3092\u56de\u907f\u3067\u304d\u308b\u304b\u3069\u3046\u304b\u3092\u6700\u521d\u306b\u78ba\u8a8d\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#41","title":"\u90e8\u5206\u6587\u5b57\u5217\u3068\u30e1\u30e2\u30ea\u30ea\u30fc\u30af (#41)","text":"\u8981\u7d04\u90e8\u5206\u6587\u5b57\u5217\u306e\u4ee3\u308f\u308a\u306b\u30b3\u30d4\u30fc\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u90e8\u5206\u6587\u5b57\u5217\u64cd\u4f5c\u306b\u3088\u3063\u3066\u8fd4\u3055\u308c\u308b\u6587\u5b57\u5217\u304c\u540c\u3058\u30d0\u30a4\u30c8\u914d\u5217\u306b\u3088\u3063\u3066\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u308b\u305f\u3081\u3001\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\u30b9\u30e9\u30a4\u30b9\u3068\u30e1\u30e2\u30ea\u30ea\u30fc\u30af (#26) \u3067\u306f\u3001\u30b9\u30e9\u30a4\u30b9\u307e\u305f\u306f\u914d\u5217\u306e\u30b9\u30e9\u30a4\u30b9\u304c\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u306e\u72b6\u6cc1\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3057\u305f\u3002\u3053\u306e\u539f\u5247\u306f\u3001\u6587\u5b57\u5217\u304a\u3088\u3073\u90e8\u5206\u6587\u5b57\u5217\u306e\u64cd\u4f5c\u306b\u3082\u5f53\u3066\u306f\u307e\u308a\u307e\u3059\u3002
Go\u8a00\u8a9e\u3067\u90e8\u5206\u6587\u5b57\u5217\u64cd\u4f5c\u3092\u4f7f\u7528\u3059\u308b\u3068\u304d\u306f\u30012 \u3064\u306e\u3053\u3068\u306b\u7559\u610f\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u307e\u305a\u3001\u63d0\u4f9b\u3055\u308c\u308b\u9593\u9694\u306f\u30eb\u30fc\u30f3\u306e\u6570\u3067\u306f\u306a\u304f\u3001\u30d0\u30a4\u30c8\u6570\u306b\u57fa\u3065\u3044\u3066\u3044\u307e\u3059\u3002\u6b21\u306b\u3001\u7d50\u679c\u306e\u90e8\u5206\u6587\u5b57\u5217\u304c\u6700\u521d\u306e\u6587\u5b57\u5217\u3068\u540c\u3058\u30d0\u30c3\u30ad\u30f3\u30b0\u914d\u5217\u3092\u5171\u6709\u3059\u308b\u305f\u3081\u3001\u90e8\u5206\u6587\u5b57\u5217\u64cd\u4f5c\u306b\u3088\u308a\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u9632\u3050\u65b9\u6cd5\u306f\u3001\u6587\u5b57\u5217\u306e\u30b3\u30d4\u30fc\u3092\u624b\u52d5\u3067\u5b9f\u884c\u3059\u308b\u304b\u3001Go 1.18 \u304b\u3089\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b strings.Clone
\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#_7","title":"\u95a2\u6570\u3068\u30e1\u30bd\u30c3\u30c9","text":""},{"location":"ja/#42","title":"\u3069\u306e\u578b\u306e\u30ec\u30b7\u30fc\u30d0\u30fc\u3092\u4f7f\u7528\u3059\u308c\u3070\u3088\u3044\u304b\u308f\u304b\u3089\u306a\u3044 (#42)","text":"\u8981\u7d04\u5024\u30ec\u30b7\u30fc\u30d0\u30fc\u3068\u30dd\u30a4\u30f3\u30bf\u30ec\u30b7\u30fc\u30d0\u30fc\u306e\u3069\u3061\u3089\u3092\u4f7f\u7528\u3059\u308b\u304b\u306f\u3001\u3069\u306e\u578b\u306a\u306e\u304b\u3001\u5909\u5316\u3055\u305b\u308b\u5fc5\u8981\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3001\u30b3\u30d4\u30fc\u3067\u304d\u306a\u3044\u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3069\u308c\u304f\u3089\u3044\u5927\u304d\u3044\u306e\u304b\u3001\u306a\u3069\u306e\u8981\u7d20\u306b\u57fa\u3065\u3044\u3066\u6c7a\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5206\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u3001\u30dd\u30a4\u30f3\u30bf\u30ec\u30b7\u30fc\u30d0\u3092\u4f7f\u7528\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u5024\u30ec\u30b7\u30fc\u30d0\u30fc\u3068\u30dd\u30a4\u30f3\u30bf\u30ec\u30b7\u30fc\u30d0\u30fc\u306e\u3069\u3061\u3089\u3092\u9078\u629e\u3059\u308b\u304b\u306f\u3001\u5fc5\u305a\u3057\u3082\u7c21\u5358\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u9078\u629e\u306b\u5f79\u7acb\u3064\u3044\u304f\u3064\u304b\u306e\u6761\u4ef6\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u307e\u3057\u3087\u3046\u3002
\u30dd\u30a4\u30f3\u30bf\u30ec\u30b7\u30fc\u30d0\u30fc\u3067\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044 \u3068\u304d
type slice []int\nfunc (s *slice) add(element int) {\n*s = append(*s, element)\n}\n
\u30dd\u30a4\u30f3\u30bf\u30ec\u30b7\u30fc\u30d0\u30fc\u3067\u3042\u308b\u3079\u304d \u3068\u304d
\u5024\u30ec\u30b7\u30fc\u30d0\u30fc\u3067\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044 \u3068\u304d
\u5024\u30ec\u30b7\u30fc\u30d0\u30fc\u3067\u3042\u308b\u3079\u304d \u3068\u304d
time.Time
\u306a\u3069\u306e\u5c0f\u3055\u306a\u914d\u5217\u307e\u305f\u306f\u69cb\u9020\u4f53\u3067\u3001\u53ef\u5909\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u6301\u305f\u306a\u3044\u5024\u578b\u3067\u3042\u308b\u5834\u5408\u3002 int
\u3001float64
\u3001\u307e\u305f\u306f string
\u306a\u3069\u306e\u57fa\u672c\u7684\u306a\u578b\u306e\u5834\u5408\u3002 \u3082\u3061\u308d\u3093\u3001\u7279\u6b8a\u306a\u30b1\u30fc\u30b9\u306f\u5e38\u306b\u5b58\u5728\u3059\u308b\u305f\u3081\u3001\u3059\u3079\u3066\u3092\u7db2\u7f85\u3059\u308b\u3053\u3068\u306f\u4e0d\u53ef\u80fd\u3067\u3059\u304c\u3001\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306e\u76ee\u6a19\u306f\u3001\u307b\u3068\u3093\u3069\u306e\u30b1\u30fc\u30b9\u3092\u30ab\u30d0\u30fc\u3059\u308b\u305f\u3081\u306e\u30ac\u30a4\u30c0\u30f3\u30b9\u3092\u63d0\u4f9b\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u901a\u5e38\u306f\u3001\u305d\u3046\u3057\u306a\u3044\u6b63\u5f53\u306a\u7406\u7531\u304c\u306a\u3044\u9650\u308a\u3001\u5024\u30ec\u30b7\u30fc\u30d0\u30fc\u3092\u4f7f\u7528\u3057\u3066\u9593\u9055\u3044\u3042\u308a\u307e\u305b\u3093\u3002\u5206\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u3001\u30dd\u30a4\u30f3\u30bf\u30ec\u30b7\u30fc\u30d0\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#43","title":"\u540d\u524d\u4ed8\u304d\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u307e\u3063\u305f\u304f\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#43)","text":"\u8981\u7d04\u540d\u524d\u4ed8\u304d\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306e\u4f7f\u7528\u306f\u3001\u7279\u306b\u8907\u6570\u306e\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u304c\u540c\u3058\u578b\u3092\u6301\u3064\u5834\u5408\u3001\u95a2\u6570\u30fb\u30e1\u30bd\u30c3\u30c9\u306e\u8aad\u307f\u3084\u3059\u3055\u3092\u5411\u4e0a\u3055\u305b\u308b\u52b9\u7387\u7684\u306a\u65b9\u6cd5\u3067\u3059\u3002\u5834\u5408\u306b\u3088\u3063\u3066\u306f\u3001\u540d\u524d\u4ed8\u304d\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u30bc\u30ed\u5024\u306b\u521d\u671f\u5316\u3055\u308c\u308b\u305f\u3081\u3001\u3053\u306e\u65b9\u6cd5\u304c\u4fbf\u5229\u3067\u3059\u3089\u3042\u308b\u3053\u3068\u3082\u3042\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u6f5c\u5728\u7684\u306a\u526f\u4f5c\u7528\u306b\u306f\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u95a2\u6570\u307e\u305f\u306f\u30e1\u30bd\u30c3\u30c9\u3067\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u8fd4\u3059\u3068\u304d\u3001\u3053\u308c\u3089\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u540d\u524d\u3092\u4ed8\u3051\u3066\u3001\u901a\u5e38\u306e\u5909\u6570\u3068\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306b\u540d\u524d\u3092\u4ed8\u3051\u308b\u3068\u3001\u95a2\u6570\u30fb\u30e1\u30bd\u30c3\u30c9\u306e\u958b\u59cb\u6642\u306b\u305d\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306f\u30bc\u30ed\u5024\u306b\u521d\u671f\u5316\u3055\u308c\u307e\u3059\u3002\u540d\u524d\u4ed8\u304d\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001 \u3080\u304d\u51fa\u3057\u306e return \u6587\uff08\u5f15\u6570\u306a\u3057\uff09 \u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u305d\u306e\u5834\u5408\u3001\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u73fe\u5728\u306e\u5024\u304c\u623b\u308a\u5024\u3068\u3057\u3066\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002
\u4ee5\u4e0b\u306f\u3001\u540d\u524d\u4ed8\u304d\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf b
\u3092\u7528\u3044\u305f\u4f8b\u3067\u3059\u3002
func f(a int) (b int) {\nb = a\nreturn\n}\n
\u3053\u306e\u4f8b\u3067\u306f\u3001\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u540d\u524d b
\u3092\u4ed8\u3051\u3066\u3044\u307e\u3059\u3002\u5f15\u6570\u306a\u3057\u3067 return \u3092\u547c\u3073\u51fa\u3059\u3068\u3001b
\u306e\u73fe\u5728\u306e\u5024\u304c\u8fd4\u3055\u308c\u307e\u3059\u3002
\u5834\u5408\u306b\u3088\u3063\u3066\u306f\u3001\u540d\u524d\u4ed8\u304d\u306e\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306b\u3088\u3063\u3066\u53ef\u8aad\u6027\u304c\u5411\u4e0a\u3059\u308b\u3053\u3068\u3082\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u30012 \u3064\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u304c\u540c\u3058\u578b\u3067\u3042\u308b\u5834\u5408\u306a\u3069\u3067\u3059\u3002\u305d\u306e\u4ed6\u306b\u3082\u3001\u5229\u4fbf\u6027\u306e\u305f\u3081\u306b\u7528\u3044\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3086\u3048\u306b\u3001\u660e\u78ba\u306a\u5229\u70b9\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u614e\u91cd\u306b\u306a\u308a\u306a\u304c\u3089\u3082\u540d\u524d\u4ed8\u304d\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4f7f\u7528\u3059\u308b\u3079\u304d\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#44","title":"\u540d\u524d\u4ed8\u304d\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u3088\u308b\u4e88\u671f\u305b\u306c\u526f\u4f5c\u7528 (#44)","text":"\u8981\u7d04#43 \u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u540d\u524d\u4ed8\u304d\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u72b6\u6cc1\u306b\u3088\u3063\u3066\u306f\u5f79\u7acb\u3064\u7406\u7531\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u307e\u3057\u305f\u3002 \u305f\u3060\u3057\u3001\u3053\u308c\u3089\u306f\u30bc\u30ed\u5024\u306b\u521d\u671f\u5316\u3055\u308c\u308b\u305f\u3081\u3001\u5341\u5206\u306b\u6ce8\u610f\u3057\u306a\u3044\u3068\u3001\u8efd\u5fae\u306a\u30d0\u30b0\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3069\u3053\u304c\u9593\u9055\u3063\u3066\u3044\u308b\u3067\u3057\u3087\u3046\u304b\u3002
func (l loc) getCoordinates(ctx context.Context, address string) (\nlat, lng float32, err error) {\nisValid := l.validateAddress(address) (1)\nif !isValid {\nreturn 0, 0, errors.New(\"invalid address\")\n}\nif ctx.Err() != nil { (2)\nreturn 0, 0, err\n}\n// \u5ea7\u6a19\u3092\u53d6\u5f97\u3057\u3066\u8fd4\u3059\n}\n
\u4e00\u77a5\u3057\u305f\u3060\u3051\u3067\u306f\u30a8\u30e9\u30fc\u306f\u660e\u3089\u304b\u3067\u306f\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002if ctx.Err() != nil
\u30b9\u30b3\u30fc\u30d7\u3067\u8fd4\u3055\u308c\u308b\u30a8\u30e9\u30fc\u306f err
\u3067\u3059\u3002\u3057\u304b\u3057\u3001err
\u5909\u6570\u306b\u306f\u5024\u3092\u5272\u308a\u5f53\u3066\u3066\u3044\u307e\u305b\u3093\u3002error
\u578b\u306e\u30bc\u30ed\u5024\u3001 nil
\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u307e\u307e\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u30b3\u30fc\u30c9\u306f\u5e38\u306b nil \u30a8\u30e9\u30fc\u3092\u8fd4\u3057\u307e\u3059\u3002
\u540d\u524d\u4ed8\u304d\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u5404\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u30bc\u30ed\u5024\u306b\u521d\u671f\u5316\u3055\u308c\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u8aac\u660e\u3057\u305f\u3088\u3046\u306b\u3001\u3053\u308c\u306b\u3088\u308a\u3001\u898b\u3064\u3051\u308b\u306e\u304c\u5fc5\u305a\u3057\u3082\u7c21\u5358\u3067\u306f\u306a\u3044\u8efd\u5fae\u306a\u30d0\u30b0\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3086\u3048\u306b\u3001\u6f5c\u5728\u7684\u306a\u526f\u4f5c\u7528\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u540d\u524d\u4ed8\u304d\u7d50\u679c\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092\u4f7f\u7528\u3059\u308b\u3068\u304d\u306f\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#nil-45","title":"nil \u30ec\u30b7\u30fc\u30d0\u30fc\u3092\u8fd4\u3059 (#45)","text":"\u8981\u7d04\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u8fd4\u3059\u3068\u304d\u306f\u3001nil \u30dd\u30a4\u30f3\u30bf\u3092\u8fd4\u3059\u306e\u3067\u306f\u306a\u304f\u3001\u660e\u793a\u7684\u306a nil \u5024\u3092\u8fd4\u3059\u3088\u3046\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u305d\u3046\u3057\u306a\u3051\u308c\u3070\u3001\u610f\u56f3\u3057\u306a\u3044\u7d50\u679c\u304c\u767a\u751f\u3057\u3001\u547c\u3073\u51fa\u3057\u5143\u304c nil \u3067\u306f\u306a\u3044\u5024\u3092\u53d7\u3051\u53d6\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#46","title":"\u95a2\u6570\u5165\u529b\u306b\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b (#46)","text":"\u8981\u7d04\u30d5\u30a1\u30a4\u30eb\u540d\u306e\u4ee3\u308f\u308a\u306b io.Reader
\u578b\u3092\u53d7\u3051\u53d6\u308b\u3088\u3046\u306b\u95a2\u6570\u3092\u8a2d\u8a08\u3059\u308b\u3068\u3001\u95a2\u6570\u306e\u518d\u5229\u7528\u6027\u304c\u5411\u4e0a\u3057\u3001\u30c6\u30b9\u30c8\u304c\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002
\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8aad\u307f\u53d6\u308b\u305f\u3081\u306e\u95a2\u6570\u5165\u529b\u3068\u3057\u3066\u53d7\u3051\u5165\u308c\u308b\u3053\u3068\u306f\u3001\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u300c\u30b3\u30fc\u30c9\u306e\u81ed\u3044\u300d\u3068\u307f\u306a\u3055\u308c\u3079\u304d\u3067\u3059\uff08 os.Open
\u306a\u3069\u306e\u7279\u5b9a\u306e\u95a2\u6570\u3092\u9664\u304f\uff09\u3002\u5b9f\u969b\u3001\u8907\u6570\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u306e\u3067\u3001\u5358\u4f53\u30c6\u30b9\u30c8\u304c\u3088\u308a\u8907\u96d1\u306b\u306a\u308a\u307e\u3059\u3002\u307e\u305f\u3001\u95a2\u6570\u306e\u518d\u5229\u7528\u6027\u3082\u4f4e\u4e0b\u3057\u307e\u3059 \uff08\u305f\u3060\u3057\u3001\u3059\u3079\u3066\u306e\u95a2\u6570\u304c\u518d\u5229\u7528\u3055\u308c\u308b\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3002 io.Reader
\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u30bd\u30fc\u30b9\u304c\u62bd\u8c61\u5316\u3055\u308c\u307e\u3059\u3002\u5165\u529b\u304c\u30d5\u30a1\u30a4\u30eb\u3001\u6587\u5b57\u5217\u3001HTTP \u30ea\u30af\u30a8\u30b9\u30c8\u3001gRPC \u30ea\u30af\u30a8\u30b9\u30c8\u306e\u3044\u305a\u308c\u3067\u3042\u308b\u304b\u306b\u95a2\u4fc2\u306a\u304f\u3001\u5b9f\u88c5\u306f\u518d\u5229\u7528\u3067\u304d\u3001\u7c21\u5358\u306b\u30c6\u30b9\u30c8\u3067\u304d\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#defer-47","title":"defer
\u5f15\u6570\u3068\u30ec\u30b7\u30fc\u30d0\u30fc\u304c\u3069\u306e\u3088\u3046\u306b\u8a55\u4fa1\u3055\u308c\u308b\u304b\u3092\u77e5\u3089\u306a\u3044\uff08\u5f15\u6570\u306e\u8a55\u4fa1\u3001\u30dd\u30a4\u30f3\u30bf\u30fc\u3001\u304a\u3088\u3073\u5024\u30ec\u30b7\u30fc\u30d0\u30fc\uff09 (#47)","text":"\u8981\u7d04 \u30dd\u30a4\u30f3\u30bf\u3092 defer
\u95a2\u6570\u306b\u6e21\u3059\u3053\u3068\u3068\u3001\u547c\u3073\u51fa\u3057\u3092\u30af\u30ed\u30fc\u30b8\u30e3\u5185\u306b\u30e9\u30c3\u30d7\u3059\u308b\u3053\u3068\u304c\u3001\u5f15\u6570\u3068\u30ec\u30b7\u30fc\u30d0\u30fc\u306e\u5373\u6642\u8a55\u4fa1\u3092\u514b\u670d\u3059\u308b\u305f\u3081\u306b\u5b9f\u73fe\u53ef\u80fd\u306a\u89e3\u6c7a\u7b56\u3067\u3059\u3002
defer
\u95a2\u6570\u3067\u306f\u3001\u5f15\u6570\u306f\u3001\u4e0a\u4f4d\u30d6\u30ed\u30c3\u30af\u306e\u95a2\u6570\u304c\u623b\u3063\u3066\u304b\u3089\u3067\u306f\u306a\u304f\u3001\u3059\u3050\u306b\u8a55\u4fa1\u3055\u308c\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u5e38\u306b\u540c\u3058\u30b9\u30c6\u30fc\u30bf\u30b9\u2015\u2015\u7a7a\u306e\u6587\u5b57\u5217\u2015\u2015\u3067 notify
\u3068 incrementCounter
\u3092\u547c\u3073\u51fa\u3057\u307e\u3059\u3002
const (\nStatusSuccess = \"success\"\nStatusErrorFoo = \"error_foo\"\nStatusErrorBar = \"error_bar\"\n)\nfunc f() error {\nvar status string\ndefer notify(status)\ndefer incrementCounter(status)\nif err := foo(); err != nil {\nstatus = StatusErrorFoo\nreturn err\n}\nif err := bar(); err != nil {\nstatus = StatusErrorBar\nreturn err\n}\nstatus = StatusSuccess <5>\nreturn nil\n}\n
\u305f\u3057\u304b\u306b\u3001notify(status)
\u3068 incrementCounter(status)
\u3092 defer
\u95a2\u6570\u3068\u3057\u3066\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001Go\u8a00\u8a9e\u306f\u3001defer \u3092\u4f7f\u7528\u3057\u305f\u6bb5\u968e\u3067 f
\u304c\u30b9\u30c6\u30fc\u30bf\u30b9\u306e\u73fe\u5728\u306e\u5024\u3092\u8fd4\u3059\u3068\u3001\u3053\u308c\u3089\u306e\u547c\u3073\u51fa\u3057\u306e\u5b9f\u884c\u3092\u9045\u3089\u305b\u3001\u7a7a\u306e\u6587\u5b57\u5217\u3092\u6e21\u3057\u307e\u3059\u3002
defer
\u3092\u4f7f\u3044\u7d9a\u3051\u305f\u3044\u5834\u5408\u306e\u4e3b\u306a\u65b9\u6cd5\u306f 2 \u3064\u3042\u308a\u307e\u3059\u3002
\u6700\u521d\u306e\u89e3\u6c7a\u7b56\u306f\u6587\u5b57\u5217\u30dd\u30a4\u30f3\u30bf\u3092\u6e21\u3059\u3053\u3068\u3067\u3059\u3002
func f() error {\nvar status string\ndefer notify(&status) defer incrementCounter(&status)\n// \u95a2\u6570\u306e\u305d\u308c\u4ee5\u5916\u306e\u90e8\u5206\u306f\u5909\u66f4\u306a\u3057\n}\n
defer
\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u5f15\u6570\uff08\u3053\u3053\u3067\u306f\u30b9\u30c6\u30fc\u30bf\u30b9\u306e\u30a2\u30c9\u30ec\u30b9\uff09\u304c\u3059\u3050\u306b\u8a55\u4fa1\u3055\u308c\u307e\u3059\u3002\u30b9\u30c6\u30fc\u30bf\u30b9\u81ea\u4f53\u306f\u95a2\u6570\u5168\u4f53\u3067\u5909\u66f4\u3055\u308c\u307e\u3059\u304c\u3001\u305d\u306e\u30a2\u30c9\u30ec\u30b9\u306f\u5272\u308a\u5f53\u3066\u306b\u95a2\u4fc2\u306a\u304f\u4e00\u5b9a\u306e\u307e\u307e\u3067\u3059\u3002\u3088\u3063\u3066\u3001notify
\u307e\u305f\u306f incrementCounter
\u304c\u6587\u5b57\u5217\u30dd\u30a4\u30f3\u30bf\u306b\u3088\u3063\u3066\u53c2\u7167\u3055\u308c\u308b\u5024\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u671f\u5f85\u3069\u304a\u308a\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u3053\u306e\u89e3\u6c7a\u7b56\u3067\u306f 2 \u3064\u306e\u95a2\u6570\u306e\u30b7\u30b0\u30cd\u30c1\u30e3\u3092\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u305d\u308c\u304c\u5e38\u306b\u53ef\u80fd\u3067\u3042\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002
\u5225\u306e\u89e3\u6c7a\u7b56\u304c\u3042\u308a\u307e\u3059\u2015\u2015\u30af\u30ed\u30fc\u30b8\u30e3\uff08\u672c\u4f53\u306e\u5916\u90e8\u304b\u3089\u5909\u6570\u3092\u53c2\u7167\u3059\u308b\u533f\u540d\u95a2\u6570\u5024\uff09\u3092 defer
\u6587\u3068\u3057\u3066\u547c\u3073\u51fa\u3059\u3053\u3068\u3067\u3059\u3002
func f() error {\nvar status string\ndefer func() {\nnotify(status)\nincrementCounter(status)\n}()\n// \u95a2\u6570\u306e\u305d\u308c\u4ee5\u5916\u306e\u90e8\u5206\u306f\u5909\u66f4\u306a\u3057\n}\n
\u3053\u3053\u3067\u306f\u3001notify
\u3068 incrementCounter
\u306e\u4e21\u65b9\u306e\u547c\u3073\u51fa\u3057\u3092\u30af\u30ed\u30fc\u30b8\u30e3\u5185\u306b\u30e9\u30c3\u30d7\u3057\u307e\u3059\u3002\u3053\u306e\u30af\u30ed\u30fc\u30b8\u30e3\u306f\u3001\u672c\u4f53\u306e\u5916\u90e8\u304b\u3089\u30b9\u30c6\u30fc\u30bf\u30b9\u5909\u6570\u3092\u53c2\u7167\u3057\u307e\u3059\u3002\u3086\u3048\u306b\u3001status
\u306f\u3001defer
\u3092\u547c\u3073\u51fa\u3057\u305f\u3068\u304d\u3067\u306f\u306a\u304f\u3001\u30af\u30ed\u30fc\u30b8\u30e3\u304c\u5b9f\u884c\u3055\u308c\u305f\u3068\u304d\u306b\u8a55\u4fa1\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u89e3\u6c7a\u7b56\u306f\u6b63\u3057\u304f\u6a5f\u80fd\u3059\u308b\u4e0a\u306b\u3001\u30b7\u30b0\u30cd\u30c1\u30e3\u3092\u5909\u66f4\u3059\u308b\u305f\u3081\u306b notify
\u3084 incrementCounter
\u3092\u5fc5\u8981\u3068\u3057\u307e\u305b\u3093\u3002
\u3053\u306e\u52d5\u4f5c\u306f\u30e1\u30bd\u30c3\u30c9\u30ec\u30b7\u30fc\u30d0\u30fc\u306b\u3082\u9069\u7528\u3055\u308c\u308b\u3053\u3068\u306b\u3082\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u30ec\u30b7\u30fc\u30d0\u30fc\u306f\u3059\u3050\u306b\u8a55\u4fa1\u3055\u308c\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#_8","title":"\u30a8\u30e9\u30fc\u51e6\u7406","text":""},{"location":"ja/#48","title":"\u30d1\u30cb\u30c3\u30af (#48)","text":"\u8981\u7d04panic
\u306e\u4f7f\u7528\u306f\u3001Go\u8a00\u8a9e\u3067\u30a8\u30e9\u30fc\u306b\u5bfe\u51e6\u3059\u308b\u305f\u3081\u306e\u624b\u6bb5\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u3053\u308c\u306f\u56de\u5fa9\u4e0d\u80fd\u306a\u72b6\u6cc1\u3067\u306e\u307f\u4f7f\u7528\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u305f\u3068\u3048\u3070\u3001\u30d2\u30e5\u30fc\u30de\u30f3\u30a8\u30e9\u30fc\u3092\u901a\u77e5\u3059\u308b\u5834\u5408\u3084\u3001\u5fc5\u9808\u306e\u4f9d\u5b58\u95a2\u4fc2\u306e\u8aad\u307f\u8fbc\u307f\u306b\u5931\u6557\u3057\u305f\u5834\u5408\u306a\u3069\u3067\u3059\u3002
Go\u8a00\u8a9e\u3067\u306f\u3001panic \u306f\u901a\u5e38\u306e\u6d41\u308c\u3092\u505c\u6b62\u3059\u308b\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u3067\u3059\u3002
func main() {\nfmt.Println(\"a\")\npanic(\"foo\")\nfmt.Println(\"b\")\n}\n
\u3053\u306e\u30b3\u30fc\u30c9\u306f a \u3092\u51fa\u529b\u3057\u3001b \u3092\u51fa\u529b\u3059\u308b\u524d\u306b\u505c\u6b62\u3057\u307e\u3059\u3002
a\npanic: foo\n\ngoroutine 1 [running]:\nmain.main()\n main.go:7 +0xb3\n
panic \u306e\u4f7f\u7528\u306f\u614e\u91cd\u306b\u3059\u3079\u304d\u3067\u3059\u3002\u4ee3\u8868\u7684\u306a\u30b1\u30fc\u30b9\u304c 2 \u3064\u3042\u308a\u30011 \u3064\u306f\u30d2\u30e5\u30fc\u30de\u30f3\u30a8\u30e9\u30fc\u3092\u901a\u77e5\u3059\u308b\u5834\u5408\uff08\u4f8b: sql.Register
\u30c9\u30e9\u30a4\u30d0\u30fc\u304c nil
\u307e\u305f\u306f\u65e2\u306b\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306b panic \u3092\u8d77\u3053\u3057\u307e\u3059\uff09\u3001\u3082\u3046 1 \u3064\u306f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u5fc5\u9808\u306e\u4f9d\u5b58\u95a2\u4fc2\u306e\u4f5c\u6210\u306b\u5931\u6557\u3057\u305f\u5834\u5408\u3067\u3059\u3002\u7d50\u679c\u3068\u3057\u3066\u3001\u4f8b\u5916\u7684\u306b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u505c\u6b62\u3057\u307e\u3059\u3002\u305d\u308c\u4ee5\u5916\u306e\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u306b\u304a\u3044\u3066\u306f\u3001\u30a8\u30e9\u30fc\u51e6\u7406\u306f\u3001\u6700\u5f8c\u306e\u623b\u308a\u5f15\u6570\u3068\u3057\u3066\u9069\u5207\u306a\u30a8\u30e9\u30fc\u578b\u3092\u8fd4\u3059\u95a2\u6570\u3092\u901a\u3058\u3066\u884c\u3046\u3079\u304d\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#49","title":"\u30a8\u30e9\u30fc\u3092\u30e9\u30c3\u30d7\u3059\u3079\u304d\u3068\u304d\u3092\u77e5\u3089\u306a\u3044 (#49)","text":"\u8981\u7d04\u30a8\u30e9\u30fc\u3092\u30e9\u30c3\u30d7\u3059\u308b\u3068\u3001\u30a8\u30e9\u30fc\u3092\u30de\u30fc\u30af\u3057\u305f\u308a\u3001\u8ffd\u52a0\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u63d0\u4f9b\u3057\u305f\u308a\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u30a8\u30e9\u30fc\u30e9\u30c3\u30d4\u30f3\u30b0\u306b\u3088\u308a\u3001\u547c\u3073\u51fa\u3057\u5143\u304c\u30bd\u30fc\u30b9\u30a8\u30e9\u30fc\u3092\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u305f\u3081\u3001\u6f5c\u5728\u7684\u306a\u7d50\u5408\u304c\u767a\u751f\u3057\u307e\u3059\u3002\u305d\u308c\u3092\u907f\u3051\u305f\u3044\u5834\u5408\u306f\u3001\u30a8\u30e9\u30fc\u30e9\u30c3\u30d4\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002
Go 1.13 \u4ee5\u964d\u3001%w \u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3092\u4f7f\u7528\u3059\u308c\u3070\u7c21\u5358\u306b\u30a8\u30e9\u30fc\u3092\u30e9\u30c3\u30d7\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u30a8\u30e9\u30fc\u30e9\u30c3\u30d4\u30f3\u30b0\u3068\u306f\u3001\u30bd\u30fc\u30b9\u30a8\u30e9\u30fc\u3082\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u30e9\u30c3\u30d1\u30fc\u30b3\u30f3\u30c6\u30ca\u5185\u3067\u30a8\u30e9\u30fc\u3092\u30e9\u30c3\u30d7\u307e\u305f\u306f\u30d1\u30c3\u30af\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u4e00\u822c\u306b\u3001\u30a8\u30e9\u30fc\u30e9\u30c3\u30d4\u30f3\u30b0\u306e\u4e3b\u306a\u4f7f\u7528\u4f8b\u306f\u6b21\u306e 2 \u3064\u3067\u3059\u3002
\u30a8\u30e9\u30fc\u3092\u51e6\u7406\u3059\u308b\u3068\u304d\u3001\u30a8\u30e9\u30fc\u3092\u30e9\u30c3\u30d7\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u6c7a\u5b9a\u3067\u304d\u307e\u3059\u3002\u30e9\u30c3\u30d4\u30f3\u30b0\u3068\u306f\u3001\u30a8\u30e9\u30fc\u306b\u3055\u3089\u306b\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u8ffd\u52a0\u3057\u305f\u308a\u3001\u30a8\u30e9\u30fc\u3092\u7279\u5b9a\u306e\u30bf\u30a4\u30d7\u3068\u3057\u3066\u30de\u30fc\u30af\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u30a8\u30e9\u30fc\u3092\u30de\u30fc\u30af\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u72ec\u81ea\u306e\u30a8\u30e9\u30fc\u578b\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3067\u3059\u304c\u3001\u65b0\u305f\u306b\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u52a0\u3048\u305f\u3044\u3060\u3051\u306e\u5834\u5408\u306f\u3001\u65b0\u3057\u3044\u30a8\u30e9\u30fc\u578b\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u305f\u3081\u3001%w \u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3092\u6307\u5b9a\u3057\u3066 fmt.Errorf \u3092\u4f7f\u7528\u3057\u307e\u3057\u3087\u3046\u3002\u305f\u3060\u3057\u3001\u30a8\u30e9\u30fc\u30e9\u30c3\u30d4\u30f3\u30b0\u306b\u3088\u308a\u3001\u547c\u3073\u51fa\u3057\u5143\u304c\u30bd\u30fc\u30b9\u30a8\u30e9\u30fc\u3092\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u305f\u3081\u3001\u6f5c\u5728\u7684\u306a\u7d50\u5408\u304c\u751f\u3058\u307e\u3059\u3002\u305d\u308c\u3092\u907f\u3051\u305f\u3044\u5834\u5408\u306f\u3001\u30a8\u30e9\u30fc\u306e\u30e9\u30c3\u30d4\u30f3\u30b0\u3067\u306f\u306a\u304f\u3001\u30a8\u30e9\u30fc\u306e\u5909\u63db\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001%v \u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3092\u6307\u5b9a\u3057\u305f fmt.Errorf \u3092\u4f7f\u7528\u3057\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#50","title":"\u30a8\u30e9\u30fc\u578b\u306e\u4e0d\u6b63\u78ba\u306a\u6bd4\u8f03 (#50)","text":"\u8981\u7d04Go 1.13 \u306e\u30a8\u30e9\u30fc\u30e9\u30c3\u30d4\u30f3\u30b0\u3092 %w
\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3068 fmt.Errorf
\u3067\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u578b\u306b\u5bfe\u3059\u308b\u30a8\u30e9\u30fc\u306e\u6bd4\u8f03\u306f errors.As
\u3092\u901a\u3058\u3066\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u3001\u8fd4\u3055\u308c\u305f\u30a8\u30e9\u30fc\u304c\u30e9\u30c3\u30d7\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u8a55\u4fa1\u306b\u5931\u6557\u3057\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#51","title":"\u30a8\u30e9\u30fc\u5024\u306e\u4e0d\u6b63\u78ba\u306a\u6bd4\u8f03 (#51)","text":"\u8981\u7d04Go 1.13 \u306e\u30a8\u30e9\u30fc\u30e9\u30c3\u30d4\u30f3\u30b0\u3092 %w
\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3068 fmt.Errorf
\u3067\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u30a8\u30e9\u30fc\u3068\u5024\u306e\u6bd4\u8f03\u306f errors.As
\u3092\u901a\u3058\u3066\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u3001\u8fd4\u3055\u308c\u305f\u30a8\u30e9\u30fc\u304c\u30e9\u30c3\u30d7\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u8a55\u4fa1\u306b\u5931\u6557\u3057\u307e\u3059\u3002
\u30bb\u30f3\u30c1\u30cd\u30eb\u30a8\u30e9\u30fc\u306f\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u305f\u30a8\u30e9\u30fc\u306e\u3053\u3068\u3067\u3059\u3002
import \"errors\"\nvar ErrFoo = errors.New(\"foo\")\n
\u4e00\u822c\u306b\u3001\u6163\u4f8b\u3068\u3057\u3066 Err
\u3067\u59cb\u3081\u3001\u305d\u306e\u5f8c\u306b\u30a8\u30e9\u30fc\u578b\u3092\u7d9a\u3051\u307e\u3059\u3002\u3053\u3053\u3067\u306f ErrFoo
\u3067\u3059\u3002\u30bb\u30f3\u30c1\u30cd\u30eb\u30a8\u30e9\u30fc\u306f\u3001\u4e88\u671f\u3055\u308c\u308b \u30a8\u30e9\u30fc\u3001\u3064\u307e\u308a\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u78ba\u8a8d\u3059\u308b\u3053\u3068\u3092\u671f\u5f85\u3059\u308b\u30a8\u30e9\u30fc\u3092\u4f1d\u3048\u307e\u3059\u3002\u4e00\u822c\u7684\u306a\u30ac\u30a4\u30c9\u30e9\u30a4\u30f3\u3068\u3057\u3066 var ErrFoo =errors.New(\"foo\")
\u3002 BarError
\u306f error
\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u5b9f\u88c5\u3057\u305f\u4e0a\u3067 type BarError struct { ... }
\u3002 \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067 %w
\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3068 fmt.Errorf
\u3092\u4f7f\u7528\u3057\u3066\u30a8\u30e9\u30fc\u30e9\u30c3\u30d7\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u7279\u5b9a\u306e\u5024\u306b\u5bfe\u3059\u308b\u30a8\u30e9\u30fc\u306e\u30c1\u30a7\u30c3\u30af\u306f ==
\u306e\u4ee3\u308f\u308a\u306b errors.Is
\u3092\u4f7f\u7528\u3057\u3066\u884c\u3044\u307e\u3057\u3087\u3046\u3002\u305d\u308c\u306b\u3088\u3063\u3066\u3001\u30bb\u30f3\u30c1\u30cd\u30eb\u30a8\u30e9\u30fc\u304c\u30e9\u30c3\u30d7\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3067\u3082\u3001errors.Is
\u306f\u305d\u308c\u3092\u518d\u5e30\u7684\u306b\u30a2\u30f3\u30e9\u30c3\u30d7\u3057\u3001\u30c1\u30a7\u30fc\u30f3\u5185\u306e\u5404\u30a8\u30e9\u30fc\u3092\u63d0\u4f9b\u3055\u308c\u305f\u5024\u3068\u6bd4\u8f03\u3067\u304d\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#2-52","title":"\u30a8\u30e9\u30fc\u306e 2 \u56de\u51e6\u7406 (#52)","text":"\u8981\u7d04\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u3001\u30a8\u30e9\u30fc\u306f 1 \u56de\u3067\u51e6\u7406\u3055\u308c\u308b\u3079\u304d\u3067\u3059\u3002\u30a8\u30e9\u30fc\u3092\u30ed\u30b0\u306b\u8a18\u9332\u3059\u308b\u3053\u3068\u306f\u3001\u30a8\u30e9\u30fc\u3092\u51e6\u7406\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3059\u306a\u308f\u3061\u3001\u30ed\u30b0\u306b\u8a18\u9332\u3059\u308b\u304b\u30a8\u30e9\u30fc\u3092\u8fd4\u3059\u304b\u3092\u9078\u629e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u591a\u304f\u306e\u5834\u5408\u3001\u30a8\u30e9\u30fc\u30e9\u30c3\u30d4\u30f3\u30b0\u306f\u3001\u30a8\u30e9\u30fc\u306b\u8ffd\u52a0\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u63d0\u4f9b\u3057\u3001\u30bd\u30fc\u30b9\u30a8\u30e9\u30fc\u3092\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u305f\u3081\u3001\u89e3\u6c7a\u7b56\u306b\u306a\u308a\u307e\u3059\u3002
\u30a8\u30e9\u30fc\u3092\u8907\u6570\u56de\u51e6\u7406\u3059\u308b\u3053\u3068\u306f\u3001\u7279\u306bGo\u8a00\u8a9e\u306b\u9650\u3089\u305a\u3001\u958b\u767a\u8005\u304c\u983b\u7e41\u306b\u3084\u3063\u3066\u3057\u307e\u3046\u30df\u30b9\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u540c\u3058\u30a8\u30e9\u30fc\u304c\u8907\u6570\u56de\u30ed\u30b0\u306b\u8a18\u9332\u3055\u308c\u3001\u30c7\u30d0\u30c3\u30b0\u304c\u56f0\u96e3\u306b\u306a\u308b\u72b6\u6cc1\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
\u30a8\u30e9\u30fc\u51e6\u7406\u306f 1 \u5ea6\u3067\u6e08\u307e\u3059\u3079\u304d\u3060\u3068\u3044\u3046\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002\u30a8\u30e9\u30fc\u3092\u30ed\u30b0\u306b\u8a18\u9332\u3059\u308b\u3053\u3068\u306f\u3001\u30a8\u30e9\u30fc\u3092\u51e6\u7406\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3064\u307e\u308a\u3001\u884c\u3046\u3079\u304d\u306f\u3001\u30ed\u30b0\u306b\u8a18\u9332\u3059\u308b\u304b\u3001\u30a8\u30e9\u30fc\u3092\u8fd4\u3059\u304b\u306e\u3069\u3061\u3089\u304b\u3060\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30b3\u30fc\u30c9\u304c\u7c21\u7d20\u5316\u3055\u308c\u3001\u30a8\u30e9\u30fc\u306e\u72b6\u6cc1\u306b\u3064\u3044\u3066\u3088\u308a\u9069\u5207\u306a\u6d1e\u5bdf\u304c\u5f97\u3089\u308c\u307e\u3059\u3002\u30a8\u30e9\u30fc\u30e9\u30c3\u30d4\u30f3\u30b0\u306f\u3001\u30bd\u30fc\u30b9\u30a8\u30e9\u30fc\u3092\u4f1d\u3048\u3001\u30a8\u30e9\u30fc\u306b\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u8ffd\u52a0\u3067\u304d\u308b\u305f\u3081\u3001\u6700\u3082\u4f7f\u3044\u52dd\u624b\u306e\u826f\u3044\u624b\u6bb5\u306b\u306a\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#53","title":"\u30a8\u30e9\u30fc\u51e6\u7406\u3092\u3057\u306a\u3044 (#53)","text":"\u8981\u7d04\u95a2\u6570\u547c\u3073\u51fa\u3057\u4e2d\u3067\u3042\u3063\u3066\u3082\u3001defer
\u95a2\u6570\u5185\u3067\u3042\u3063\u3066\u3082\u3001\u30a8\u30e9\u30fc\u3092\u7121\u8996\u3059\u308b\u3068\u304d\u306f\u3001\u30d6\u30e9\u30f3\u30af\u8b58\u5225\u5b50\u3092\u4f7f\u7528\u3057\u3066\u660e\u78ba\u306b\u884c\u3046\u3079\u304d\u3067\u3059\u3002\u305d\u3046\u3057\u306a\u3044\u3068\u3001\u5c06\u6765\u306e\u8aad\u307f\u624b\u304c\u305d\u308c\u304c\u610f\u56f3\u7684\u3060\u3063\u305f\u306e\u304b\u3001\u305d\u308c\u3068\u3082\u30df\u30b9\u3060\u3063\u305f\u306e\u304b\u56f0\u60d1\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#defer-54","title":"defer
\u30a8\u30e9\u30fc\u3092\u51e6\u7406\u3057\u306a\u3044 (#54)","text":"\u8981\u7d04 \u591a\u304f\u306e\u5834\u5408\u3001defer
\u95a2\u6570\u306b\u3088\u3063\u3066\u8fd4\u3055\u308c\u308b\u30a8\u30e9\u30fc\u3092\u7121\u8996\u3059\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u72b6\u6cc1\u306b\u5fdc\u3058\u3066\u3001\u76f4\u63a5\u51e6\u7406\u3059\u308b\u304b\u3001\u547c\u3073\u51fa\u3057\u5143\u306b\u4f1d\u3048\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u3092\u7121\u8996\u3059\u308b\u5834\u5408\u306f\u3001\u30d6\u30e9\u30f3\u30af\u8b58\u5225\u5b50\u3092\u4f7f\u7528\u3057\u3066\u304f\u3060\u3055\u3044\u3002
\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002
func f() {\n// ...\nnotify() // \u30a8\u30e9\u30fc\u51e6\u7406\u306f\u7701\u7565\u3055\u308c\u3066\u3044\u307e\u3059\n}\nfunc notify() error {\n// ...\n}\n
\u4fdd\u5b88\u6027\u306e\u89b3\u70b9\u304b\u3089\u3001\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3044\u304f\u3064\u304b\u306e\u554f\u984c\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3042\u308b\u4eba\u304c\u3053\u308c\u3092\u8aad\u3080\u3053\u3068\u3092\u8003\u3048\u3066\u307f\u307e\u3059\u3002\u8aad\u307f\u624b\u306f\u3001notify \u304c\u30a8\u30e9\u30fc\u3092\u8fd4\u3059\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u305d\u306e\u30a8\u30e9\u30fc\u304c\u89aa\u95a2\u6570\u306b\u3088\u3063\u3066\u51e6\u7406\u3055\u308c\u306a\u3044\u3053\u3068\u306b\u6c17\u3065\u304d\u307e\u3059\u3002\u30a8\u30e9\u30fc\u51e6\u7406\u304c\u610f\u56f3\u7684\u3067\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u679c\u305f\u3057\u3066\u63a8\u6e2c\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\u3002\u4ee5\u524d\u306e\u958b\u767a\u8005\u304c\u305d\u308c\u3092\u51e6\u7406\u3059\u308b\u306e\u3092\u5fd8\u308c\u305f\u306e\u304b\u3001\u305d\u308c\u3068\u3082\u610f\u56f3\u7684\u306b\u51e6\u7406\u3057\u305f\u306e\u304b\u3092\u77e5\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\u3002
\u3053\u308c\u3089\u306e\u7406\u7531\u306b\u3088\u308a\u3001\u30a8\u30e9\u30fc\u3092\u7121\u8996\u3057\u305f\u3044\u5834\u5408\u3001\u30d6\u30e9\u30f3\u30af\u8b58\u5225\u5b50\uff08 _
\uff09\u3092\u4f7f\u3046\u307b\u304b\u3042\u308a\u307e\u305b\u3093\u3002
_ = notify\n
\u30b3\u30f3\u30d1\u30a4\u30eb\u3068\u5b9f\u884c\u6642\u9593\u306e\u70b9\u3067\u306f\u3001\u3053\u306e\u65b9\u6cd5\u306f\u6700\u521d\u306e\u30b3\u30fc\u30c9\u90e8\u5206\u3068\u6bd4\u3079\u3066\u4f55\u3082\u5909\u308f\u308a\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u3053\u306e\u65b0\u3057\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u306f\u3001\u79c1\u305f\u3061\u304c\u30a8\u30e9\u30fc\u306b\u95a2\u5fc3\u304c\u306a\u3044\u3053\u3068\u3092\u660e\u3089\u304b\u306b\u3057\u3066\u3044\u307e\u3059\u3002\u307e\u305f\u3001\u30a8\u30e9\u30fc\u304c\u7121\u8996\u3055\u308c\u308b\u7406\u7531\u3092\u793a\u3059\u30b3\u30e1\u30f3\u30c8\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002
// \u6700\u5927\u3067\u3082 1 \u56de\u306e\u4f1d\u9054 \n// \u305d\u308c\u3086\u3048\u3001\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306b\u305d\u308c\u3089\u306e\u4e00\u90e8\u304c\u5931\u308f\u308c\u308b\u3053\u3068\u306f\u8a31\u5bb9\u3055\u308c\u307e\u3059\u3002\n_ = notify()\n
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#_9","title":"\u4e26\u884c\u51e6\u7406\uff1a\u57fa\u790e","text":""},{"location":"ja/#55","title":"\u4e26\u884c\u51e6\u7406\u3068\u4e26\u5217\u51e6\u7406\u306e\u6df7\u540c (#55)","text":"\u8981\u7d04\u4e26\u884c\u51e6\u7406\u3068\u4e26\u5217\u51e6\u7406\u306e\u57fa\u672c\u7684\u306a\u9055\u3044\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u306f\u3001 Go \u958b\u767a\u8005\u306b\u3068\u3063\u3066\u5fc5\u9808\u3067\u3059\u3002\u4e26\u884c\u51e6\u7406\u306f\u69cb\u9020\u306b\u95a2\u3059\u308b\u3082\u306e\u3067\u3059\u304c\u3001\u4e26\u5217\u51e6\u7406\u306f\u5b9f\u884c\u306b\u95a2\u3059\u308b\u3082\u306e\u3067\u3059\u3002
\u4e26\u884c\u51e6\u7406\u3068\u4e26\u5217\u51e6\u7406\u306f\u540c\u3058\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002
\u307e\u3068\u3081\u308b\u3068\u3001\u4e26\u884c\u51e6\u7406\u306f\u3001\u4e26\u5217\u5316\u3067\u304d\u308b\u90e8\u5206\u3092\u3082\u3064\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u69cb\u9020\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\u3059\u306a\u308f\u3061\u3001\u4e26\u884c\u51e6\u7406\u306b\u3088\u308a\u4e26\u5217\u51e6\u7406\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059 \u3002
"},{"location":"ja/#56","title":"\u4e26\u884c\u51e6\u7406\u306e\u307b\u3046\u304c\u5e38\u306b\u65e9\u3044\u3068\u8003\u3048\u3066\u3044\u308b (#56)","text":"\u8981\u7d04\u719f\u7df4\u3057\u305f\u958b\u767a\u8005\u306b\u306a\u308b\u306b\u306f\u3001\u4e26\u884c\u51e6\u7406\u304c\u5fc5\u305a\u3057\u3082\u9ad8\u901f\u3067\u3042\u308b\u3068\u306f\u9650\u3089\u306a\u3044\u3053\u3068\u3092\u8a8d\u8b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6700\u5c0f\u9650\u306e\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u306e\u4e26\u5217\u51e6\u7406\u3092\u4f34\u3046\u89e3\u6c7a\u7b56\u306f\u3001\u5fc5\u305a\u3057\u3082\u9010\u6b21\u51e6\u7406\u3088\u308a\u9ad8\u901f\u3067\u3042\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002\u9010\u6b21\u51e6\u7406\u3068\u4e26\u884c\u51e6\u7406\u306e\u30d9\u30f3\u30c1\u30de\u30fc\u30af\u306f\u3001\u4eee\u5b9a\u3092\u691c\u8a3c\u3059\u308b\u65b9\u6cd5\u3067\u3042\u308b\u3079\u304d\u3067\u3059\u3002
\u30bb\u30af\u30b7\u30e7\u30f3\u5168\u6587\u306f\u3053\u3061\u3089.
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#57","title":"\u30c1\u30e3\u30cd\u30eb\u307e\u305f\u306f\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u3092\u3044\u3064\u4f7f\u7528\u3059\u308b\u3079\u304d\u304b\u306b\u3064\u3044\u3066\u6238\u60d1\u3063\u3066\u3044\u308b (#57)","text":"\u8981\u7d04\u30b4\u30eb\u30fc\u30c1\u30f3\u306e\u76f8\u4e92\u4f5c\u7528\u3092\u8a8d\u8b58\u3057\u3066\u3044\u308b\u3053\u3068\u306f\u3001\u30c1\u30e3\u30cd\u30eb\u3068\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u306e\u3069\u3061\u3089\u3092\u9078\u629e\u3059\u308b\u304b\u3092\u6c7a\u5b9a\u3059\u308b\u3068\u304d\u306b\u3082\u5f79\u7acb\u3061\u307e\u3059\u3002\u4e00\u822c\u306b\u3001\u4e26\u5217\u30b4\u30eb\u30fc\u30c1\u30f3\u306b\u306f\u540c\u671f\u304c\u5fc5\u8981\u3067\u3042\u308a\u3001\u3057\u305f\u304c\u3063\u3066\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u304c\u5fc5\u8981\u3067\u3059\u3002\u53cd\u5bfe\u306b\u3001\u4e26\u884c\u30b4\u30eb\u30fc\u30c1\u30f3\u306f\u901a\u5e38\u3001\u8abf\u6574\u3068\u30aa\u30fc\u30b1\u30b9\u30c8\u30ec\u30fc\u30b7\u30e7\u30f3\u3001\u3064\u307e\u308a\u30c1\u30e3\u30cd\u30eb\u3092\u5fc5\u8981\u3068\u3057\u307e\u3059\u3002
\u4e26\u884c\u51e6\u7406\u306e\u554f\u984c\u3092\u8003\u616e\u3059\u308b\u3068\u3001\u30c1\u30e3\u30cd\u30eb\u307e\u305f\u306f\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3057\u305f\u89e3\u6c7a\u7b56\u3092\u5b9f\u88c5\u3067\u304d\u308b\u304b\u3069\u3046\u304b\u304c\u5fc5\u305a\u3057\u3082\u660e\u78ba\u3067\u306f\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002Go\u8a00\u8a9e\u306f\u901a\u4fe1\u306b\u3088\u308b\u30e1\u30e2\u30ea\u306e\u5171\u6709\u3092\u4fc3\u9032\u3059\u308b\u305f\u3081\u3001\u8d77\u3053\u308a\u3046\u308b\u9593\u9055\u3044\u306e\u3046\u3061\u306e\u4e00\u3064\u306f\u3001\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u306b\u304b\u304b\u308f\u3089\u305a\u3001\u30c1\u30e3\u30cd\u30eb\u306e\u4f7f\u7528\u3092\u5e38\u306b\u5f37\u5236\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3057\u304b\u3057\u306a\u304c\u3089\u30012 \u3064\u306e\u65b9\u6cd5\u306f\u88dc\u5b8c\u7684\u306a\u3082\u306e\u3067\u3042\u308b\u3068\u898b\u306a\u3059\u3079\u304d\u3067\u3059\u3002
\u30c1\u30e3\u30cd\u30eb\u307e\u305f\u306f\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u306f\u3069\u306e\u3088\u3046\u306a\u5834\u5408\u306b\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002\u6b21\u306e\u56f3\u306e\u4f8b\u3092\u30d0\u30c3\u30af\u30dc\u30fc\u30f3\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u306e\u4f8b\u306b\u306f\u3001\u7279\u5b9a\u306e\u95a2\u4fc2\u3092\u6301\u3064 3 \u3064\u306e\u7570\u306a\u308b\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u3042\u308a\u307e\u3059\u3002
\u539f\u5247\u3068\u3057\u3066\u3001\u4e26\u5217\u30b4\u30eb\u30fc\u30c1\u30f3\u306f\u3001\u30b9\u30e9\u30a4\u30b9\u306a\u3069\u306e\u5171\u6709\u30ea\u30bd\u30fc\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u308a\u5909\u66f4\u3057\u305f\u308a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306a\u3069\u306b\u3001_\u540c\u671f_\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u540c\u671f\u306f\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u3067\u306f\u5f37\u5236\u3055\u308c\u307e\u3059\u304c\u3001\u3069\u306e\u30c1\u30e3\u30cd\u30eb\u578b\u3067\u3082\u5f37\u5236\u3055\u308c\u307e\u305b\u3093\uff08\u30d0\u30c3\u30d5\u30a1\u3042\u308a\u30c1\u30e3\u30cd\u30eb\u3092\u9664\u304f\uff09\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u4e00\u822c\u306b\u3001\u4e26\u5217\u30b4\u30eb\u30fc\u30c1\u30f3\u9593\u306e\u540c\u671f\u306f\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u3092\u4ecb\u3057\u3066\u9054\u6210\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u4e00\u65b9\u3001\u4e00\u822c\u306b\u3001\u4e26\u884c\u30b4\u30eb\u30fc\u30c1\u30f3\u306f \u8abf\u6574\u304a\u3088\u3073\u30aa\u30fc\u30b1\u30b9\u30c8\u30ec\u30fc\u30b7\u30e7\u30f3 \u3092\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001G3 \u304c G1 \u3068 G2 \u306e\u4e21\u65b9\u304b\u3089\u306e\u7d50\u679c\u3092\u96c6\u7d04\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u3001G1 \u3068 G2 \u306f\u65b0\u3057\u3044\u4e2d\u9593\u7d50\u679c\u304c\u5229\u7528\u53ef\u80fd\u3067\u3042\u308b\u3053\u3068\u3092 G3 \u306b\u901a\u77e5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u8abf\u6574\u306f\u30b3\u30df\u30e5\u30cb\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u7bc4\u56f2\u3001\u3064\u307e\u308a\u30c1\u30e3\u30cd\u30eb\u306b\u8a72\u5f53\u3057\u307e\u3059\u3002
\u4e26\u884c\u30b4\u30eb\u30fc\u30c1\u30f3\u306b\u95a2\u3057\u3066\u306f\u3001\u30ea\u30bd\u30fc\u30b9\u306e\u6240\u6709\u6a29\u3092\u3042\u308b\u30b9\u30c6\u30c3\u30d7\uff08G1 \u304a\u3088\u3073 G2\uff09\u304b\u3089\u5225\u306e\u30b9\u30c6\u30c3\u30d7\uff08G3\uff09\u306b\u79fb\u7ba1\u3057\u305f\u3044\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001G1 \u3068 G2 \u306b\u3088\u3063\u3066\u5171\u6709\u30ea\u30bd\u30fc\u30b9\u304c\u8c4a\u304b\u306b\u306a\u3063\u3066\u3044\u308b\u5834\u5408\u3001\u3042\u308b\u6642\u70b9\u3067\u3053\u306e\u30b8\u30e7\u30d6\u306f\u5b8c\u4e86\u3057\u305f\u3068\u898b\u306a\u3055\u308c\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3001\u7279\u5b9a\u306e\u30ea\u30bd\u30fc\u30b9\u306e\u6e96\u5099\u304c\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u3092\u901a\u77e5\u3057\u3001\u6240\u6709\u6a29\u306e\u79fb\u8ee2\u3092\u51e6\u7406\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u3068\u30c1\u30e3\u30cd\u30eb\u306b\u306f\u7570\u306a\u308b\u30bb\u30de\u30f3\u30c6\u30a3\u30af\u30b9\u304c\u3042\u308a\u307e\u3059\u3002\u72b6\u614b\u3092\u5171\u6709\u3057\u305f\u3044\u3068\u304d\u3001\u307e\u305f\u306f\u5171\u6709\u30ea\u30bd\u30fc\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u3044\u3068\u304d\u306f\u3001\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u306b\u3088\u3063\u3066\u3053\u306e\u30ea\u30bd\u30fc\u30b9\u3078\u306e\u6392\u4ed6\u7684\u30a2\u30af\u30bb\u30b9\u304c\u4fdd\u8a3c\u3055\u308c\u307e\u3059\u3002\u53cd\u5bfe\u306b\u3001\u30c1\u30e3\u30cd\u30eb\u306f\u30c7\u30fc\u30bf\u306e\u6709\u7121\uff08chan struct{}
\u306e\u6709\u7121\uff09\u306b\u95a2\u4fc2\u306a\u304f\u30b7\u30b0\u30ca\u30ea\u30f3\u30b0\u3092\u884c\u3046\u4ed5\u7d44\u307f\u3067\u3059\u3002\u8abf\u6574\u3084\u6240\u6709\u6a29\u306e\u79fb\u8ee2\u306f\u30c1\u30e3\u30cd\u30eb\u3092\u901a\u3058\u3066\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u4e26\u5217\u304b\u4e26\u884c\u304b\u3092\u77e5\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u4e00\u822c\u306b\u3001\u4e26\u5217\u30b4\u30eb\u30fc\u30c1\u30f3\u306b\u306f\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u304c\u5fc5\u8981\u3067\u3001\u4e26\u884c\u30b4\u30eb\u30fc\u30c1\u30f3\u306b\u306f\u30c1\u30e3\u30cd\u30eb\u304c\u5fc5\u8981\u3067\u3059\u3002
\u4e26\u884c\u51e6\u7406\u306b\u719f\u9054\u3059\u308b\u3068\u3044\u3046\u3053\u3068\u306f\u3001\u30c7\u30fc\u30bf\u7af6\u5408\u3068\u7af6\u5408\u72b6\u614b\u304c\u7570\u306a\u308b\u6982\u5ff5\u3067\u3042\u308b\u3053\u3068\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3082\u610f\u5473\u3057\u307e\u3059\u3002\u30c7\u30fc\u30bf\u7af6\u5408\u306f\u3001\u8907\u6570\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u540c\u3058\u30e1\u30e2\u30ea\u4f4d\u7f6e\u306b\u540c\u6642\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u305d\u306e\u3046\u3061\u306e\u5c11\u306a\u304f\u3068\u3082 1 \u3064\u304c\u66f8\u304d\u8fbc\u307f\u3092\u884c\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u767a\u751f\u3057\u307e\u3059\u3002\u4e00\u65b9\u3001\u30c7\u30fc\u30bf\u7af6\u5408\u304c\u306a\u3044\u3053\u3068\u304c\u5fc5\u305a\u3057\u3082\u6c7a\u5b9a\u7684\u5b9f\u884c\u3092\u610f\u5473\u3059\u308b\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u52d5\u4f5c\u304c\u5236\u5fa1\u3067\u304d\u306a\u3044\u30a4\u30d9\u30f3\u30c8\u306e\u9806\u5e8f\u3084\u30bf\u30a4\u30df\u30f3\u30b0\u306b\u4f9d\u5b58\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u3053\u308c\u306f\u7af6\u5408\u72b6\u614b\u3067\u3059\u3002
\u7af6\u5408\u554f\u984c\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u304c\u76f4\u9762\u3059\u308b\u53ef\u80fd\u6027\u306e\u3042\u308b\u30d0\u30b0\u306e\u4e2d\u3067\u6700\u3082\u56f0\u96e3\u304b\u3064\u6700\u3082\u6f5c\u4f0f\u6027\u306e\u9ad8\u3044\u30d0\u30b0\u306e 1 \u3064\u3068\u306a\u308a\u307e\u3059\u3002Go \u958b\u767a\u8005\u3068\u3057\u3066\u3001\u79c1\u305f\u3061\u306f\u30c7\u30fc\u30bf\u7af6\u5408\u3068\u7af6\u5408\u72b6\u614b\u3001\u305d\u308c\u3089\u304c\u53ca\u307c\u3057\u3046\u308b\u5f71\u97ff\u3001\u304a\u3088\u3073\u305d\u308c\u3089\u3092\u56de\u907f\u3059\u308b\u65b9\u6cd5\u306a\u3069\u306e\u91cd\u8981\u306a\u5074\u9762\u3092\u7406\u89e3\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
"},{"location":"ja/#_10","title":"\u30c7\u30fc\u30bf\u7af6\u5408","text":"\u30c7\u30fc\u30bf\u7af6\u5408\u306f\u30012 \u3064\u4ee5\u4e0a\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u540c\u3058\u30e1\u30e2\u30ea\u4f4d\u7f6e\u306b\u540c\u6642\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u5c11\u306a\u304f\u3068\u3082 1 \u3064\u304c\u66f8\u304d\u8fbc\u307f\u3092\u884c\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u767a\u751f\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u5371\u967a\u306a\u7d50\u679c\u304c\u751f\u3058\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3055\u3089\u306b\u60aa\u3044\u3053\u3068\u306b\u3001\u72b6\u6cc1\u306b\u3088\u3063\u3066\u306f\u3001\u30e1\u30e2\u30ea\u4f4d\u7f6e\u306b\u7121\u610f\u5473\u306a\u30d3\u30c3\u30c8\u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u542b\u3080\u5024\u304c\u4fdd\u6301\u3055\u308c\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
\u3055\u307e\u3056\u307e\u306a\u624b\u6cd5\u3092\u99c6\u4f7f\u3057\u3066\u3001\u30c7\u30fc\u30bf\u7af6\u5408\u306e\u767a\u751f\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3068\u3048\u3070
sync/atomic
\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u7528\u3059\u308b \u5b9f\u884c\u3057\u305f\u3044\u64cd\u4f5c\u306b\u5fdc\u3058\u3066\u3001\u30c7\u30fc\u30bf\u7af6\u5408\u306e\u306a\u3044\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u5fc5\u305a\u3057\u3082\u6c7a\u5b9a\u7684\u306a\u7d50\u679c\u3092\u610f\u5473\u3059\u308b\u3067\u3057\u3087\u3046\u304b\u3002\u305d\u3046\u3068\u306f\u3044\u3048\u307e\u305b\u3093\u3002
\u7af6\u5408\u72b6\u614b\u306f\u3001\u52d5\u4f5c\u304c\u5236\u5fa1\u3067\u304d\u306a\u3044\u30a4\u30d9\u30f3\u30c8\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u307e\u305f\u306f\u30bf\u30a4\u30df\u30f3\u30b0\u306b\u4f9d\u5b58\u3059\u308b\u5834\u5408\u306b\u767a\u751f\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u30a4\u30d9\u30f3\u30c8\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u304c\u30b4\u30eb\u30fc\u30c1\u30f3\u306e\u5b9f\u884c\u9806\u5e8f\u3067\u3059\u3002
\u307e\u3068\u3081\u308b\u3068\u3001\u4e26\u884c\u51e6\u7406\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u4f5c\u696d\u3059\u308b\u5834\u5408\u3001\u30c7\u30fc\u30bf\u7af6\u5408\u306f\u7af6\u5408\u72b6\u614b\u3068\u306f\u7570\u306a\u308b\u3053\u3068\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u4e0d\u53ef\u6b20\u3067\u3059\u3002\u30c7\u30fc\u30bf\u7af6\u5408\u306f\u3001\u8907\u6570\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u540c\u3058\u30e1\u30e2\u30ea\u4f4d\u7f6e\u306b\u540c\u6642\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u305d\u306e\u3046\u3061\u306e\u5c11\u306a\u304f\u3068\u3082 1 \u3064\u304c\u66f8\u304d\u8fbc\u307f\u3092\u884c\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u767a\u751f\u3057\u307e\u3059\u3002\u30c7\u30fc\u30bf\u7af6\u5408\u3068\u306f\u3001\u4e88\u671f\u3057\u306a\u3044\u52d5\u4f5c\u3092\u610f\u5473\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u30c7\u30fc\u30bf\u7af6\u5408\u306e\u306a\u3044\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u5fc5\u305a\u3057\u3082\u6c7a\u5b9a\u7684\u306a\u7d50\u679c\u3092\u610f\u5473\u3059\u308b\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u30c7\u30fc\u30bf\u7af6\u5408\u304c\u306a\u304f\u3066\u3082\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306f\u5236\u5fa1\u3055\u308c\u3066\u3044\u306a\u3044\u30a4\u30d9\u30f3\u30c8\uff08\u30b4\u30eb\u30fc\u30c1\u30f3\u306e\u5b9f\u884c\u3001\u30c1\u30e3\u30cd\u30eb\u3078\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u767a\u4fe1\u901f\u5ea6\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e\u547c\u3073\u51fa\u3057\u306e\u7d99\u7d9a\u6642\u9593\u306a\u3069\uff09\u306b\u4f9d\u5b58\u3059\u308b\u6319\u52d5\u3092\u6301\u3064\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u306e\u5834\u5408\u306f\u7af6\u5408\u72b6\u614b\u3067\u3059\u3002\u4e26\u884c\u51e6\u7406\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u8a2d\u8a08\u306b\u719f\u7df4\u3059\u308b\u306b\u306f\u3001\u4e21\u65b9\u306e\u6982\u5ff5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u809d\u8981\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#59","title":"\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u30bf\u30a4\u30d7\u3054\u3068\u306e\u4e26\u884c\u51e6\u7406\u306e\u5f71\u97ff\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#59)","text":"\u8981\u7d04\u4e00\u5b9a\u6570\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u306f\u3001\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u306e\u30bf\u30a4\u30d7\u3092\u8003\u616e\u3057\u3066\u304f\u3060\u3055\u3044\u3002CPU \u30d0\u30a6\u30f3\u30c9\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u4f5c\u6210\u3059\u308b\u3068\u3044\u3046\u3053\u3068\u306f\u3001\u3053\u306e\u6570\u3092 GOMAXPROCS
\u5909\u6570\uff08\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u30db\u30b9\u30c8\u4e0a\u306e CPU \u30b3\u30a2\u306e\u6570\u306b\u57fa\u3065\u304f\uff09\u306b\u8fd1\u3065\u3051\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002 I/O \u30d0\u30a6\u30f3\u30c9\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u306e\u4f5c\u6210\u306f\u3001\u5916\u90e8\u30b7\u30b9\u30c6\u30e0\u306a\u3069\u306e\u4ed6\u306e\u8981\u56e0\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002
\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u306f\u3001\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u306e\u5b9f\u884c\u6642\u9593\u306f\u6b21\u306e\u3044\u305a\u308c\u304b\u306b\u3088\u3063\u3066\u5236\u9650\u3055\u308c\u307e\u3059\u3002
\u3053\u3053\u6570\u5341\u5e74\u3067\u30e1\u30e2\u30ea\u304c\u975e\u5e38\u306b\u5b89\u4fa1\u306b\u306a\u3063\u305f\u3053\u3068\u3092\u8003\u616e\u3059\u308b\u3068\u3001\u5f8c\u8005\u306f\u73fe\u5728\u3067\u306f\u6700\u3082\u307e\u308c\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u6700\u521d\u306e 2 \u3064\u306e\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u30bf\u30a4\u30d7\u3001CPU \u30d0\u30a6\u30f3\u30c9\u3068 I/O \u30d0\u30a6\u30f3\u30c9\u306b\u7126\u70b9\u3092\u5f53\u3066\u307e\u3059\u3002
\u30ef\u30fc\u30ab\u30fc\u306b\u3088\u3063\u3066\u5b9f\u884c\u3055\u308c\u308b\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u304c I/O \u30d0\u30a6\u30f3\u30c9\u3067\u3042\u308b\u5834\u5408\u3001\u5024\u306f\u4e3b\u306b\u5916\u90e8\u30b7\u30b9\u30c6\u30e0\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u9006\u306b\u3001\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u304c CPU \u306b\u4f9d\u5b58\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u306e\u6700\u9069\u306a\u6570\u306f\u5229\u7528\u53ef\u80fd\u306a CPU \u30b3\u30a2\u306e\u6570\u306b\u8fd1\u304f\u306a\u308a\u307e\u3059\uff08\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u306f runtime.GOMAXPROCS
\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3059\uff09\u3002\u4e26\u884c\u51e6\u7406\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u8a2d\u8a08\u3059\u308b\u5834\u5408\u3001\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u306e\u30bf\u30a4\u30d7\uff08 I/O \u3042\u308b\u3044\u306f CPU \uff09\u3092\u77e5\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#go-context-60","title":"Go Context \u306b\u5bfe\u3059\u308b\u8aa4\u89e3 (#60)","text":"\u8981\u7d04Go Context \u306f\u3001Go\u8a00\u8a9e\u306e\u4e26\u884c\u51e6\u7406\u306e\u57fa\u790e\u306e\u4e00\u90e8\u3067\u3082\u3042\u308a\u307e\u3059\u3002 Context \u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30c7\u30c3\u30c9\u30e9\u30a4\u30f3\u3001\u30ad\u30fc\u3068\u5024\u306e\u30ea\u30b9\u30c8\u3092\u4fdd\u6301\u3067\u304d\u307e\u3059\u3002
https://pkg.go.dev/context
Context \u306f\u3001\u30c7\u30c3\u30c9\u30e9\u30a4\u30f3\u3001\u30ad\u30e3\u30f3\u30bb\u30eb\u30b7\u30b0\u30ca\u30eb\u3001\u305d\u306e\u4ed6\u306e\u5024\u3092 API \u306e\u5883\u754c\u3092\u8d8a\u3048\u3066\u4f1d\u9054\u3057\u307e\u3059\u3002
"},{"location":"ja/#_12","title":"\u30c7\u30c3\u30c9\u30e9\u30a4\u30f3","text":"\u30c7\u30c3\u30c9\u30e9\u30a4\u30f3\u3068\u306f\u3001\u6b21\u306e\u3044\u305a\u308c\u304b\u3067\u6c7a\u5b9a\u3055\u308c\u308b\u7279\u5b9a\u306e\u6642\u70b9\u3092\u6307\u3057\u307e\u3059\u3002
time.Duration
\uff08\u4f8b\uff1a250 ms\uff09time.Time
\uff08\u4f8b\uff1a2023-02-07 00:00:00 UTC\uff09 \u30c7\u30c3\u30c9\u30e9\u30a4\u30f3\u306e\u30bb\u30de\u30f3\u30c6\u30a3\u30af\u30b9\u306f\u3001\u3053\u308c\u3092\u904e\u304e\u305f\u5834\u5408\u306f\u9032\u884c\u4e2d\u306e\u30a2\u30af\u30c6\u30a3\u30d3\u30c6\u30a3\u3092\u505c\u6b62\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u3092\u4f1d\u3048\u307e\u3059\u3002\u30a2\u30af\u30c6\u30a3\u30d3\u30c6\u30a3\u3068\u306f\u3001\u305f\u3068\u3048\u3070\u3001\u30c1\u30e3\u30cd\u30eb\u304b\u3089\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u53d7\u4fe1\u3092\u5f85\u6a5f\u3057\u3066\u3044\u308b I/O \u30ea\u30af\u30a8\u30b9\u30c8\u3084\u30b4\u30eb\u30fc\u30c1\u30f3\u3067\u3059\u3002
"},{"location":"ja/#_13","title":"\u30ad\u30e3\u30f3\u30bb\u30eb\u30b7\u30b0\u30ca\u30eb","text":"Go Context \u306e\u3082\u3046 1 \u3064\u306e\u4f7f\u7528\u4f8b\u306f\u3001\u30ad\u30e3\u30f3\u30bb\u30eb\u30b7\u30b0\u30ca\u30eb\u3092\u4f1d\u9001\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u5225\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u5185\u3067 CreateFileWatcher(ctx context.Context, filename string)
\u3092\u547c\u3073\u51fa\u3059\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u3092\u60f3\u50cf\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u3053\u306e\u95a2\u6570\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8aad\u307f\u53d6\u308a\u3092\u7d9a\u3051\u3066\u66f4\u65b0\u3092\u30ad\u30e3\u30c3\u30c1\u3059\u308b\u7279\u5b9a\u306e\u30d5\u30a1\u30a4\u30eb\u30a6\u30a9\u30c3\u30c1\u30e3\u30fc\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u63d0\u4f9b\u3055\u308c\u305f Context \u304c\u671f\u9650\u5207\u308c\u306b\u306a\u308b\u304b\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u308b\u3068\u3001\u3053\u306e\u95a2\u6570\u306f\u305d\u308c\u3092\u51e6\u7406\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u8a18\u8ff0\u5b50\u3092\u9589\u3058\u307e\u3059\u3002
Go Context \u306e\u6700\u5f8c\u306e\u4f7f\u7528\u4f8b\u306f\u3001\u30ad\u30fc\u3068\u5024\u306e\u30ea\u30b9\u30c8\u3092\u904b\u3076\u3053\u3068\u3067\u3059\u3002 Context \u306b\u30ad\u30fc\u3068\u5024\u306e\u30ea\u30b9\u30c8\u3092\u542b\u3081\u308b\u610f\u5473\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\u3002Go Context \u306f\u6c4e\u7528\u7684\u3067\u3042\u308b\u305f\u3081\u3001\u4f7f\u7528\u4f8b\u306f\u7121\u9650\u306b\u3042\u308a\u307e\u3059\u3002
\u305f\u3068\u3048\u3070\u3001\u30c8\u30ec\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u7570\u306a\u308b\u526f\u6b21\u6a5f\u80fd\u9593\u3067\u540c\u3058\u76f8\u95a2 ID \u3092\u5171\u6709\u3057\u305f\u3044\u3053\u3068\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u4e00\u90e8\u306e\u958b\u767a\u8005\u306f\u3001\u3053\u306e ID \u3092\u95a2\u6570\u30b7\u30b0\u30cd\u30c1\u30e3\u306e\u4e00\u90e8\u306b\u3059\u308b\u306b\u306f\u3042\u307e\u308a\u306b\u4fb5\u7565\u7684\u3060\u3068\u8003\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3053\u306e\u70b9\u306b\u95a2\u3057\u3066\u3001\u4e0e\u3048\u3089\u308c\u305f Context \u306e\u4e00\u90e8\u3068\u3057\u3066\u305d\u308c\u3092\u542b\u3081\u308b\u3053\u3068\u3092\u6c7a\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002
"},{"location":"ja/#context","title":"Context \u306e\u30ad\u30e3\u30f3\u30bb\u30eb\u3092\u30ad\u30e3\u30c3\u30c1\u3059\u308b","text":"context.Context
\u30bf\u30a4\u30d7\u306f\u3001\u53d7\u4fe1\u5c02\u7528\u306e\u901a\u77e5\u30c1\u30e3\u30cd\u30eb <-chan struct{}
\u3092\u8fd4\u3059 Done
\u30e1\u30bd\u30c3\u30c9\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u307e\u3059\u3002\u3053\u306e\u30c1\u30e3\u30cd\u30eb\u306f\u3001 Context \u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u4f5c\u696d\u3092\u30ad\u30e3\u30f3\u30bb\u30eb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306b\u9589\u3058\u3089\u308c\u307e\u3059\u3002\u305f\u3068\u3048\u3070
context.WithCancel
\u3067\u4f5c\u6210\u3055\u308c\u305f Context \u306b\u95a2\u9023\u3059\u308bDone\u30c1\u30e3\u30cd\u30eb\u306f\u3001cancel\u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u3068\u9589\u3058\u3089\u308c\u307e\u3059\u3002 context.WithDeadline
\u3067\u4f5c\u6210\u3057\u305f Context \u306b\u95a2\u9023\u3059\u308bDone\u30c1\u30e3\u30cd\u30eb\u306f\u3001deadline \u3092\u904e\u304e\u308b\u3068\u9589\u3058\u3089\u308c\u307e\u3059\u3002 \u6ce8\u610f\u3059\u3079\u304d\u70b9\u306e 1 \u3064\u306f\u3001\u5185\u90e8\u30c1\u30e3\u30cd\u30eb\u306f\u3001\u7279\u5b9a\u306e\u5024\u3092\u53d7\u3051\u53d6\u3063\u305f\u3068\u304d\u3067\u306f\u306a\u304f\u3001 Context \u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u305f\u3068\u304d\u3001\u307e\u305f\u306f\u30c7\u30c3\u30c9\u30e9\u30a4\u30f3\u306b\u9054\u3057\u305f\u3068\u304d\u306b\u9589\u3058\u308b\u5fc5\u8981\u304c\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u30c1\u30e3\u30cd\u30eb\u306e\u30af\u30ed\u30fc\u30ba\u306f\u3001\u3059\u3079\u3066\u306e\u6d88\u8cbb\u8005\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u53d7\u3051\u53d6\u308b\u552f\u4e00\u306e\u30c1\u30e3\u30cd\u30eb\u30a2\u30af\u30b7\u30e7\u30f3\u3067\u3042\u308b\u305f\u3081\u3067\u3059\u3002\u3053\u306e\u3088\u3046\u306b\u3057\u3066\u3001 Context \u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u308b\u304b\u3001\u30c7\u30c3\u30c9\u30e9\u30a4\u30f3\u306b\u9054\u3059\u308b\u3068\u3001\u3059\u3079\u3066\u306e\u6d88\u8cbb\u8005\u306b\u901a\u77e5\u304c\u5c4a\u304d\u307e\u3059\u3002
\u307e\u3068\u3081\u308b\u3068\u3001\u719f\u7df4\u3057\u305f Go \u958b\u767a\u8005\u306b\u306a\u308b\u306b\u306f\u3001 Context \u3068\u305d\u306e\u4f7f\u7528\u65b9\u6cd5\u306b\u3064\u3044\u3066\u7406\u89e3\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u539f\u5247\u3068\u3057\u3066\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u5f85\u6a5f\u3055\u305b\u3089\u308c\u308b\u95a2\u6570\u306f Context \u3092\u53d6\u5f97\u3059\u308b\u3079\u304d\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u4e0a\u6d41\u306e\u547c\u3073\u51fa\u3057\u5143\u304c\u3053\u306e\u95a2\u6570\u3092\u3044\u3064\u547c\u3073\u51fa\u3059\u304b\u3092\u6c7a\u5b9a\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u304b\u3089\u3067\u3059\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#sync-74","title":"sync
\u578b\u306e\u30b3\u30d4\u30fc (#74)","text":"\u8981\u7d04 sync
\u578b\u306f\u30b3\u30d4\u30fc\u3055\u308c\u308b\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002
\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9
"},{"location":"ja/#97","title":"\u30a4\u30f3\u30e9\u30a4\u30f3\u5c55\u958b\u3092\u3057\u3066\u3044\u306a\u3044 (#97)","text":"\u8981\u7d04\u30d5\u30a1\u30b9\u30c8\u30d1\u30b9\u306e\u30a4\u30f3\u30e9\u30a4\u30f3\u5316\u624b\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u3001\u95a2\u6570\u306e\u547c\u3073\u51fa\u3057\u306b\u304b\u304b\u308b\u511f\u5374\u6642\u9593\u3092\u52b9\u7387\u7684\u306b\u524a\u6e1b\u3057\u307e\u3059\u3002
"},{"location":"ja/#go-98","title":"Go\u8a00\u8a9e\u306e\u8a3a\u65ad\u30c4\u30fc\u30eb\u3092\u5229\u7528\u3057\u3066\u3044\u306a\u3044 (#98)","text":"\u8981\u7d04\u30d7\u30ed\u30d5\u30a1\u30a4\u30ea\u30f3\u30b0\u3068\u5b9f\u884c\u30c8\u30ec\u30fc\u30b5\u3092\u5229\u7528\u3057\u3066\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u6700\u9069\u5316\u3059\u3079\u304d\u90e8\u5206\u306b\u3064\u3044\u3066\u7406\u89e3\u3057\u307e\u3057\u3087\u3046\u3002
\u30bb\u30af\u30b7\u30e7\u30f3\u5168\u6587\u306f\u3053\u3061\u3089\u3002
"},{"location":"ja/#gc-99","title":"GC \u306e\u4ed5\u7d44\u307f\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#99)","text":"\u8981\u7d04GC \u306e\u8abf\u6574\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3068\u3001\u7a81\u7136\u306e\u8ca0\u8377\u306e\u5897\u52a0\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u51e6\u7406\u3067\u304d\u308b\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u6069\u6075\u304c\u5f97\u3089\u308c\u307e\u3059\u3002
"},{"location":"ja/#docker-kubernetes-go-100","title":"Docker \u3068 Kubernetes \u4e0a\u3067Go\u8a00\u8a9e\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u306e\u5f71\u97ff\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#100)","text":"\u8981\u7d04Docker \u3068 Kubernetes \u306b\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u969b\u306eCPU\u30b9\u30ed\u30c3\u30c8\u30ea\u30f3\u30b0\u3092\u56de\u907f\u3059\u308b\u306b\u306f\u3001Go\u8a00\u8a9e\u304cCFS\u5bfe\u5fdc\u3067\u306f\u306a\u3044\u3053\u3068\u306b\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002
"},{"location":"jobs/","title":"Go Jobs","text":"Is your company hiring? Sponsor this repository and let a significant audience of Go developers know about your opportunities in this section (book sales: +8k, website traffic: +2000 views and +400 unique visitors per week).
"},{"location":"zh/","title":"100\u4e2aGo\u5e38\u89c1\u9519\u8bef\u53ca\u5982\u4f55\u907f\u514d","text":""},{"location":"zh/#_1","title":"\u4ee3\u7801\u53ca\u5de5\u7a0b\u7ec4\u7ec7","text":""},{"location":"zh/#1","title":"\u610f\u5916\u7684\u53d8\u91cf\u9690\u85cf (#1)","text":"\u907f\u514d\u53d8\u91cf\u9690\u85cf\uff08\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\u88ab\u5185\u90e8\u4f5c\u7528\u57df\u540c\u540d\u53d8\u91cf\u9690\u85cf\uff09\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u53d8\u91cf\u5f15\u7528\u9519\u8bef\uff0c\u6709\u52a9\u4e8e\u4ed6\u4eba\u9605\u8bfb\u7406\u89e3\u3002
"},{"location":"zh/#2","title":"\u4e0d\u5fc5\u8981\u7684\u4ee3\u7801\u5d4c\u5957 (#2)","text":"\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u3001\u8fc7\u591a\u7684\u5d4c\u5957\u5c42\u6b21\uff0c\u5e76\u4e14\u8ba9\u6b63\u5e38\u4ee3\u7801\u8def\u5f84\u5c3d\u91cf\u5de6\u5bf9\u9f50\uff08\u800c\u4e0d\u662f\u653e\u5728\u5206\u652f\u8def\u5f84\u4e2d\uff09\uff0c\u6709\u52a9\u4e8e\u6784\u5efa\u53ef\u8bfb\u6027\u66f4\u597d\u7684\u4ee3\u7801\u3002
"},{"location":"zh/#init-3","title":"\u8bef\u7528init\u51fd\u6570 (#3)","text":"\u521d\u59cb\u5316\u53d8\u91cf\u65f6\uff0c\u8bf7\u8bb0\u4f4f init \u51fd\u6570\u5177\u6709\u6709\u9650\u7684\u9519\u8bef\u5904\u7406\u80fd\u529b\uff0c\u5e76\u4e14\u4f1a\u4f7f\u72b6\u6001\u5904\u7406\u548c\u6d4b\u8bd5\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u521d\u59cb\u5316\u5e94\u8be5\u4f5c\u4e3a\u7279\u5b9a\u51fd\u6570\u6765\u5904\u7406\u3002
"},{"location":"zh/#getterssetters-4","title":"\u6ee5\u7528getters/setters (#4)","text":"\u5728Go\u8bed\u8a00\u4e2d\uff0c\u5f3a\u5236\u4f7f\u7528getter\u548csetter\u65b9\u6cd5\u5e76\u4e0d\u7b26\u5408Go\u60ef\u4f8b\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u5e94\u8be5\u627e\u5230\u6548\u7387\u548c\u76f2\u76ee\u9075\u5faa\u67d0\u4e9b\u60ef\u7528\u6cd5\u4e4b\u95f4\u7684\u5e73\u8861\u70b9\u3002
"},{"location":"zh/#5","title":"\u63a5\u53e3\u6c61\u67d3 (#5)","text":"\u62bd\u8c61\u5e94\u8be5\u88ab\u53d1\u73b0\uff0c\u800c\u4e0d\u662f\u88ab\u521b\u9020\u3002\u4e3a\u4e86\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\uff0c\u9700\u8981\u65f6\u624d\u521b\u5efa\u63a5\u53e3\uff0c\u800c\u4e0d\u662f\u9884\u89c1\u5230\u9700\u8981\u5b83\uff0c\u6216\u8005\u81f3\u5c11\u53ef\u4ee5\u8bc1\u660e\u8fd9\u79cd\u62bd\u8c61\u662f\u6709\u4ef7\u503c\u7684\u3002
"},{"location":"zh/#6","title":"\u5c06\u63a5\u53e3\u5b9a\u4e49\u5728\u5b9e\u73b0\u65b9\u4e00\u4fa7 (#6)","text":"\u5c06\u63a5\u53e3\u4fdd\u7559\u5728\u5f15\u7528\u65b9\u4e00\u4fa7\uff08\u800c\u4e0d\u662f\u5b9e\u73b0\u65b9\u4e00\u4fa7\uff09\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62bd\u8c61\u3002
"},{"location":"zh/#7","title":"\u5c06\u63a5\u53e3\u4f5c\u4e3a\u8fd4\u56de\u503c (#7)","text":"\u4e3a\u4e86\u907f\u514d\u5728\u7075\u6d3b\u6027\u65b9\u9762\u53d7\u5230\u9650\u5236\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u51fd\u6570\u4e0d\u5e94\u8be5\u8fd4\u56de\u63a5\u53e3\uff0c\u800c\u5e94\u8be5\u8fd4\u56de\u5177\u4f53\u7684\u5b9e\u73b0\u3002\u76f8\u53cd\uff0c\u51fd\u6570\u5e94\u8be5\u5c3d\u53ef\u80fd\u5730\u4f7f\u7528\u63a5\u53e3\u4f5c\u4e3a\u53c2\u6570\u3002
"},{"location":"zh/#any-8","title":"any
\u6ca1\u4f20\u9012\u4efb\u4f55\u4fe1\u606f (#8)","text":"\u53ea\u6709\u5728\u9700\u8981\u63a5\u53d7\u6216\u8fd4\u56de\u4efb\u610f\u7c7b\u578b\u65f6\uff0c\u624d\u4f7f\u7528 any
\uff0c\u4f8b\u5982 json.Marshal
\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u56e0\u4e3a any
\u4e0d\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u4fe1\u606f\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u65f6\u95ee\u9898\uff0c\u5982\u5141\u8bb8\u8c03\u7528\u8005\u8c03\u7528\u65b9\u6cd5\u5904\u7406\u4efb\u610f\u7c7b\u578b\u6570\u636e\u3002
\u4f7f\u7528\u6cdb\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u53c2\u6570\u5206\u79bb\u5177\u4f53\u7684\u6570\u636e\u7c7b\u578b\u548c\u884c\u4e3a\uff0c\u907f\u514d\u5199\u5f88\u591a\u91cd\u590d\u5ea6\u5f88\u9ad8\u7684\u4ee3\u7801\u3002\u7136\u800c\uff0c\u4e0d\u8981\u8fc7\u65e9\u5730\u4f7f\u7528\u6cdb\u578b\u3001\u7c7b\u578b\u53c2\u6570\uff0c\u53ea\u6709\u5728\u4f60\u770b\u5230\u771f\u6b63\u9700\u8981\u65f6\u624d\u4f7f\u7528\u3002\u5426\u5219\uff0c\u5b83\u4eec\u4f1a\u5f15\u5165\u4e0d\u5fc5\u8981\u7684\u62bd\u8c61\u548c\u590d\u6742\u6027\u3002
"},{"location":"zh/#10","title":"\u672a\u610f\u8bc6\u5230\u7c7b\u578b\u5d4c\u5957\u7684\u53ef\u80fd\u95ee\u9898 (#10)","text":"\u4f7f\u7528\u7c7b\u578b\u5d4c\u5957\u4e5f\u53ef\u4ee5\u907f\u514d\u5199\u4e00\u4e9b\u91cd\u590d\u4ee3\u7801\uff0c\u7136\u800c\uff0c\u5728\u4f7f\u7528\u65f6\u9700\u8981\u786e\u4fdd\u4e0d\u4f1a\u5bfc\u81f4\u4e0d\u5408\u7406\u7684\u53ef\u89c1\u6027\u95ee\u9898\uff0c\u6bd4\u5982\u6709\u4e9b\u5b57\u6bb5\u5e94\u8be5\u5bf9\u5916\u9690\u85cf\u4e0d\u5e94\u8be5\u88ab\u66b4\u6f0f\u3002
"},{"location":"zh/#function-option-11","title":"\u4e0d\u4f7f\u7528function option\u6a21\u5f0f (#11)","text":"\u4e3a\u4e86\u8bbe\u8ba1\u5e76\u63d0\u4f9b\u66f4\u53cb\u597d\u7684API\uff08\u53ef\u9009\u53c2\u6570\uff09\uff0c\u4e3a\u4e86\u66f4\u597d\u5730\u5904\u7406\u9009\u9879\uff0c\u5e94\u8be5\u4f7f\u7528function option\u6a21\u5f0f\u3002
"},{"location":"zh/#12","title":"\u5de5\u7a0b\u7ec4\u7ec7\u4e0d\u5408\u7406 (\u5de5\u7a0b\u7ed3\u6784\u548c\u5305\u7684\u7ec4\u7ec7) (#12)","text":"\u9075\u5faa\u50cf project-layout \u7684\u5efa\u8bae\u6765\u7ec4\u7ec7Go\u5de5\u7a0b\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u4f60\u6b63\u5728\u5bfb\u627e\u4e00\u4e9b\u7c7b\u4f3c\u7684\u7ecf\u9a8c\u3001\u60ef\u4f8b\u6765\u7ec4\u7ec7\u4e00\u4e2a\u65b0\u7684Go\u5de5\u7a0b\u7684\u65f6\u5019\u3002
"},{"location":"zh/#13","title":"\u521b\u5efa\u5de5\u5177\u5305 (#13)","text":"\u547d\u540d\u662f\u8f6f\u4ef6\u8bbe\u8ba1\u5f00\u53d1\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u90e8\u5206\uff0c\u521b\u5efa\u4e00\u4e9b\u540d\u5982 common
\u3001util
\u3001shared
\u4e4b\u7c7b\u7684\u5305\u540d\u5e76\u4e0d\u4f1a\u7ed9\u8bfb\u8005\u5e26\u6765\u592a\u5927\u4ef7\u503c\uff0c\u5e94\u8be5\u5c06\u8fd9\u4e9b\u5305\u540d\u91cd\u6784\u4e3a\u66f4\u6e05\u6670\u3001\u66f4\u805a\u7126\u7684\u5305\u540d\u3002
\u4e3a\u4e86\u907f\u514d\u53d8\u91cf\u540d\u548c\u5305\u540d\u4e4b\u95f4\u7684\u51b2\u7a81\uff0c\u5bfc\u81f4\u6df7\u6dc6\u6216\u751a\u81f3\u9519\u8bef\uff0c\u5e94\u4e3a\u6bcf\u4e2a\u53d8\u91cf\u548c\u5305\u4f7f\u7528\u552f\u4e00\u7684\u540d\u79f0\u3002\u5982\u679c\u8fd9\u4e0d\u53ef\u884c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5bfc\u5165\u522b\u540d import importAlias 'importPath'
\uff0c\u4ee5\u533a\u5206\u5305\u540d\u548c\u53d8\u91cf\u540d\uff0c\u6216\u8005\u8003\u8651\u4e00\u4e2a\u66f4\u597d\u7684\u53d8\u91cf\u540d\u3002
\u4e3a\u4e86\u8ba9\u4f7f\u7528\u65b9\u3001\u7ef4\u62a4\u4eba\u5458\u80fd\u66f4\u6e05\u6670\u5730\u4e86\u89e3\u4f60\u7684\u4ee3\u7801\u7684\u610f\u56fe\uff0c\u5bfc\u51fa\u7684\u5143\u7d20\uff08\u51fd\u6570\u3001\u7c7b\u578b\u3001\u5b57\u6bb5\uff09\u9700\u8981\u6dfb\u52a0godoc\u6ce8\u91ca\u3002
"},{"location":"zh/#linters-16","title":"\u4e0d\u4f7f\u7528linters\u68c0\u67e5 (#16)","text":"\u4e3a\u4e86\u6539\u5584\u4ee3\u7801\u8d28\u91cf\u3001\u6574\u4f53\u4ee3\u7801\u7684\u4e00\u81f4\u6027\uff0c\u5e94\u8be5\u4f7f\u7528linters\u3001formatters\u3002
"},{"location":"zh/#_2","title":"\u6570\u636e\u7c7b\u578b","text":""},{"location":"zh/#17","title":"\u516b\u8fdb\u5236\u5b57\u9762\u91cf\u5f15\u53d1\u7684\u56f0\u60d1 (#17)","text":"\u5728\u9605\u8bfb\u73b0\u6709\u4ee3\u7801\u65f6\uff0c\u8bf7\u8bb0\u4f4f\u4ee5 0 \u5f00\u5934\u7684\u6574\u6570\u5b57\u9762\u91cf\u662f\u516b\u8fdb\u5236\u6570\u3002\u6b64\u5916\uff0c\u4e3a\u4e86\u63d0\u9ad8\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u524d\u9762\u52a0\u4e0a 0o \u6765\u663e\u5f0f\u5730\u8868\u793a\u516b\u8fdb\u5236\u6574\u6570\u3002
"},{"location":"zh/#18","title":"\u672a\u6ce8\u610f\u53ef\u80fd\u7684\u6574\u6570\u6ea2\u51fa (#18)","text":"\u5728 Go \u4e2d\u6574\u6570\u4e0a\u6ea2\u51fa\u548c\u4e0b\u6ea2\u662f\u9759\u9ed8\u5904\u7406\u7684\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5df1\u7684\u51fd\u6570\u6765\u6355\u83b7\u5b83\u4eec\u3002
"},{"location":"zh/#19","title":"\u6ca1\u6709\u900f\u5f7b\u7406\u89e3\u6d6e\u70b9\u6570 (#19)","text":"\u6bd4\u8f83\u6d6e\u70b9\u6570\u65f6\uff0c\u901a\u8fc7\u6bd4\u8f83\u4e8c\u8005\u7684delta\u503c\u662f\u5426\u4ecb\u4e8e\u4e00\u5b9a\u7684\u8303\u56f4\u5185\uff0c\u80fd\u8ba9\u4f60\u5199\u51fa\u53ef\u79fb\u690d\u6027\u66f4\u597d\u7684\u4ee3\u7801\u3002
\u5728\u8fdb\u884c\u52a0\u6cd5\u6216\u51cf\u6cd5\u65f6\uff0c\u5c06\u5177\u6709\u76f8\u4f3c\u6570\u91cf\u7ea7\u7684\u64cd\u4f5c\u5206\u6210\u540c\u4e00\u7ec4\u4ee5\u63d0\u9ad8\u7cbe\u5ea6 (\u8fc7\u65e9\u6307\u6570\u5bf9\u9f50\u4e22\u5931\u7cbe\u5ea6)\u3002\u6b64\u5916\uff0c\u5728\u8fdb\u884c\u52a0\u6cd5\u548c\u51cf\u6cd5\u4e4b\u524d\uff0c\u5e94\u5148\u8fdb\u884c\u4e58\u6cd5\u548c\u9664\u6cd5 (\u52a0\u51cf\u6cd5\u8bef\u5dee\u4f1a\u88ab\u4e58\u9664\u653e\u5927)\u3002
"},{"location":"zh/#slice-20","title":"\u4e0d\u7406\u89e3slice\u7684\u957f\u5ea6\u548c\u5bb9\u91cf (#20)","text":"\u7406\u89e3slice\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u7684\u533a\u522b\uff0c\u662f\u4e00\u4e2aGo\u5f00\u53d1\u8005\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\u4e4b\u4e00\u3002slice\u7684\u957f\u5ea6\u6307\u7684\u662fslice\u5df2\u7ecf\u5b58\u50a8\u7684\u5143\u7d20\u7684\u6570\u91cf\uff0c\u800c\u5bb9\u91cf\u6307\u7684\u662fslice\u5f53\u524d\u5e95\u5c42\u5f00\u8f9f\u7684\u6570\u7ec4\u6700\u591a\u80fd\u5bb9\u7eb3\u7684\u5143\u7d20\u7684\u6570\u91cf\u3002
"},{"location":"zh/#slice-21","title":"\u4e0d\u9ad8\u6548\u7684slice\u521d\u59cb\u5316 (#21)","text":"\u5f53\u521b\u5efa\u4e00\u4e2aslice\u65f6\uff0c\u5982\u679c\u5176\u957f\u5ea6\u53ef\u4ee5\u9884\u5148\u786e\u5b9a\uff0c\u90a3\u4e48\u53ef\u4ee5\u5728\u5b9a\u4e49\u65f6\u6307\u5b9a\u5b83\u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u3002\u8fd9\u53ef\u4ee5\u6539\u5584\u540e\u671fappend\u65f6\u4e00\u6b21\u6216\u8005\u591a\u6b21\u7684\u5185\u5b58\u5206\u914d\u64cd\u4f5c\uff0c\u4ece\u800c\u6539\u5584\u6027\u80fd\u3002\u5bf9\u4e8emap\u7684\u521d\u59cb\u5316\u4e5f\u662f\u8fd9\u6837\u7684\u3002
"},{"location":"zh/#nilslice-22","title":"\u56f0\u60d1\u4e8enil\u548c\u7a7aslice (#22)","text":"\u4e3a\u4e86\u907f\u514d\u5e38\u89c1\u7684\u5bf9nil\u548cempty slice\u5904\u7406\u884c\u4e3a\u7684\u6df7\u6dc6\uff0c\u4f8b\u5982\u5728\u4f7f\u7528 encoding/json \u6216 reflect \u5305\u65f6\uff0c\u4f60\u9700\u8981\u7406\u89e3 nil \u548c empty slice\u7684\u533a\u522b\u3002\u4e24\u8005\u90fd\u662f\u957f\u5ea6\u4e3a\u96f6\u3001\u5bb9\u91cf\u4e3a\u96f6\u7684\u5207\u7247\uff0c\u4f46\u662f nil \u5207\u7247\u4e0d\u9700\u8981\u5206\u914d\u5185\u5b58\u3002
"},{"location":"zh/#slice-23","title":"\u6ca1\u6709\u9002\u5f53\u68c0\u67e5slice\u662f\u5426\u4e3a\u7a7a (#23)","text":"\u68c0\u67e5\u4e00\u4e2aslice\u7684\u662f\u5426\u5305\u542b\u4efb\u4f55\u5143\u7d20\uff0c\u53ef\u4ee5\u68c0\u67e5\u5176\u957f\u5ea6\uff0c\u4e0d\u7ba1slice\u662fnil\u8fd8\u662fempty\uff0c\u68c0\u67e5\u957f\u5ea6\u90fd\u662f\u6709\u6548\u7684\u3002\u8fd9\u4e2a\u68c0\u67e5\u65b9\u6cd5\u4e5f\u9002\u7528\u4e8emap\u3002
\u4e3a\u4e86\u8bbe\u8ba1\u66f4\u660e\u786e\u7684API\uff0cAPI\u4e0d\u5e94\u533a\u5206nil\u548c\u7a7a\u5207\u7247\u3002
"},{"location":"zh/#slice-24","title":"\u6ca1\u6709\u6b63\u786e\u62f7\u8d1dslice (#24)","text":"\u4f7f\u7528 copy
\u62f7\u8d1d\u4e00\u4e2aslice\u5143\u7d20\u5230\u53e6\u4e00\u4e2aslice\u65f6\uff0c\u9700\u8981\u8bb0\u5f97\uff0c\u5b9e\u9645\u62f7\u8d1d\u7684\u5143\u7d20\u6570\u91cf\u662f\u4e8c\u8005slice\u957f\u5ea6\u4e2d\u7684\u8f83\u5c0f\u503c\u3002
\u5982\u679c\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u64cd\u4f5c\u7684slice\u590d\u7528\u4e86\u76f8\u540c\u7684\u5e95\u5c42\u6570\u7ec4\uff0c\u5b83\u4eec\u5bf9slice\u6267\u884cappend\u64cd\u4f5c\u65f6\u53ef\u80fd\u4f1a\u4ea7\u751f\u51b2\u7a81\u3002\u4f7f\u7528copy\u6765\u5b8c\u6574\u590d\u5236\u4e00\u4e2aslice\u6216\u8005\u4f7f\u7528\u5b8c\u6574\u7684slice\u8868\u8fbe\u5f0f[low:high:max]\u9650\u5236\u6700\u5927\u5bb9\u91cf\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u4ea7\u751f\u51b2\u7a81\u3002\u5f53\u60f3\u5bf9\u4e00\u4e2a\u5927slice\u8fdb\u884cshrink\u64cd\u4f5c\u65f6\uff0c\u4e24\u79cd\u65b9\u5f0f\u4e2d\uff0c\u53ea\u6709copy\u624d\u53ef\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002
"},{"location":"zh/#slice-26","title":"slice\u548c\u5185\u5b58\u6cc4\u6f0f (#26)","text":"\u5bf9\u4e8eslice\u5143\u7d20\u4e3a\u6307\u9488\uff0c\u6216\u8005slice\u5143\u7d20\u4e3astruct\u4f46\u662f\u8be5struct\u542b\u6709\u6307\u9488\u5b57\u6bb5\uff0c\u5f53\u901a\u8fc7slice[low:high]\u64cd\u4f5c\u53d6subslice\u65f6\uff0c\u5bf9\u4e8e\u90a3\u4e9b\u4e0d\u53ef\u8bbf\u95ee\u7684\u5143\u7d20\u53ef\u4ee5\u663e\u793a\u8bbe\u7f6e\u4e3anil\u6765\u907f\u514d\u5185\u5b58\u6cc4\u9732\u3002
"},{"location":"zh/#map-27","title":"\u4e0d\u9ad8\u6548\u7684map\u521d\u59cb\u5316 (#27)","text":"\u89c1 #21.
"},{"location":"zh/#map-28","title":"map\u548c\u5185\u5b58\u6cc4\u6f0f (#28)","text":"\u4e00\u4e2amap\u7684buckets\u5360\u7528\u7684\u5185\u5b58\u53ea\u4f1a\u589e\u957f\uff0c\u4e0d\u4f1a\u7f29\u51cf\u3002\u56e0\u6b64\uff0c\u5982\u679c\u5b83\u5bfc\u81f4\u4e86\u4e00\u4e9b\u5185\u5b58\u5360\u7528\u7684\u95ee\u9898\uff0c\u4f60\u9700\u8981\u5c1d\u8bd5\u4e0d\u540c\u7684\u9009\u9879\u6765\u89e3\u51b3\uff0c\u6bd4\u5982\u91cd\u65b0\u521b\u5efa\u4e00\u4e2amap\u4ee3\u66ff\u539f\u6765\u7684\uff08\u539f\u6765\u7684map\u4f1a\u88abGC\u6389\uff09\uff0c\u6216\u8005map[keyType]valueType\u4e2d\u7684valueType\u4f7f\u7528\u6307\u9488\u4ee3\u66ff\u957f\u5ea6\u56fa\u5b9a\u7684\u6570\u7ec4\u6216\u8005sliceHeader\u6765\u7f13\u89e3\u8fc7\u591a\u7684\u5185\u5b58\u5360\u7528\u3002
"},{"location":"zh/#29","title":"\u4e0d\u6b63\u786e\u7684\u503c\u6bd4\u8f83 (#29)","text":"Go\u4e2d\u6bd4\u8f83\u4e24\u4e2a\u7c7b\u578b\u503c\u65f6\uff0c\u5982\u679c\u662f\u53ef\u6bd4\u8f83\u7c7b\u578b\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 ==
\u6216\u8005 !=
\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\uff0c\u6bd4\u5982\uff1abooleans\u3001numerals\u3001strings\u3001pointers\u3001channels\uff0c\u4ee5\u53ca\u5b57\u6bb5\u5168\u90e8\u662f\u53ef\u6bd4\u8f83\u7c7b\u578b\u7684structs\u3002\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 reflect.DeepEqual
\u6765\u6bd4\u8f83\uff0c\u7528\u53cd\u5c04\u7684\u8bdd\u4f1a\u727a\u7272\u4e00\u70b9\u6027\u80fd\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u5b9e\u73b0\u548c\u5176\u4ed6\u5e93\u6765\u5b8c\u6210\u3002
range
\u5faa\u73af\u53d8\u91cf\u662f\u4e00\u4e2a\u62f7\u8d1d (#30)","text":"range
\u5faa\u73af\u4e2d\u7684\u5faa\u73af\u53d8\u91cf\u662f\u904d\u5386\u5bb9\u5668\u4e2d\u5143\u7d20\u503c\u7684\u4e00\u4e2a\u62f7\u8d1d\u3002\u56e0\u6b64\uff0c\u5982\u679c\u5143\u7d20\u503c\u662f\u4e00\u4e2astruct\u5e76\u4e14\u60f3\u5728 range
\u4e2d\u4fee\u6539\u5b83\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u503c\u6765\u8bbf\u95ee\u5e76\u4fee\u6539\u5b83\uff0c\u6216\u8005\u4f7f\u7528\u7ecf\u5178\u7684for\u5faa\u73af+\u7d22\u5f15\u503c\u7684\u5199\u6cd5\uff08\u9664\u975e\u904d\u5386\u7684\u5143\u7d20\u662f\u4e00\u4e2a\u6307\u9488\uff09\u3002
range
\u5faa\u73af\u4e2d\u8fed\u4ee3\u76ee\u6807\u503c\u7684\u8ba1\u7b97\u65b9\u5f0f (channels \u548c arrays) (#31)","text":"\u4f20\u9012\u7ed9 range
\u64cd\u4f5c\u7684\u8fed\u4ee3\u76ee\u6807\u5bf9\u5e94\u7684\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u53ea\u4f1a\u5728\u5faa\u73af\u6267\u884c\u524d\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u7406\u89e3\u8fd9\u4e2a\u6709\u52a9\u4e8e\u907f\u514d\u72af\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u4f8b\u5982\u4e0d\u9ad8\u6548\u7684channel\u8d4b\u503c\u64cd\u4f5c\u3001slice\u8fed\u4ee3\u64cd\u4f5c\u3002
range
\u5faa\u73af\u4e2d\u6307\u9488\u5143\u7d20\u7684\u5f71\u54cd range
loops (#32)","text":"\u8fd9\u91cc\u5176\u5b9e\u5f3a\u8c03\u7684\u662f range
\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\uff0c\u8fed\u4ee3\u53d8\u91cf\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u62f7\u8d1d\uff0c\u5047\u8bbe\u7ed9\u53e6\u5916\u4e00\u4e2a\u5bb9\u5668\u5143\u7d20\uff08\u6307\u9488\u7c7b\u578b\uff09\u8d4b\u503c\uff0c\u4e14\u9700\u8981\u5bf9\u8fed\u4ee3\u53d8\u91cf\u53d6\u5730\u5740\u8f6c\u6362\u6210\u6307\u9488\u518d\u8d4b\u503c\u7684\u8bdd\uff0c\u8fd9\u91cc\u6f5c\u85cf\u7740\u4e00\u4e2a\u9519\u8bef\uff0c\u5c31\u662ffor\u5faa\u73af\u8fed\u4ee3\u53d8\u91cf\u662f per-variable-per-loop \u800c\u4e0d\u662f per-variable-per-iteration\u3002\u5982\u679c\u662f\u901a\u8fc7\u5c40\u90e8\u53d8\u91cf\uff08\u7528\u8fed\u4ee3\u53d8\u91cf\u6765\u521d\u59cb\u5316\uff09\u6216\u8005\u4f7f\u7528\u7d22\u5f15\u503c\u6765\u76f4\u63a5\u5f15\u7528\u8fed\u4ee3\u7684\u5143\u7d20\uff0c\u5c06\u6709\u52a9\u4e8e\u907f\u514d\u62f7\u8d1d\u6307\u9488(\u8fed\u4ee3\u53d8\u91cf\u7684\u5730\u5740)\u4e4b\u7c7b\u7684bug\u3002
\u4f7f\u7528map\u65f6\uff0c\u4e3a\u4e86\u80fd\u5f97\u5230\u786e\u5b9a\u4e00\u81f4\u7684\u7ed3\u679c\uff0c\u5e94\u8be5\u8bb0\u4f4fGo\u4e2d\u7684map\u6570\u636e\u7ed3\u6784\uff1a * \u4e0d\u4f1a\u6309\u7167key\u5bf9data\u8fdb\u884c\u6392\u5e8f\uff0c\u904d\u5386\u65f6\u4e0d\u662f\u6309key\u6709\u5e8f\u7684\uff1b * \u904d\u5386\u65f6\u7684\u987a\u5e8f\uff0c\u4e5f\u4e0d\u662f\u6309\u7167\u63d2\u5165\u65f6\u7684\u987a\u5e8f\uff1b * \u6ca1\u6709\u4e00\u4e2a\u786e\u5b9a\u6027\u7684\u904d\u5386\u987a\u5e8f\uff0c\u6bcf\u6b21\u904d\u5386\u987a\u5e8f\u662f\u4e0d\u540c\u7684\uff1b * \u4e0d\u80fd\u4fdd\u8bc1\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u65b0\u63d2\u5165\u7684\u5143\u7d20\uff0c\u5728\u5f53\u524d\u8fed\u4ee3\u4e2d\u80fd\u591f\u88ab\u904d\u5386\u5230\uff1b
"},{"location":"zh/#break-34","title":"\u5ffd\u7565\u4e86break
\u8bed\u53e5\u662f\u5982\u4f55\u5de5\u4f5c\u7684 (#34)","text":"\u914d\u5408label\u4f7f\u7528 break
\u548c continue
\uff0c\u80fd\u591f\u8df3\u8fc7\u4e00\u4e2a\u7279\u5b9a\u7684\u8bed\u53e5\uff0c\u5728\u67d0\u4e9b\u5faa\u73af\u4e2d\u5b58\u5728 switch
\u548cselect
\u8bed\u53e5\u7684\u573a\u666f\u4e2d\u5c31\u6bd4\u8f83\u6709\u5e2e\u52a9\u3002
defer
(#35)","text":"\u5728\u5faa\u73af\u4e2d\u4f7f\u7528defer\u4e0d\u80fd\u5728\u6bcf\u8f6e\u8fed\u4ee3\u7ed3\u675f\u65f6\u6267\u884cdefer\u8bed\u53e5\uff0c\u4f46\u662f\u5c06\u5faa\u73af\u903b\u8f91\u63d0\u53d6\u5230\u51fd\u6570\u5185\u90e8\u4f1a\u5728\u6bcf\u6b21\u8fed\u4ee3\u7ed3\u675f\u65f6\u6267\u884c defer \u8bed\u53e5\u3002
"},{"location":"zh/#_4","title":"\u5b57\u7b26\u4e32","text":""},{"location":"zh/#rune-36","title":"\u6ca1\u6709\u7406\u89e3rune (#36)","text":"\u7406\u89e3rune\u7c7b\u578b\u5bf9\u5e94\u7684\u662f\u4e00\u4e2aunicode\u7801\u70b9\uff0c\u6bcf\u4e00\u4e2aunicode\u7801\u70b9\u5176\u5b9e\u662f\u4e00\u4e2a\u591a\u5b57\u8282\u7684\u5e8f\u5217\uff0c\u4e0d\u662f\u4e00\u4e2abyte\u3002\u8fd9\u5e94\u8be5\u662fGo\u5f00\u53d1\u8005\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\u4e4b\u4e00\uff0c\u7406\u89e3\u4e86\u8fd9\u4e2a\u6709\u52a9\u4e8e\u66f4\u51c6\u786e\u5730\u5904\u7406\u5b57\u7b26\u4e32\u3002
"},{"location":"zh/#37","title":"\u4e0d\u6b63\u786e\u7684\u5b57\u7b26\u4e32\u904d\u5386 (#37)","text":"\u4f7f\u7528 range
\u64cd\u4f5c\u7b26\u5bf9\u4e00\u4e2astring\u8fdb\u884c\u904d\u5386\u5b9e\u9645\u4e0a\u662f\u5bf9string\u5bf9\u5e94\u7684 []rune
\u8fdb\u884c\u904d\u5386\uff0c\u8fed\u4ee3\u53d8\u91cf\u4e2d\u7684\u7d22\u5f15\u503c\uff0c\u8868\u793a\u7684\u5f53\u524drune\u5bf9\u5e94\u7684 []byte
\u5728\u6574\u4e2a []byte(string)
\u4e2d\u7684\u8d77\u59cb\u7d22\u5f15\u3002\u5982\u679c\u8981\u8bbf\u95eestring\u4e2d\u7684\u67d0\u4e00\u4e2arune\uff08\u6bd4\u5982\u7b2c\u4e09\u4e2a\uff09\uff0c\u9996\u5148\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a []rune
\u7136\u540e\u518d\u6309\u7d22\u5f15\u503c\u8bbf\u95ee\u3002
strings.TrimRight
/strings.TrimLeft
\u79fb\u9664\u5728\u5b57\u7b26\u4e32\u5c3e\u90e8\u6216\u8005\u5f00\u5934\u51fa\u73b0\u7684\u4e00\u4e9brunes\uff0c\u51fd\u6570\u4f1a\u6307\u5b9a\u4e00\u4e2arune\u96c6\u5408\uff0c\u51fa\u73b0\u5728\u96c6\u5408\u4e2d\u7684rune\u5c06\u88ab\u4ece\u5b57\u7b26\u4e32\u79fb\u9664\u3002\u800c strings.TrimSuffix
/strings.TrimPrefix
\u662f\u79fb\u9664\u5b57\u7b26\u4e32\u7684\u4e00\u4e2a\u540e\u7f00/\u524d\u7f00\u3002
\u5bf9\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u8fdb\u884c\u904d\u5386\u62fc\u63a5\u64cd\u4f5c\uff0c\u5e94\u8be5\u901a\u8fc7 strings.Builder
\u6765\u5b8c\u6210\uff0c\u4ee5\u907f\u514d\u6bcf\u6b21\u8fed\u4ee3\u62fc\u63a5\u65f6\u90fd\u5206\u914d\u4e00\u4e2a\u65b0\u7684string\u5bf9\u8c61\u51fa\u6765\u3002
bytes
\u5305\u63d0\u4f9b\u4e86\u4e00\u4e9b\u548c strings
\u5305\u76f8\u4f3c\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d []byte/string \u4e4b\u95f4\u7684\u8f6c\u6362\u3002
\u4f7f\u7528\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff0c\u56e0\u4e3a\u5bf9\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684s[low:high]\u64cd\u4f5c\u8fd4\u56de\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5176\u4f7f\u7528\u4e86\u548c\u539f\u5b57\u7b26\u4e32s\u76f8\u540c\u7684\u5e95\u5c42\u6570\u7ec4\u3002
"},{"location":"zh/#_5","title":"\u51fd\u6570\u548c\u65b9\u6cd5","text":""},{"location":"zh/#42","title":"\u4e0d\u77e5\u9053\u4f7f\u7528\u54ea\u79cd\u63a5\u6536\u5668\u7c7b\u578b (#42)","text":"\u5bf9\u4e8e\u63a5\u6536\u5668\u7c7b\u578b\u662f\u91c7\u7528value\u7c7b\u578b\u8fd8\u662fpointer\u7c7b\u578b\uff0c\u5e94\u8be5\u53d6\u51b3\u4e8e\u4e0b\u9762\u8fd9\u51e0\u79cd\u56e0\u7d20\uff0c\u6bd4\u5982\uff1a\u65b9\u6cd5\u5185\u662f\u5426\u4f1a\u5bf9\u5b83\u8fdb\u884c\u4fee\u6539\uff0c\u5b83\u662f\u5426\u5305\u542b\u4e86\u4e00\u4e2a\u4e0d\u80fd\u88ab\u62f7\u8d1d\u7684\u5b57\u6bb5\uff0c\u4ee5\u53ca\u5b83\u8868\u793a\u7684\u5bf9\u8c61\u6709\u591a\u5927\u3002\u5982\u679c\u6709\u7591\u95ee\uff0c\u63a5\u6536\u5668\u53ef\u4ee5\u8003\u8651\u4f7f\u7528pointer\u7c7b\u578b\u3002
"},{"location":"zh/#43","title":"\u4ece\u4e0d\u4f7f\u7528\u547d\u540d\u7684\u8fd4\u56de\u503c (#43)","text":"\u4f7f\u7528\u547d\u540d\u7684\u8fd4\u56de\u503c\uff0c\u662f\u4e00\u79cd\u6709\u6548\u6539\u5584\u51fd\u6570\u3001\u65b9\u6cd5\u53ef\u8bfb\u6027\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u8fd4\u56de\u503c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u7c7b\u578b\u76f8\u540c\u7684\u53c2\u6570\u3002\u53e6\u5916\uff0c\u56e0\u4e3a\u8fd4\u56de\u503c\u5217\u8868\u4e2d\u7684\u53c2\u6570\u662f\u7ecf\u8fc7\u96f6\u503c\u521d\u59cb\u5316\u8fc7\u7684\uff0c\u67d0\u4e9b\u573a\u666f\u4e0b\u4e5f\u4f1a\u7b80\u5316\u51fd\u6570\u3001\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\u5b83\u7684\u4e00\u4e9b\u6f5c\u5728\u526f\u4f5c\u7528\u3002
"},{"location":"zh/#44","title":"\u4f7f\u7528\u547d\u540d\u7684\u8fd4\u56de\u503c\u65f6\u9884\u671f\u5916\u7684\u526f\u4f5c\u7528 (#44)","text":"\u89c1 #43.
\u4f7f\u7528\u547d\u540d\u7684\u8fd4\u56de\u503c\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u521d\u59cb\u5316\u4e86\u96f6\u503c\uff0c\u9700\u8981\u6ce8\u610f\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5f02\u5e38\u8fd4\u56de\u65f6\u662f\u5426\u9700\u8981\u7ed9\u5b83\u8d4b\u4e88\u4e00\u4e2a\u4e0d\u540c\u7684\u503c\uff0c\u6bd4\u5982\u8fd4\u56de\u503c\u5217\u8868\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6709\u540d\u53c2\u6570 err error
\uff0c\u9700\u8981\u6ce8\u610f return err
\u65f6\u662f\u5426\u6b63\u786e\u5730\u5bf9 err
\u8fdb\u884c\u4e86\u8d4b\u503c\u3002
\u5f53\u8fd4\u56de\u4e00\u4e2ainterface\u53c2\u6570\u65f6\uff0c\u9700\u8981\u5c0f\u5fc3\uff0c\u4e0d\u8981\u8fd4\u56de\u4e00\u4e2anil\u6307\u9488\uff0c\u800c\u662f\u5e94\u8be5\u663e\u793a\u8fd4\u56de\u4e00\u4e2anil\u503c\u3002\u5426\u5219\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u4e00\u4e9b\u9884\u671f\u5916\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u8c03\u7528\u65b9\u4f1a\u6536\u5230\u4e00\u4e2a\u975enil\u7684\u503c\u3002
"},{"location":"zh/#46","title":"\u4f7f\u7528\u6587\u4ef6\u540d\u4f5c\u4e3a\u51fd\u6570\u5165\u53c2 (#46)","text":"\u8bbe\u8ba1\u51fd\u6570\u65f6\u4f7f\u7528 io.Reader
\u7c7b\u578b\u4f5c\u4e3a\u5165\u53c2\uff0c\u800c\u4e0d\u662f\u6587\u4ef6\u540d\uff0c\u5c06\u6709\u52a9\u4e8e\u6539\u5584\u51fd\u6570\u7684\u53ef\u590d\u7528\u6027\u3001\u6613\u6d4b\u8bd5\u6027\u3002
defer
\u8bed\u53e5\u4e2d\u53c2\u6570\u3001\u63a5\u6536\u5668\u503c\u7684\u8ba1\u7b97\u65b9\u5f0f (\u53c2\u6570\u503c\u8ba1\u7b97, \u6307\u9488, \u548c value\u7c7b\u578b\u63a5\u6536\u5668) (#47)","text":"\u4e3a\u4e86\u907f\u514d defer
\u8bed\u53e5\u6267\u884c\u65f6\u5c31\u7acb\u5373\u8ba1\u7b97\u5bf9defer\u8981\u6267\u884c\u7684\u51fd\u6570\u7684\u53c2\u6570\u8fdb\u884c\u8ba1\u7b97\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u8981\u6267\u884c\u7684\u51fd\u6570\u653e\u5230\u95ed\u5305\u91cc\u9762\uff0c\u7136\u540e\u901a\u8fc7\u6307\u9488\u4f20\u9012\u53c2\u6570\u7ed9\u95ed\u5305\u5185\u51fd\u6570\uff08\u6216\u8005\u901a\u8fc7\u95ed\u5305\u6355\u83b7\u5916\u90e8\u53d8\u91cf\uff09\uff0c\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002
\u4f7f\u7528 panic
\u662fGo\u4e2d\u4e00\u79cd\u5904\u7406\u9519\u8bef\u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u53ea\u80fd\u5728\u9047\u5230\u4e0d\u53ef\u6062\u590d\u7684\u9519\u8bef\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a\u901a\u77e5\u5f00\u53d1\u4eba\u5458\u4e00\u4e2a\u5f3a\u4f9d\u8d56\u7684\u6a21\u5757\u52a0\u8f7d\u5931\u8d25\u4e86\u3002
Wrapping\uff08\u5305\u88c5\uff09\u9519\u8bef\u5141\u8bb8\u60a8\u6807\u8bb0\u9519\u8bef\u3001\u63d0\u4f9b\u989d\u5916\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u7136\u800c\uff0c\u5305\u88c5\u9519\u8bef\u4f1a\u521b\u5efa\u6f5c\u5728\u7684\u8026\u5408\uff0c\u56e0\u4e3a\u5b83\u4f7f\u5f97\u539f\u6765\u7684\u9519\u8bef\u5bf9\u8c03\u7528\u8005\u53ef\u89c1\u3002\u5982\u679c\u60a8\u60f3\u8981\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\uff0c\u8bf7\u4e0d\u8981\u4f7f\u7528\u5305\u88c5\u9519\u8bef\u7684\u65b9\u5f0f\u3002
"},{"location":"zh/#50","title":"\u4e0d\u6b63\u786e\u7684\u9519\u8bef\u7c7b\u578b\u6bd4\u8f83 (#50)","text":"\u5982\u679c\u4f60\u4f7f\u7528 Go 1.13 \u5f15\u5165\u7684\u7279\u6027 fmt.Errorf
+ %w
\u6765\u5305\u88c5\u4e00\u4e2a\u9519\u8bef\uff0c\u5f53\u8fdb\u884c\u9519\u8bef\u6bd4\u8f83\u65f6\uff0c\u5982\u679c\u60f3\u5224\u65ad\u8be5\u5305\u88c5\u540e\u7684\u9519\u8bef\u662f\u4e0d\u662f\u6307\u5b9a\u7684\u9519\u8bef\u7c7b\u578b\uff0c\u5c31\u9700\u8981\u4f7f\u7528 errors.As
\uff0c\u5982\u679c\u60f3\u5224\u65ad\u662f\u4e0d\u662f\u6307\u5b9a\u7684error\u5bf9\u8c61\u5c31\u9700\u8981\u7528 errors.Is
\u3002
\u89c1 #50.
\u4e3a\u4e86\u8868\u8fbe\u4e00\u4e2a\u9884\u671f\u5185\u7684\u9519\u8bef\uff0c\u8bf7\u4f7f\u7528\u9519\u8bef\u503c\u7684\u65b9\u5f0f\uff0c\u5e76\u901a\u8fc7 ==
\u6216\u8005 errors.Is
\u6765\u6bd4\u8f83\u3002\u800c\u5bf9\u4e8e\u610f\u5916\u9519\u8bef\uff0c\u5219\u5e94\u4f7f\u7528\u7279\u5b9a\u7684\u9519\u8bef\u7c7b\u578b\uff08\u53ef\u4ee5\u901a\u8fc7 errors.As
\u6765\u6bd4\u8f83\uff09\u3002
\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9519\u8bef\u4ec5\u9700\u8981\u5904\u7406\u4e00\u6b21\u3002\u6253\u5370\u9519\u8bef\u65e5\u5fd7\u4e5f\u662f\u4e00\u79cd\u9519\u8bef\u5904\u7406\u3002\u56e0\u6b64\uff0c\u5f53\u51fd\u6570\u5185\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u5e94\u8be5\u5728\u6253\u5370\u65e5\u5fd7\u548c\u8fd4\u56de\u9519\u8bef\u4e2d\u9009\u62e9\u5176\u4e2d\u4e00\u79cd\u3002\u5305\u88c5\u9519\u8bef\u4e5f\u53ef\u4ee5\u63d0\u4f9b\u95ee\u9898\u53d1\u751f\u7684\u989d\u5916\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u4e5f\u5305\u62ec\u4e86\u539f\u6765\u7684\u9519\u8bef\uff08\u53ef\u8003\u8651\u4ea4\u7ed9\u8c03\u7528\u65b9\u8d1f\u8d23\u6253\u65e5\u5fd7\uff09\u3002
"},{"location":"zh/#53","title":"\u4e0d\u5904\u7406\u9519\u8bef (#53)","text":"\u4e0d\u7ba1\u662f\u5728\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u8fd8\u662f\u5728\u4e00\u4e2a defer
\u51fd\u6570\u6267\u884c\u65f6\uff0c\u5982\u679c\u60f3\u8981\u5ffd\u7565\u4e00\u4e2a\u9519\u8bef\uff0c\u5e94\u8be5\u663e\u793a\u5730\u901a\u8fc7 _
\u6765\u5ffd\u7565\uff08\u53ef\u6ce8\u660e\u5ffd\u7565\u7684\u539f\u56e0\uff09\u3002\u5426\u5219\uff0c\u5c06\u6765\u7684\u8bfb\u8005\u5c31\u4f1a\u611f\u89c9\u5230\u56f0\u60d1\uff0c\u5ffd\u7565\u8fd9\u4e2a\u9519\u8bef\u662f\u6709\u610f\u4e3a\u4e4b\u8fd8\u662f\u65e0\u610f\u4e2d\u6f0f\u6389\u4e86\u3002
defer
\u4e2d\u7684\u9519\u8bef (#54)","text":"\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u5e94\u8be5\u5ffd\u7565 defer
\u51fd\u6570\u6267\u884c\u65f6\u8fd4\u56de\u7684\u9519\u8bef\uff0c\u6216\u8005\u663e\u793a\u5904\u7406\u5b83\uff0c\u6216\u8005\u5c06\u5b83\u4f20\u9012\u7ed9\u8c03\u7528\u65b9\u5904\u7406\uff0c\u53ef\u4ee5\u6839\u636e\u60c5\u666f\u8fdb\u884c\u9009\u62e9\u3002\u5982\u679c\u4f60\u786e\u5b9a\u8981\u5ffd\u7565\u8fd9\u4e2a\u9519\u8bef\uff0c\u8bf7\u663e\u793a\u4f7f\u7528 _
\u6765\u5ffd\u7565\u3002
\u7406\u89e3\u5e76\u53d1\uff08concurrency\uff09\u3001\u5e76\u884c\uff08parallelism\uff09\u4e4b\u95f4\u7684\u672c\u8d28\u533a\u522b\u662fGo\u5f00\u53d1\u4eba\u5458\u5fc5\u987b\u8981\u638c\u63e1\u7684\u3002\u5e76\u53d1\u662f\u5173\u4e8e\u7ed3\u6784\u8bbe\u8ba1\u4e0a\u7684\uff0c\u5e76\u884c\u662f\u5173\u4e8e\u5177\u4f53\u6267\u884c\u4e0a\u7684\u3002
"},{"location":"zh/#56","title":"\u8ba4\u4e3a\u5e76\u53d1\u603b\u662f\u66f4\u5feb (#56)","text":"\u8981\u6210\u4e3a\u4e00\u540d\u719f\u7ec3\u7684\u5f00\u53d1\u4eba\u5458\uff0c\u60a8\u5fc5\u987b\u610f\u8bc6\u5230\u5e76\u975e\u6240\u6709\u573a\u666f\u4e0b\u90fd\u662f\u5e76\u53d1\u7684\u65b9\u6848\u66f4\u5feb\u3002\u5bf9\u4e8e\u4efb\u52a1\u4e2d\u7684\u6700\u5c0f\u5de5\u4f5c\u8d1f\u8f7d\u90e8\u5206\uff0c\u5bf9\u5b83\u4eec\u8fdb\u884c\u5e76\u884c\u5316\u5904\u7406\u5e76\u4e0d\u4e00\u5b9a\u5c31\u6709\u660e\u663e\u6536\u76ca\u6216\u8005\u6bd4\u4e32\u884c\u5316\u65b9\u6848\u66f4\u5feb\u3002\u5bf9\u4e32\u884c\u5316\u3001\u5e76\u53d1\u65b9\u6848\u8fdb\u884cbenchmark\u6d4b\u8bd5\uff0c\u662f\u9a8c\u8bc1\u5047\u8bbe\u7684\u597d\u529e\u6cd5\u3002
"},{"location":"zh/#channelsmutexes-57","title":"\u4e0d\u6e05\u695a\u4f55\u65f6\u4f7f\u7528channels\u6216mutexes (#57)","text":"\u4e86\u89e3 goroutine \u4e4b\u95f4\u7684\u4ea4\u4e92\u4e5f\u53ef\u4ee5\u5728\u9009\u62e9\u4f7f\u7528channels\u6216mutexes\u65f6\u6709\u6240\u5e2e\u52a9\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5e76\u884c\u7684 goroutine \u9700\u8981\u540c\u6b65\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528mutexes\u3002\u76f8\u53cd\uff0c\u5e76\u53d1\u7684 goroutine \u901a\u5e38\u9700\u8981\u534f\u8c03\u548c\u7f16\u6392\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528channels\u3002
"},{"location":"zh/#vs-go-58","title":"\u4e0d\u660e\u767d\u7ade\u6001\u95ee\u9898 (\u6570\u636e\u7ade\u6001 vs. \u7ade\u6001\u6761\u4ef6 \u548c Go\u5185\u5b58\u6a21\u578b) (#58)","text":"\u638c\u63e1\u5e76\u53d1\u610f\u5473\u7740\u8981\u8ba4\u8bc6\u5230\u6570\u636e\u7ade\u4e89\uff08data races\uff09\u548c\u7ade\u6001\u6761\u4ef6\uff08race conditions\uff09\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u6982\u5ff5\u3002\u6570\u636e\u7ade\u4e89\uff0c\u6307\u7684\u662f\u6709\u591a\u4e2agoroutines\u540c\u65f6\u8bbf\u95ee\u76f8\u540c\u5185\u5b58\u533a\u57df\u65f6\uff0c\u6ca1\u6709\u5fc5\u8981\u7684\u540c\u6b65\u63a7\u5236\uff0c\u4e14\u5176\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2agoroutine\u662f\u6267\u884c\u7684\u5199\u64cd\u4f5c\u3002\u540c\u65f6\u8981\u8ba4\u8bc6\u5230\uff0c\u6ca1\u6709\u53d1\u751f\u6570\u636e\u7ade\u4e89\u4e0d\u4ee3\u8868\u7a0b\u5e8f\u7684\u6267\u884c\u662f\u786e\u5b9a\u6027\u7684\u3001\u6ca1\u95ee\u9898\u7684\u3002\u5f53\u5728\u67d0\u4e2a\u7279\u5b9a\u7684\u64cd\u4f5c\u987a\u5e8f\u6216\u8005\u7279\u5b9a\u7684\u4e8b\u4ef6\u53d1\u751f\u987a\u5e8f\u4e0b\uff0c\u5982\u679c\u6700\u7ec8\u7684\u884c\u4e3a\u662f\u4e0d\u53ef\u63a7\u7684\uff0c\u8fd9\u5c31\u662f\u7ade\u6001\u6761\u4ef6\u3002
ps\uff1a\u6570\u636e\u7ade\u4e89\u662f\u7ade\u6001\u6761\u4ef6\u7684\u5b50\u96c6\uff0c\u7ade\u6001\u6761\u4ef6\u4e0d\u4ec5\u5c40\u9650\u4e8e\u8bbf\u5b58\u672a\u540c\u6b65\uff0c\u5b83\u53ef\u4ee5\u53d1\u751f\u5728\u66f4\u9ad8\u7684\u5c42\u9762\u3002go test -race
\u68c0\u6d4b\u7684\u662f\u6570\u636e\u7ade\u4e89\uff0c\u9700\u8981\u540c\u6b65\u6765\u89e3\u51b3\uff0c\u800c\u5f00\u53d1\u8005\u8fd8\u9700\u8981\u5173\u6ce8\u9762\u66f4\u5e7f\u7684\u7ade\u6001\u6761\u4ef6\uff0c\u5b83\u9700\u8981\u5bf9\u591a\u4e2agoroutines\u7684\u6267\u884c\u8fdb\u884c\u7f16\u6392\u3002
\u7406\u89e3 Go \u7684\u5185\u5b58\u6a21\u578b\u4ee5\u53ca\u6709\u5173\u987a\u5e8f\u548c\u540c\u6b65\u7684\u5e95\u5c42\u4fdd\u8bc1\u662f\u9632\u6b62\u53ef\u80fd\u7684\u6570\u636e\u7ade\u4e89\u548c\u7ade\u6001\u6761\u4ef6\u7684\u5173\u952e\u3002
"},{"location":"zh/#59","title":"\u4e0d\u7406\u89e3\u4e0d\u540c\u5de5\u4f5c\u8d1f\u8f7d\u7c7b\u578b\u5bf9\u5e76\u53d1\u7684\u5f71\u54cd (#59)","text":"\u5f53\u521b\u5efa\u4e00\u5b9a\u6570\u91cf\u7684goroutines\u662f\uff0c\u9700\u8981\u8003\u8651\u5de5\u4f5c\u8d1f\u8f7d\u7684\u7c7b\u578b\u3002\u5982\u679c\u5de5\u4f5c\u8d1f\u8f7d\u662fCPU\u5bc6\u96c6\u578b\u7684\uff0c\u90a3\u4e48goroutines\u6570\u91cf\u5e94\u8be5\u63a5\u8fd1\u4e8e GOMAXPROCS
\u7684\u503c\uff08\u8be5\u503c\u53d6\u51b3\u4e8e\u4e3b\u673a\u5904\u7406\u5668\u6838\u5fc3\u6570\uff09\u3002\u5982\u679c\u5de5\u4f5c\u8d1f\u8f7d\u662fIO\u5bc6\u96c6\u578b\u7684\uff0cgoroutines\u6570\u91cf\u5c31\u9700\u8981\u8003\u8651\u591a\u79cd\u56e0\u7d20\uff0c\u6bd4\u5982\u5916\u90e8\u7cfb\u7edf\uff08\u8003\u8651\u8bf7\u6c42\u3001\u54cd\u5e94\u901f\u7387\uff09\u3002
Go \u7684\u4e0a\u4e0b\u6587\uff08context\uff09\u4e5f\u662f Go \u5e76\u53d1\u7f16\u7a0b\u7684\u57fa\u77f3\u4e4b\u4e00\u3002\u4e0a\u4e0b\u6587\u5141\u8bb8\u60a8\u643a\u5e26\u622a\u6b62\u65f6\u95f4\u3001\u53d6\u6d88\u4fe1\u53f7\u548c\u952e\u503c\u5217\u8868\u3002
"},{"location":"zh/#_8","title":"\u5e76\u53d1\u7f16\u7a0b: \u5b9e\u8df5","text":""},{"location":"zh/#context-61","title":"\u4f20\u9012\u4e0d\u5408\u9002\u7684context (#61)","text":"\u5f53\u6211\u4eec\u4f20\u9012\u4e86\u4e00\u4e2acontext\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u8fd9\u4e2acontext\u4ec0\u4e48\u65f6\u5019\u53ef\u4ee5\u88ab\u53d6\u6d88\uff0c\u8fd9\u70b9\u5f88\u91cd\u8981\uff0c\u4f8b\u5982\uff1a\u4e00\u4e2aHTTP\u8bf7\u6c42\u5904\u7406\u5668\u5728\u53d1\u9001\u5b8c\u54cd\u5e94\u540e\u53d6\u6d88context\u3002
ps: \u5b9e\u9645\u4e0acontext\u8868\u8fbe\u7684\u662f\u4e00\u4e2a\u52a8\u4f5c\u53ef\u4ee5\u6301\u7eed\u591a\u4e45\u4e4b\u540e\u88ab\u505c\u6b62\u3002
"},{"location":"zh/#goroutine-62","title":"\u542f\u52a8\u4e86\u4e00\u4e2agoroutine\u4f46\u662f\u4e0d\u77e5\u9053\u5b83\u4f55\u65f6\u4f1a\u505c\u6b62 (#62)","text":"\u907f\u514dgoroutine\u6cc4\u6f0f\uff0c\u8981\u6709\u8fd9\u79cd\u610f\u8bc6\uff0c\u5f53\u521b\u5efa\u5e76\u542f\u52a8\u4e00\u4e2agoroutine\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u6709\u5bf9\u5e94\u7684\u8bbe\u8ba1\u8ba9\u5b83\u80fd\u6b63\u5e38\u9000\u51fa\u3002
"},{"location":"zh/#goroutines-63","title":"\u4e0d\u6ce8\u610f\u5904\u7406 goroutines \u548c \u5faa\u73af\u4e2d\u7684\u8fed\u4ee3\u53d8\u91cf (#63)","text":"\u4e3a\u4e86\u907f\u514dgoroutines\u548c\u5faa\u73af\u4e2d\u7684\u8fed\u4ee3\u53d8\u91cf\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u521b\u5efa\u5c40\u90e8\u53d8\u91cf\u5e76\u5c06\u8fed\u4ee3\u53d8\u91cf\u8d4b\u503c\u7ed9\u5c40\u90e8\u53d8\u91cf\uff0c\u6216\u8005goroutine\u8c03\u7528\u5e26\u53c2\u6570\u7684\u51fd\u6570\uff0c\u5c06\u8fed\u4ee3\u53d8\u91cf\u503c\u4f5c\u4e3a\u53c2\u6570\u503c\u4f20\u5165\uff0c\u6765\u4ee3\u66ffgoroutine\u8c03\u7528\u95ed\u5305\u3002
"},{"location":"zh/#select-channels-64","title":"\u4f7f\u7528select + channels \u65f6\u8bef\u4ee5\u4e3a\u5206\u652f\u9009\u62e9\u987a\u5e8f\u662f\u786e\u5b9a\u7684 (#64)","text":"\u8981\u660e\u767d\uff0cselect
\u591a\u4e2achannels\u65f6\uff0c\u5982\u679c\u591a\u4e2achannels\u4e0a\u7684\u64cd\u4f5c\u90fd\u5c31\u7eea\uff0c\u90a3\u4e48\u4f1a\u968f\u673a\u9009\u62e9\u4e00\u4e2a case
\u5206\u652f\u6765\u6267\u884c\uff0c\u56e0\u6b64\u8981\u907f\u514d\u6709\u5206\u652f\u9009\u62e9\u987a\u5e8f\u662f\u4ece\u4e0a\u5230\u4e0b\u7684\u8fd9\u79cd\u9519\u8bef\u9884\u8bbe\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bbe\u8ba1\u4e0a\u7684bug\u3002
\u53d1\u9001\u901a\u77e5\u65f6\u4f7f\u7528 chan struct{}
\u7c7b\u578b\u3002
ps: \u5148\u660e\u767d\u4ec0\u4e48\u662f\u901a\u77e5channels\uff0c\u4e00\u4e2a\u901a\u77e5channels\u6307\u7684\u662f\u53ea\u662f\u7528\u6765\u505a\u901a\u77e5\uff0c\u800c\u5176\u4e2d\u4f20\u9012\u7684\u6570\u636e\u6ca1\u6709\u610f\u4e49\uff0c\u6216\u8005\u7406\u89e3\u6210\u4e0d\u4f20\u9012\u6570\u636e\u7684channels\uff0c\u8fd9\u79cd\u79f0\u4e3a\u901a\u77e5channels\u3002\u5176\u4e2d\u4f20\u9012\u7684\u6570\u636e\u7684\u7c7b\u578bstruct{}\u66f4\u5408\u9002\u3002
"},{"location":"zh/#nil-channels-66","title":"\u4e0d\u4f7f\u7528 nil channels (#66)","text":"\u4f7f\u7528 nil channels \u5e94\u8be5\u662f\u5e76\u53d1\u5904\u7406\u65b9\u5f0f\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982\uff0c\u5b83\u80fd\u591f\u5e2e\u52a9\u7981\u7528 select
\u8bed\u53e5\u4e2d\u7684\u7279\u5b9a\u7684\u5206\u652f\u3002
\u6839\u636e\u6307\u5b9a\u7684\u573a\u666f\u4ed4\u7ec6\u8bc4\u4f30\u5e94\u8be5\u4f7f\u7528\u54ea\u4e00\u79cd channel \u7c7b\u578b\uff08\u5e26\u7f13\u51b2\u7684\uff0c\u4e0d\u5e26\u7f13\u51b2\u7684\uff09\u3002\u53ea\u6709\u4e0d\u5e26\u7f13\u51b2\u7684 channels \u53ef\u4ee5\u63d0\u4f9b\u5f3a\u540c\u6b65\u4fdd\u8bc1\u3002
\u4f7f\u7528\u5e26\u7f13\u51b2\u7684 channels \u65f6\u5982\u679c\u4e0d\u786e\u5b9a size \u8be5\u5982\u4f55\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u5148\u8bbe\u4e3a1\uff0c\u5982\u679c\u6709\u5408\u7406\u7684\u7406\u7531\u518d\u53bb\u6307\u5b9a channels size\u3002
ps: \u6839\u636edisruptor\u8fd9\u4e2a\u9ad8\u6027\u80fd\u5185\u5b58\u6d88\u606f\u961f\u5217\u7684\u5b9e\u8df5\uff0c\u5728\u67d0\u79cd\u8bfb\u5199pacing\u4e0b\uff0c\u961f\u5217\u8981\u4e48\u6ee1\u8981\u4e48\u7a7a\uff0c\u4e0d\u5927\u53ef\u80fd\u5904\u4e8e\u67d0\u79cd\u4ecb\u4e8e\u4e2d\u95f4\u7684\u7a33\u6001\u3002
"},{"location":"zh/#etcd-68","title":"\u5fd8\u8bb0\u4e86\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u53ef\u80fd\u5e26\u6765\u7684\u526f\u4f5c\u7528\uff08\u4f8b\u5982 etcd \u6570\u636e\u7ade\u4e89\u548c\u6b7b\u9501\uff09(#68)","text":"\u610f\u8bc6\u5230\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8c03\u7528\u73b0\u6709\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u9700\u8981\u6ce8\u610f\u53ef\u80fd\u7684\u6b7b\u9501\u548c\u5176\u4ed6\u6570\u636e\u7ade\u4e89\u95ee\u9898\u3002
ps: \u6838\u5fc3\u662f\u8981\u5173\u6ce8 fmt.Sprintf
+ %v
\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65f6 %v
\u5177\u4f53\u5230\u4e0d\u540c\u7684\u7c7b\u578b\u503c\u65f6\uff0c\u5b9e\u9645\u4e0a\u6267\u884c\u7684\u64cd\u4f5c\u662f\u4ec0\u4e48\u3002\u6bd4\u5982 %v
\u8fd9\u4e2aplaceholder\u5bf9\u5e94\u7684\u503c\u65f6\u4e00\u4e2a context.Context
\uff0c\u90a3\u4e48\u4f1a\u5c31\u904d\u5386\u5176\u901a\u8fc7 context.WithValue
\u9644\u52a0\u5728\u5176\u4e2d\u7684 values\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u6d89\u53ca\u5230\u6570\u636e\u7ade\u4e89\u95ee\u9898\u3002\u4e66\u4e2d\u63d0\u53ca\u7684\u53e6\u4e00\u4e2a\u5bfc\u81f4\u6b7b\u9501\u7684\u6848\u4f8b\u672c\u8d28\u4e0a\u4e5f\u662f\u4e00\u6837\u7684\u95ee\u9898\uff0c\u53ea\u4e0d\u8fc7\u53c8\u989d\u5916\u7275\u626f\u5230\u4e86 sync.RWMutex
\u4e0d\u53ef\u91cd\u5165\u7684\u95ee\u9898\u3002
\u8c03\u7528 append
\u4e0d\u603b\u662f\u6ca1\u6709\u6570\u636e\u7ade\u4e89\u7684\uff0c\u56e0\u6b64\u4e0d\u8981\u5728\u4e00\u4e2a\u5171\u4eab\u7684 slice
\u4e0a\u5e76\u53d1\u5730\u6267\u884c append
\u3002
\u8bf7\u8bb0\u4f4f slices \u548c maps \u5f15\u7528\u7c7b\u578b\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u5e38\u89c1\u7684\u6570\u636e\u7ade\u4e89\u95ee\u9898\u3002
ps: \u8fd9\u91cc\u5b9e\u9645\u662f\u56e0\u4e3a\u9519\u8bef\u7406\u89e3\u4e86 slices \u548c maps\uff0c\u5bfc\u81f4\u5199\u51fa\u4e86\u9519\u8bef\u7684\u62f7\u8d1d slices \u548c maps \u7684\u4ee3\u7801\uff0c\u8fdb\u800c\u5bfc\u81f4\u9501\u4fdd\u62a4\u65e0\u6548\u3001\u51fa\u73b0\u6570\u636e\u7ade\u4e89\u95ee\u9898\u3002
"},{"location":"zh/#syncwaitgroup-71","title":"\u8bef\u7528sync.WaitGroup
(#71)","text":"\u6b63\u786e\u5730\u4f7f\u7528 sync.WaitGroup
\u9700\u8981\u5728\u542f\u52a8 goroutines \u4e4b\u524d\u5148\u8c03\u7528 Add
\u65b9\u6cd5\u3002
sync.Cond
(#72)","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 sync.Cond
\u5411\u591a\u4e2a goroutines \u53d1\u9001\u91cd\u590d\u7684\u901a\u77e5\u3002
errgroup
(#73)","text":"\u4f60\u53ef\u4ee5\u4f7f\u7528 errgroup
\u5305\u6765\u540c\u6b65\u4e00\u7ec4 goroutines \u5e76\u5904\u7406\u9519\u8bef\u548c\u4e0a\u4e0b\u6587\u3002
sync
\u4e0b\u7684\u7c7b\u578b (#74)","text":"sync
\u5305\u4e0b\u7684\u7c7b\u578b\u4e0d\u5e94\u8be5\u88ab\u62f7\u8d1d\u3002
\u6ce8\u610f\u6709\u4e9b\u51fd\u6570\u63a5\u6536\u4e00\u4e2a time.Duration
\u7c7b\u578b\u7684\u53c2\u6570\u65f6\uff0c\u5c3d\u7ba1\u76f4\u63a5\u4f20\u9012\u4e00\u4e2a\u6574\u6570\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u6700\u597d\u8fd8\u662f\u4f7f\u7528 time API \u4e2d\u7684\u65b9\u6cd5\u6765\u4f20\u9012 duration\uff0c\u4ee5\u907f\u514d\u53ef\u80fd\u9020\u6210\u7684\u56f0\u60d1\u3001bug\u3002
ps: \u91cd\u70b9\u662f\u6ce8\u610f time.Duration \u5b9a\u4e49\u7684\u662f nanoseconds \u6570\u3002
"},{"location":"zh/#timeafter-76","title":"time.After
\u548c\u5185\u5b58\u6cc4\u6f0f (#76)","text":"\u907f\u514d\u5728\u91cd\u590d\u6267\u884c\u5f88\u591a\u6b21\u7684\u51fd\u6570 \uff08\u5982\u5faa\u73af\u4e2d\u6216HTTP\u5904\u7406\u51fd\u6570\uff09\u4e2d\u8c03\u7528 time.After
\uff0c\u8fd9\u53ef\u4ee5\u907f\u514d\u5185\u5b58\u5cf0\u503c\u6d88\u8017\u3002\u7531 time.After
\u521b\u5efa\u7684\u8d44\u6e90\u4ec5\u5728\u8ba1\u65f6\u5668\u8d85\u65f6\u624d\u4f1a\u88ab\u91ca\u653e\u3002
\u8981\u5f53\u5fc3\u5728Go\u7ed3\u6784\u4f53\u4e2d\u5d4c\u5165\u5b57\u6bb5\uff0c\u8fd9\u6837\u505a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bf8\u5982\u5d4c\u5165\u7684 time.Time
\u5b57\u6bb5\u5b9e\u73b0 json.Marshaler
\u63a5\u53e3\uff0c\u4ece\u800c\u8986\u76d6\u9ed8\u8ba4\u7684json\u5e8f\u5217\u3002
\u5f53\u5bf9\u4e24\u4e2a time.Time
\u7c7b\u578b\u503c\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u9700\u8981\u8bb0\u4f4f time.Time
\u5305\u542b\u4e86\u4e00\u4e2a\u5899\u4e0a\u65f6\u949f\uff08wall clock\uff09\u548c\u4e00\u4e2a\u5355\u8c03\u65f6\u949f \uff08monotonic clock\uff09\uff0c\u800c\u4f7f\u7528 ==
\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\u65f6\u4f1a\u540c\u65f6\u6bd4\u8f83\u8fd9\u4e24\u4e2a\u3002
any
\u5f53\u63d0\u4f9b\u4e00\u4e2amap\u7528\u6765unmarshal JSON\u6570\u636e\u65f6\uff0c\u4e3a\u4e86\u907f\u514d\u4e0d\u786e\u5b9a\u7684value\u7ed3\u6784\u6211\u4eec\u4f1a\u4f7f\u7528 any
\u6765\u4f5c\u4e3avalue\u7684\u7c7b\u578b\u800c\u4e0d\u662f\u5b9a\u4e49\u4e00\u4e2astruct\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u9700\u8981\u8bb0\u5f97\u6570\u503c\u9ed8\u8ba4\u4f1a\u88ab\u8f6c\u6362\u4e3a float64
\u3002
sql.Open
\u5e76\u6ca1\u6709\u4e0edb\u670d\u52a1\u5668\u5efa\u7acb\u5b9e\u9645\u8fde\u63a5\u9700\u8981\u8c03\u7528 Ping
\u6216\u8005 PingContext
\u65b9\u6cd5\u6765\u6d4b\u8bd5\u914d\u7f6e\u5e76\u786e\u4fdd\u6570\u636e\u5e93\u662f\u53ef\u8fbe\u7684\u3002
\u4f5c\u4e3a\u751f\u4ea7\u7ea7\u522b\u7684\u5e94\u7528\uff0c\u8bbf\u95ee\u6570\u636e\u5e93\u65f6\u5e94\u8be5\u5173\u6ce8\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u53c2\u6570\u3002
\u4f7f\u7528 SQL prepared \u8bed\u53e5\u80fd\u591f\u8ba9\u67e5\u8be2\u66f4\u52a0\u9ad8\u6548\u548c\u5b89\u5168\u3002
\u4f7f\u7528 sql.NullXXX
\u7c7b\u578b\u5904\u7406\u8868\u4e2d\u7684\u53ef\u7a7a\u5217\u3002
\u8c03\u7528 sql.Rows
\u7684 Err
\u65b9\u6cd5\u6765\u786e\u4fdd\u5728\u51c6\u5907\u4e0b\u4e00\u4e2a\u884c\u65f6\u6ca1\u6709\u9057\u6f0f\u9519\u8bef\u3002
sql.Rows
\u548c os.File
) (#79)","text":"\u6700\u7ec8\u8981\u6ce8\u610f\u5173\u95ed\u6240\u6709\u5b9e\u73b0 io.Closer
\u63a5\u53e3\u7684\u7ed3\u6784\u4f53,\u4ee5\u907f\u514d\u53ef\u80fd\u7684\u6cc4\u6f0f\u3002
\u4e3a\u4e86\u907f\u514d\u5728HTTP\u5904\u7406\u51fd\u6570\u4e2d\u51fa\u73b0\u67d0\u4e9b\u610f\u5916\u7684\u95ee\u9898\uff0c\u5982\u679c\u60f3\u5728\u53d1\u751f http.Error
\u540e\u8ba9HTTP\u5904\u7406\u51fd\u6570\u505c\u6b62\uff0c\u90a3\u4e48\u5c31\u4e0d\u8981\u5fd8\u8bb0\u4f7f\u7528return
\u8bed\u53e5\u6765\u963b\u6b62\u540e\u7eed\u4ee3\u7801\u7684\u6267\u884c\u3002
\u5bf9\u4e8e\u751f\u4ea7\u7ea7\u522b\u7684\u5e94\u7528\uff0c\u4e0d\u8981\u4f7f\u7528\u9ed8\u8ba4\u7684HTTP client\u548cserver\u5b9e\u73b0\u3002\u8fd9\u4e9b\u5b9e\u73b0\u7f3a\u5c11\u8d85\u65f6\u548c\u751f\u4ea7\u73af\u5883\u4e2d\u5e94\u8be5\u5f3a\u5236\u4f7f\u7528\u7684\u884c\u4e3a\u3002
"},{"location":"zh/#_10","title":"\u6d4b\u8bd5","text":""},{"location":"zh/#build-tags-82","title":"\u4e0d\u5bf9\u6d4b\u8bd5\u8fdb\u884c\u5206\u7c7b \uff08build tags, \u73af\u5883\u53d8\u91cf\uff0c\u77ed\u6a21\u5f0f\uff09(#82)","text":"\u5bf9\u6d4b\u8bd5\u8fdb\u884c\u5fc5\u8981\u7684\u5206\u7c7b\uff0c\u53ef\u4ee5\u501f\u52a9 build tags\u3001\u73af\u5883\u53d8\u91cf\u4ee5\u53ca\u77ed\u6a21\u5f0f\uff0c\u6765\u4f7f\u5f97\u6d4b\u8bd5\u8fc7\u7a0b\u66f4\u52a0\u9ad8\u6548\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 build tags \u6216\u73af\u5883\u53d8\u91cf\u6765\u521b\u5efa\u6d4b\u8bd5\u7c7b\u522b\uff08\u4f8b\u5982\u5355\u5143\u6d4b\u8bd5\u4e0e\u96c6\u6210\u6d4b\u8bd5\uff09\uff0c\u5e76\u533a\u5206\u77ed\u6d4b\u8bd5\u4e0e\u957f\u65f6\u95f4\u6d4b\u8bd5\uff0c\u6765\u51b3\u5b9a\u6267\u884c\u54ea\u79cd\u7c7b\u578b\u7684\u3002
ps: \u4e86\u89e3\u4e0bgo build tags\uff0c\u4ee5\u53ca go test -short
\u3002
\u6253\u5f00 -race
\u5f00\u5173\u5728\u7f16\u5199\u5e76\u53d1\u5e94\u7528\u65f6\u975e\u5e38\u91cd\u8981\u3002\u8fd9\u80fd\u5e2e\u52a9\u4f60\u6355\u83b7\u53ef\u80fd\u7684\u6570\u636e\u7ade\u4e89,\u4ece\u800c\u907f\u514d\u8f6f\u4ef6bug\u3002
\u6253\u5f00\u5f00\u5173 -parallel
\u6709\u52a9\u4e8e\u52a0\u901f\u6d4b\u8bd5\u7684\u6267\u884c\uff0c\u7279\u522b\u662f\u6d4b\u8bd5\u4e2d\u5305\u542b\u4e00\u4e9b\u9700\u8981\u957f\u671f\u8fd0\u884c\u7684\u7528\u4f8b\u7684\u65f6\u5019\u3002
\u6253\u5f00\u5f00\u5173 -shuffle
\u80fd\u591f\u6253\u4e71\u6d4b\u8bd5\u7528\u4f8b\u6267\u884c\u7684\u987a\u5e8f\uff0c\u907f\u514d\u4e00\u4e2a\u6d4b\u8bd5\u4f9d\u8d56\u4e8e\u67d0\u4e9b\u4e0d\u7b26\u5408\u771f\u5b9e\u60c5\u51b5\u7684\u9884\u8bbe\uff0c\u6709\u52a9\u4e8e\u53ca\u65e9\u66b4\u6f0fbug\u3002
\u8868\u9a71\u52a8\u7684\u6d4b\u8bd5\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u5f0f,\u53ef\u4ee5\u5c06\u4e00\u7ec4\u76f8\u4f3c\u7684\u6d4b\u8bd5\u5206\u7ec4\u5728\u4e00\u8d77,\u4ee5\u907f\u514d\u4ee3\u7801\u91cd\u590d\u548c\u4f7f\u672a\u6765\u7684\u66f4\u65b0\u66f4\u5bb9\u6613\u5904\u7406\u3002
"},{"location":"zh/#sleep-86","title":"\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u6267\u884csleep\u64cd\u4f5c (#86)","text":"\u4f7f\u7528\u540c\u6b65\u7684\u65b9\u5f0f\u3001\u907f\u514dsleep\uff0c\u6765\u5c3d\u91cf\u51cf\u5c11\u6d4b\u8bd5\u7684\u4e0d\u7a33\u5b9a\u6027\u548c\u63d0\u9ad8\u9c81\u68d2\u6027\u3002\u5982\u679c\u65e0\u6cd5\u4f7f\u7528\u540c\u6b65\u624b\u6bb5,\u53ef\u4ee5\u8003\u8651\u91cd\u8bd5\u7684\u65b9\u5f0f\u3002
"},{"location":"zh/#time-api-87","title":"\u6ca1\u6709\u9ad8\u6548\u5730\u5904\u7406 time API (#87)","text":"\u7406\u89e3\u5982\u4f55\u5904\u7406\u4f7f\u7528 time API \u7684\u51fd\u6570\uff0c\u662f\u4f7f\u6d4b\u8bd5\u66f4\u52a0\u7a33\u5b9a\u7684\u53e6\u4e00\u79cd\u65b9\u5f0f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u6280\u672f\uff0c\u4f8b\u5982\u5c06\u65f6\u95f4\u4f5c\u4e3a\u9690\u85cf\u4f9d\u8d56\u9879\u7684\u4e00\u90e8\u5206\u6765\u5904\u7406\uff0c\u6216\u8005\u8981\u6c42\u5ba2\u6237\u7aef\u63d0\u4f9b\u65f6\u95f4\u3002
"},{"location":"zh/#httptest-iotest-88","title":"\u4e0d\u4f7f\u7528\u6d4b\u8bd5\u76f8\u5173\u7684\u5de5\u5177\u5305 (httptest
\u548c iotest
) (#88)","text":"\u8fd9\u4e2a httptest
\u5305\u5bf9\u5904\u7406HTTP\u5e94\u7528\u7a0b\u5e8f\u5f88\u6709\u5e2e\u52a9\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5b9e\u7528\u7a0b\u5e8f\u6765\u6d4b\u8bd5\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u3002
\u8fd9\u4e2a iotest
\u5305\u6709\u52a9\u4e8e\u7f16\u5199 io.Reader \u5e76\u6d4b\u8bd5\u5e94\u7528\u7a0b\u5e8f\u662f\u5426\u80fd\u591f\u5bb9\u5fcd\u9519\u8bef\u3002
\u4f7f\u7528 time \u65b9\u6cd5\u6765\u4fdd\u6301\u57fa\u51c6\u6d4b\u8bd5\u7684\u51c6\u786e\u6027\u3002
\u589e\u52a0 benchtime
\u6216\u8005\u4f7f\u7528 benchstat
\u7b49\u5de5\u5177\u53ef\u4ee5\u6709\u52a9\u4e8e\u5fae\u57fa\u51c6\u6d4b\u8bd5\u3002
\u5c0f\u5fc3\u5fae\u57fa\u51c6\u6d4b\u8bd5\u7684\u7ed3\u679c,\u5982\u679c\u6700\u7ec8\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u7684\u7cfb\u7edf\u4e0e\u8fd0\u884c\u5fae\u57fa\u51c6\u6d4b\u8bd5\u7684\u7cfb\u7edf\u4e0d\u540c\u3002
\u786e\u4fdd\u6d4b\u8bd5\u51fd\u6570\u662f\u5426\u4f1a\u4ea7\u751f\u4e00\u4e9b\u526f\u4f5c\u7528\uff0c\u9632\u6b62\u7f16\u8bd1\u5668\u4f18\u5316\u6b3a\u9a97\u4f60\u5f97\u5230\u7684\u57fa\u51c6\u6d4b\u8bd5\u7ed3\u679c\u3002
\u4e3a\u4e86\u907f\u514d\u88ab\u89c2\u5bdf\u8005\u6548\u5e94\u6b3a\u9a97,\u5f3a\u5236\u91cd\u65b0\u521b\u5efaCPU\u5bc6\u96c6\u578b\u51fd\u6570\u4f7f\u7528\u7684\u6570\u636e\u3002
"},{"location":"zh/#go-test-90","title":"\u6ca1\u6709\u53bb\u63a2\u7d22go test\u6240\u6709\u7684\u7279\u6027 (#90)","text":"\u4f7f\u7528 -coverprofile
\u53c2\u6570\u53ef\u4ee5\u5feb\u901f\u67e5\u770b\u4ee3\u7801\u7684\u6d4b\u8bd5\u8986\u76d6\u60c5\u51b5\uff0c\u65b9\u4fbf\u5feb\u901f\u67e5\u770b\u54ea\u4e2a\u90e8\u5206\u9700\u8981\u66f4\u591a\u7684\u5173\u6ce8\u3002
\u5355\u5143\u6d4b\u8bd5\u7ec4\u7ec7\u5230\u4e00\u4e2a\u72ec\u7acb\u7684\u5305\u4e2d\uff0c\u5bf9\u4e8e\u5bf9\u5916\u5c42\u66b4\u6f0f\u7684\u63a5\u53e3\uff0c\u9700\u8981\u5199\u4e00\u4e9b\u6d4b\u8bd5\u7528\u4f8b\u3002\u6d4b\u8bd5\u5e94\u8be5\u5173\u6ce8\u516c\u5f00\u7684\u884c\u4e3a\uff0c\u800c\u975e\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\u3002
\u5904\u7406\u9519\u8bef\u65f6,\u4f7f\u7528 *testing.T
\u53d8\u91cf\u800c\u4e0d\u662f\u7ecf\u5178\u7684 if err != nil
\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3002
\u4f60\u53ef\u4ee5\u4f7f\u7528setup\u548cteardown\u51fd\u6570\u6765\u914d\u7f6e\u4e00\u4e2a\u590d\u6742\u7684\u73af\u5883\uff0c\u6bd4\u5982\u5728\u96c6\u6210\u6d4b\u8bd5\u7684\u60c5\u51b5\u4e0b\u3002
"},{"location":"zh/#_11","title":"\u4e0d\u4f7f\u7528\u6a21\u7cca\u6d4b\u8bd5 (\u793e\u533a\u53cd\u9988\u9519\u8bef)","text":"\u6a21\u7cca\u6d4b\u8bd5\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u7b56\u7565\uff0c\u4f7f\u7528\u5b83\u80fd\u68c0\u6d4b\u51fa\u968f\u673a\u3001\u610f\u6599\u5916\u7684\u548c\u4e00\u4e9b\u6076\u610f\u7684\u6570\u636e\u8f93\u5165\uff0c\u6765\u5b8c\u6210\u4e00\u4e9b\u590d\u6742\u7684\u64cd\u4f5c\u3002
\u89c1: @jeromedoucet
"},{"location":"zh/#_12","title":"\u4f18\u5316\u6280\u672f","text":""},{"location":"zh/#cpu-cache-91","title":"\u4e0d\u7406\u89e3CPU cache (#91)","text":"\u7406\u89e3CPU\u7f13\u5b58\u7684\u4f7f\u7528\u5bf9\u4e8e\u4f18\u5316CPU\u5bc6\u96c6\u578b\u5e94\u7528\u5f88\u91cd\u8981\uff0c\u56e0\u4e3aL1\u7f13\u5b58\u6bd4\u4e3b\u5b58\u5feb50\u5230100\u500d\u3002
\u610f\u8bc6\u5230 cache line \u6982\u5ff5\u5bf9\u4e8e\u7406\u89e3\u5982\u4f55\u5728\u6570\u636e\u5bc6\u96c6\u578b\u5e94\u7528\u4e2d\u7ec4\u7ec7\u6570\u636e\u975e\u5e38\u5173\u952e\u3002CPU \u5e76\u4e0d\u662f\u4e00\u4e2a\u4e00\u4e2a\u5b57\u6765\u83b7\u53d6\u5185\u5b58\u3002\u76f8\u53cd\uff0c\u5b83\u901a\u5e38\u590d\u5236\u4e00\u4e2a 64\u5b57\u8282\u957f\u5ea6\u7684 cache line\u3002\u4e3a\u4e86\u83b7\u5f97\u6bcf\u4e2a cache line \u7684\u6700\u5927\u6548\u7528\uff0c\u9700\u8981\u5b9e\u65bd\u7a7a\u95f4\u5c40\u90e8\u6027\u3002
\u4e00\u7cfb\u5217struct\u5143\u7d20\u6784\u6210\u7684slice vs. \u591a\u4e2aslice\u5b57\u6bb5\u6784\u6210\u7684struct
\u6982\u7387\u6027\u7684\u95ee\u9898
\u63d0\u9ad8CPU\u6267\u884c\u4ee3\u7801\u65f6\u7684\u53ef\u9884\u6d4b\u6027\uff0c\u4e5f\u662f\u4f18\u5316\u67d0\u4e9b\u51fd\u6570\u7684\u4e00\u4e2a\u6709\u6548\u65b9\u6cd5\u3002\u6bd4\u5982\uff0c\u56fa\u5b9a\u6b65\u957f\u6216\u8fde\u7eed\u8bbf\u95ee\u5bf9CPU\u6765\u8bf4\u662f\u53ef\u9884\u6d4b\u7684\uff0c\u4f46\u975e\u8fde\u7eed\u8bbf\u95ee\uff08\u4f8b\u5982\u94fe\u8868\uff09\u5c31\u662f\u4e0d\u53ef\u9884\u6d4b\u7684\u3002
\u8981\u6ce8\u610f\u73b0\u4ee3\u7f13\u5b58\u662f\u5206\u533a\u7684\uff08set associative placement\uff0c\u7ec4\u76f8\u8fde\u6620\u5c04\uff09\uff0c\u8981\u6ce8\u610f\u907f\u514d\u4f7f\u7528 critical stride
\uff0c\u8fd9\u79cd\u6b65\u957f\u60c5\u51b5\u4e0b\u53ea\u80fd\u5229\u7528 cache \u7684\u4e00\u5c0f\u90e8\u5206\u3002
critical stride\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u6b65\u957f\uff0c\u6307\u7684\u662f\u5185\u5b58\u8bbf\u95ee\u7684\u6b65\u957f\u521a\u597d\u7b49\u4e8e cache \u5927\u5c0f\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u5c11\u90e8\u5206 cacheline \u88ab\u5229\u7528\u3002
"},{"location":"zh/#false-sharing-92","title":"\u5199\u7684\u5e76\u53d1\u5904\u7406\u903b\u8f91\u4f1a\u5bfc\u81f4false sharing (#92)","text":"\u4e86\u89e3 CPU \u7f13\u5b58\u7684\u8f83\u4f4e\u5c42\u7684 L1\u3001L2 cache \u4e0d\u4f1a\u5728\u6240\u6709\u6838\u95f4\u5171\u4eab\uff0c\u7f16\u5199\u5e76\u53d1\u5904\u7406\u903b\u8f91\u65f6\u80fd\u907f\u514d\u5199\u51fa\u4e00\u4e9b\u964d\u4f4e\u6027\u80fd\u7684\u95ee\u9898\uff0c\u6bd4\u5982\u4f2a\u5171\u4eab\uff08false sharing\uff09\u3002\u5185\u5b58\u5171\u4eab\u53ea\u662f\u4e00\u79cd\u5047\u8c61\u3002
"},{"location":"zh/#93","title":"\u6ca1\u6709\u8003\u8651\u6307\u4ee4\u7ea7\u7684\u5e76\u884c (#93)","text":"\u4f7f\u7528\u6307\u4ee4\u7ea7\u5e76\u884c\uff08ILP\uff09\u4f18\u5316\u4ee3\u7801\u7684\u7279\u5b9a\u90e8\u5206\uff0c\u4ee5\u5141\u8bb8CPU\u5c3d\u53ef\u80fd\u6267\u884c\u66f4\u591a\u53ef\u4ee5\u5e76\u884c\u6267\u884c\u7684\u6307\u4ee4\u3002\u8bc6\u522b\u6307\u4ee4\u7684\u6570\u636e\u4f9d\u8d56\u95ee\u9898\uff08data hazards\uff09\u662f\u4e3b\u8981\u6b65\u9aa4\u4e4b\u4e00\u3002
"},{"location":"zh/#94","title":"\u4e0d\u4e86\u89e3\u6570\u636e\u5bf9\u9f50 (#94)","text":"\u8bb0\u4f4fGo\u4e2d\u57fa\u672c\u7c7b\u578b\u4e0e\u5176\u81ea\u8eab\u5927\u5c0f\u5bf9\u9f50\uff0c\u4f8b\u5982\uff0c\u6309\u964d\u5e8f\u4ece\u5927\u5230\u5c0f\u91cd\u65b0\u7ec4\u7ec7\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u53ef\u4ee5\u5f62\u6210\u66f4\u7d27\u51d1\u7684\u7ed3\u6784\u4f53\uff08\u51cf\u5c11\u5185\u5b58\u5206\u914d\uff0c\u66f4\u597d\u7684\u7a7a\u95f4\u5c40\u90e8\u6027\uff09\uff0c\u8fd9\u6709\u52a9\u4e8e\u907f\u514d\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u3002
"},{"location":"zh/#stack-vs-heap-95","title":"\u4e0d\u4e86\u89e3 stack vs. heap (#95)","text":"\u4e86\u89e3\u5806\u548c\u6808\u4e4b\u95f4\u7684\u533a\u522b\u662f\u5f00\u53d1\u4eba\u5458\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\uff0c\u7279\u522b\u662f\u8981\u53bb\u4f18\u5316\u4e00\u4e2aGo\u7a0b\u5e8f\u65f6\u3002\u6808\u5206\u914d\u7684\u5f00\u9500\u51e0\u4e4e\u4e3a\u96f6\uff0c\u800c\u5806\u5206\u914d\u5219\u8f83\u6162\uff0c\u5e76\u4e14\u4f9d\u8d56GC\u6765\u6e05\u7406\u5185\u5b58\u3002
"},{"location":"zh/#api-compiler-optimizations-and-syncpool-96","title":"\u4e0d\u77e5\u9053\u5982\u4f55\u51cf\u5c11\u5185\u5b58\u5206\u914d\u6b21\u6570 (API\u8c03\u6574, compiler optimizations, andsync.Pool
) (#96)","text":"\u51cf\u5c11\u5185\u5b58\u5206\u914d\u6b21\u6570\u4e5f\u662f\u4f18\u5316Go\u5e94\u7528\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0,\u6bd4\u5982\u4ed4\u7ec6\u8bbe\u8ba1API\u6765\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u4ee5\u53ca\u4f7f\u7528 sync.Pool
\u6765\u5bf9\u5f85\u5206\u914d\u5bf9\u8c61\u8fdb\u884c\u6c60\u5316\u3002
\u4f7f\u7528\u5feb\u901f\u8def\u5f84\u7684\u5185\u8054\u6280\u672f\u6765\u66f4\u52a0\u6709\u6548\u5730\u51cf\u5c11\u8c03\u7528\u51fd\u6570\u7684\u644a\u9500\u65f6\u95f4\u3002
"},{"location":"zh/#go-98","title":"\u4e0d\u4f7f\u7528Go\u95ee\u9898\u8bca\u65ad\u5de5\u5177 (#98)","text":"\u4e86\u89e3Go profilng\u5de5\u5177\u3001\u6267\u884c\u65f6tracer\u6765\u8f85\u52a9\u5224\u65ad\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u662f\u5426\u6b63\u5e38\uff0c\u4ee5\u53ca\u5217\u51fa\u9700\u8981\u4f18\u5316\u7684\u90e8\u5206\u3002
"},{"location":"zh/#gc-99","title":"\u4e0d\u7406\u89e3GC\u662f\u5982\u4f55\u5de5\u4f5c\u7684 (#99)","text":"\u7406\u89e3\u5982\u4f55\u8c03\u4f18GC\u80fd\u591f\u5e26\u6765\u5f88\u591a\u6536\u76ca\uff0c\u4f8b\u5982\u6709\u52a9\u4e8e\u66f4\u9ad8\u6548\u5730\u5904\u7406\u7a81\u589e\u7684\u8d1f\u8f7d\u3002
"},{"location":"zh/#dockerk8sgo-100","title":"\u4e0d\u4e86\u89e3Docker\u6216\u8005K8S\u5bf9\u8fd0\u884c\u7684Go\u5e94\u7528\u7684\u6027\u80fd\u5f71\u54cd (#100)","text":"\u4e3a\u4e86\u907f\u514dCPU throttling\uff08CPU\u9650\u9891\uff09\u95ee\u9898\uff0c\u5f53\u6211\u4eec\u5728Docker\u548cKubernetes\u90e8\u7f72\u5e94\u7528\u65f6\uff0c\u8981\u77e5\u9053Go\u8bed\u8a00\u5bf9CFS(\u5b8c\u5168\u516c\u5e73\u8c03\u5ea6\u5668)\u65e0\u611f\u77e5\u3002
"}]} \ No newline at end of file diff --git a/site/sitemap.xml b/site/sitemap.xml index d3dd37c4..d9c8ee42 100644 --- a/site/sitemap.xml +++ b/site/sitemap.xml @@ -2,67 +2,67 @@