From a6c4ae916da1d386f19841f372632b849f67ba4f Mon Sep 17 00:00:00 2001 From: Teiva Harsanyi Date: Wed, 9 Oct 2024 13:38:14 +0200 Subject: [PATCH] Announce --- overrides/main.html | 2 +- site/20-slice/index.html | 2 +- site/28-maps-memory-leaks/index.html | 2 +- site/404.html | 2 +- site/5-interface-pollution/index.html | 4 +- site/56-concurrency-faster/index.html | 2 +- site/89-benchmarks/index.html | 2 +- site/9-generics/index.html | 2 +- site/92-false-sharing/index.html | 2 +- .../98-profiling-execution-tracing/index.html | 2 +- site/book/index.html | 2 +- site/chapter-1/index.html | 2 +- site/external/index.html | 2 +- site/index.html | 4 +- site/ja/index.html | 2 +- site/pt-br/index.html | 2 +- site/search/search_index.json | 2 +- site/sitemap.xml | 30 +- site/sitemap.xml.gz | Bin 348 -> 348 bytes site/stylesheets/extra.css | 7 + site/zh/index.html | 450 +++++++++--------- 21 files changed, 266 insertions(+), 259 deletions(-) diff --git a/overrides/main.html b/overrides/main.html index c489064a..f8142e61 100644 --- a/overrides/main.html +++ b/overrides/main.html @@ -1,5 +1,5 @@ {% extends "base.html" %} {% block announce %} -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. {% endblock %} diff --git a/site/20-slice/index.html b/site/20-slice/index.html index 610a80e1..d8bc0354 100644 --- a/site/20-slice/index.html +++ b/site/20-slice/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/28-maps-memory-leaks/index.html b/site/28-maps-memory-leaks/index.html index a24b7293..caab715a 100644 --- a/site/28-maps-memory-leaks/index.html +++ b/site/28-maps-memory-leaks/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/404.html b/site/404.html index c7b59a2d..3b48415f 100644 --- a/site/404.html +++ b/site/404.html @@ -91,7 +91,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/5-interface-pollution/index.html b/site/5-interface-pollution/index.html index 5b4881fd..9e03236f 100644 --- a/site/5-interface-pollution/index.html +++ b/site/5-interface-pollution/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. @@ -1190,7 +1190,7 @@

Concepts

  • io.Writer writes data to a target
  • What is the rationale for having these two interfaces in the language? What is the point of creating these abstractions?

    -

    Let’s assume we need to implement a function that should copy the content of one file to another. We could create a specific function that would take as input two *os.Files. Or, we can choose to create a more generic function using io.Reader and io.Writer abstractions:

    +

    Let’s assume we need to implement a function that should copy the content of one file to another. We could create a specific function that would take as input two *os.File. Or, we can choose to create a more generic function using io.Reader and io.Writer abstractions:

    func copySourceToDest(source io.Reader, dest io.Writer) error {
         // ...
     }
    diff --git a/site/56-concurrency-faster/index.html b/site/56-concurrency-faster/index.html
    index 01a1cbfd..0f70ecf1 100644
    --- a/site/56-concurrency-faster/index.html
    +++ b/site/56-concurrency-faster/index.html
    @@ -137,7 +137,7 @@
                   
                 
                 
    -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing.
    +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe.
     
               
    diff --git a/site/89-benchmarks/index.html b/site/89-benchmarks/index.html index 39bd8ad9..fc124649 100644 --- a/site/89-benchmarks/index.html +++ b/site/89-benchmarks/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/9-generics/index.html b/site/9-generics/index.html index 15f9d279..b0f98fc0 100644 --- a/site/9-generics/index.html +++ b/site/9-generics/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/92-false-sharing/index.html b/site/92-false-sharing/index.html index d1ea5b84..87fdbfba 100644 --- a/site/92-false-sharing/index.html +++ b/site/92-false-sharing/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/98-profiling-execution-tracing/index.html b/site/98-profiling-execution-tracing/index.html index 87cfff4f..25af8f6e 100644 --- a/site/98-profiling-execution-tracing/index.html +++ b/site/98-profiling-execution-tracing/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/book/index.html b/site/book/index.html index cc6ae297..489813ff 100644 --- a/site/book/index.html +++ b/site/book/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/chapter-1/index.html b/site/chapter-1/index.html index 14373c42..a40e09e2 100644 --- a/site/chapter-1/index.html +++ b/site/chapter-1/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/external/index.html b/site/external/index.html index 862286b2..249e6fbb 100644 --- a/site/external/index.html +++ b/site/external/index.html @@ -135,7 +135,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/index.html b/site/index.html index cb17b212..3e346611 100644 --- a/site/index.html +++ b/site/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. @@ -5087,7 +5087,7 @@

    Not enabling the race flag (#83)

    fmt.Println(i) }
    -

    Runnig this code with the -race logs the following warning:

    +

    Running this code with the -race logs the following warning:

    ==================
     WARNING: DATA RACE
     Write at 0x00c000026078 by goroutine 7: # (1)
    diff --git a/site/ja/index.html b/site/ja/index.html
    index 3fb66f6f..ddd5fb78 100644
    --- a/site/ja/index.html
    +++ b/site/ja/index.html
    @@ -137,7 +137,7 @@
                   
                 
                 
    -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing.
    +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe.
     
               
    diff --git a/site/pt-br/index.html b/site/pt-br/index.html index 4aae3adc..80115d29 100644 --- a/site/pt-br/index.html +++ b/site/pt-br/index.html @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe. diff --git a/site/search/search_index.json b/site/search/search_index.json index 348b04c8..c687808c 100644 --- a/site/search/search_index.json +++ b/site/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Common Go Mistakes","text":"

    This page is a summary of the mistakes in the 100 Go Mistakes and How to Avoid Them book. Meanwhile, it's also open to the community. If you believe that a common Go mistake should be added, please create an issue.

    Support

    This website is 100% free as I wanted to provide to the Go community as much free content as possible from my book. If you find the content useful and want to support my work, feel free to contribute \ud83d\ude42:

    Beta

    You're viewing a beta version enriched with significantly more content. However, this version is not yet complete, and I'm looking for volunteers to help me summarize the remaining mistakes (GitHub issue #43).

    Progress:

    "},{"location":"#code-and-project-organization","title":"Code and Project Organization","text":""},{"location":"#unintended-variable-shadowing-1","title":"Unintended variable shadowing (#1)","text":"TL;DR

    Avoiding 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;DR

    Avoiding 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 foo() {\n    // ...\n    return true\n} else {\n    // ...\n}\n

    Instead, we omit the else block like this:

    if foo() {\n    // ...\n    return true\n}\n// ...\n
    if s != \"\" {\n    // ...\n} else {\n    return 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 == \"\" {\n    return 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;DR

    When 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;DR

    Forcing 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;DR

    Abstractions 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.

    Read the full section here.

    Source code

    "},{"location":"#interface-on-the-producer-side-6","title":"Interface on the producer side (#6)","text":"TL;DR

    Keeping 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;DR

    To 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;DR

    Relying 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;DR

    Using 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 {\n    Bar // Embedded field\n}\n\ntype Bar struct {\n    Baz 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:

    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;DR

    To 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 options struct {\n  port *int\n}\n\ntype Option func(options *options) error\n\nfunc WithPort(port int) Option {\n  return func(options *options) error {\n    if port < 0 {\n    return errors.New(\"port should be positive\")\n  }\n  options.port = &port\n  return nil\n  }\n}\n\nfunc NewServer(addr string, opts ...Option) ( *http.Server, error) {\n  var options options\n  for _, opt := range opts {\n    err := opt(&options)\n    if err != nil {\n      return nil, err\n    }\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\n  var port int\n  if options.port == nil {\n    port = defaultHTTPPort\n  } else {\n      if *options.port == 0 {\n      port = randomPort()\n    } else {\n      port = *options.port\n    }\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.

    Note

    In 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;DR

    Naming 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;DR

    To 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;DR

    To 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;DR

    To 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:

    Besides linters, we should also use code formatters to fix code style. Here is a list of some code formatters for you to try:

    Meanwhile, we should also look at golangci-lint (https://github.com/golangci/golangci-lint). It\u2019s a linting tool that provides a facade on top of many useful linters and formatters. Also, it allows running the linters in parallel to improve analysis speed, which is quite handy.

    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;DR

    When 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:

    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;DR

    Because 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;DR

    Making 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;DR

    Understanding 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;DR

    When 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;DR

    To 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

    The 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;DR

    To 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;DR

    To 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;DR

    Using 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.

    Note

    s[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;DR

    Working 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;DR

    When 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;DR

    A 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;DR

    To 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:

    Note

    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 in range 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 in range 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 {\n    a[2] = 10\n    if i == 2 {\n        fmt.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 in range loops (#32)","text":"Warning

    This mistake isn't relevant anymore from Go 1.22 (details).

    "},{"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;DR

    To 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 the break 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++ {\n    fmt.Printf(\"%d \", i)\n\n    switch i {\n    default:\n    case 2:\n        break\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:\n    for i := 0; i < 5; i++ {\n        fmt.Printf(\"%d \", i)\n\n        switch i {\n        default:\n        case 2:\n            break 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":"Using defer 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 {\n    for path := range ch {\n        file, err := os.Open(path)\n        if err != nil {\n            return err\n        }\n\n        defer file.Close()\n\n        // Do something with file\n    }\n    return 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 {\n    for path := range ch {\n        if err := readFile(path); err != nil {\n            return err\n        }\n    }\n    return nil\n}\n\nfunc readFile(path string) error {\n    file, err := os.Open(path)\n    if err != nil {\n        return err\n    }\n\n    defer file.Close()\n\n    // Do something with file\n    return 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;DR

    Understanding 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:

    Source code

    "},{"location":"#inaccurate-string-iteration-37","title":"Inaccurate string iteration (#37)","text":"TL;DR

    Iterating 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 {\n    fmt.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 {\n    fmt.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 {\n    fmt.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;DR

    strings.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;DR

    Concatenating 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 {\n    s := \"\"\n    for _, value := range values {\n        s += value\n    }\n    return 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 {\n    sb := strings.Builder{}\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return 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.

    Note

    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 {\n    total := 0\n    for i := 0; i < len(values); i++ {\n        total += len(values[i])\n    }\n\n    sb := strings.Builder{}\n    sb.Grow(total) (2)\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return 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;DR

    Remembering 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;DR

    Using 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;DR

    The 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\n\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

    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;DR

    Using 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) {\n    b = a\n    return\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;DR

    See #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) (\n    lat, lng float32, err error) {\n    isValid := l.validateAddress(address) (1)\n    if !isValid {\n        return 0, 0, errors.New(\"invalid address\")\n    }\n\n    if ctx.Err() != nil { (2)\n        return 0, 0, err\n    }\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;DR

    When 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;DR

    Designing 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 how defer 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 (\n    StatusSuccess  = \"success\"\n    StatusErrorFoo = \"error_foo\"\n    StatusErrorBar = \"error_bar\"\n)\n\nfunc f() error {\n    var status string\n    defer notify(status)\n    defer incrementCounter(status)\n\n    if err := foo(); err != nil {\n        status = StatusErrorFoo\n        return err\n    }\n\n    if err := bar(); err != nil {\n        status = StatusErrorBar\n        return err\n    }\n\n    status = StatusSuccess\n    return 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 {\n    var status string\n    defer notify(&status) \n    defer incrementCounter(&status)\n\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 {\n    var status string\n    defer func() {\n        notify(status)\n        incrementCounter(status)\n    }()\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;DR

    Using 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() {\n    fmt.Println(\"a\")\n    panic(\"foo\")\n    fmt.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;DR

    Wrapping 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;DR

    If 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;DR

    If 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\"\n\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:

    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;DR

    In 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;DR

    Ignoring 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 handling defer 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  // ...\n  notify() // Error handling is omitted\n}\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;DR

    Understanding 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;DR

    To 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;DR

    Being 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.

    "},{"location":"#not-understanding-race-problems-data-races-vs-race-conditions-and-the-go-memory-model-58","title":"Not understanding race problems (data races vs. race conditions and the Go memory model) (#58)","text":"TL;DR

    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:

    "},{"location":"#race-condition","title":"Race Condition","text":"

    Depending 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;DR

    When 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:

    Note

    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;DR

    Go 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:

    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.

    "},{"location":"#context-values","title":"Context values","text":"

    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,

    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;DR

    Understanding 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) {\n    response, err := doSomeTask(r.Context(), r)\n    if err != nil {\n        http.Error(w, err.Error(), http.StatusInternalServerError)\n    return\n    }\n    go func() {\n        err := publish(r.Context(), response)\n        // Do something with err\n    }()\n    writeResponse(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.

    Note

    From 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;DR

    Avoiding 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() {\n    newWatcher()\n    // Run the application\n}\n\ntype watcher struct { /* Some resources */ }\n\nfunc newWatcher() {\n    w := watcher{}\n    go 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() {\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n    newWatcher(ctx)\n    // Run the application\n}\n\nfunc newWatcher(ctx context.Context) {\n    w := watcher{}\n    go 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() {\n    w := newWatcher()\n    defer w.close()\n    // Run the application\n}\n\nfunc newWatcher() watcher {\n    w := watcher{}\n    go w.watch()\n    return w\n}\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":"Warning

    This mistake isn't relevant anymore from Go 1.22 (details).

    "},{"location":"#expecting-a-deterministic-behavior-using-select-and-channels-64","title":"Expecting a deterministic behavior using select and channels (#64)","text":"TL;DR

    Understanding 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() {\n  for i := 0; i < 10; i++ {\n      messageCh <- i\n    }\n    disconnectCh <- struct{}{}\n}()\n\nfor {\n    select {\n    case v := <-messageCh:\n        fmt.Println(v)\n    case <-disconnectCh:\n        fmt.Println(\"disconnection, return\")\n        return\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 statement 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;DR

    Send 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{}.

    "},{"location":"#not-using-nil-channels-66","title":"Not using nil channels (#66)","text":"TL;DR

    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 {\n    ch := make(chan int, 1)\n\n    go func() {\n        for ch1 != nil || ch2 != nil { // Continue if at least one channel isn\u2019t nil\n            select {\n            case v, open := <-ch1:\n                if !open {\n                    ch1 = nil // Assign ch1 to a nil channel once closed\n                    break\n                }\n                ch <- v\n            case v, open := <-ch2:\n                if !open {\n                    ch2 = nil // Assigns ch2 to a nil channel once closed\n                    break\n                }\n                ch <- v\n            }\n        }\n        close(ch)\n    }()\n\n    return 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;DR

    Carefully decide on the right channel type to use, given a problem. Only unbuffered channels provide strong synchronization guarantees. For buffered channels, you should have a good reason to specify a channel size other than one.

    An unbuffered channel is a channel without any capacity. It can be created by either omitting the size or providing a 0 size:

    ch1 := make(chan int)\nch2 := make(chan int, 0)\n

    With an unbuffered channel (sometimes called a synchronous channel), the sender will block until the receiver receives data from the channel.

    Conversely, a buffered channel has a capacity, and it must be created with a size greater than or equal to 1:

    ch3 := make(chan int, 1)\n

    With a buffered channel, a sender can send messages while the channel isn\u2019t full. Once the channel is full, it will block until a receiver goroutine receives a message:

    ch3 := make(chan int, 1)\nch3 <-1 // Non-blocking\nch3 <-2 // Blocking\n

    The first send isn\u2019t blocking, whereas the second one is, as the channel is full at this stage.

    What's the main difference between unbuffered and buffered channels:

    If we need a buffered channel, what size should we provide?

    The default value we should use for buffered channels is its minimum: 1. So, we may approach the problem from this standpoint: is there any good reason not to use a value of 1? Here\u2019s a list of possible cases where we should use another size:

    If we are outside of these cases, using a different channel size should be done cautiously. Let\u2019s bear in mind that deciding about an accurate queue size isn\u2019t an easy problem:

    Martin Thompson

    Queues are typically always close to full or close to empty due to the differences in pace between consumers and producers. They very rarely operate in a balanced middle ground where the rate of production and consumption is evenly matched.

    "},{"location":"#forgetting-about-possible-side-effects-with-string-formatting-68","title":"Forgetting about possible side effects with string formatting (#68)","text":"TL;DR

    Being aware that string formatting may lead to calling existing functions means watching out for possible deadlocks and other data races.

    It\u2019s pretty easy to forget the potential side effects of string formatting while working in a concurrent application.

    "},{"location":"#etcd-data-race","title":"etcd data race","text":"

    github.com/etcd-io/etcd/pull/7816 shows an example of an issue where a map's key was formatted based on a mutable values from a context.

    "},{"location":"#deadlock","title":"Deadlock","text":"

    Can you see what the problem is in this code with a Customer struct exposing an UpdateAge method and implementing the fmt.Stringer interface?

    type Customer struct {\n    mutex sync.RWMutex // Uses a sync.RWMutex to protect concurrent accesses\n    id    string\n    age   int\n}\n\nfunc (c *Customer) UpdateAge(age int) error {\n    c.mutex.Lock() // Locks and defers unlock as we update Customer\n    defer c.mutex.Unlock()\n\n    if age < 0 { // Returns an error if age is negative\n        return fmt.Errorf(\"age should be positive for customer %v\", c)\n    }\n\n    c.age = age\n    return nil\n}\n\nfunc (c *Customer) String() string {\n    c.mutex.RLock() // Locks and defers unlock as we read Customer\n    defer c.mutex.RUnlock()\n    return fmt.Sprintf(\"id %s, age %d\", c.id, c.age)\n}\n

    The problem here may not be straightforward. If the provided age is negative, we return an error. Because the error is formatted, using the %s directive on the receiver, it will call the String method to format Customer. But because UpdateAge already acquires the mutex lock, the String method won\u2019t be able to acquire it. Hence, this leads to a deadlock situation. If all goroutines are also asleep, it leads to a panic.

    One possible solution is to restrict the scope of the mutex lock:

    func (c *Customer) UpdateAge(age int) error {\n    if age < 0 {\n        return fmt.Errorf(\"age should be positive for customer %v\", c)\n    }\n\n    c.mutex.Lock()\n    defer c.mutex.Unlock()\n\n    c.age = age\n    return nil\n}\n

    Yet, such an approach isn't always possible. In these conditions, we have to be extremely careful with string formatting.

    Another approach is to access the id field directly:

    func (c *Customer) UpdateAge(age int) error {\n    c.mutex.Lock()\n    defer c.mutex.Unlock()\n\n    if age < 0 {\n        return fmt.Errorf(\"age should be positive for customer id %s\", c.id)\n    }\n\n    c.age = age\n    return nil\n}\n

    In concurrent applications, we should remain cautious about the possible side effects of string formatting.

    Source code

    "},{"location":"#creating-data-races-with-append-69","title":"Creating data races with append (#69)","text":"TL;DR

    Calling append isn\u2019t always data-race-free; hence, it shouldn\u2019t be used concurrently on a shared slice.

    Should adding an element to a slice using append is data-race-free? Spoiler: it depends.

    Do you believe this example has a data race?

    s := make([]int, 1)\n\ngo func() { // In a new goroutine, appends a new element on s\n    s1 := append(s, 1)\n    fmt.Println(s1)\n}()\n\ngo func() { // Same\n    s2 := append(s, 1)\n    fmt.Println(s2)\n}()\n

    The answer is no.

    In this example, we create a slice with make([]int, 1). The code creates a one-length, one-capacity slice. Thus, because the slice is full, using append in each goroutine returns a slice backed by a new array. It doesn\u2019t mutate the existing array; hence, it doesn\u2019t lead to a data race.

    Now, let\u2019s run the same example with a slight change in how we initialize s. Instead of creating a slice with a length of 1, we create it with a length of 0 but a capacity of 1. How about this new example? Does it contain a data race?

    s := make([]int, 0, 1)\n\ngo func() { \n    s1 := append(s, 1)\n    fmt.Println(s1)\n}()\n\ngo func() {\n    s2 := append(s, 1)\n    fmt.Println(s2)\n}()\n

    The answer is yes. We create a slice with make([]int, 0, 1). Therefore, the array isn\u2019t full. Both goroutines attempt to update the same index of the backing array (index 1), which is a data race.

    How can we prevent the data race if we want both goroutines to work on a slice containing the initial elements of s plus an extra element? One solution is to create a copy of s.

    We should remember that using append on a shared slice in concurrent applications can lead to a data race. Hence, it should be avoided.

    Source code

    "},{"location":"#using-mutexes-inaccurately-with-slices-and-maps-70","title":"Using mutexes inaccurately with slices and maps (#70)","text":"TL;DR

    Remembering that slices and maps are pointers can prevent common data races.

    Let's implement a Cache struct used to handle caching for customer balances. This struct will contain a map of balances per customer ID and a mutex to protect concurrent accesses:

    type Cache struct {\n    mu       sync.RWMutex\n    balances map[string]float64\n}\n

    Next, we add an AddBalance method that mutates the balances map. The mutation is done in a critical section (within a mutex lock and a mutex unlock):

    func (c *Cache) AddBalance(id string, balance float64) {\n    c.mu.Lock()\n    c.balances[id] = balance\n    c.mu.Unlock()\n}\n

    Meanwhile, we have to implement a method to calculate the average balance for all the customers. One idea is to handle a minimal critical section this way:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    balances := c.balances // Creates a copy of the balances map\n    c.mu.RUnlock()\n\n    sum := 0.\n    for _, balance := range balances { // Iterates over the copy, outside of the critical section\n        sum += balance\n    }\n    return sum / float64(len(balances))\n}\n

    What's the problem with this code?

    If we run a test using the -race flag with two concurrent goroutines, one calling AddBalance (hence mutating balances) and another calling AverageBalance, a data race occurs. What\u2019s the problem here?

    Internally, a map is a runtime.hmap struct containing mostly metadata (for example, a counter) and a pointer referencing data buckets. So, balances := c.balances doesn\u2019t copy the actual data. Therefore, the two goroutines perform operations on the same data set, and one mutates it. Hence, it's a data race.

    One possible solution is to protect the whole AverageBalance function:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    defer c.mu.RUnlock() // Unlocks when the function returns\n\n    sum := 0.\n    for _, balance := range c.balances {\n        sum += balance\n    }\n    return sum / float64(len(c.balances))\n}\n

    Another option, if the iteration operation isn\u2019t lightweight, is to work on an actual copy of the data and protect only the copy:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    m := make(map[string]float64, len(c.balances)) // Copies the map\n    for k, v := range c.balances {\n        m[k] = v\n    }\n    c.mu.RUnlock()\n\n    sum := 0.\n    for _, balance := range m {\n        sum += balance\n    }\n    return sum / float64(len(m))\n}\n

    Once we have made a deep copy, we release the mutex. The iterations are done on the copy outside of the critical section.

    In summary, we have to be careful with the boundaries of a mutex lock. In this section, we have seen why assigning an existing map (or an existing slice) to a map isn\u2019t enough to protect against data races. The new variable, whether a map or a slice, is backed by the same data set. There are two leading solutions to prevent this: protect the whole function, or work on a copy of the actual data. In all cases, let\u2019s be cautious when designing critical sections and make sure the boundaries are accurately defined.

    Source code

    "},{"location":"#misusing-syncwaitgroup-71","title":"Misusing sync.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 about sync.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 using errgroup (#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 a sync 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;DR

    Remain 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 {\n    select {\n    case <-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.

    Developers often use time.After in loops or HTTP handlers repeatedly to implement the timing function. But it can lead to unintended peak memory consumption due to the delayed release of resources, just like the following code:

    func consumer(ch <-chan Event) {\n    for {\n        select {\n        case event := <-ch:\n            handle(event)\n        case <-time.After(time.Hour):\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    The source code of the function time.After is as follows:

    func After(d Duration) <-chan Time {\n    return NewTimer(d).C\n}\n

    As we see, it returns receive-only channel.

    When time.After is used in a loop or repeated context, a new channel is created in each iteration. If these channels are not properly closed or if their associated timers are not stopped, they can accumulate and consume memory. The resources associated with each timer and channel are only released when the timer expires or the channel is closed.

    To avoid this happening, We can use context's timeout setting instead of time.After, like below:

    func consumer(ch <-chan Event) {\n    for {\n        ctx, cancel := context.WithTimeout(context.Background(), time.Hour)\n        select {\n        case event := <-ch:\n            cancel()\n            handle(event)\n        case <-ctx.Done():\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    We can also use time.NewTimer like so:

    func consumer(ch <-chan Event) {\n    timerDuration := 1 * time.Hour\n    timer := time.NewTimer(timerDuration)\n\n    for {\n        timer.Reset(timerDuration)\n        select {\n        case event := <-ch:\n            handle(event)\n        case <-timer.C:\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    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

    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":"

    Call 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;DR

    To 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.

    Consider the following HTTP handler that handles an error from foo using http.Error:

    func handler(w http.ResponseWriter, req *http.Request) {\n    err := foo(req)\n    if err != nil {\n        http.Error(w, \"foo\", http.StatusInternalServerError)\n    }\n\n    _, _ = w.Write([]byte(\"all good\"))\n    w.WriteHeader(http.StatusCreated)\n}\n

    If we run this code and err != nil, the HTTP response would be:

    foo\nall good\n

    The response contains both the error and success messages, and also the first HTTP status code, 500. There would also be a warning log indicating that we attempted to write the status code multiple times:

    2023/10/10 16:45:33 http: superfluous response.WriteHeader call from main.handler (main.go:20)\n

    The mistake in this code is that http.Error does not stop the handler's execution, which means the success message and status code get written in addition to the error. Beyond an incorrect response, failing to return after writing an error can lead to the unwanted execution of code and unexpected side-effects. The following code adds the return statement following the http.Error and exhibits the desired behavior when ran:

    func handler(w http.ResponseWriter, req *http.Request) {\n    err := foo(req)\n    if err != nil {\n        http.Error(w, \"foo\", http.StatusInternalServerError)\n        return // Adds the return statement\n    }\n\n    _, _ = w.Write([]byte(\"all good\"))\n    w.WriteHeader(http.StatusCreated)\n}\n

    Source code

    "},{"location":"#using-the-default-http-client-and-server-81","title":"Using the default HTTP client and server (#81)","text":"TL;DR

    For 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;DR

    Categorizing 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;DR

    Enabling 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.

    In Go, the race detector isn\u2019t a static analysis tool used during compilation; instead, it\u2019s a tool to find data races that occur at runtime. To enable it, we have to enable the -race flag while compiling or running a test. For example:

    go test -race ./...\n

    Once the race detector is enabled, the compiler instruments the code to detect data races. Instrumentation refers to a compiler adding extra instructions: here, tracking all memory accesses and recording when and how they occur.

    Enabling the race detector adds an overhead in terms of memory and execution time; hence, it's generally recommended to enable it only during local testing or continuous integration, not production.

    If a race is detected, Go raises a warning. For example:

    package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    i := 0\n    go func() { i++ }()\n    fmt.Println(i)\n}\n

    Runnig this code with the -race logs the following warning:

    ==================\nWARNING: DATA RACE\nWrite at 0x00c000026078 by goroutine 7: # (1)\n  main.main.func1()\n      /tmp/app/main.go:9 +0x4e\n\nPrevious read at 0x00c000026078 by main goroutine: # (2)\n  main.main()\n      /tmp/app/main.go:10 +0x88\n\nGoroutine 7 (running) created at: # (3)\n  main.main()\n      /tmp/app/main.go:9 +0x7a\n==================\n
    1. Indicates that goroutine 7 was writing
    2. Indicates that the main goroutine was reading
    3. Indicates when the goroutine 7 was created

    Let\u2019s make sure we are comfortable reading these messages. Go always logs the following:

    In addition, if a specific file contains tests that lead to data races, we can exclude it from race detection using the !race build tag:

    //go:build !race\n\npackage main\n\nimport (\n    \"testing\"\n)\n\nfunc TestFoo(t *testing.T) {\n    // ...\n}\n
    "},{"location":"#not-using-test-execution-modes-parallel-and-shuffle-84","title":"Not using test execution modes (parallel and shuffle) (#84)","text":"TL;DR

    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.

    "},{"location":"#not-using-table-driven-tests-85","title":"Not using table-driven tests (#85)","text":"TL;DR

    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;DR

    Avoid 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;DR

    Understanding 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":"

    Source code

    Source code

    "},{"location":"#writing-inaccurate-benchmarks-89","title":"Writing inaccurate benchmarks (#89)","text":"TL;DR

    Regarding 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;DR

    Fuzzing 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;DR

    Knowing 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.

    Read the full section here.

    Source code

    "},{"location":"#not-taking-into-account-instruction-level-parallelism-93","title":"Not taking into account instruction-level parallelism (#93)","text":"TL;DR

    Use 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;DR

    You 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;DR

    Understanding 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, and sync.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;DR

    Use 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;DR

    Rely 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;DR

    Understanding 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;DR

    To help avoid CPU throttling when deployed in Docker and Kubernetes, keep in mind that Go isn\u2019t CFS-aware.

    By default, GOMAXPROCS is set to the number of OS-apparent logical CPU cores.

    When running some Go code inside Docker and Kubernetes, we must know that Go isn't CFS-aware (github.com/golang/go/issues/33803). Therefore, GOMAXPROCS isn't automatically set to the value of spec.containers.resources.limits.cpu (see Kubernetes Resource Management for Pods and Containers); instead, it's set to the number of logical cores on the host machine. The main implication is that it can lead to an increased tail latency in some specific situations.

    One solution is to rely on uber-go/automaxprocs that automatically set GOMAXPROCS to match the Linux container CPU quota.

    "},{"location":"#community","title":"Community","text":"

    Thanks to all the contributors:

    "},{"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.

    Note

    In 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:

    1. Allocate an empty map.
    2. Add 1 million elements.
    3. Remove all the elements, and run a Garbage Collection (GC).

    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() {\n    n := 1_000_000\n    m := make(map[int][128]byte)\n    printAlloc()\n\n    for i := 0; i < n; i++ { // Adds 1 million elements\n        m[i] = [128]byte{}\n    }\n    printAlloc()\n\n    for i := 0; i < n; i++ { // Deletes 1 million elements\n        delete(m, i)\n    }\n\n    runtime.GC() // Triggers a manual GC\n    printAlloc()\n    runtime.KeepAlive(m) // Keeps a reference to m so that the map isn\u2019t collected\n}\n\nfunc printAlloc() {\n    var m runtime.MemStats\n    runtime.ReadMemStats(&m)\n    fmt.Printf(\"%d MB\\n\", m.Alloc/(1024*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 {\n    B 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.

    Step map[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":"5-interface-pollution/","title":"Interface pollution","text":"

    Interfaces are one of the cornerstones of the Go language when designing and structuring our code. However, like many tools or concepts, abusing them is generally not a good idea. Interface pollution is about overwhelming our code with unnecessary abstractions, making it harder to understand. It\u2019s a common mistake made by developers coming from another language with different habits. Before delving into the topic, let\u2019s refresh our minds about Go\u2019s interfaces. Then, we will see when it\u2019s appropriate to use interfaces and when it may be considered pollution.

    "},{"location":"5-interface-pollution/#concepts","title":"Concepts","text":"

    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.

    To understand what makes interfaces so powerful, we will dig into two popular ones from the standard library: io.Reader and io.Writer. The io package provides abstractions for I/O primitives. Among these abstractions, io.Reader relates to reading data from a data source and io.Writer to writing data to a target, as represented in the next figure:

    The io.Reader contains a single Read method:

    type Reader interface {\n    Read(p []byte) (n int, err error)\n}\n

    Custom implementations of the io.Reader interface should accept a slice of bytes, filling it with its data and returning either the number of bytes read or an error.

    On the other hand, io.Writer defines a single method, Write:

    type Writer interface {\n    Write(p []byte) (n int, err error)\n}\n

    Custom implementations of io.Writer should write the data coming from a slice to a target and return either the number of bytes written or an error. Therefore, both interfaces provide fundamental abstractions:

    What is the rationale for having these two interfaces in the language? What is the point of creating these abstractions?

    Let\u2019s assume we need to implement a function that should copy the content of one file to another. We could create a specific function that would take as input two *os.Files. Or, we can choose to create a more generic function using io.Reader and io.Writer abstractions:

    func copySourceToDest(source io.Reader, dest io.Writer) error {\n    // ...\n}\n

    This function would work with *os.File parameters (as *os.File implements both io.Reader and io.Writer) and any other type that would implement these interfaces. For example, we could create our own io.Writer that writes to a database, and the code would remain the same. It increases the genericity of the function; hence, its reusability.

    Furthermore, writing a unit test for this function is easier because, instead of having to handle files, we can use the strings and bytes packages that provide helpful implementations:

    func TestCopySourceToDest(t *testing.T) {\n    const input = \"foo\"\n    source := strings.NewReader(input) // Creates an io.Reader\n    dest := bytes.NewBuffer(make([]byte, 0)) // Creates an io.Writer\n\n    err := copySourceToDest(source, dest) // Calls copySourceToDest from a *strings.Reader and a *bytes.Buffer\n    if err != nil {\n        t.FailNow()\n    }\n\n    got := dest.String()\n    if got != input {\n        t.Errorf(\"expected: %s, got: %s\", input, got)\n    }\n}\n

    In the example, source is a *strings.Reader, whereas dest is a *bytes.Buffer. Here, we test the behavior of copySourceToDest without creating any files.

    While designing interfaces, the granularity (how many methods the interface contains) is also something to keep in mind. A known proverb in Go relates to how big an interface should be:

    Rob Pike

    The bigger the interface, the weaker the abstraction.

    Indeed, adding methods to an interface can decrease its level of reusability. io.Reader and io.Writer are powerful abstractions because they cannot get any simpler. Furthermore, we can also combine fine-grained interfaces to create higher-level abstractions. This is the case with io.ReadWriter, which combines the reader and writer behaviors:

    type ReadWriter interface {\n    Reader\n    Writer\n}\n
    Note

    As Einstein said, \u201cEverything should be made as simple as possible, but no simpler.\u201d Applied to interfaces, this denotes that finding the perfect granularity for an interface isn\u2019t necessarily a straightforward process.

    Let\u2019s now discuss common cases where interfaces are recommended.

    "},{"location":"5-interface-pollution/#when-to-use-interfaces","title":"When to use interfaces","text":"

    When should we create interfaces in Go? Let\u2019s look at three concrete use cases where interfaces are usually considered to bring value. Note that the goal isn\u2019t to be exhaustive because the more cases we add, the more they would depend on the context. However, these three cases should give us a general idea:

    "},{"location":"5-interface-pollution/#common-behavior","title":"Common behavior","text":"

    The first option we will discuss is to use interfaces when multiple types implement a common behavior. In such a case, we can factor out the behavior inside an interface. If we look at the standard library, we can find many examples of such a use case. For example, sorting a collection can be factored out via three methods:

    Hence, the following interface was added to the sort package:

    type Interface interface {\n    Len() int // Number of elements\n    Less(i, j int) bool // Checks two elements\n    Swap(i, j int) // Swaps two elements\n}\n

    This interface has a strong potential for reusability because it encompasses the common behavior to sort any collection that is index-based.

    Throughout the sort package, we can find dozens of implementations. If at some point we compute a collection of integers, for example, and we want to sort it, are we necessarily interested in the implementation type? Is it important whether the sorting algorithm is a merge sort or a quicksort? In many cases, we don\u2019t care. Hence, the sorting behavior can be abstracted, and we can depend on the sort.Interface.

    Finding the right abstraction to factor out a behavior can also bring many benefits. For example, the sort package provides utility functions that also rely on sort.Interface, such as checking whether a collection is already sorted. For instance:

    func IsSorted(data Interface) bool {\n    n := data.Len()\n    for i := n - 1; i > 0; i-- {\n        if data.Less(i, i-1) {\n            return false\n        }\n    }\n    return true\n}\n

    Because sort.Interface is the right level of abstraction, it makes it highly valuable.

    Let\u2019s now see another main use case when using interfaces.

    "},{"location":"5-interface-pollution/#decoupling","title":"Decoupling","text":"

    Another important use case is about decoupling our code from an implementation. If we rely on an abstraction instead of a concrete implementation, the implementation itself can be replaced with another without even having to change our code. This is the Liskov Substitution Principle (the L in Robert C. Martin\u2019s SOLID design principles).

    One benefit of decoupling can be related to unit testing. Let\u2019s assume we want to implement a CreateNewCustomer method that creates a new customer and stores it. We decide to rely on the concrete implementation directly (let\u2019s say a mysql.Store struct):

    type CustomerService struct {\n    store mysql.Store // Depends on the concrete implementation\n}\n\nfunc (cs CustomerService) CreateNewCustomer(id string) error {\n    customer := Customer{id: id}\n    return cs.store.StoreCustomer(customer)\n}\n

    Now, what if we want to test this method? Because customerService relies on the actual implementation to store a Customer, we are obliged to test it through integration tests, which requires spinning up a MySQL instance (unless we use an alternative technique such as go-sqlmock, but this isn\u2019t the scope of this section). Although integration tests are helpful, that\u2019s not always what we want to do. To give us more flexibility, we should decouple CustomerService from the actual implementation, which can be done via an interface like so:

    type customerStorer interface { // Creates a storage abstraction\n    StoreCustomer(Customer) error\n}\n\ntype CustomerService struct {\n    storer customerStorer // Decouples CustomerService from the actual implementation\n}\n\nfunc (cs CustomerService) CreateNewCustomer(id string) error {\n    customer := Customer{id: id}\n    return cs.storer.StoreCustomer(customer)\n}\n

    Because storing a customer is now done via an interface, this gives us more flexibility in how we want to test the method. For instance, we can:

    Let\u2019s now discuss another use case: to restrict a behavior.

    "},{"location":"5-interface-pollution/#restricting-behavior","title":"Restricting behavior","text":"

    The last use case we will discuss can be pretty counterintuitive at first sight. It\u2019s about restricting a type to a specific behavior. Let\u2019s imagine we implement a custom configuration package to deal with dynamic configuration. We create a specific container for int configurations via an IntConfig struct that also exposes two methods: Get and Set. Here\u2019s how that code would look:

    type IntConfig struct {\n    // ...\n}\n\nfunc (c *IntConfig) Get() int {\n    // Retrieve configuration\n}\n\nfunc (c *IntConfig) Set(value int) {\n    // Update configuration\n}\n

    Now, suppose we receive an IntConfig that holds some specific configuration, such as a threshold. Yet, in our code, we are only interested in retrieving the configuration value, and we want to prevent updating it. How can we enforce that, semantically, this configuration is read-only, if we don\u2019t want to change our configuration package? By creating an abstraction that restricts the behavior to retrieving only a config value:

    type intConfigGetter interface {\n    Get() int\n}\n

    Then, in our code, we can rely on intConfigGetter instead of the concrete implementation:

    type Foo struct {\n    threshold intConfigGetter\n}\n\nfunc NewFoo(threshold intConfigGetter) Foo { // Injects the configuration getter\n    return Foo{threshold: threshold}\n}\n\nfunc (f Foo) Bar()  {\n    threshold := f.threshold.Get() // Reads the configuration\n    // ...\n}\n

    In this example, the configuration getter is injected into the NewFoo factory method. It doesn\u2019t impact a client of this function because it can still pass an IntConfig struct as it implements intConfigGetter. Then, we can only read the configuration in the Bar method, not modify it. Therefore, we can also use interfaces to restrict a type to a specific behavior for various reasons, such as semantics enforcement.

    In this section, we saw three potential 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. Again, this list isn\u2019t exhaustive, but it should give us a general understanding of when interfaces are helpful in Go.

    Now, let\u2019s finish this section and discuss the problems with interface pollution.

    "},{"location":"5-interface-pollution/#interface-pollution_1","title":"Interface pollution","text":"

    It\u2019s fairly common to see interfaces being overused in Go projects. Perhaps the developer\u2019s background was C# or Java, and they found it natural to create interfaces before concrete types. However, this isn\u2019t how things should work in Go.

    As we discussed, 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?

    Note

    We may also experience performance overhead when calling a method through an interface. It requires a lookup in a hash table\u2019s data structure to find the concrete type an interface points to. But this isn\u2019t an issue in many contexts as the overhead is minimal.

    In summary, we should be cautious when creating abstractions in our code\u2014abstractions 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.

    "},{"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:

    Note

    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.

    Note

    Context 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) {\n    if len(s) <= 1 {\n        return\n    }\n\n    middle := len(s) / 2\n    sequentialMergesort(s[:middle]) // First half\n    sequentialMergesort(s[middle:]) // Second half\n    merge(s, middle) // Merges the two halves\n}\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) {\n    if len(s) <= 1 {\n        return\n    }\n\n    middle := len(s) / 2\n\n    var wg sync.WaitGroup\n    wg.Add(2)\n\n    go func() { // Spins up the first half of the work in a goroutine\n        defer wg.Done()\n        parallelMergesortV1(s[:middle])\n    }()\n\n    go func() { // Spins up the second half of the work in a goroutine\n        defer wg.Done()\n        parallelMergesortV1(s[middle:])\n    }()\n\n    wg.Wait()\n    merge(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\n\nfunc parallelMergesortV2(s []int) {\n    if len(s) <= 1 {\n        return\n    }\n\n    if len(s) <= max {\n        sequentialMergesort(s) // Calls our initial sequential version\n    } else { // If bigger than the threshold, keeps the parallel version\n        middle := len(s) / 2\n\n        var wg sync.WaitGroup\n        wg.Add(2)\n\n        go func() {\n            defer wg.Done()\n            parallelMergesortV2(s[:middle])\n        }()\n\n        go func() {\n            defer wg.Done()\n            parallelMergesortV2(s[middle:])\n        }()\n\n        wg.Wait()\n        merge(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.

    Note

    Why 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:

    "},{"location":"89-benchmarks/#general-concepts","title":"General concepts","text":"

    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) {\n    for i := 0; i < b.N; i++ {\n        foo()\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) {\n    expensiveSetup()\n    for i := 0; i < b.N; i++ {\n        functionUnderTest()\n    }\n}\n

    In this case, we can use the ResetTimer method before entering the loop:

    func BenchmarkFoo(b *testing.B) {\n    expensiveSetup()\n    b.ResetTimer() // Reset the benchmark timer\n    for i := 0; i < b.N; i++ {\n        functionUnderTest()\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) {\n    for i := 0; i < b.N; i++ {\n        expensiveSetup()\n        functionUnderTest()\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) {\n    for i := 0; i < b.N; i++ {\n        b.StopTimer() // Pause the benchmark timer\n        expensiveSetup()\n        b.StartTimer() // Resume the benchmark timer\n        functionUnderTest()\n    }\n}\n

    Here, we pause the benchmark timer to perform the expensive setup and then resume the timer.

    Note

    There\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) {\n    var v int32\n    for i := 0; i < b.N; i++ {\n        atomic.StoreInt32(&v, 1)\n    }\n}\n\nfunc BenchmarkAtomicStoreInt64(b *testing.B) {\n    var v int64\n    for i := 0; i < b.N; i++ {\n        atomic.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.

    Note

    We 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\n\nfunc popcnt(x uint64) uint64 {\n    x -= (x >> 1) & m1\n    x = (x & m2) + ((x >> 2) & m2)\n    x = (x + (x >> 4)) & m4\n    return (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) {\n    for i := 0; i < b.N; i++ {\n        popcnt(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) {\n    for 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:

    1. During each loop iteration, assign the result to a local variable (local in the context of the benchmark function).
    2. Assign the latest result to a global variable.

    In our case, we write the following benchmark:

    var global uint64 // Define a global variable\n\nfunc BenchmarkPopcnt2(b *testing.B) {\n    var v uint64 // Define a local variable\n    for i := 0; i < b.N; i++ {\n        v = popcnt(uint64(i)) // Assign the result to the local variable\n    }\n    global = 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.

    Note

    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 {\n    var sum int64\n    for i := 0; i < len(s); i++ { // Iterate over each row\n        for j := 0; j < 8; j++ { // Iterate over the first eight columns\n            sum += s[i][j] // Increment sum\n        }\n    }\n    return sum\n}\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\n\nvar res int64\n\nfunc BenchmarkCalculateSum512(b *testing.B) {\n    var sum int64\n    s := createMatrix512(rows) // Create a matrix of 512 columns\n    b.ResetTimer()\n    for i := 0; i < b.N; i++ {\n        sum = calculateSum512(s) // Create a matrix of 512 columns\n    }\n    res = sum\n}\n\nfunc BenchmarkCalculateSum513(b *testing.B) {\n    var sum int64\n    s := createMatrix513(rows) // Create a matrix of 513 columns\n    b.ResetTimer()\n    for i := 0; i < b.N; i++ {\n        sum = calculateSum513(s) // Calculate the sum\n    }\n    res = 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).

    Note

    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) {\n    var sum int64\n    for i := 0; i < b.N; i++ {\n        b.StopTimer()\n        s := createMatrix512(rows) // Create a new matrix during each loop iteration\n        b.StartTimer()\n        sum = calculateSum512(s)\n    }\n    res = 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 {\n    var keys []string\n    for k := range m {\n        keys = append(keys, k)\n    }\n    return 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) {\n    switch t := m.(type) {\n    default:\n        return nil, fmt.Errorf(\"unknown type: %T\", t)\n    case map[string]int:\n        var keys []any\n        for k := range t {\n            keys = append(keys, k)\n        }\n        return keys, nil\n    case map[int]string:\n        // Copy the extraction logic\n    }\n}\n

    We can start noticing a couple of issues:

    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 {\n  var keys []K\n  for k := range m {\n    keys = append(keys, k)\n  }\n  return 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\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\n   String() string\n}\n

    Using this constraint will restrict type arguments to custom types like this one:

    type customInt int\n\nfunc (i customInt) String() string {\n   return 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\n   Val  T\n   next *Node[T]\n}\n\nfunc (n *Node[T]) Add(next *Node[T]) { // Instantiate type receiver\n   n.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 {}\n\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
    type sliceFn[T any] struct { // Use type parameter\n   s       []T\n   compare func(T, T) bool // Compare two T elements\n}\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?

    func foo[T io.Writer](w T) {\n   b := getBytes()\n   _, _ = w.Write(b)\n}\n
    "},{"location":"9-generics/#conclusion","title":"Conclusion","text":"

    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":"92-false-sharing/","title":"Writing concurrent code that leads to false sharing","text":"

    In previous sections, we have discussed the fundamental concepts of CPU caching. We have seen that some specific caches (typically, L1 and L2) aren\u2019t shared among all the logical cores but are specific to a physical core. This specificity has some concrete impacts such as concurrency and the concept of false sharing, which can lead to a significant performance decrease. Let\u2019s look at what false sharing is via an example and then see how to prevent it.

    In this example, we use two structs, Input and Result:

    type Input struct {\n    a int64\n    b int64\n}\n\ntype Result struct {\n    sumA int64\n    sumB int64\n}\n

    The goal is to implement a count function that receives a slice of Input and computes the following:

    For the sake of the example, we implement a concurrent solution with one goroutine that computes sumA and another that computes sumB:

    func count(inputs []Input) Result {\n    wg := sync.WaitGroup{}\n    wg.Add(2)\n\n    result := Result{} // Init the result struct\n\n    go func() {\n        for i := 0; i < len(inputs); i++ {\n            result.sumA += inputs[i].a // Computes sumA\n        }\n        wg.Done()\n    }()\n\n    go func() {\n        for i := 0; i < len(inputs); i++ {\n            result.sumB += inputs[i].b // Computes sumB\n        }\n        wg.Done()\n    }()\n\n    wg.Wait()\n    return result\n}\n

    We spin up two goroutines: one that iterates over each a field and another that iterates over each b field. This example is fine from a concurrency perspective. For instance, it doesn\u2019t lead to a data race, because each goroutine increments its own variable. But this example illustrates the false sharing concept that degrades expected performance.

    Let\u2019s look at the main memory. Because sumA and sumB are allocated contiguously, in most cases (seven out of eight), both variables are allocated to the same memory block:

    In this example, sumA and sumB are part of the same memory block.

    Now, let\u2019s assume that the machine contains two cores. In most cases, we should eventually have two threads scheduled on different cores. So if the CPU decides to copy this memory block to a cache line, it is copied twice:

    Each block is copied to a cache line on both code 0 and core 1.

    Both cache lines are replicated because L1D (L1 data) is per core. Recall that in our example, each goroutine updates its own variable: sumA on one side, and sumB on the other side:

    Each goroutine updates its own variable.

    Because these cache lines are replicated, one of the goals of the CPU is to guarantee cache coherency. For example, if one goroutine updates sumA and another reads sumA (after some synchronization), we expect our application to get the latest value.

    However, our example doesn\u2019t do exactly this. Both goroutines access their own variables, not a shared one. We might expect the CPU to know about this and understand that it isn\u2019t a conflict, but this isn\u2019t the case. When we write a variable that\u2019s in a cache, the granularity tracked by the CPU isn\u2019t the variable: it\u2019s the cache line.

    When a cache line is shared across multiple cores and at least one goroutine is a writer, the entire cache line is invalidated. This happens even if the updates are logically independent (for example, sumA and sumB). This is the problem of false sharing, and it degrades performance.

    Note

    Internally, a CPU uses the MESI protocol to guarantee cache coherency. It tracks each cache line, marking it modified, exclusive, shared, or invalid (MESI).

    One of the most important aspects to understand about memory and caching is that sharing memory across cores isn\u2019t real\u2014it\u2019s an illusion. This understanding comes from the fact that we don\u2019t consider a machine a black box; instead, we try to have mechanical sympathy with underlying levels.

    So how do we solve false sharing? There are two main solutions.

    The first solution is to use the same approach we\u2019ve shown but ensure that sumA and sumB aren\u2019t part of the same cache line. For example, we can update the Result struct to add padding between the fields. Padding is a technique to allocate extra memory. Because an int64 requires an 8-byte allocation and a cache line 64 bytes long, we need 64 \u2013 8 = 56 bytes of padding:

    type Result struct {\n    sumA int64\n    _    [56]byte // Padding\n    sumB int64\n}\n

    The next figure shows a possible memory allocation. Using padding, sumA and sumB will always be part of different memory blocks and hence different cache lines.

    sumA and sumB are part of different memory blocks.

    If we benchmark both solutions (with and without padding), we see that the padding solution is significantly faster (about 40% on my machine). This is an important improvement that results from the addition of padding between the two fields to prevent false sharing.

    The second solution is to rework the structure of the algorithm. For example, instead of having both goroutines share the same struct, we can make them communicate their local result via channels. The result benchmark is roughly the same as with padding.

    In summary, we must remember that sharing memory across goroutines is an illusion at the lowest memory levels. False sharing occurs when a cache line is shared across two cores when at least one goroutine is a writer. If we need to optimize an application that relies on concurrency, we should check whether false sharing applies, because this pattern is known to degrade application performance. We can prevent false sharing with either padding or communication.

    "},{"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:

    Profiling 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.

    "},{"location":"98-profiling-execution-tracing/#enabling-pprof","title":"Enabling pprof","text":"

    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\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"net/http\"\n    _ \"net/http/pprof\" // Blank import to pprof\n)\n\nfunc main() {\n    // Exposes an HTTP endpoint\n    http.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n        fmt.Fprintf(w, \"\")\n    })\n    log.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.

    "},{"location":"98-profiling-execution-tracing/#cpu-profiling","title":"CPU Profiling","text":"

    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.

    Note

    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.

    Note

    We 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:

    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.

    Note

    We 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.

    "},{"location":"98-profiling-execution-tracing/#heap-profiling","title":"Heap Profiling","text":"

    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:

    Another very helpful capability with heap profiling is tracking memory leaks. With a GC-based language, the usual procedure is the following:

    1. Trigger a GC.
    2. Download heap data.
    3. Wait for a few seconds/minutes.
    4. Trigger another GC.
    5. Download another heap data.
    6. Compare.

    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:

    1. Go to /debug/pprof/heap?gc=1 (trigger the GC and download the heap profile).
    2. Wait for a few seconds/minutes.
    3. Go to /debug/pprof/heap?gc=1 again.
    4. Use go tool to compare both heap profiles:
    $ 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.

    "},{"location":"98-profiling-execution-tracing/#goroutine-profiling","title":"Goroutine Profiling","text":"

    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.

    Note

    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:

    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() {\n    v = fibonacci(10)\n})\nfibTask.End()\n\n// Creates a store task\nctx, fibStore := trace.NewTask(ctx, \"store\")\ntrace.WithRegion(ctx, \"main\", func() {\n    atomic.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?

    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.

    "},{"location":"book/","title":"100 Go Mistakes and How to Avoid Them","text":""},{"location":"book/#description","title":"Description","text":"

    If you're a Go developer looking to improve your skills, the 100 Go Mistakes and How to Avoid Them book is for you. With a focus on practical examples, this book 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 or the first chapter.

    "},{"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":"

    Covers (English, Japanese, Chinese, and Korean)"},{"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":"
    1. 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

    2. J. Metcalfe, \u201cLearning from Errors,\u201d Annual Review of Psychology, vol. 68, pp. 465\u2013489, Jan. 2017.\u00a0\u21a9

    3. 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

    "},{"location":"external/","title":"External Resources","text":""},{"location":"external/#english","title":"English","text":""},{"location":"external/#the-best-golang-book-prime-reacts","title":"The Best Golang Book | Prime Reacts","text":""},{"location":"external/#book-review-100-go-mistakes-and-how-to-avoid-them","title":"Book Review: 100 Go Mistakes (And How to Avoid Them)","text":"

    Post

    "},{"location":"external/#the-most-useful-book-for-a-go-programmer","title":"The Most Useful Book for a Go Programmer?","text":""},{"location":"external/#how-to-make-mistakes-in-go-go-time-190","title":"How to make mistakes in Go - Go Time #190","text":""},{"location":"external/#go-is-amazing","title":"Go is AMAZING","text":""},{"location":"external/#8lu-100-test-coverage","title":"8LU - 100% Test Coverage","text":""},{"location":"external/#some-tips-i-learned-from-100-mistakes-in-go","title":"Some Tips I learned from 100 Mistakes in Go","text":"

    Post

    "},{"location":"external/#what-can-be-summarized-from-100-go-mistakes","title":"What can be summarized from 100 Go Mistakes?","text":"

    Post

    "},{"location":"external/#book-review-100-go-mistakes-and-how-to-avoid-them_1","title":"Book review: 100 Go Mistakes and How to Avoid Them","text":"

    Post

    "},{"location":"external/#chinese","title":"Chinese","text":""},{"location":"external/#100-go-mistakes-and-how-to-avoid-them","title":"\u6df1\u5ea6\u9605\u8bfb\u4e4b\u300a100 Go Mistakes and How to Avoid Them","text":"

    Post

    "},{"location":"external/#100-go-mistakes","title":"100 Go Mistakes \u968f\u8bb0","text":"

    Post

    "},{"location":"external/#go","title":"\u6211\u4e3a\u4ec0\u4e48\u653e\u5f03Go\u8bed\u8a00\uff1f","text":"

    Post

    "},{"location":"external/#japanese","title":"Japanese","text":""},{"location":"external/#go100-go-mistakes-and-how-to-avoid-them","title":"\u6700\u8fd1\u8aad\u3093\u3060Go\u8a00\u8a9e\u306e\u672c\u306e\u7d39\u4ecb\uff1a100 Go Mistakes and How to Avoid Them","text":"

    Post

    "},{"location":"external/#100-go-mistakes-and-how-to-avoid-them_1","title":"\u300e100 Go Mistakes and How to Avoid Them\u300f\u3092\u8aad\u3080","text":"

    Post

    "},{"location":"external/#portuguese","title":"Portuguese","text":""},{"location":"external/#um-otimo-livro-para-programadores-go","title":"Um \u00d3TIMO livro para programadores Go","text":""},{"location":"ja/","title":"Go\u8a00\u8a9e\u3067\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044","text":"

    \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

    Jobs

    Is your company hiring? Sponsor the Japanese version of this repository and let a significant audience of Go developers (~1k unique visitors per week) know about your opportunities in this section.

    \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 foo() {\n    // ...\n    return 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    // ...\n    return true\n}\n// ...\n
    if s != \"\" {\n    // ...\n} else {\n    return 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 == \"\" {\n    return 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\u30b9\u30c6\u30fc\u30c8\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\u30b9\u30c6\u30fc\u30c8\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\u7279\u5b9a\u306e\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\u7d04

    Go\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\u5316\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\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\u60f3\u5916\u306e\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 {\n    Bar // \u57cb\u3081\u8fbc\u307f\u30d5\u30a3\u30fc\u30eb\u30c9\n}\n\ntype Bar struct {\n    Baz 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

    \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\u7d04

    API \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 options struct {\n  port *int\n}\n\ntype Option func(options *options) error\n\nfunc WithPort(port int) Option {\n  return func(options *options) error {\n    if port < 0 {\n    return errors.New(\"port should be positive\")\n  }\n  options.port = &port\n  return nil\n  }\n}\n\nfunc NewServer(addr string, opts ...Option) ( *http.Server, error) {\n  var options options\n  for _, opt := range opts { \n    err := opt(&options) \n    if err != nil {\n      return nil, err\n    }\n  }\n\n// \u3053\u306e\u6bb5\u968e\u3067\u3001options \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\n  var port int\n  if options.port == nil {\n    port = defaultHTTPPort\n  } else {\n      if *options.port == 0 {\n      port = randomPort()\n    } else {\n      port = *options.port\n    }\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\u8db3

    Go \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\u3092 2023 \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\u5229\u7528\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\u3046 2 \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\u305f\u3060\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\u3068\u3044\u3046\u3053\u3068\u306f\u7406\u89e3\u3057\u3001\u899a\u3048\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002

    \u30ea\u30f3\u30bf\u30fc\u306e\u307b\u304b\u306b\u3001\u30b3\u30fc\u30c9\u30b9\u30bf\u30a4\u30eb\u3092\u4fee\u6b63\u3059\u308b\u305f\u3081\u306b\u30b3\u30fc\u30c9\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u30fc\u3082\u4f7f\u7528\u3057\u307e\u3057\u3087\u3046\u3002\u4ee5\u4e0b\u306b\u3001\u3044\u304f\u3064\u304b\u306e\u30b3\u30fc\u30c9\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u30fc\u3092\u793a\u3057\u307e\u3059\u3002

    \u307b\u304b\u306b golangci-lint (https://github.com/golangci/golangci-lint) \u3068\u3044\u3046\u3082\u306e\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u591a\u304f\u306e\u4fbf\u5229\u306a\u30ea\u30f3\u30bf\u30fc\u3084\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u30fc\u306e\u4e0a\u306b\u30d5\u30a1\u30b5\u30fc\u30c9\u3092\u63d0\u4f9b\u3059\u308b\u30ea\u30f3\u30c6\u30a3\u30f3\u30b0\u30c4\u30fc\u30eb\u3067\u3059\u3002\u307e\u305f\u3001\u30ea\u30f3\u30bf\u30fc\u3092\u4e26\u5217\u5b9f\u884c\u3057\u3066\u5206\u6790\u901f\u5ea6\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u975e\u5e38\u306b\u4fbf\u5229\u3067\u3059\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\u3044\u3066\u3057\u307e\u3046 (#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\u304c 8 \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\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

    \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\u3092\u7121\u8996\u3057\u3066\u3044\u308b (#18)","text":"\u8981\u7d04

    Go\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\u9664\u3044\u305f\u5834\u5408\uff09 float32 \u3068 float64 \u3068\u3044\u3046 2 \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\u60f3\u5916\u306e\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\u7d04

    Go \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\u3002

    \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\u7d04

    encoding/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

    \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\u60f3\u5916\u306e\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\u30de\u30c3\u30d7\u3067\u3082\u540c\u3058\u3067\u3059\u3002\u30de\u30c3\u30d7\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\u306e\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\u304c 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\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/#append-25","title":"append \u306e\u4f7f\u7528\u306b\u3088\u308b\u4e88\u60f3\u5916\u306e\u526f\u4f5c\u7528 (#25)","text":"\u8981\u7d04

    2\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\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\u306e\u30b3\u30d4\u30fc\u3060\u3051\u3067\u3059\u3002

    \u30b9\u30e9\u30a4\u30b9\u3092\u4f7f\u7528\u3059\u308b\u3068\u304d\u306f\u3001\u4e88\u60f3\u5916\u306e\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\u306e\u30b3\u30d4\u30fc\u307e\u305f\u306f\u5b8c\u5168\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

    \u88dc\u8db3

    s[low:high:max]\uff08\u5b8c\u5168\u30b9\u30e9\u30a4\u30b9\u5f0f\uff09\u2015\u2015\u3053\u306e\u547d\u4ee4\u6587\u306f\u3001\u5bb9\u91cf\u304c max - low \u306b\u7b49\u3057\u3044\u3053\u3068\u3092\u9664\u3051\u3070\u3001s[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

    \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\u306e\u30b3\u30d4\u30fc\u3092\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","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\u30de\u30c3\u30d7\u306e\u521d\u671f\u5316 (#27)","text":"\u8981\u7d04

    \u30de\u30c3\u30d7\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

    \u30de\u30c3\u30d7\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\u30de\u30c3\u30d7\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

    \u30de\u30c3\u30d7\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\u30de\u30c3\u30d7\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":"\u30de\u30c3\u30d7\u3068\u30e1\u30e2\u30ea\u30ea\u30fc\u30af (#28)","text":"\u8981\u7d04

    \u30de\u30c3\u30d7\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\u30de\u30c3\u30d7\u3092\u5f37\u5236\u7684\u306b\u518d\u751f\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\u3002

    \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\u7d04

    Go\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\u507d\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

    \u88dc\u8db3

    ? \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\u30de\u30c3\u30d7\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\u578b\u306b\u52a0\u3048\u3066\u3001\u914d\u5217\u3001\u69cb\u9020\u4f53\u3001\u30b9\u30e9\u30a4\u30b9\u3001\u30de\u30c3\u30d7\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\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":"\u5236\u5fa1\u69cb\u9020","text":""},{"location":"ja/#range-30","title":"\u8981\u7d20\u304c range \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 {\n    a[2] = 10\n    if i == 2 {\n        fmt.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":"\u30de\u30c3\u30d7\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\u30de\u30c3\u30d7\u306e\u633f\u5165\uff09 (#33)","text":"\u8981\u7d04

    \u30de\u30c3\u30d7\u3092\u4f7f\u7528\u3059\u308b\u3068\u304d\u306b\u4e88\u6e2c\u53ef\u80fd\u306a\u51fa\u529b\u3092\u4fdd\u8a3c\u3059\u308b\u306b\u306f\u3001\u30de\u30c3\u30d7\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++ {\n    fmt.Printf(\"%d \", i)\n\n    switch i {\n    default:\n    case 2:\n        break\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:\n    for i := 0; i < 5; i++ {\n        fmt.Printf(\"%d \", i)\n\n        switch i {\n        default:\n        case 2:\n            break 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\u3067 defer \u3092\u4f7f\u7528\u3059\u308b (#35)","text":"\u8981\u7d04

    \u95a2\u6570\u5185\u306e\u30eb\u30fc\u30d7\u30ed\u30b8\u30c3\u30af\u306e\u62bd\u51fa\u306f\u3001\u5404\u53cd\u5fa9\u306e\u6700\u5f8c\u3067\u306e defer \u6587\u306e\u5b9f\u884c\u306b\u3064\u306a\u304c\u308a\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 {\n    for path := range ch {\n        file, err := os.Open(path)\n        if err != nil {\n            return err\n        }\n\n        defer file.Close()\n\n        // \u30d5\u30a1\u30a4\u30eb\u306e\u51e6\u7406\u3092\u3059\u308b\n    }\n    return 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 {\n    for path := range ch {\n        if err := readFile(path); err != nil {\n            return err\n        }\n    }\n    return nil\n}\n\nfunc readFile(path string) error {\n    file, err := os.Open(path)\n    if err != nil {\n        return err\n    }\n\n    defer file.Close()\n\n    // \u30d5\u30a1\u30a4\u30eb\u306e\u51e6\u7406\u3092\u3059\u308b\n    return 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

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#37","title":"\u6587\u5b57\u5217\u306b\u5bfe\u3059\u308b\u4e0d\u6b63\u306a\u53cd\u5fa9\u51e6\u7406 (#37)","text":"\u8981\u7d04

    range \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\u51e6\u7406\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 {\n    fmt.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\u308b 3 \u70b9\u3092\u53d6\u308a\u4e0a\u3052\u307e\u3057\u3087\u3046\u3002

    \u7d50\u679c\u306e\u6700\u5f8c\u304b\u3089\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002len \u306f\u30eb\u30fc\u30f3\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 {\n    fmt.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 {\n    fmt.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\u7d04

    strings.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 {\n    s := \"\"\n    for _, value := range values {\n        s += value\n    }\n    return 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 {\n    sb := strings.Builder{}\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return 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

    \u88dc\u8db3

    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 {\n    total := 0\n    for i := 0; i < len(values); i++ {\n        total += len(values[i])\n    }\n\n    sb := strings.Builder{}\n    sb.Grow(total) (2)\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return sb.String()\n}\n

    \u30d9\u30f3\u30c1\u30de\u30fc\u30af\u3092\u5b9f\u884c\u3057\u3066 3 \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\u7d04

    bytes \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\u30b3\u30b9\u30c8\u3092\u56de\u907f\u3067\u304d\u308b\u304b\u3069\u3046\u304b\u3092\u6700\u521d\u306b\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\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\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\u3063\u3066\u3044\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\n\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

    \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) {\n    b = a\n    return\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\u60f3\u5916\u306e\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) (\n    lat, lng float32, err error) {\n    isValid := l.validateAddress(address) (1)\n    if !isValid {\n        return 0, 0, errors.New(\"invalid address\")\n    }\n\n    if ctx.Err() != nil { (2)\n        return 0, 0, err\n    }\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\u308b\u3079\u304d\u3067\u3059\uff08 os.Open \u306a\u3069\u306e\u7279\u5b9a\u306e\u95a2\u6570\u3092\u9664\u304f\uff09\u3002\u8907\u6570\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u306b\u306b\u306a\u308b\u304b\u3082\u3057\u308c\u305a\u3001\u5358\u4f53\u30c6\u30b9\u30c8\u304c\u3088\u308a\u8907\u96d1\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u304b\u3089\u3067\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 (\n    StatusSuccess  = \"success\"\n    StatusErrorFoo = \"error_foo\"\n    StatusErrorBar = \"error_bar\"\n)\n\nfunc f() error {\n    var status string\n    defer notify(status)\n    defer incrementCounter(status)\n\n    if err := foo(); err != nil {\n        status = StatusErrorFoo\n        return err\n    }\n\n    if err := bar(); err != nil {\n        status = StatusErrorBar\n        return err\n    }\n\n    status = StatusSuccess\n    return 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 {\n    var status string\n    defer notify(&status) \n    defer incrementCounter(&status)\n\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 {\n    var status string\n    defer func() {\n        notify(status)\n        incrementCounter(status)\n    }()\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\u7d04

    panic \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() {\n    fmt.Println(\"a\")\n    panic(\"foo\")\n    fmt.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\u751f\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\u306a\u6bd4\u8f03 (#50)","text":"\u8981\u7d04

    Go 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\u306a\u6bd4\u8f03 (#51)","text":"\u8981\u7d04

    Go 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\"\n\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

    \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  // ...\n  notify() // \u30a8\u30e9\u30fc\u51e6\u7406\u306f\u7701\u7565\u3055\u308c\u3066\u3044\u307e\u3059\n}\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\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\u3002

    \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\u30b9\u30c6\u30fc\u30c8\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\u30eb\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

    "},{"location":"ja/#go-58","title":"\u7af6\u5408\u554f\u984c\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044\uff08\u30c7\u30fc\u30bf\u7af6\u5408\u3068\u7af6\u5408\u72b6\u614b\u3001\u305d\u3057\u3066Go\u8a00\u8a9e\u306e\u30e1\u30e2\u30ea\u30e2\u30c7\u30eb\uff09 (#58)","text":"\u8981\u7d04

    \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

    "},{"location":"ja/#_11","title":"\u7af6\u5408\u72b6\u614b","text":"

    \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\u60f3\u5916\u306e\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\u3002I/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

    \u88dc\u8db3

    \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 3 \u3064\u76ee\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\u7d04

    Go 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

    \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

    "},{"location":"ja/#context-value","title":"Context Value","text":"

    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\u30b5\u30d6\u95a2\u6570\u306e\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

    \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/#_14","title":"\u4e26\u884c\u51e6\u7406\uff1a\u5b9f\u8df5","text":""},{"location":"ja/#context-61","title":"\u4e0d\u9069\u5207\u306a Context \u3092\u5e83\u3081\u3066\u3057\u307e\u3046 (#61)","text":"\u8981\u7d04

    Context \u3092\u4f1d\u64ad\u3059\u308b\u969b\u306b\u306f\u3001Context \u3092\u30ad\u30e3\u30f3\u30bb\u30eb\u3067\u304d\u308b\u6761\u4ef6\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u9001\u4fe1\u3055\u308c\u305f\u969b\u306b HTTP \u30cf\u30f3\u30c9\u30e9\u304c Context \u3092\u30ad\u30e3\u30f3\u30bb\u30eb\u3059\u308b\u3068\u304d\u306a\u3069\u3067\u3059\u3002

    \u591a\u304f\u306e\u72b6\u6cc1\u3067\u306f\u3001Go Context \u3092\u4f1d\u64ad\u3059\u308b\u3053\u3068\u304c\u63a8\u5968\u3055\u308c\u307e\u3059\u3002\u305f\u3060\u3057\u3001Context \u306e\u4f1d\u64ad\u306b\u3088\u3063\u3066\u8efd\u5fae\u306a\u30d0\u30b0\u304c\u767a\u751f\u3057\u3001\u30b5\u30d6\u95a2\u6570\u304c\u6b63\u3057\u304f\u5b9f\u884c\u3055\u308c\u306a\u304f\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

    \u6b21\u306e\u4f8b\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u3044\u304f\u3064\u304b\u306e\u30bf\u30b9\u30af\u3092\u5b9f\u884c\u3057\u3066\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059 HTTP \u30cf\u30f3\u30c9\u30e9\u3092\u516c\u958b\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\u76f4\u524d\u306b\u3001\u305d\u308c\u3092 Kafka \u30c8\u30d4\u30c3\u30af\u306b\u9001\u4fe1\u3057\u305f\u3044\u3068\u601d\u3063\u3066\u3044\u307e\u3059\u3002HTTP \u30b3\u30f3\u30b7\u30e5\u30fc\u30de\u306b\u30ec\u30a4\u30c6\u30f3\u30b7\u306e\u70b9\u3067\u30da\u30ca\u30eb\u30c6\u30a3\u3092\u8ab2\u3057\u305f\u304f\u306a\u3044\u306e\u3067\u3001publish \u30a2\u30af\u30b7\u30e7\u30f3\u3092\u65b0\u3057\u3044\u30b4\u30eb\u30fc\u30c1\u30f3\u5185\u3067\u975e\u540c\u671f\u306b\u51e6\u7406\u3057\u305f\u3044\u3068\u8003\u3048\u3066\u3044\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001Context \u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u305f\u5834\u5408\u306b\u30e1\u30c3\u30bb\u30fc\u30b8\u306e publish \u30a2\u30af\u30b7\u30e7\u30f3\u3092\u4e2d\u65ad\u3067\u304d\u308b\u3088\u3046\u306b\u3001Context \u3092\u53d7\u3051\u5165\u308c\u308b publish \u95a2\u6570\u3092\u81ea\u7531\u306b\u4f7f\u3048\u308b\u3068\u3057\u307e\u3059\u3002\u53ef\u80fd\u306a\u5b9f\u88c5\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002

    func handler(w http.ResponseWriter, r *http.Request) {\n    response, err := doSomeTask(r.Context(), r)\n    if err != nil {\n        http.Error(w, err.Error(), http.StatusInternalServerError)\n    return\n    }\n    go func() {\n        err := publish(r.Context(), response)\n        // err \u306e\u51e6\u7406\u3092\u3059\u308b\n    }()\n    writeResponse(response)\n}\n

    \u3053\u306e\u30b3\u30fc\u30c9\u306e\u4f55\u304c\u554f\u984c\u306a\u306e\u3067\u3057\u3087\u3046\u304b\u3002HTTP \u30ea\u30af\u30a8\u30b9\u30c8\u306b\u4ed8\u3055\u308c\u305f Context \u306f\u3001\u3055\u307e\u3056\u307e\u306a\u72b6\u6cc1\u3067\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u77e5\u3063\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

    \u6700\u521d\u306e 2 \u3064\u306e\u30b1\u30fc\u30b9\u3067\u306f\u3001\u51e6\u7406\u306f\u304a\u305d\u3089\u304f\u6b63\u3057\u304f\u884c\u308f\u308c\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001doSomeTask \u304b\u3089\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d7\u3051\u53d6\u3063\u305f\u3082\u306e\u306e\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u63a5\u7d9a\u3092\u9589\u3058\u305f\u5834\u5408\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u304c publish \u3055\u308c\u306a\u3044\u3088\u3046\u306b\u3001Context \u304c\u65e2\u306b\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u305f\u72b6\u614b\u3067 publish \u3092\u547c\u3073\u51fa\u3057\u3066\u3082\u554f\u984c\u306f\u304a\u305d\u3089\u304f\u8d77\u304d\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u6700\u5f8c\u306e\u30b1\u30fc\u30b9\u306f\u3069\u3046\u3067\u3057\u3087\u3046\u304b\u3002

    \u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u66f8\u304d\u8fbc\u307e\u308c\u308b\u3068\u3001\u8981\u6c42\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f Context \u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7af6\u5408\u72b6\u614b\u306b\u76f4\u9762\u3057\u307e\u3059\u3002

    \u5f8c\u8005\u306e\u5834\u5408\u3001HTTP \u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u3059\u3050\u306b\u8fd4\u3059\u306e\u3067\u3001publish \u3092\u547c\u3073\u51fa\u3059\u3068\u30a8\u30e9\u30fc\u304c\u8fd4\u3055\u308c\u307e\u3059\u3002

    \u88dc\u8db3

    Go 1.21 \u304b\u3089\u306f\u3001\u30ad\u30e3\u30f3\u30bb\u30eb\u305b\u305a\u306b\u65b0\u3057\u3044 Context \u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3057\u305f\u3002 context.WithoutCancel \u306f\u3001\u89aa\u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u305f\u3068\u304d\u306b\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u3066\u3044\u306a\u3044\u89aa\u306e\u30b3\u30d4\u30fc\u3092\u8fd4\u3057\u307e\u3059\u3002

    \u307e\u3068\u3081\u308b\u3068\u3001Context \u306e\u4f1d\u64ad\u306f\u614e\u91cd\u306b\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#62","title":"\u505c\u6b62\u3059\u3079\u304d\u3068\u304d\u3092\u77e5\u3089\u305a\u306b\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u958b\u59cb\u3057\u3066\u3057\u307e\u3046 (#62)","text":"\u8981\u7d04

    \u30ea\u30fc\u30af\u3092\u907f\u3051\u308b\u3053\u3068\u306f\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u958b\u59cb\u3055\u308c\u308b\u305f\u3073\u306b\u3001\u6700\u7d42\u7684\u306b\u505c\u6b62\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

    \u30b4\u30eb\u30fc\u30c1\u30f3\u306f\u7c21\u5358\u306b\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u975e\u5e38\u306b\u7c21\u5358\u3067\u3042\u308b\u305f\u3081\u3001\u65b0\u3057\u3044\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u3044\u3064\u505c\u6b62\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u306e\u8a08\u753b\u3092\u5fc5\u305a\u3057\u3082\u7acb\u3066\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u30ea\u30fc\u30af\u306b\u3064\u306a\u304c\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u3044\u3064\u505c\u6b62\u3059\u308c\u3070\u3088\u3044\u304b\u308f\u304b\u3089\u306a\u3044\u306e\u306f\u3001Go\u8a00\u8a9e\u3067\u3088\u304f\u3042\u308b\u8a2d\u8a08\u4e0a\u306e\u554f\u984c\u3067\u3042\u308a\u3001\u4e26\u884c\u51e6\u7406\u306b\u304a\u3051\u308b\u30df\u30b9\u3067\u3059\u3002

    \u5177\u4f53\u7684\u306a\u4f8b\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u307e\u3057\u3087\u3046\u3002\u5916\u90e8\u8a2d\u5b9a\uff08\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u306a\u3069\u3092\u4f7f\u7528\u3057\u305f\u3082\u306e\u306a\u3069\uff09\u3092\u76e3\u8996\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u8a2d\u8a08\u3057\u307e\u3059\u3002\u307e\u305a\u3001\u6b21\u306e\u3088\u3046\u306a\u5b9f\u88c5\u3092\u3057\u3066\u307f\u307e\u3059\u3002

    func main() {\n    newWatcher()\n    // \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\n}\n\ntype watcher struct { /* \u3044\u304f\u3064\u304b\u306e\u30ea\u30bd\u30fc\u30b9 */ }\n\nfunc newWatcher() {\n    w := watcher{}\n    go w.watch() // \u5916\u90e8\u8a2d\u5b9a\u3092\u76e3\u8996\u3059\u308b\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u4f5c\u6210\u3059\u308b\n}\n

    \u3053\u306e\u30b3\u30fc\u30c9\u306e\u554f\u984c\u306f\u3001\u30e1\u30a4\u30f3\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u7d42\u4e86\u3059\u308b\u3068\uff08\u304a\u305d\u3089\u304f OS \u30b7\u30b0\u30ca\u30eb\u307e\u305f\u306f\u6709\u9650\u306e\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u306e\u305f\u3081\uff09\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u505c\u6b62\u3057\u3066\u3057\u307e\u3046\u3053\u3068\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30a6\u30a9\u30c3\u30c1\u30e3\u30fc\u306b\u3088\u3063\u3066\u4f5c\u6210\u3055\u308c\u305f\u30ea\u30bd\u30fc\u30b9\u306f\u6b63\u5e38\u306b\u9589\u3058\u3089\u308c\u307e\u305b\u3093\u3002\u3053\u308c\u3092\u9632\u3050\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u3067\u3057\u3087\u3046\u304b\u3002

    1 \u3064\u306e\u65b9\u6cd5\u3068\u3057\u3066\u306f\u3001main \u304c\u623b\u3063\u305f\u3068\u304d\u306b\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u308b Context \u3092 newWatcher \u306b\u6e21\u3059\u3053\u3068\u304c\u6319\u3052\u3089\u308c\u307e\u3059\u3002

    func main() {\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n    newWatcher(ctx)\n    // \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\n}\n\nfunc newWatcher(ctx context.Context) {\n    w := watcher{}\n    go w.watch(ctx)\n}\n

    \u4f5c\u6210\u3057\u305f Context \u3092 watch \u30e1\u30bd\u30c3\u30c9\u306b\u4f1d\u64ad\u3057\u307e\u3059\u3002Context \u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u308b\u3068\u3001\u30a6\u30a9\u30c3\u30c1\u30e3\u30fc\u69cb\u9020\u4f53\u306f\u305d\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u9589\u3058\u307e\u3059\u3002\u3057\u304b\u3057\u3001watch \u304c\u305d\u308c\u3092\u884c\u3046\u6642\u9593\u304c\u78ba\u5b9f\u306b\u3042\u308b\u3068\u306f\u3044\u3048\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u8a2d\u8a08\u4e0a\u306e\u6b20\u9665\u3067\u3059\u3002

    \u554f\u984c\u306f\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u505c\u6b62\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u3092\u4f1d\u3048\u308b\u305f\u3081\u306b\u30b7\u30b0\u30ca\u30eb\u3092\u4f7f\u7528\u3057\u305f\u3053\u3068\u3067\u3059\u3002\u30ea\u30bd\u30fc\u30b9\u304c\u9589\u3058\u3089\u308c\u308b\u307e\u3067\u3001\u89aa\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u30d6\u30ed\u30c3\u30af\u3057\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u305d\u3046\u306a\u3089\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002

    func main() {\n    w := newWatcher()\n    defer w.close()\n    // \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\n}\n\nfunc newWatcher() watcher {\n    w := watcher{}\n    go w.watch()\n    return w\n}\n\nfunc (w watcher) close() {\n    // \u30ea\u30bd\u30fc\u30b9\u3092\u9589\u3058\u308b\n}\n

    \u30ea\u30bd\u30fc\u30b9\u3092\u9589\u3058\u308b\u6642\u9593\u306b\u306a\u3063\u305f\u3053\u3068\u3092 watcher \u306b\u901a\u77e5\u3059\u308b\u4ee3\u308f\u308a\u306b\u3001 defer \u3092\u4f7f\u7528\u3057\u3066\u3053\u306e\u3000close \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u7d42\u4e86\u3059\u308b\u524d\u306b\u30ea\u30bd\u30fc\u30b9\u304c\u78ba\u5b9f\u306b\u9589\u3058\u3089\u308c\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002

    \u307e\u3068\u3081\u308b\u3068\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u306f\u4ed6\u306e\u30ea\u30bd\u30fc\u30b9\u3068\u540c\u69d8\u3001\u30e1\u30e2\u30ea\u3084\u4ed6\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u89e3\u653e\u3059\u308b\u305f\u3081\u306b\u6700\u7d42\u7684\u306b\u9589\u3058\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u3044\u3064\u505c\u6b62\u3059\u308b\u304b\u3092\u77e5\u3089\u305a\u306b\u958b\u59cb\u3059\u308b\u306e\u306f\u8a2d\u8a08\u4e0a\u306e\u554f\u984c\u3067\u3059\u3002\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u958b\u59cb\u3055\u308c\u308b\u3068\u304d\u306f\u5e38\u306b\u3001\u3044\u3064\u505c\u6b62\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u660e\u78ba\u306a\u8a08\u753b\u3092\u7acb\u3066\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6700\u5f8c\u306b\u306a\u308a\u307e\u3057\u305f\u304c\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u30ea\u30bd\u30fc\u30b9\u3092\u4f5c\u6210\u3057\u3001\u305d\u306e\u6709\u52b9\u671f\u9593\u304c\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u5b58\u7d9a\u671f\u9593\u306b\u30d0\u30a4\u30f3\u30c9\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u7d42\u4e86\u3059\u308b\u524d\u306b\u305d\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u5b8c\u4e86\u3059\u308b\u306e\u3092\u5f85\u3063\u305f\u65b9\u304c\u304a\u305d\u3089\u304f\u78ba\u5b9f\u3067\u3059\u3002\u305d\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u30ea\u30bd\u30fc\u30b9\u3092\u9593\u9055\u3044\u306a\u304f\u89e3\u653e\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#63","title":"\u30b4\u30eb\u30fc\u30c1\u30f3\u3068\u30eb\u30fc\u30d7\u5909\u6570\u306b\u6ce8\u610f\u3057\u306a\u3044 (#63)","text":"\u6ce8\u610f

    \u3053\u306e\u30df\u30b9\u306f Go\u30001.22 \u304b\u3089\u306f\u6c17\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\uff08\u8a73\u7d30\uff09\u3002

    "},{"location":"ja/#select-64","title":"select \u3068\u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3057\u3066\u6c7a\u5b9a\u7684\u52d5\u4f5c\u3092\u671f\u5f85\u3059\u308b (#64)","text":"\u8981\u7d04

    \u8907\u6570\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u53ef\u80fd\u306a\u5834\u5408\u3001\u8907\u6570\u306e\u30c1\u30e3\u30cd\u30eb\u3067 select \u3059\u308b\u3068\u30b1\u30fc\u30b9\u304c\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u3092\u7406\u89e3\u3059\u308b\u3068\u3001\u4e26\u884c\u51e6\u7406\u306b\u304a\u3051\u308b\u8efd\u5fae\u306a\u30d0\u30b0\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u306e\u3042\u308b\u8aa4\u3063\u305f\u4eee\u5b9a\u3092\u7acb\u3066\u308b\u3053\u3068\u304c\u306a\u304f\u306a\u308a\u307e\u3059\u3002

    Go \u958b\u767a\u8005\u304c\u30c1\u30e3\u30cd\u30eb\u3092\u64cd\u4f5c\u3059\u308b\u3068\u304d\u306b\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u306e 1 \u3064\u306f\u3001select \u304c\u8907\u6570\u306e\u30c1\u30e3\u30cd\u30eb\u3067\u3069\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u8aa4\u3063\u305f\u7406\u89e3\u3092\u3059\u308b\u3053\u3068\u3067\u3059\u3002

    \u305f\u3068\u3048\u3070\u3001\u6b21\u306e\u5834\u5408\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff08 disconnectCh \u306f\u30d0\u30c3\u30d5\u30a1\u306a\u3057\u30c1\u30e3\u30cd\u30eb\u3067\u3059\uff09\u3002

    go func() {\n  for i := 0; i < 10; i++ {\n      messageCh <- i\n    }\n    disconnectCh <- struct{}{}\n}()\n\nfor {\n    select {\n    case v := <-messageCh:\n        fmt.Println(v)\n    case <-disconnectCh:\n        fmt.Println(\"disconnection, return\")\n        return\n    }\n}\n

    \u3053\u306e\u4f8b\u3092\u8907\u6570\u56de\u5b9f\u884c\u3057\u305f\u5834\u5408\u3001\u7d50\u679c\u306f\u30e9\u30f3\u30c0\u30e0\u306b\u306a\u308a\u307e\u3059\u3002

    0\n1\n2\ndisconnection, return\n\n0\ndisconnection, return\n

    \u3069\u3046\u3044\u3046\u308f\u3051\u304b 10 \u901a\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u6d88\u8cbb\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u305d\u306e\u3046\u3061\u306e\u6570\u901a\u3060\u3051\u3092\u53d7\u4fe1\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u306f\u3001\u8907\u6570\u306e\u30c1\u30e3\u30cd\u30eb\u3068\u4f75\u7528\u3057\u305f\u5834\u5408\u306e select \u6587\u306e\u4ed5\u69d8\u306b\u3088\u308b\u3082\u306e\u3067\u3059\uff08https:// go.dev/ref/spec\uff09\u3002

    Quote

    1 \u3064\u4ee5\u4e0a\u306e\u901a\u4fe1\u3092\u7d9a\u884c\u3067\u304d\u308b\u5834\u5408\u3001\u5747\u4e00\u306e\u64ec\u4f3c\u30e9\u30f3\u30c0\u30e0\u9078\u629e\u306b\u3088\u3063\u3066\u3001\u7d9a\u884c\u3067\u304d\u308b 1 \u3064\u306e\u901a\u4fe1\u304c\u9078\u629e\u3055\u308c\u307e\u3059\u3002

    \u6700\u521d\u306b\u4e00\u81f4\u3057\u305f\u30b1\u30fc\u30b9\u304c\u512a\u5148\u3055\u308c\u308b switch \u6587\u3068\u306f\u7570\u306a\u308a\u3001select \u6587\u306f\u8907\u6570\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u53ef\u80fd\u306a\u5834\u5408\u306b\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u629e\u3057\u307e\u3059\u3002

    \u3053\u306e\u52d5\u4f5c\u306f\u6700\u521d\u306f\u5947\u5999\u306b\u601d\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u3053\u308c\u306f\u30b9\u30bf\u30d9\u30fc\u30b7\u30e7\u30f3\u3092\u9632\u3050\u3068\u3044\u3046\u7406\u7531\u304c\u3042\u3063\u3066\u306e\u3053\u3068\u3067\u3059\u3002\u6700\u521d\u306b\u9078\u629e\u3055\u308c\u305f\u901a\u4fe1\u304c\u30bd\u30fc\u30b9\u306e\u9806\u5e8f\u306b\u57fa\u3065\u3044\u3066\u3044\u308b\u3068\u3057\u307e\u3059\u3002\u305d\u306e\u5834\u5408\u3001\u9001\u4fe1\u901f\u5ea6\u304c\u901f\u3044\u305f\u3081\u306b\u3001\u305f\u3068\u3048\u3070 1 \u3064\u306e\u30c1\u30e3\u30cd\u30eb\u304b\u3089\u3057\u304b\u53d7\u4fe1\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u72b6\u6cc1\u306b\u9665\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u9632\u3050\u305f\u3081\u306b\u3001Go\u8a00\u8a9e\u306e\u8a2d\u8a08\u8005\u306f\u30e9\u30f3\u30c0\u30e0\u9078\u629e\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002

    \u8907\u6570\u306e\u30c1\u30e3\u30cd\u30eb\u3067 select \u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u8907\u6570\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u3042\u308b\u306a\u3089\u3001\u30bd\u30fc\u30b9\u9806\u5e8f\u306e\u6700\u521d\u306e\u30b1\u30fc\u30b9\u304c\u81ea\u52d5\u7684\u306b\u512a\u5148\u3055\u308c\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee3\u308f\u308a\u306b\u3001Go\u8a00\u8a9e\u306f\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u629e\u3059\u308b\u305f\u3081\u3001\u3069\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u9078\u629e\u3055\u308c\u308b\u304b\u306f\u4fdd\u8a3c\u3055\u308c\u307e\u305b\u3093\u3002\u3053\u306e\u52d5\u4f5c\u3092\u514b\u670d\u3059\u308b\u306b\u306f\u3001\u5358\u4e00\u306e\u751f\u7523\u8005\u30b4\u30eb\u30fc\u30c1\u30f3\u306e\u5834\u5408\u3001\u30d0\u30c3\u30d5\u30a1\u306a\u3057\u306e\u30c1\u30e3\u30cd\u30eb\u307e\u305f\u306f\u5358\u4e00\u306e\u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#65","title":"\u901a\u77e5\u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#65)","text":"\u8981\u7d04

    chan struct{} \u578b\u3092\u4f7f\u7528\u3057\u3066\u901a\u77e5\u3092\u9001\u4fe1\u3057\u307e\u3057\u3087\u3046\u3002

    \u30c1\u30e3\u30cd\u30eb\u306f\u3001\u30b7\u30b0\u30ca\u30eb\u3092\u4ecb\u3057\u3066\u30b4\u30eb\u30fc\u30c1\u30f3\u9593\u3067\u901a\u4fe1\u3059\u308b\u305f\u3081\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u3067\u3059\u3002\u30b7\u30b0\u30ca\u30eb\u306b\u306f\u30c7\u30fc\u30bf\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u306f\u95a2\u4fc2\u3042\u308a\u307e\u305b\u3093\u3002

    \u5177\u4f53\u7684\u306a\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u901a\u4fe1\u306e\u5207\u65ad\u304c\u767a\u751f\u3059\u308b\u305f\u3073\u306b\u305d\u308c\u3092\u901a\u77e5\u3059\u308b\u30c1\u30e3\u30cd\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 1 \u3064\u306e\u65b9\u6cd5\u3068\u3057\u3066\u3001\u3053\u308c\u3092 chan bool \u3068\u3057\u3066\u6271\u3046\u3053\u3068\u304c\u6319\u3052\u3089\u308c\u307e\u3059\u3002

    disconnectCh := make(chan bool)\n

    \u3053\u3053\u3067\u3001\u305d\u306e\u3088\u3046\u306a\u30c1\u30e3\u30cd\u30eb\u3092\u63d0\u4f9b\u3059\u308b API \u3068\u5bfe\u8a71\u3059\u308b\u3068\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u771f\u507d\u5024\u306e\u30c1\u30e3\u30cd\u30eb\u3067\u3042\u308b\u305f\u3081\u3001true \u307e\u305f\u306f false \u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u53d7\u4fe1\u3067\u304d\u307e\u3059\u3002true \u304c\u4f55\u3092\u4f1d\u3048\u3066\u3044\u308b\u304b\u306f\u304a\u305d\u3089\u304f\u660e\u3089\u304b\u3067\u3057\u3087\u3046\u3002\u3057\u304b\u3057\u3001 false \u3068\u306f\u4f55\u3092\u610f\u5473\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002\u901a\u4fe1\u304c\u5207\u65ad\u3055\u308c\u3066\u3044\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3057\u3087\u3046\u304b\u3002\u305d\u306e\u5834\u5408\u3001\u3069\u308c\u304f\u3089\u3044\u306e\u983b\u5ea6\u3067\u305d\u306e\u3088\u3046\u306a\u30b7\u30b0\u30ca\u30eb\u3092\u53d7\u4fe1\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002\u3042\u308b\u3044\u306f\u518d\u63a5\u7d9a\u3057\u305f\u3068\u3044\u3046\u3053\u3068\u3067\u3057\u3087\u3046\u304b\u3002\u305d\u3082\u305d\u3082 false \u3092\u53d7\u3051\u53d6\u308b\u3053\u3068\u3092\u671f\u5f85\u3059\u3079\u304d\u306a\u306e\u3067\u3057\u3087\u3046\u304b\u3002\u304a\u305d\u3089\u304f true \u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u53d7\u3051\u53d6\u308b\u3053\u3068\u3060\u3051\u3092\u671f\u5f85\u3059\u3079\u304d\u3067\u3057\u3087\u3046\u3002

    \u305d\u306e\u5834\u5408\u3001\u60c5\u5831\u3092\u4f1d\u3048\u308b\u305f\u3081\u306b\u7279\u5b9a\u306e\u5024\u306f\u5fc5\u8981\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u30c7\u30fc\u30bf\u306e \u306a\u3044 \u30c1\u30e3\u30cd\u30eb\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u51e6\u7406\u3059\u308b\u6163\u7528\u7684\u306a\u65b9\u6cd5\u306f\u3001\u7a7a\u306e\u69cb\u9020\u4f53\u306e\u30c1\u30e3\u30cd\u30eb\u2015\u2015 chan struct{}\u2015\u2015\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3059\u3002

    "},{"location":"ja/#nil-66","title":"nil \u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#66)","text":"\u8981\u7d04

    nil \u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u305f\u3068\u3048\u3070 select \u6587\u304b\u3089\u30b1\u30fc\u30b9\u3092 \u524a\u9664 \u3067\u304d\u308b\u305f\u3081\u3001\u4e26\u884c\u51e6\u7406\u3092\u884c\u3046\u969b\u306e\u9053\u5177\u306e\u4e00\u3064\u3068\u3057\u3066\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u3079\u304d\u3067\u3059\u3002

    \u6b21\u306e\u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066\u4f55\u304c\u884c\u308f\u308c\u308b\u3067\u3057\u3087\u3046\u304b\u3002

    var ch chan int\n<-ch\n

    ch \u306f chan int \u578b\u3067\u3059\u3002\u30c1\u30e3\u30cd\u30eb\u306e\u30bc\u30ed\u5024\u306f nil \u3067\u3042\u308b\u306e\u3067\u3001 ch \u306f nil \u3067\u3059\u3002\u30b4\u30eb\u30fc\u30c1\u30f3\u306f panic \u3092\u8d77\u3053\u3057\u307e\u305b\u3093\u3002\u305f\u3060\u3057\u3001\u6c38\u4e45\u306b\u30d6\u30ed\u30c3\u30af\u3057\u307e\u3059\u3002

    nil \u30c1\u30e3\u30cd\u30eb\u306b\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u9001\u4fe1\u3059\u308b\u5834\u5408\u3082\u539f\u7406\u306f\u540c\u3058\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u306f\u6c38\u4e45\u306b\u30d6\u30ed\u30c3\u30af\u3057\u307e\u3059\u3002

    var ch chan int\nch <- 0\n

    \u3067\u306f\u3001Go\u8a00\u8a9e\u304c nil \u30c1\u30e3\u30cd\u30eb\u3068\u306e\u9593\u3067\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u9001\u53d7\u4fe1\u3092\u8a31\u53ef\u3059\u308b\u76ee\u7684\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\u3002\u305f\u3068\u3048\u3070\u30012 \u3064\u306e\u30c1\u30e3\u30cd\u30eb\u3092\u30de\u30fc\u30b8\u3059\u308b\u6163\u7528\u7684\u306a\u65b9\u6cd5\u3092\u5b9f\u88c5\u3059\u308b\u306e\u306b\u3001 nil \u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

    func merge(ch1, ch2 <-chan int) <-chan int {\n    ch := make(chan int, 1)\n\n    go func() {\n        for ch1 != nil || ch2 != nil { // \u6700\u4f4e\u3067\u3082\u4e00\u3064\u306e\u30c1\u30e3\u30cd\u30eb\u304c nil \u3067\u306a\u3051\u308c\u3070\u7d9a\u884c\u3059\u308b\n            select {\n            case v, open := <-ch1:\n                if !open {\n                    ch1 = nil // \u9589\u3058\u305f\u3089 ch1 \u3092 nil \u30c1\u30e3\u30cd\u30eb\u306b\u5272\u308a\u5f53\u3066\u308b\n                    break\n                }\n                ch <- v\n            case v, open := <-ch2:\n                if !open {\n                    ch2 = nil // \u9589\u3058\u305f\u3089 ch2 \u3092 nil \u30c1\u30e3\u30cd\u30eb\u306b\u5272\u308a\u5f53\u3066\u308b\n                    break\n                }\n                ch <- v\n            }\n        }\n        close(ch)\n    }()\n\n    return ch\n}\n

    \u3053\u306e\u6d17\u7df4\u3055\u308c\u305f\u89e3\u6c7a\u7b56\u306f\u3001nil \u30c1\u30e3\u30cd\u30eb\u3092\u5229\u7528\u3057\u3066\u3001\u4f55\u3089\u304b\u306e\u65b9\u6cd5\u3067 select \u6587\u304b\u3089 1 \u3064\u306e\u30b1\u30fc\u30b9\u3092 \u524a\u9664 \u3057\u307e\u3059\u3002

    nil \u30c1\u30e3\u30cd\u30eb\u306f\u72b6\u6cc1\u306b\u3088\u3063\u3066\u306f\u4fbf\u5229\u3067\u3042\u308a\u3001Go \u958b\u767a\u8005\u306f\u4e26\u884c\u51e6\u7406\u3092\u6271\u3046\u969b\u306b\u4f7f\u3044\u3053\u306a\u305b\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u304a\u304f\u3079\u304d\u3067\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#67","title":"\u30c1\u30e3\u30cd\u30eb\u306e\u5bb9\u91cf\u306b\u3064\u3044\u3066\u56f0\u60d1\u3057\u3066\u3044\u308b (#67)","text":"\u8981\u7d04

    \u554f\u984c\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306f\u3001\u4f7f\u7528\u3059\u308b\u30c1\u30e3\u30cd\u30eb\u306e\u578b\u3092\u614e\u91cd\u306b\u6c7a\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u540c\u671f\u3092\u5f37\u529b\u306b\u4fdd\u8a3c\u3057\u3066\u304f\u308c\u308b\u306e\u306f\u30d0\u30c3\u30d5\u30a1\u306a\u3057\u30c1\u30e3\u30cd\u30eb\u306e\u307f\u3067\u3059\u3002

    \u30d0\u30c3\u30d5\u30a1\u3042\u308a\u30c1\u30e3\u30cd\u30eb\u4ee5\u5916\u306e\u30c1\u30e3\u30cd\u30eb\u306e\u5bb9\u91cf\u3092\u6307\u5b9a\u3059\u308b\u306b\u306f\u6b63\u5f53\u306a\u7406\u7531\u304c\u3042\u308b\u3079\u304d\u3067\u3059\u3002

    "},{"location":"ja/#etcd-68","title":"\u6587\u5b57\u5217\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3067\u8d77\u3053\u308a\u5f97\u308b\u526f\u4f5c\u7528\u3092\u5fd8\u308c\u3066\u3057\u307e\u3046\uff08 etcd \u30c7\u30fc\u30bf\u7af6\u5408\u306e\u4f8b\u3068\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\uff09 (#68)","text":"\u8981\u7d04

    \u6587\u5b57\u5217\u306e\u66f8\u5f0f\u8a2d\u5b9a\u304c\u65e2\u5b58\u306e\u95a2\u6570\u304c\u547c\u3073\u51fa\u3059\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u8a8d\u8b58\u3059\u308b\u3053\u3068\u306f\u3001\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u3084\u305d\u306e\u4ed6\u306e\u30c7\u30fc\u30bf\u7af6\u5408\u306e\u53ef\u80fd\u6027\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#append-69","title":"append \u3067\u30c7\u30fc\u30bf\u7af6\u5408\u3092\u8d77\u3053\u3057\u3066\u3057\u307e\u3046 (#69)","text":"\u8981\u7d04

    append \u306e\u547c\u3073\u51fa\u3057\u306f\u5fc5\u305a\u3057\u3082\u30c7\u30fc\u30bf\u7af6\u5408\u304c\u306a\u3044\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3086\u3048\u306b\u3001\u5171\u6709\u30b9\u30e9\u30a4\u30b9\u4e0a\u3067\u540c\u6642\u306b\u4f7f\u7528\u3057\u3066\u306f\u3044\u3051\u307e\u305b\u3093\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#70","title":"\u30b9\u30e9\u30a4\u30b9\u3068\u30de\u30c3\u30d7\u3067\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u3092\u6b63\u3057\u304f\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#70)","text":"\u8981\u7d04

    \u30b9\u30e9\u30a4\u30b9\u3068\u30de\u30c3\u30d7\u306f\u30dd\u30a4\u30f3\u30bf\u3067\u3042\u308b\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304f\u3068\u3001\u5178\u578b\u7684\u306a\u30c7\u30fc\u30bf\u7af6\u5408\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#syncwaitgroup-71","title":"sync.WaitGroup \u3092\u6b63\u3057\u304f\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#71)","text":"\u8981\u7d04

    sync.WaitGroup \u3092\u6b63\u3057\u304f\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u8d77\u52d5\u3059\u308b\u524d\u306b Add \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#synccond-72","title":"sync.Cond \u306b\u3064\u3044\u3066\u5fd8\u308c\u3066\u3057\u307e\u3046 (#72)","text":"\u8981\u7d04

    sync.Cond \u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u8907\u6570\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u306b\u7e70\u308a\u8fd4\u3057\u901a\u77e5\u3092\u9001\u4fe1\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#errgroup-73","title":"errgroup \u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#73)","text":"\u8981\u7d04

    errgroup \u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u7528\u3057\u3066\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u306e\u30b0\u30eb\u30fc\u30d7\u3092\u540c\u671f\u3057\u3001\u30a8\u30e9\u30fc\u3068 Context \u3092\u51e6\u7406\u3067\u304d\u307e\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/#_15","title":"\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea","text":""},{"location":"ja/#75","title":"\u9593\u9055\u3063\u305f\u6642\u9593\u3092\u6307\u5b9a\u3059\u308b (#75)","text":"\u8981\u7d04

    time.Duration \u3092\u53d7\u3051\u5165\u308c\u308b\u95a2\u6570\u306b\u306f\u6ce8\u610f\u3092\u6255\u3063\u3066\u304f\u3060\u3055\u3044\u3002\u6574\u6570\u3092\u6e21\u3059\u3053\u3068\u306f\u8a31\u53ef\u3055\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u6df7\u4e71\u3092\u62db\u304b\u306a\u3044\u3088\u3046\u306b time API \u3092\u4f7f\u7528\u3059\u308b\u3088\u3046\u52aa\u3081\u3066\u304f\u3060\u3055\u3044\u3002

    \u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u591a\u304f\u306e\u95a2\u6570\u306f\u3001int64 \u578b\u306e\u30a8\u30a4\u30ea\u30a2\u30b9\u3067\u3042\u308b time.Duration \u3092\u53d7\u3051\u5165\u308c\u307e\u3059\u3002\u305f\u3060\u3057\u30011 \u5358\u4f4d\u306e time.Duration \u306f\u3001\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u4e00\u822c\u7684\u306b\u898b\u3089\u308c\u308b 1 \u30df\u30ea\u79d2\u3067\u306f\u306a\u304f\u30011 \u30ca\u30ce\u79d2\u3092\u8868\u3057\u307e\u3059\u3002\u305d\u306e\u7d50\u679c\u3001time.Duration API \u3092\u4f7f\u7528\u3059\u308b\u4ee3\u308f\u308a\u306b\u6570\u5024\u578b\u3092\u6e21\u3059\u3068\u3001\u4e88\u60f3\u5916\u306e\u52d5\u4f5c\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

    \u4ed6\u8a00\u8a9e\u3092\u4f7f\u7528\u3057\u305f\u3053\u3068\u306e\u3042\u308b\u958b\u767a\u8005\u306e\u65b9\u306f\u3001\u6b21\u306e\u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066 1 \u79d2\u5468\u671f\u306e\u65b0\u3057\u3044 time.Ticker \u304c\u751f\u6210\u3055\u308c\u308b\u3068\u8003\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002

    ticker := time.NewTicker(1000)\nfor {\n    select {\n    case <-ticker.C:\n        // \u51e6\u7406\u3092\u3059\u308b\n    }\n}\n

    \u3057\u304b\u3057\u306a\u304c\u3089\u30011,000 time.Duration = 1,000 \u30ca\u30ce\u79d2\u3067\u3042\u308b\u305f\u3081\u3001\u60f3\u5b9a\u3055\u308c\u3066\u3044\u308b 1\u79d2 \u3067\u306f\u306a\u304f\u30011,000 \u30ca\u30ce\u79d2 = 1 \u30de\u30a4\u30af\u30ed\u79d2\u306e\u5468\u671f\u306b\u306a\u308a\u307e\u3059\u3002

    \u6df7\u4e71\u3084\u4e88\u60f3\u5916\u306e\u52d5\u4f5c\u3092\u62db\u304b\u306a\u3044\u3088\u3046\u3001\u3044\u3064\u3082 time.Duration API \u3092\u4f7f\u7528\u3059\u308b\u3079\u304d\u3067\u3059\u3002

    ticker = time.NewTicker(time.Microsecond)\n// \u3082\u3057\u304f\u306f\nticker = time.NewTicker(1000 * time.Nanosecond)\n

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#timeafter-76","title":"time.After \u3068\u30e1\u30e2\u30ea\u30ea\u30fc\u30af (#76)","text":"\u8981\u7d04

    \u7e70\u308a\u8fd4\u3055\u308c\u308b\u95a2\u6570\uff08\u30eb\u30fc\u30d7\u3084 HTTP \u30cf\u30f3\u30c9\u30e9\u306a\u3069\uff09\u3067 time.After \u306e\u547c\u3073\u51fa\u3057\u3092\u56de\u907f\u3059\u308b\u3068\u3001\u30d4\u30fc\u30af\u6642\u306e\u30e1\u30e2\u30ea\u6d88\u8cbb\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002time.After \u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u305f\u30ea\u30bd\u30fc\u30b9\u306f\u3001 timer \u304c\u7d42\u4e86\u3057\u305f\u3068\u304d\u306b\u306e\u307f\u89e3\u653e\u3055\u308c\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#json-77","title":"JSON \u51e6\u7406\u3067\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044 (#77)","text":"

    Go \u69cb\u9020\u4f53\u3067\u57cb\u3081\u8fbc\u307f\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002 \u306a\u305c\u306a\u3089 json.Marshaler \u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u5b9f\u88c5\u3059\u308b time.Time \u57cb\u3081\u8fbc\u307f\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u3088\u3046\u306a\u3084\u3063\u304b\u3044\u306a\u30d0\u30b0\u304c\u767a\u751f\u3057\u3066\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30de\u30fc\u30b7\u30e3\u30ea\u30f3\u30b0\u52d5\u4f5c\u304c\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    2 \u3064\u306e time.Time \u69cb\u9020\u4f53\u3092\u6bd4\u8f03\u3059\u308b\u5834\u5408\u3001time.Time \u306b\u306f wall clock \u3068 monotonic clock \u306e\u4e21\u65b9\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u3001== \u6f14\u7b97\u5b50\u3092\u4f7f\u7528\u3057\u305f\u6bd4\u8f03\u306f\u4e21\u65b9\u306e clock \u306b\u5bfe\u3057\u3066\u884c\u308f\u308c\u308b\u3053\u3068\u3092\u601d\u3044\u51fa\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    JSON \u30c7\u30fc\u30bf\u306e\u30a2\u30f3\u30de\u30fc\u30b7\u30e3\u30ea\u30f3\u30b0\u4e2d\u306b\u30de\u30c3\u30d7\u3092\u63d0\u4f9b\u3059\u308b\u3068\u304d\u306b\u9593\u9055\u3044\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u6570\u5024\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067 float64 \u306b\u5909\u63db\u3055\u308c\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#sql-78","title":"SQL \u3067\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044 (#78)","text":"

    \u8a2d\u5b9a\u3092\u8a66\u3057\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u3001 Ping \u307e\u305f\u306f PingContext \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u5b9f\u904b\u7528\u6c34\u6e96\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u8a2d\u5b9a\u3057\u307e\u3057\u3087\u3046\u3002

    SQL \u306e\u30d7\u30ea\u30da\u30a2\u30c9\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30af\u30a8\u30ea\u304c\u3088\u308a\u52b9\u7387\u7684\u304b\u3064\u78ba\u5b9f\u306b\u306a\u308a\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u30c6\u30fc\u30d6\u30eb\u5185\u306e null \u304c\u8a31\u5bb9\u3055\u308c\u3066\u3044\u308b\u5217\u306f\u3001\u30dd\u30a4\u30f3\u30bf\u307e\u305f\u306f sql.NullXXX \u578b\u3092\u4f7f\u7528\u3057\u3066\u51e6\u7406\u3057\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u884c\u306e\u53cd\u5fa9\u51e6\u7406\u306e\u5f8c\u306b sql.Rows \u306e Err \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u3001\u6b21\u306e\u884c\u306e\u6e96\u5099\u4e2d\u306b\u30a8\u30e9\u30fc\u3092\u898b\u9003\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#http-bodysqlrows-osfile-79","title":"\u4e00\u6642\u7684\u306a\u30ea\u30bd\u30fc\u30b9\uff08 HTTP body\u3001sql.Rows\u3001\u304a\u3088\u3073 os.File \uff09\u3092\u9589\u3058\u3066\u3044\u306a\u3044 (#79)","text":"\u8981\u7d04

    \u30ea\u30fc\u30af\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001 io.Closer \u3092\u5b9f\u88c5\u3057\u3066\u3044\u308b\u3059\u3079\u3066\u306e\u69cb\u9020\u4f53\u3092\u6700\u5f8c\u306b\u306f\u9589\u3058\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#http-return-80","title":"HTTP \u30ea\u30af\u30a8\u30b9\u30c8\u306b\u5fdc\u7b54\u3057\u305f\u5f8c\u306e return \u6587\u3092\u5fd8\u308c\u3066\u3057\u307e\u3046 (#80)","text":"\u8981\u7d04

    HTTP \u30cf\u30f3\u30c9\u30e9\u306e\u5b9f\u88c5\u3067\u306e\u4e88\u60f3\u5916\u306e\u52d5\u4f5c\u3092\u907f\u3051\u308b\u305f\u3081\u3001http.Error \u306e\u5f8c\u306b\u30cf\u30f3\u30c9\u30e9\u3092\u505c\u6b62\u3057\u305f\u3044\u5834\u5408\u306f\u3001return \u6587\u3092\u5fd8\u308c\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#http-81","title":"\u6a19\u6e96\u306e HTTP \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3068\u30b5\u30fc\u30d0\u30fc\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b (#81)","text":"\u8981\u7d04

    \u5b9f\u904b\u7528\u6c34\u6e96\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u6c42\u3081\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u6a19\u6e96\u306e HTTP \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3068\u30b5\u30fc\u30d0\u30fc\u306e\u5b9f\u88c5\u3092\u4f7f\u7528\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u3089\u306e\u5b9f\u88c5\u306b\u306f\u3001\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u3084\u7a3c\u50cd\u74b0\u5883\u3067\u5fc5\u9808\u3067\u3042\u308b\u3079\u304d\u52d5\u4f5c\u304c\u6b20\u843d\u3057\u3066\u3044\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#_16","title":"\u30c6\u30b9\u30c8","text":""},{"location":"ja/#82","title":"\u30c6\u30b9\u30c8\u3092\u5206\u985e\u3057\u3066\u3044\u306a\u3044\uff08\u30d3\u30eb\u30c9\u30bf\u30b0\u3001\u74b0\u5883\u5909\u6570\u3001\u30b7\u30e7\u30fc\u30c8\u30e2\u30fc\u30c9\uff09 (#82)","text":"\u8981\u7d04

    \u30d3\u30eb\u30c9\u30d5\u30e9\u30b0\u3001\u74b0\u5883\u5909\u6570\u3001\u307e\u305f\u306f\u30b7\u30e7\u30fc\u30c8\u30e2\u30fc\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30c6\u30b9\u30c8\u3092\u5206\u985e\u3059\u308b\u3068\u3001\u30c6\u30b9\u30c8\u30d7\u30ed\u30bb\u30b9\u304c\u3088\u308a\u52b9\u7387\u7684\u306b\u306a\u308a\u307e\u3059\u3002\u30d3\u30eb\u30c9\u30d5\u30e9\u30b0\u307e\u305f\u306f\u74b0\u5883\u5909\u6570\u3092\u4f7f\u7528\u3057\u3066\u30c6\u30b9\u30c8\u30ab\u30c6\u30b4\u30ea\uff08\u305f\u3068\u3048\u3070\u3001\u5358\u4f53\u30c6\u30b9\u30c8\u3068\u7d71\u5408\u30c6\u30b9\u30c8\uff09\u3092\u4f5c\u6210\u3057\u3001\u77ed\u671f\u9593\u306e\u30c6\u30b9\u30c8\u3068\u9577\u6642\u9593\u306e\u30c6\u30b9\u30c8\u3092\u533a\u5225\u3059\u308b\u3053\u3068\u3067\u3001\u5b9f\u884c\u3059\u308b\u30c6\u30b9\u30c8\u306e\u7a2e\u985e\u3092\u6c7a\u5b9a\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#-race-83","title":"-race \u30d5\u30e9\u30b0\u3092\u6709\u52b9\u306b\u3057\u3066\u3044\u306a\u3044 (#83)","text":"\u8981\u7d04

    \u4e26\u884c\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u5834\u5408\u306f\u3001 -race \u30d5\u30e9\u30b0\u3092\u6709\u52b9\u306b\u3059\u308b\u3053\u3068\u3092\u5f37\u304f\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u305d\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306e\u30d0\u30b0\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u306e\u3042\u308b\u6f5c\u5728\u7684\u306a\u30c7\u30fc\u30bf\u7af6\u5408\u3092\u767a\u898b\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002

    "},{"location":"ja/#-parallel-shuffle-84","title":"\u30c6\u30b9\u30c8\u5b9f\u884c\u30e2\u30fc\u30c9\uff08 -parallel \u304a\u3088\u3073 -shuffle \uff09\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#84)","text":"\u8981\u7d04

    -parallel \u30d5\u30e9\u30b0\u3092\u4f7f\u7528\u3059\u308b\u306e\u306f\u3001\u7279\u306b\u9577\u6642\u9593\u5b9f\u884c\u3055\u308c\u308b\u30c6\u30b9\u30c8\u3092\u9ad8\u901f\u5316\u3059\u308b\u306e\u306b\u52b9\u679c\u7684\u3067\u3059\u3002 -shuffle \u30d5\u30e9\u30b0\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30c6\u30b9\u30c8\u30b9\u30a4\u30fc\u30c8\u304c\u30d0\u30b0\u3092\u96a0\u3059\u53ef\u80fd\u6027\u306e\u3042\u308b\u9593\u9055\u3063\u305f\u4eee\u5b9a\u306b\u4f9d\u5b58\u3057\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

    "},{"location":"ja/#85","title":"\u30c6\u30fc\u30d6\u30eb\u99c6\u52d5\u30c6\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u306a\u3044 (#85)","text":"\u8981\u7d04

    \u30c6\u30fc\u30d6\u30eb\u99c6\u52d5\u30c6\u30b9\u30c8\u306f\u3001\u30b3\u30fc\u30c9\u306e\u91cd\u8907\u3092\u9632\u304e\u3001\u5c06\u6765\u306e\u66f4\u65b0\u306e\u51e6\u7406\u3092\u5bb9\u6613\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u4e00\u9023\u306e\u985e\u4f3c\u3057\u305f\u30c6\u30b9\u30c8\u3092\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b\u52b9\u7387\u7684\u306a\u65b9\u6cd5\u3067\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#86","title":"\u5358\u4f53\u30c6\u30b9\u30c8\u3067\u306e\u30b9\u30ea\u30fc\u30d7 (#86)","text":"\u8981\u7d04

    \u30c6\u30b9\u30c8\u306e\u4e0d\u5b89\u5b9a\u3055\u3092\u306a\u304f\u3057\u3001\u3088\u308a\u5805\u7262\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u540c\u671f\u3092\u4f7f\u7528\u3057\u3066\u30b9\u30ea\u30fc\u30d7\u3092\u56de\u907f\u3057\u307e\u3057\u3087\u3046\u3002\u540c\u671f\u304c\u4e0d\u53ef\u80fd\u306a\u5834\u5408\u306f\u3001\u30ea\u30c8\u30e9\u30a4\u624b\u6cd5\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#time-api-87","title":"time API \u3092\u52b9\u7387\u7684\u306b\u51e6\u7406\u3067\u304d\u3066\u3044\u306a\u3044 (#87)","text":"\u8981\u7d04

    time API \u3092\u4f7f\u7528\u3057\u3066\u95a2\u6570\u3092\u51e6\u7406\u3059\u308b\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u30c6\u30b9\u30c8\u306e\u4e0d\u5b89\u5b9a\u3055\u3092\u8efd\u6e1b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u96a0\u308c\u305f\u4f9d\u5b58\u95a2\u4fc2\u306e\u4e00\u90e8\u3068\u3057\u3066 time \u3092\u51e6\u7406\u3057\u305f\u308a\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b time \u3092\u63d0\u4f9b\u3059\u308b\u3088\u3046\u306b\u8981\u6c42\u3057\u305f\u308a\u3059\u308b\u306a\u3069\u3001\u6a19\u6e96\u7684\u306a\u624b\u6bb5\u3092\u5229\u7528\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#httptest-iotest-88","title":"\u30c6\u30b9\u30c8\u306b\u95a2\u3059\u308b\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u30d1\u30c3\u30b1\u30fc\u30b8\uff08 httptest \u304a\u3088\u3073 iotest \uff09\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#88)","text":"

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#89","title":"\u4e0d\u6b63\u78ba\u306a\u30d9\u30f3\u30c1\u30de\u30fc\u30af\u306e\u4f5c\u6210 (#89)","text":"\u8981\u7d04

    \u30d9\u30f3\u30c1\u30de\u30fc\u30af\u306b\u3064\u3044\u3066

    \u30bb\u30af\u30b7\u30e7\u30f3\u5168\u6587\u306f\u3053\u3061\u3089\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#go-90","title":"Go\u8a00\u8a9e\u306e\u30c6\u30b9\u30c8\u6a5f\u80fd\u3092\u3059\u3079\u3066\u8a66\u3057\u3066\u3044\u306a\u3044 (#90)","text":"

    \u30b3\u30fc\u30c9\u306e\u3069\u306e\u90e8\u5206\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u304b\u3092\u3059\u3050\u306b\u78ba\u8a8d\u3059\u308b\u305f\u3081\u306b\u3001-coverprofile \u30d5\u30e9\u30b0\u3092\u6307\u5b9a\u3057\u3066\u30b3\u30fc\u30c9\u30ab\u30d0\u30ec\u30c3\u30b8\u3092\u4f7f\u7528\u3057\u307e\u3057\u3087\u3046\u3002

    \u5185\u90e8\u3067\u306f\u306a\u304f\u516c\u958b\u3055\u308c\u305f\u52d5\u4f5c\u306b\u7126\u70b9\u3092\u5f53\u3066\u305f\u30c6\u30b9\u30c8\u306e\u4f5c\u6210\u3092\u5f37\u5236\u3059\u308b\u305f\u3081\u306b\u3001\u5358\u4f53\u30c6\u30b9\u30c8\u306f\u5225\u3005\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u914d\u7f6e\u3057\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u5f93\u6765\u306e if err != nil \u306e\u4ee3\u308f\u308a\u306b *testing.T \u5909\u6570\u3092\u4f7f\u7528\u3057\u3066\u30a8\u30e9\u30fc\u3092\u51e6\u7406\u3059\u308b\u3068\u3001\u30b3\u30fc\u30c9\u304c\u77ed\u304f\u3001\u8aad\u307f\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    setup \u304a\u3088\u3073 teardown \u6a5f\u80fd\u3092\u5229\u7528\u3057\u3066\u3001\u7d71\u5408\u30c6\u30b9\u30c8\u306e\u5834\u5408\u306a\u3069\u3001\u8907\u96d1\u306a\u74b0\u5883\u3092\u69cb\u6210\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#community-mistake","title":"\u30d5\u30a1\u30b8\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044\uff08community mistake\uff09","text":"\u8981\u7d04

    \u30d5\u30a1\u30b8\u30f3\u30b0\u306f\u3001\u8907\u96d1\u306a\u95a2\u6570\u3084\u30e1\u30bd\u30c3\u30c9\u3078\u306e\u30e9\u30f3\u30c0\u30e0\u306a\u3001\u4e88\u60f3\u5916\u306e\u3001\u307e\u305f\u306f\u4e0d\u6b63\u306a\u5165\u529b\u3092\u691c\u51fa\u3057\u3001\u8106\u5f31\u6027\u3001\u30d0\u30b0\u3001\u3055\u3089\u306b\u306f\u6f5c\u5728\u7684\u306a\u30af\u30e9\u30c3\u30b7\u30e5\u3092\u767a\u898b\u3059\u308b\u306e\u306b\u52b9\u7387\u7684\u3067\u3059\u3002

    @jeromedoucet \u3055\u3093\u306e\u3054\u5354\u529b\u306b\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\u3002

    "},{"location":"ja/#_17","title":"\u6700\u9069\u5316","text":""},{"location":"ja/#cpu-91","title":"CPU \u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#91)","text":"

    L1 \u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u30e1\u30a4\u30f3\u30e1\u30e2\u30ea\u3088\u308a\u3082\u7d04 50 \uff5e 100 \u500d\u9ad8\u901f\u3067\u3042\u308b\u305f\u3081\u3001CPU \u30d0\u30a6\u30f3\u30c9\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u6700\u9069\u5316\u3059\u308b\u306b\u306f\u3001CPU \u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u4f7f\u7528\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

    \u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\u306e\u6982\u5ff5\u3092\u610f\u8b58\u3059\u308b\u3053\u3068\u306f\u3001\u30c7\u30fc\u30bf\u96c6\u7d04\u578b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u30c7\u30fc\u30bf\u3092\u6574\u7406\u3059\u308b\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u306e\u306b\u91cd\u8981\u3067\u3059\u3002CPU \u306f\u30e1\u30e2\u30ea\u3092\u30ef\u30fc\u30c9\u3054\u3068\u306b\u30d5\u30a7\u30c3\u30c1\u3057\u307e\u305b\u3093\u3002\u4ee3\u308f\u308a\u306b\u3001\u901a\u5e38\u306f\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u3092 64 \u30d0\u30a4\u30c8\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\u306b\u30b3\u30d4\u30fc\u3057\u307e\u3059\u3002\u500b\u3005\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\u3092\u6700\u5927\u9650\u306b\u6d3b\u7528\u3059\u308b\u306b\u306f\u3001\u7a7a\u9593\u7684\u5c40\u6240\u6027\u3092\u5f37\u5236\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    CPU \u306b\u3068\u3063\u3066\u4e88\u6e2c\u53ef\u80fd\u306a\u30b3\u30fc\u30c9\u306b\u3059\u308b\u3053\u3068\u306f\u3001\u7279\u5b9a\u306e\u95a2\u6570\u3092\u6700\u9069\u5316\u3059\u308b\u52b9\u7387\u7684\u306a\u65b9\u6cd5\u3067\u3082\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u30e6\u30cb\u30c3\u30c8\u307e\u305f\u306f\u5b9a\u6570\u30b9\u30c8\u30e9\u30a4\u30c9\u306f CPU \u306b\u3068\u3063\u3066\u4e88\u6e2c\u53ef\u80fd\u3067\u3059\u304c\u3001\u975e\u30e6\u30cb\u30c3\u30c8\u30b9\u30c8\u30e9\u30a4\u30c9\uff08\u9023\u7d50\u30ea\u30b9\u30c8\u306a\u3069\uff09\u306f\u4e88\u6e2c\u3067\u304d\u307e\u305b\u3093\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u30ad\u30e3\u30c3\u30b7\u30e5\u304c\u30d1\u30fc\u30c6\u30a3\u30b7\u30e7\u30f3\u5316\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u8a8d\u8b58\u3059\u308b\u3053\u3068\u3067\u3001\u91cd\u5927\u306a\u30b9\u30c8\u30e9\u30a4\u30c9\u3092\u56de\u907f\u3057\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u3054\u304f\u4e00\u90e8\u306e\u307f\u3092\u4f7f\u7528\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

    "},{"location":"ja/#92","title":"\u8aa4\u3063\u305f\u5171\u6709\u3092\u5f15\u304d\u8d77\u3053\u3059\u4e26\u884c\u51e6\u7406(#92)","text":"\u8981\u7d04

    \u4e0b\u4f4d\u30ec\u30d9\u30eb\u306e CPU \u30ad\u30e3\u30c3\u30b7\u30e5\u304c\u3059\u3079\u3066\u306e\u30b3\u30a2\u3067\u5171\u6709\u3055\u308c\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3053\u3068\u3092\u77e5\u3063\u3066\u304a\u304f\u3068\u3001\u4e26\u884c\u51e6\u7406\u306b\u304a\u3051\u308b\u306e\u8aa4\u3063\u305f\u5171\u6709\u306a\u3069\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u4f4e\u4e0b\u3055\u305b\u3066\u3057\u307e\u3046\u3053\u3068\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002\u30e1\u30e2\u30ea\u306e\u5171\u6709\u306f\u3042\u308a\u3048\u306a\u3044\u306e\u3067\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#93","title":"\u547d\u4ee4\u30ec\u30d9\u30eb\u306e\u4e26\u5217\u6027\u3092\u8003\u616e\u3057\u306a\u3044 (#93)","text":"\u8981\u7d04

    \u547d\u4ee4\u30ec\u30d9\u30eb\u306e\u4e26\u5217\u6027\uff08ILP\uff09\u3092\u4f7f\u7528\u3057\u3066\u30b3\u30fc\u30c9\u306e\u7279\u5b9a\u306e\u90e8\u5206\u3092\u6700\u9069\u5316\u3057\u3001CPU \u304c\u3067\u304d\u308b\u3060\u3051\u591a\u304f\u306e\u547d\u4ee4\u3092\u4e26\u5217\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002\u4e3b\u306a\u624b\u9806\u306e 1 \u3064\u306b\u30c7\u30fc\u30bf\u30cf\u30b6\u30fc\u30c9\u306e\u7279\u5b9a\u304c\u3042\u308a\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#94","title":"\u30c7\u30fc\u30bf\u306e\u914d\u7f6e\u3092\u610f\u8b58\u3057\u3066\u3044\u306a\u3044 (#94)","text":"\u8981\u7d04

    Go\u8a00\u8a9e\u3067\u306f\u3001\u57fa\u672c\u578b\u306f\u5404\u3005\u306e\u30b5\u30a4\u30ba\u306b\u5408\u308f\u305b\u3066\u914d\u7f6e\u3055\u308c\u308b\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304f\u3053\u3068\u3067\u3001\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u69cb\u9020\u4f53\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u30b5\u30a4\u30ba\u3067\u964d\u9806\u306b\u518d\u7de8\u6210\u3059\u308b\u3068\u3001\u69cb\u9020\u4f53\u304c\u3088\u308a\u30b3\u30f3\u30d1\u30af\u30c8\u306b\u306a\u308b\uff08\u30e1\u30e2\u30ea\u5272\u308a\u5f53\u3066\u304c\u5c11\u306a\u304f\u306a\u308a\u3001\u7a7a\u9593\u7684\u5c40\u6240\u6027\u304c\u5411\u4e0a\u3059\u308b\uff09\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u306b\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#95","title":"\u30d2\u30fc\u30d7\u3068\u30b9\u30bf\u30c3\u30af\u306e\u9055\u3044\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#95)","text":"\u8981\u7d04

    \u30d2\u30fc\u30d7\u3068\u30b9\u30bf\u30c3\u30af\u306e\u57fa\u672c\u7684\u306a\u9055\u3044\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3082\u3001Go \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u6700\u9069\u5316\u3059\u308b\u969b\u306b\u306f\u5927\u5207\u3067\u3059\u3002\u30b9\u30bf\u30c3\u30af\u5272\u308a\u5f53\u3066\u306f\u5bb9\u6613\u306a\u306e\u306b\u5bfe\u3057\u3066\u3001\u30d2\u30fc\u30d7\u5272\u308a\u5f53\u3066\u306f\u9045\u304f\u3001\u30e1\u30e2\u30ea\u306e\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u306b GC \u3092\u5229\u7528\u3057\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#api-syncpool-96","title":"\u5272\u308a\u5f53\u3066\u3092\u6e1b\u3089\u3059\u65b9\u6cd5\u304c\u308f\u304b\u3063\u3066\u3044\u306a\u3044\uff08 API \u306e\u5909\u66f4\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u6700\u9069\u5316\u3001\u304a\u3088\u3073 sync.Pool\uff09 (#96)","text":"\u8981\u7d04

    \u5272\u308a\u5f53\u3066\u3092\u6e1b\u3089\u3059\u3053\u3068\u3082\u3001Go \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u6700\u9069\u5316\u3059\u308b\u4e0a\u3067\u91cd\u8981\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u5171\u6709\u3092\u9632\u3050\u305f\u3081\u306b API \u3092\u614e\u91cd\u306b\u8a2d\u8a08\u3059\u308b\u3001\u4e00\u822c\u7684\u306a Go \u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u6700\u9069\u5316\u3092\u7406\u89e3\u3059\u308b\u3001sync.Pool \u3092\u4f7f\u7528\u3059\u308b\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u65b9\u6cd5\u3067\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\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\u3057\u3087\u3046\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\u7d04

    GC \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\u7d04

    Docker \u3068 Kubernetes \u306b\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u969b\u306e CPU \u30b9\u30ed\u30c3\u30c8\u30ea\u30f3\u30b0\u3092\u56de\u907f\u3059\u308b\u306b\u306f\u3001Go\u8a00\u8a9e\u304c CFS \u5bfe\u5fdc\u3067\u306f\u306a\u3044\u3053\u3068\u306b\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    "},{"location":"pt-br/","title":"Erros comuns de Go","text":"

    Esta p\u00e1gina \u00e9 um resumo dos erros do 100 Go Mistakes and How to Avoid Them book. Enquanto isso, tamb\u00e9m est\u00e1 aberto \u00e0 comunidade. Se voc\u00ea acredita que um erro comum do Go deve ser adicionado, crie uma issue.

    Jobs

    Sua empresa est\u00e1 contratando? Patrocine este reposit\u00f3rio e informe um p\u00fablico significativo de desenvolvedores Go (cerca de 1 mil visitantes \u00fanicos por semana) sobre suas oportunidades nesta se\u00e7\u00e3o.

    Beta

    Voc\u00ea est\u00e1 visualizando uma vers\u00e3o beta enriquecida com muito mais conte\u00fado. No entanto, esta vers\u00e3o ainda n\u00e3o est\u00e1 completa e estou procurando volunt\u00e1rios para me ajudar a resumir os erros restantes (GitHub issue #43).

    Progresso:

    "},{"location":"pt-br/#codigo-e-organizacao-do-projeto","title":"C\u00f3digo e Organiza\u00e7\u00e3o do Projeto","text":""},{"location":"pt-br/#sombreamento-nao-intencional-de-variavel-1","title":"Sombreamento n\u00e3o intencional de vari\u00e1vel (#1)","text":"TL;DR

    Evitar vari\u00e1veis \u200b\u200bsombreadas pode ajudar a evitar erros, como fazer refer\u00eancia \u00e0 vari\u00e1vel errada ou confundir os desenvolvedores.

    O sombreamento de vari\u00e1vel ocorre quando um nome de vari\u00e1vel \u00e9 redeclarado em um bloco interno, mas essa pr\u00e1tica est\u00e1 sujeita a erros. A imposi\u00e7\u00e3o de uma regra para proibir vari\u00e1veis \u200b\u200bobscuras depende do gosto pessoal. Por exemplo, \u00e0s vezes pode ser conveniente reutilizar um nome de vari\u00e1vel existente, como err no caso de erros. Por\u00e9m, em geral, devemos ser cautelosos porque agora sabemos que podemos enfrentar um cen\u00e1rio onde o c\u00f3digo compila, mas a vari\u00e1vel que recebe o valor n\u00e3o \u00e9 a esperada.

    C\u00f3digo fonte

    "},{"location":"pt-br/#codigo-aninhado-desnecessario-2","title":"C\u00f3digo aninhado desnecess\u00e1rio (#2)","text":"TL;DR

    Evitar n\u00edveis aninhados e manter o caminho feliz alinhado \u00e0 esquerda facilita a constru\u00e7\u00e3o de um modelo de c\u00f3digo mental.

    Em geral, quanto mais n\u00edveis aninhados uma fun\u00e7\u00e3o exigir, mais complexa ser\u00e1 sua leitura e compreens\u00e3o. Vamos ver algumas aplica\u00e7\u00f5es diferentes desta regra para otimizar a legibilidade do nosso c\u00f3digo:

    if foo() {\n    // ...\n    return true\n} else {\n    // ...\n}\n

    Em vez disso, omitimos o bloco else assim:

    if foo() {\n    // ...\n    return true\n}\n// ...\n
    if s != \"\" {\n    // ...\n} else {\n    return errors.New(\"empty string\")\n}\n

    Aqui, um s vazio representa o caminho n\u00e3o feliz. Portanto, devemos inverter a condi\u00e7\u00e3o assim:

    if s == \"\" {\n    return errors.New(\"empty string\")\n}\n// ...\n

    Escrever c\u00f3digo leg\u00edvel \u00e9 um desafio importante para todo desenvolvedor. Esfor\u00e7ar-se para reduzir o n\u00famero de blocos aninhados, alinhar o caminho feliz \u00e0 esquerda e retornar o mais cedo poss\u00edvel s\u00e3o meios concretos para melhorar a legibilidade do nosso c\u00f3digo.

    C\u00f3digo fonte

    "},{"location":"pt-br/#uso-indevido-de-funcoes-init-3","title":"Uso indevido de fun\u00e7\u00f5es init (#3)","text":"TL;DR

    Ao inicializar vari\u00e1veis, lembre-se de que as fun\u00e7\u00f5es init t\u00eam tratamento de erros limitado e tornam o tratamento de estado e os testes mais complexos. Na maioria dos casos, as inicializa\u00e7\u00f5es devem ser tratadas como fun\u00e7\u00f5es espec\u00edficas.

    Uma fun\u00e7\u00e3o init \u00e9 uma fun\u00e7\u00e3o usada para inicializar o estado de um aplicativo. N\u00e3o aceita argumentos e n\u00e3o retorna nenhum resultado (uma fun\u00e7\u00e3o func()). Quando um pacote \u00e9 inicializado, todas as declara\u00e7\u00f5es de constantes e vari\u00e1veis \u200b\u200bdo pacote s\u00e3o avaliadas. Ent\u00e3o, as fun\u00e7\u00f5es init s\u00e3o executadas.

    As fun\u00e7\u00f5es de inicializa\u00e7\u00e3o podem levar a alguns problemas:

    Devemos ser cautelosos com as fun\u00e7\u00f5es init. No entanto, elas podem ser \u00fateis em algumas situa\u00e7\u00f5es, como na defini\u00e7\u00e3o de configura\u00e7\u00e3o est\u00e1tica. Caso contr\u00e1rio, e na maioria dos casos, devemos tratar as inicializa\u00e7\u00f5es atrav\u00e9s de fun\u00e7\u00f5es ad hoc.

    C\u00f3digo fonte

    "},{"location":"pt-br/#uso-excessivo-de-getters-e-setters-4","title":"Uso excessivo de getters e setters (#4)","text":"TL;DR

    Forcing 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.

    O encapsulamento de dados refere-se a ocultar os valores ou o estado de um objeto. Getters e setters s\u00e3o meios de habilitar o encapsulamento, fornecendo m\u00e9todos exportados sobre campos de objetos n\u00e3o exportados.

    No Go, n\u00e3o h\u00e1 suporte autom\u00e1tico para getters e setters como vemos em algumas linguagens. Tamb\u00e9m n\u00e3o \u00e9 considerado obrigat\u00f3rio nem idiom\u00e1tico o uso de getters e setters para acessar campos struct. N\u00e3o devemos sobrecarregar nosso c\u00f3digo com getters e setters em structs se eles n\u00e3o trouxerem nenhum valor. Dever\u00edamos ser pragm\u00e1ticos e nos esfor\u00e7ar para encontrar o equil\u00edbrio certo entre efici\u00eancia e seguir express\u00f5es que \u00e0s vezes s\u00e3o consideradas indiscut\u00edveis em outros paradigmas de programa\u00e7\u00e3o.

    Lembre-se de que Go \u00e9 uma linguagem \u00fanica projetada para muitas caracter\u00edsticas, incluindo simplicidade. No entanto, se encontrarmos necessidade de getters e setters ou, como mencionado, prevermos uma necessidade futura e ao mesmo tempo garantirmos a compatibilidade futura, n\u00e3o h\u00e1 nada de errado em us\u00e1-los.

    "},{"location":"pt-br/#interface-poluidas-5","title":"Interface poluidas (#5)","text":"TL;DR

    Abstra\u00e7\u00f5es devem ser descobertas, n\u00e3o criadas. Para evitar complexidade desnecess\u00e1ria, crie uma interface quando precisar dela e n\u00e3o quando voc\u00ea prev\u00ear que ser\u00e1 necess\u00e1ria, ou se puder pelo menos provar que a abstra\u00e7\u00e3o \u00e9 v\u00e1lida.

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#interface-do-lado-do-producer-6","title":"Interface do lado do producer (#6)","text":"TL;DR

    Manter interfaces no lado do cliente evita abstra\u00e7\u00f5es desnecess\u00e1rias.

    As interfaces s\u00e3o satisfeitas implicitamente em Go, o que tende a ser um divisor de \u00e1guas em compara\u00e7\u00e3o com linguagens com implementa\u00e7\u00e3o expl\u00edcita. Na maioria dos casos, a abordagem a seguir \u00e9 semelhante \u00e0 que descrevemos na se\u00e7\u00e3o anterior: as abstra\u00e7\u00f5es devem ser descobertas, n\u00e3o criadas. Isso significa que n\u00e3o cabe ao producer for\u00e7ar uma determinada abstra\u00e7\u00e3o para todos os clientes. Em vez disso, cabe ao cliente decidir se precisa de alguma forma de abstra\u00e7\u00e3o e ent\u00e3o determinar o melhor n\u00edvel de abstra\u00e7\u00e3o para suas necessidades.

    Uma interface deve residir no lado do consumidor na maioria dos casos. Contudo, em contextos espec\u00edficos (por exemplo, quando sabemos \u2013 e n\u00e3o prevemos \u2013 que uma abstra\u00e7\u00e3o ser\u00e1 \u00fatil para os consumidores), podemos querer t\u00ea-la do lado do procuder. Se o fizermos, devemos nos esfor\u00e7ar para mant\u00ea-lo o m\u00ednimo poss\u00edvel, aumentando o seu potencial de reutiliza\u00e7\u00e3o e tornando-o mais facilmente combin\u00e1vel.

    C\u00f3digo fonte

    "},{"location":"pt-br/#interfaces-de-retorno-7","title":"Interfaces de retorno (#7)","text":"TL;DR

    Para evitar restri\u00e7\u00f5es em termos de flexibilidade, uma fun\u00e7\u00e3o n\u00e3o deve retornar interfaces, mas implementa\u00e7\u00f5es concretas na maioria dos casos. Por outro lado, uma fun\u00e7\u00e3o deve aceitar interfaces sempre que poss\u00edvel.

    Na maioria dos casos, n\u00e3o devemos retornar interfaces, mas implementa\u00e7\u00f5es concretas. Caso contr\u00e1rio, isso pode tornar nosso design mais complexo devido \u00e0s depend\u00eancias do pacote e pode restringir a flexibilidade porque todos os clientes teriam que contar com a mesma abstra\u00e7\u00e3o. Novamente, a conclus\u00e3o \u00e9 semelhante \u00e0s se\u00e7\u00f5es anteriores: se sabemos (n\u00e3o prevemos) que uma abstra\u00e7\u00e3o ser\u00e1 \u00fatil para os clientes, podemos considerar o retorno de uma interface. Caso contr\u00e1rio, n\u00e3o dever\u00edamos for\u00e7ar abstra\u00e7\u00f5es; eles devem ser descobertas pelos clientes. Se um cliente precisar abstrair uma implementa\u00e7\u00e3o por qualquer motivo, ele ainda poder\u00e1 fazer isso do lado do cliente.

    "},{"location":"pt-br/#any-nao-diz-nada-8","title":"any n\u00e3o diz nada (#8)","text":"TL;DR

    Use apenas any se precisar aceitar ou retornar qualquer tipo poss\u00edvel, como json.Marshal. Caso contr\u00e1rio, any n\u00e3o fornece informa\u00e7\u00f5es significativas e pode levar a problemas de tempo de compila\u00e7\u00e3o, permitindo que um chamador chame m\u00e9todos com qualquer tipo de dados.

    O tipo any pode ser \u00fatil se houver uma necessidade genu\u00edna de aceitar ou retornar qualquer tipo poss\u00edvel (por exemplo, quando se trata de empacotamento ou formata\u00e7\u00e3o). Em geral, devemos evitar a todo custo generalizar demais o c\u00f3digo que escrevemos. Talvez um pouco de c\u00f3digo duplicado possa ocasionalmente ser melhor se melhorar outros aspectos, como a expressividade do c\u00f3digo.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ficar-confuso-sobre-quando-usar-genericos-9","title":"Ficar confuso sobre quando usar gen\u00e9ricos (#9)","text":"TL;DR

    Depender de par\u00e2metros gen\u00e9ricos e de tipo pode impedir a grava\u00e7\u00e3o de c\u00f3digo clich\u00ea (boilerplate) para fatorar elementos ou comportamentos. No entanto, n\u00e3o use par\u00e2metros de tipo prematuramente, mas somente quando voc\u00ea perceber uma necessidade concreta deles. Caso contr\u00e1rio, introduzem abstra\u00e7\u00f5es e complexidade desnecess\u00e1rias.

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-estar-ciente-dos-possiveis-problemas-com-a-incorporacao-de-tipos-10","title":"N\u00e3o estar ciente dos poss\u00edveis problemas com a incorpora\u00e7\u00e3o de tipos (#10)","text":"TL;DR

    Usar a incorpora\u00e7\u00e3o de tipo (type embedding) tamb\u00e9m pode ajudar a evitar c\u00f3digo clich\u00ea (boilerplate); no entanto, certifique-se de que isso n\u00e3o leve a problemas de visibilidade onde alguns campos deveriam ter permanecido ocultos.

    Ao criar uma struct, Go oferece a op\u00e7\u00e3o de incorporar tipos. Mas isso \u00e0s vezes pode levar a comportamentos inesperados se n\u00e3o compreendermos todas as implica\u00e7\u00f5es da incorpora\u00e7\u00e3o de tipos. Ao longo desta se\u00e7\u00e3o, veremos como incorporar tipos, o que eles trazem e os poss\u00edveis problemas.

    No Go, um campo struct \u00e9 chamado de incorporado se for declarado sem nome. Por exemplo,

    type Foo struct {\n    Bar // Embedded field\n}\n\ntype Bar struct {\n    Baz int\n}\n

    Na estrutura Foo, o tipo Bar \u00e9 declarado sem nome associado; portanto, \u00e9 um campo incorporado.

    Usamos incorpora\u00e7\u00e3o para promover os campos e m\u00e9todos de um tipo incorporado. Como Bar cont\u00e9m um campo Baz, esse campo \u00e9 promovido para Foo. Portanto, Baz fica dispon\u00edvel a partir de Foo.

    O que podemos dizer sobre a incorpora\u00e7\u00e3o de tipos? Primeiro, observemos que raramente \u00e9 uma necessidade e significa que, qualquer que seja o caso de uso, provavelmente tamb\u00e9m poderemos resolv\u00ea-lo sem incorpora\u00e7\u00e3o de tipo. A incorpora\u00e7\u00e3o de tipos \u00e9 usada principalmente por conveni\u00eancia: na maioria dos casos, para promover comportamentos.

    Se decidirmos usar incorpora\u00e7\u00e3o de tipo, precisamos ter em mente duas restri\u00e7\u00f5es principais:

    Usar a incorpora\u00e7\u00e3o de tipo de forma consciente, mantendo essas restri\u00e7\u00f5es em mente, pode ajudar a evitar c\u00f3digo clich\u00ea (boilerplate) com m\u00e9todos de encaminhamento adicionais. No entanto, vamos garantir que n\u00e3o o fazemos apenas por motivos cosm\u00e9ticos e n\u00e3o promovemos elementos que deveriam permanecer ocultos.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-usar-o-padrao-de-opcoes-funcionais-functional-options-pattern-11","title":"N\u00e3o usar o padr\u00e3o de op\u00e7\u00f5es funcionais (functional options pattern) (#11)","text":"TL;DR

    Para lidar com op\u00e7\u00f5es de maneira conveniente e amig\u00e1vel \u00e0 API, use o padr\u00e3o de op\u00e7\u00f5es funcionais.

    Embora existam diferentes implementa\u00e7\u00f5es com pequenas varia\u00e7\u00f5es, a ideia principal \u00e9 a seguinte:

    type options struct {\n  port *int\n}\n\ntype Option func(options *options) error\n\nfunc WithPort(port int) Option {\n  return func(options *options) error {\n    if port < 0 {\n    return errors.New(\"port should be positive\")\n  }\n  options.port = &port\n  return nil\n  }\n}\n\nfunc NewServer(addr string, opts ...Option) ( *http.Server, error) {\n  var options options\n  for _, opt := range opts {\n    err := opt(&options)\n    if err != nil {\n      return nil, err\n    }\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\n  var port int\n  if options.port == nil {\n    port = defaultHTTPPort\n  } else {\n      if *options.port == 0 {\n      port = randomPort()\n    } else {\n      port = *options.port\n    }\n  }\n\n  // ...\n}\n

    O padr\u00e3o de op\u00e7\u00f5es funcionais fornece uma maneira pr\u00e1tica e amig\u00e1vel \u00e0 API de lidar com op\u00e7\u00f5es. Embora o padr\u00e3o do construtor possa ser uma op\u00e7\u00e3o v\u00e1lida, ele tem algumas desvantagens menores (ter que passar uma estrutura de configura\u00e7\u00e3o que pode estar vazia ou uma maneira menos pr\u00e1tica de lidar com o gerenciamento de erros) que tendem a tornar o padr\u00e3o de op\u00e7\u00f5es funcionais a maneira idiom\u00e1tica de lidar com esse tipo de problema no Go.

    C\u00f3digo fonte

    "},{"location":"pt-br/#desorganizacao-do-projeto-estrutura-do-projeto-e-organizacao-do-pacote-12","title":"Desorganiza\u00e7\u00e3o do projeto (estrutura do projeto e organiza\u00e7\u00e3o do pacote) (#12)","text":"

    No que diz respeito \u00e0 organiza\u00e7\u00e3o geral, existem diferentes escolas de pensamento. Por exemplo, devemos organizar a nossa aplica\u00e7\u00e3o por contexto ou por camada? Depende de nossas prefer\u00eancias. Podemos preferir agrupar o c\u00f3digo por contexto (como o contexto do cliente, o contexto do contrato, etc.), ou podemos preferir seguir os princ\u00edpios da arquitetura hexagonal e agrupar por camada t\u00e9cnica. Se a decis\u00e3o que tomarmos se adequar ao nosso caso de uso, n\u00e3o pode ser uma decis\u00e3o errada, desde que permane\u00e7amos consistentes com ela.

    Em rela\u00e7\u00e3o aos pacotes, existem v\u00e1rias pr\u00e1ticas recomendadas que devemos seguir. Primeiro, devemos evitar pacotes prematuros porque podem complicar demais um projeto. \u00c0s vezes, \u00e9 melhor usar uma organiza\u00e7\u00e3o simples e fazer nosso projeto evoluir quando entendemos o que ele cont\u00e9m, em vez de nos for\u00e7armos a fazer a estrutura perfeita desde o in\u00edcio. A granularidade \u00e9 outra coisa essencial a considerar. Devemos evitar dezenas de pacotes nano contendo apenas um ou dois arquivos. Se o fizermos, \u00e9 porque provavelmente perdemos algumas conex\u00f5es l\u00f3gicas entre esses pacotes, tornando nosso projeto mais dif\u00edcil de ser compreendido pelos leitores. Por outro lado, tamb\u00e9m devemos evitar pacotes grandes que diluem o significado do nome de um pacote.

    A nomenclatura dos pacotes tamb\u00e9m deve ser considerada com cuidado. Como todos sabemos (como desenvolvedores), nomear \u00e9 dif\u00edcil. Para ajudar os clientes a entender um projeto Go, devemos nomear nossos pacotes de acordo com o que eles fornecem, n\u00e3o com o que cont\u00eam. Al\u00e9m disso, a nomenclatura deve ser significativa. Portanto, o nome de um pacote deve ser curto, conciso, expressivo e, por conven\u00e7\u00e3o, uma \u00fanica palavra min\u00fascula.

    Quanto ao que exportar, a regra \u00e9 bastante simples. Devemos minimizar o que deve ser exportado tanto quanto poss\u00edvel para reduzir o acoplamento entre pacotes e manter ocultos os elementos exportados desnecess\u00e1rios. Se n\u00e3o tivermos certeza se devemos ou n\u00e3o exportar um elemento, devemos optar por n\u00e3o export\u00e1-lo. Mais tarde, se descobrirmos que precisamos export\u00e1-lo, poderemos ajustar nosso c\u00f3digo. Vamos tamb\u00e9m ter em mente algumas exce\u00e7\u00f5es, como fazer com que os campos sejam exportados para que uma estrutura possa ser desempacotada com encoding/json.

    Organizar um projeto n\u00e3o \u00e9 simples, mas seguir essas regras deve ajudar a facilitar sua manuten\u00e7\u00e3o. No entanto, lembre-se de que a consist\u00eancia tamb\u00e9m \u00e9 vital para facilitar a manuten\u00e7\u00e3o. Portanto, vamos nos certificar de manter as coisas o mais consistentes poss\u00edvel dentro de uma base de c\u00f3digo.

    Note

    Em 2023, a equipe Go publicou uma diretriz oficial para organizar/estruturar um projeto Go: go.dev/doc/modules/layout

    "},{"location":"pt-br/#criando-pacotes-de-utilitarios-13","title":"Criando pacotes de utilit\u00e1rios (#13)","text":"TL;DR

    A nomenclatura \u00e9 uma parte cr\u00edtica do design do aplicativo. Criar pacotes como common, util e shared n\u00e3o traz muito valor para o leitor. Refatore esses pacotes em nomes de pacotes significativos e espec\u00edficos.

    Al\u00e9m disso, tenha em mente que nomear um pacote com base no que ele fornece e n\u00e3o no que ele cont\u00e9m pode ser uma forma eficiente de aumentar sua expressividade.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-colisoes-de-nomes-de-pacotes-14","title":"Ignorando colis\u00f5es de nomes de pacotes (#14)","text":"TL;DR

    Para evitar colis\u00f5es de nomes entre vari\u00e1veis \u200b\u200be pacotes, levando a confus\u00e3o ou talvez at\u00e9 bugs, use nomes exclusivos para cada um. Se isso n\u00e3o for vi\u00e1vel, use um alias de importa\u00e7\u00e3o para alterar o qualificador para diferenciar o nome do pacote do nome da vari\u00e1vel ou pense em um nome melhor.

    As colis\u00f5es de pacotes ocorrem quando um nome de vari\u00e1vel colide com um nome de pacote existente, impedindo que o pacote seja reutilizado. Devemos evitar colis\u00f5es de nomes de vari\u00e1veis \u200b\u200bpara evitar ambiguidade. Se enfrentarmos uma colis\u00e3o, devemos encontrar outro nome significativo ou usar um alias de importa\u00e7\u00e3o.

    "},{"location":"pt-br/#documentacao-de-codigo-ausente-15","title":"Documenta\u00e7\u00e3o de c\u00f3digo ausente (#15)","text":"TL;DR

    Para ajudar clientes e mantenedores a entender a finalidade do seu c\u00f3digo, documente os elementos exportados.

    A documenta\u00e7\u00e3o \u00e9 um aspecto importante da programa\u00e7\u00e3o. Simplifica como os clientes podem consumir uma API, mas tamb\u00e9m pode ajudar na manuten\u00e7\u00e3o de um projeto. No Go, devemos seguir algumas regras para tornar nosso c\u00f3digo idiom\u00e1tico:

    Primeiro, cada elemento exportado deve ser documentado. Seja uma estrutura, uma interface, uma fun\u00e7\u00e3o ou qualquer outra coisa, se for exportado deve ser documentado. A conven\u00e7\u00e3o \u00e9 adicionar coment\u00e1rios, come\u00e7ando com o nome do elemento exportado.

    Por conven\u00e7\u00e3o, cada coment\u00e1rio deve ser uma frase completa que termina com pontua\u00e7\u00e3o. Tenha tamb\u00e9m em mente que quando documentamos uma fun\u00e7\u00e3o (ou um m\u00e9todo), devemos destacar o que a fun\u00e7\u00e3o pretende fazer, n\u00e3o como o faz; isso pertence ao n\u00facleo de uma fun\u00e7\u00e3o e coment\u00e1rios, n\u00e3o \u00e0 documenta\u00e7\u00e3o. Al\u00e9m disso, o ideal \u00e9 que a documenta\u00e7\u00e3o forne\u00e7a informa\u00e7\u00f5es suficientes para que o consumidor n\u00e3o precise olhar nosso c\u00f3digo para entender como usar um elemento exportado.

    Quando se trata de documentar uma vari\u00e1vel ou constante, podemos estar interessados \u200b\u200bem transmitir dois aspectos: sua finalidade e seu conte\u00fado. O primeiro deve funcionar como documenta\u00e7\u00e3o de c\u00f3digo para ser \u00fatil para clientes externos. Este \u00faltimo, por\u00e9m, n\u00e3o deveria ser necessariamente p\u00fablico.

    Para ajudar clientes e mantenedores a entender o escopo de um pacote, devemos tamb\u00e9m documentar cada pacote. A conven\u00e7\u00e3o \u00e9 iniciar o coment\u00e1rio com // Package seguido do nome do pacote. A primeira linha de um coment\u00e1rio de pacote deve ser concisa. Isso porque ele aparecer\u00e1 no pacote. Ent\u00e3o, podemos fornecer todas as informa\u00e7\u00f5es que precisamos nas linhas seguintes.

    Documentar nosso c\u00f3digo n\u00e3o deve ser uma restri\u00e7\u00e3o. Devemos aproveitar a oportunidade para garantir que isso ajude os clientes e mantenedores a entender o prop\u00f3sito do nosso c\u00f3digo.

    "},{"location":"pt-br/#nao-usando-linters-16","title":"N\u00e3o usando linters (#16)","text":"TL;DR

    Para melhorar a qualidade e consist\u00eancia do c\u00f3digo, use linters e formatadores.

    Um linter \u00e9 uma ferramenta autom\u00e1tica para analisar c\u00f3digo e detectar erros. O escopo desta se\u00e7\u00e3o n\u00e3o \u00e9 fornecer uma lista exaustiva dos linters existentes; caso contr\u00e1rio, ele ficar\u00e1 obsoleto rapidamente. Mas devemos entender e lembrar por que os linters s\u00e3o essenciais para a maioria dos projetos Go.

    No entanto, se voc\u00ea n\u00e3o \u00e9 um usu\u00e1rio regular de linters, aqui est\u00e1 uma lista que voc\u00ea pode usar diariamente:

    Al\u00e9m dos linters, tamb\u00e9m devemos usar formatadores de c\u00f3digo para corrigir o estilo do c\u00f3digo. Aqui est\u00e1 uma lista de alguns formatadores de c\u00f3digo para voc\u00ea experimentar:

    Enquanto isso, devemos tamb\u00e9m dar uma olhada em golangci-lint (https://github.com/golangci/golangci-lint). \u00c9 uma ferramenta de linting que fornece uma fachada sobre muitos linters e formatadores \u00fateis. Al\u00e9m disso, permite executar os linters em paralelo para melhorar a velocidade de an\u00e1lise, o que \u00e9 bastante \u00fatil.

    Linters e formatadores s\u00e3o uma forma poderosa de melhorar a qualidade e consist\u00eancia de nossa base de c\u00f3digo. Vamos dedicar um tempo para entender qual deles devemos usar e garantir que automatizamos sua execu\u00e7\u00e3o (como um precommit hook de CI ou Git).

    "},{"location":"pt-br/#tipos-de-dados","title":"Tipos de dados","text":""},{"location":"pt-br/#criando-confusao-com-literais-octais-17","title":"Criando confus\u00e3o com literais octais (#17)","text":"TL;DR

    Ao ler o c\u00f3digo existente, lembre-se de que literais inteiros come\u00e7ando com 0 s\u00e3o n\u00fameros octais. Al\u00e9m disso, para melhorar a legibilidade, torne os inteiros octais expl\u00edcitos prefixando-os com 0o.

    Os n\u00fameros octais come\u00e7am com 0 (por exemplo, 010 \u00e9 igual a 8 na base 10). Para melhorar a legibilidade e evitar poss\u00edveis erros para futuros leitores de c\u00f3digo, devemos tornar os n\u00fameros octais expl\u00edcitos usando o prefixo 0o (por exemplo, 0o10).

    Devemos tamb\u00e9m observar as outras representa\u00e7\u00f5es literais inteiras:

    Tamb\u00e9m podemos usar um caractere de sublinhado (_) como separador para facilitar a leitura. Por exemplo, podemos escrever 1 bilh\u00e3o desta forma: 1_000_000_000. Tamb\u00e9m podemos usar o caractere sublinhado com outras representa\u00e7\u00f5es (por exemplo, 0b00_00_01).

    C\u00f3digo fonte

    "},{"location":"pt-br/#negligenciando-estouros-de-numero-inteiro-18","title":"Negligenciando estouros de n\u00famero inteiro (#18)","text":"TL;DR

    Como os overflows e underflows de n\u00fameros inteiros s\u00e3o tratados silenciosamente no Go, voc\u00ea pode implementar suas pr\u00f3prias fun\u00e7\u00f5es para captur\u00e1-los.

    No Go, um estouro de n\u00famero inteiro que pode ser detectado em tempo de compila\u00e7\u00e3o gera um erro de compila\u00e7\u00e3o. Por exemplo,

    var counter int32 = math.MaxInt32 + 1\n
    constant 2147483648 overflows int32\n

    No entanto, em tempo de execu\u00e7\u00e3o, um overflow ou underflow de inteiro \u00e9 silencioso; isso n\u00e3o leva ao p\u00e2nico do aplicativo. \u00c9 essencial ter esse comportamento em mente, pois ele pode levar a bugs sorrateiros (por exemplo, um incremento de n\u00famero inteiro ou adi\u00e7\u00e3o de n\u00fameros inteiros positivos que leva a um resultado negativo).

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-entendendo-os-pontos-flutuantes-19","title":"N\u00e3o entendendo os pontos flutuantes (#19)","text":"TL;DR

    Fazer compara\u00e7\u00f5es de ponto flutuante dentro de um determinado delta pode garantir que seu c\u00f3digo seja port\u00e1til. Ao realizar adi\u00e7\u00e3o ou subtra\u00e7\u00e3o, agrupe as opera\u00e7\u00f5es com ordem de grandeza semelhante para favorecer a precis\u00e3o. Al\u00e9m disso, execute multiplica\u00e7\u00e3o e divis\u00e3o antes da adi\u00e7\u00e3o e subtra\u00e7\u00e3o.

    Em Go, existem dois tipos de ponto flutuante (se omitirmos os n\u00fameros imagin\u00e1rios): float32 e float64. O conceito de ponto flutuante foi inventado para resolver o principal problema dos n\u00fameros inteiros: sua incapacidade de representar valores fracion\u00e1rios. Para evitar surpresas desagrad\u00e1veis, precisamos saber que a aritm\u00e9tica de ponto flutuante \u00e9 uma aproxima\u00e7\u00e3o da aritm\u00e9tica real.

    Para isso, veremos um exemplo de multiplica\u00e7\u00e3o:

    var n float32 = 1.0001\nfmt.Println(n * n)\n

    Podemos esperar que este c\u00f3digo imprima o resultado de 1.0001 * 1.0001 = 1,00020001, certo? No entanto, execut\u00e1-lo na maioria dos processadores x86 imprime 1.0002.

    Como os tipos float32 e float64 em Go s\u00e3o aproxima\u00e7\u00f5es, temos que ter algumas regras em mente:

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-entendendo-o-comprimento-e-a-capacidade-de-slice-20","title":"N\u00e3o entendendo o comprimento e a capacidade de slice (#20)","text":"TL;DR

    Compreender a diferen\u00e7a entre comprimento e capacidade da slice deve fazer parte do conhecimento b\u00e1sico de um desenvolvedor Go. O comprimento de slice \u00e9 o n\u00famero de elementos dispon\u00edveis na slice, enquanto a capacidade de slice \u00e9 o n\u00famero de elementos na matriz de apoio.

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#inicializacao-de-slice-ineficiente-21","title":"Inicializa\u00e7\u00e3o de slice ineficiente (#21)","text":"TL;DR

    Ao criar uma fatia, inicialize-a com um determinado comprimento ou capacidade se o seu comprimento j\u00e1 for conhecido. Isso reduz o n\u00famero de aloca\u00e7\u00f5es e melhora o desempenho.

    Ao inicializar uma fatia usando make, podemos fornecer um comprimento e uma capacidade opcional. Esquecer de passar um valor apropriado para ambos os par\u00e2metros quando faz sentido \u00e9 um erro generalizado. Na verdade, isso pode levar a m\u00faltiplas c\u00f3pias e esfor\u00e7o adicional para o GC limpar as matrizes de apoio tempor\u00e1rias. Em termos de desempenho, n\u00e3o h\u00e1 uma boa raz\u00e3o para n\u00e3o ajudar o tempo de execu\u00e7\u00e3o do Go.

    Nossas op\u00e7\u00f5es s\u00e3o alocar uma fatia com determinada capacidade ou comprimento. Destas duas solu\u00e7\u00f5es, vimos que a segunda tende a ser um pouco mais r\u00e1pida. Mas usar uma determinada capacidade e anexar pode ser mais f\u00e1cil de implementar e ler em alguns contextos.

    C\u00f3digo fonte

    "},{"location":"pt-br/#estar-confuso-sobre-slice-nula-vs-slice-vazia-22","title":"Estar confuso sobre slice nula vs. slice vazia (#22)","text":"TL;DR

    To 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.

    No Go, h\u00e1 uma distin\u00e7\u00e3o entre slices nulas e vazias. Uma slice nula \u00e9 igual a nil, enquanto uma slice vazia tem comprimento zero. Uma slice nula est\u00e1 vazia, mas uma slice vazia n\u00e3o \u00e9 necessariamente nil. Enquanto isso, uma slice nula n\u00e3o requer nenhuma aloca\u00e7\u00e3o. Vimos ao longo desta se\u00e7\u00e3o como inicializar uma slice dependendo do contexto usando

    A \u00faltima op\u00e7\u00e3o, []string{} deve ser evitada se inicializarmos a fatia sem elementos. Finalmente, vamos verificar se as bibliotecas que usamos fazem distin\u00e7\u00f5es entre fatias nulas e vazias para evitar comportamentos inesperados.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-verificar-corretamente-se-um-slice-esta-vazio-23","title":"N\u00e3o verificar corretamente se um slice est\u00e1 vazio (#23)","text":"TL;DR

    Para verificar se uma fatia n\u00e3o cont\u00e9m nenhum elemento, verifique seu comprimento. Esta verifica\u00e7\u00e3o funciona independentemente de o slice estar nil ou vazio. O mesmo vale para maps. Para projetar APIs inequ\u00edvocas, voc\u00ea n\u00e3o deve distinguir entre slice nulos e vazios.

    Para determinar se um slice possui elementos, podemos faz\u00ea-lo verificando se o slice \u00e9 nulo ou se seu comprimento \u00e9 igual a 0. Verificar o comprimento \u00e9 a melhor op\u00e7\u00e3o a seguir, pois cobrir\u00e1 ambos se o slice estiver vazio ou se o slice \u00e9 nulo.

    Enquanto isso, ao projetar interfaces, devemos evitar distinguir slices nulos e vazios, o que leva a erros sutis de programa\u00e7\u00e3o. Ao retornar slices, n\u00e3o deve haver diferen\u00e7a sem\u00e2ntica nem t\u00e9cnica se retornarmos um slice nulo ou vazio. Ambos devem significar a mesma coisa para quem liga. Este princ\u00edpio \u00e9 o mesmo com maps. Para verificar se um map est\u00e1 vazio, verifique seu comprimento, n\u00e3o se \u00e9 nulo.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-fazer-copias-de-slcies-corretamente-24","title":"N\u00e3o fazer c\u00f3pias de slcies corretamente (#24)","text":"TL;DR

    Para copiar um slice para outro usando a fun\u00e7\u00e3o copy, lembre-se que o n\u00famero de elementos copiados corresponde ao m\u00ednimo entre os comprimentos dos dois slices.

    Copiar elementos de um slice para outro \u00e9 uma opera\u00e7\u00e3o razoavelmente frequente. Ao utilizar a c\u00f3pia, devemos lembrar que o n\u00famero de elementos copiados para o destino corresponde ao m\u00ednimo entre os comprimentos dos dois slices. Tenha tamb\u00e9m em mente que existem outras alternativas para copiar um slice, por isso n\u00e3o devemos nos surpreender se as encontrarmos em uma base de c\u00f3digo.

    C\u00f3digo fonte

    "},{"location":"pt-br/#efeitos-colaterais-inesperados-usando-o-slice-append-25","title":"Efeitos colaterais inesperados usando o slice append (#25)","text":"TL;DR

    Usar copy ou a express\u00e3o de slice completa \u00e9 uma forma de evitar que append crie conflitos se duas fun\u00e7\u00f5es diferentes usarem slices apoiados pela mesmo array. No entanto, apenas uma c\u00f3pia de slice evita vazamentos de mem\u00f3ria se voc\u00ea quiser reduzir um slice grande.

    Ao usar o slicing, devemos lembrar que podemos enfrentar uma situa\u00e7\u00e3o que leva a efeitos colaterais n\u00e3o intencionais. Se o slice resultante tiver um comprimento menor que sua capacidade, o acr\u00e9scimo poder\u00e1 alterar o slice original. Se quisermos restringir a gama de poss\u00edveis efeitos colaterais, podemos usar uma c\u00f3pia de slice ou a express\u00e3o de slice completa, o que nos impede de fazer uma c\u00f3pia.

    Note

    s[low:high:max](express\u00e3o de slice completo): Esta instru\u00e7\u00e3o cria um slice semelhante \u00e0quele criado com s[low:high], exceto que a capacidade de slice resultante \u00e9 igual a max - low.

    C\u00f3digo fonte

    "},{"location":"pt-br/#slices-e-vazamentos-de-memoria-26","title":"Slices e vazamentos de mem\u00f3ria (#26)","text":"TL;DR

    Trabalhando com um slice de ponteiros ou estruturas com campos de ponteiro, voc\u00ea pode evitar vazamentos de mem\u00f3ria marcando como nulos os elementos exclu\u00eddos por uma opera\u00e7\u00e3o de fatiamento.

    "},{"location":"pt-br/#vazamento-de-capacidade","title":"Vazamento de capacidade","text":"

    Lembre-se de que fatiar um slice ou array grande pode levar a um potencial alto consumo de mem\u00f3ria. O espa\u00e7o restante n\u00e3o ser\u00e1 recuperado pelo GC e podemos manter um grande array de apoio, apesar de usarmos apenas alguns elementos. Usar uma c\u00f3pia em slice \u00e9 a solu\u00e7\u00e3o para evitar tal caso.

    C\u00f3digo fonte

    "},{"location":"pt-br/#slice-e-ponteiros","title":"Slice e ponteiros","text":"

    Quando usamos a opera\u00e7\u00e3o de fatiamento com ponteiros ou estruturas com campos de ponteiro, precisamos saber que o GC n\u00e3o recuperar\u00e1 esses elementos. Nesse caso, as duas op\u00e7\u00f5es s\u00e3o realizar uma c\u00f3pia ou marcar explicitamente os elementos restantes ou seus campos como nil.

    C\u00f3digo fonte

    "},{"location":"pt-br/#inicializacao-ineficiente-do-mapa-27","title":"Inicializa\u00e7\u00e3o ineficiente do mapa (#27)","text":"TL;DR

    Ao criar um mapa, inicialize-o com um determinado comprimento se o seu comprimento j\u00e1 for conhecido. Isso reduz o n\u00famero de aloca\u00e7\u00f5es e melhora o desempenho.

    Um mapa fornece uma cole\u00e7\u00e3o n\u00e3o ordenada de pares chave-valor em que todas as chaves s\u00e3o distintas. No Go, um mapa \u00e9 baseado na estrutura de dados da tabela hash. Internamente, uma tabela hash \u00e9 uma matriz de intervalos e cada intervalo \u00e9 um ponteiro para uma matriz de pares de valores-chave.

    Se soubermos de antem\u00e3o o n\u00famero de elementos que um mapa conter\u00e1, devemos cri\u00e1-lo fornecendo um tamanho inicial. Fazer isso evita o crescimento potencial do mapa, o que \u00e9 bastante pesado em termos de computa\u00e7\u00e3o porque requer a realoca\u00e7\u00e3o de espa\u00e7o suficiente e o reequil\u00edbrio de todos os elementos.

    C\u00f3digo fonte

    "},{"location":"pt-br/#mapas-e-vazamentos-de-memoria-28","title":"Mapas e vazamentos de mem\u00f3ria (#28)","text":"TL;DR

    Um mapa sempre pode crescer na mem\u00f3ria, mas nunca diminui. Portanto, se isso causar alguns problemas de mem\u00f3ria, voc\u00ea pode tentar diferentes op\u00e7\u00f5es, como for\u00e7ar Go a recriar o mapa ou usar ponteiros.

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#comparando-valores-incorretamente-29","title":"Comparando valores incorretamente (#29)","text":"TL;DR

    Para comparar tipos em Go, voc\u00ea pode usar os operadores == e != se dois tipos forem compar\u00e1veis: booleanos, numerais, strings, ponteiros, canais e estruturas s\u00e3o compostos inteiramente de tipos compar\u00e1veis. Caso contr\u00e1rio, voc\u00ea pode usar reflect.DeepEquale pagar o pre\u00e7o da reflex\u00e3o ou usar implementa\u00e7\u00f5es e bibliotecas personalizadas.

    \u00c9 essencial entender como usar == e != para fazer compara\u00e7\u00f5es de forma eficaz. Podemos usar esses operadores em operandos compar\u00e1veis:

    Note

    Tamb\u00e9m podemos usar os operadores ?, >=, < e > com tipos num\u00e9ricos para comparar valores e com strings para comparar sua ordem lexical.

    Se os operandos n\u00e3o forem compar\u00e1veis \u200b\u200b(por exemplo, slices e mapas), teremos que usar outras op\u00e7\u00f5es, como reflex\u00e3o. A reflex\u00e3o \u00e9 uma forma de metaprograma\u00e7\u00e3o e se refere \u00e0 capacidade de um aplicativo de introspectar e modificar sua estrutura e comportamento. Por exemplo, em Go, podemos usar reflect.DeepEqual. Esta fun\u00e7\u00e3o informa se dois elementos s\u00e3o profundamente iguais percorrendo recursivamente dois valores. Os elementos que ele aceita s\u00e3o tipos b\u00e1sicos mais arrays, estruturas, slices, mapas, ponteiros, interfaces e fun\u00e7\u00f5es. No entanto, o principal problema \u00e9 a penalidade de desempenho.

    Se o desempenho for crucial em tempo de execu\u00e7\u00e3o, implementar nosso m\u00e9todo customizado pode ser a melhor solu\u00e7\u00e3o. Uma observa\u00e7\u00e3o adicional: devemos lembrar que a biblioteca padr\u00e3o possui alguns m\u00e9todos de compara\u00e7\u00e3o existentes. Por exemplo, podemos usar a fun\u00e7\u00e3o bytes.Compare otimizada para comparar duas slices de bytes. Antes de implementar um m\u00e9todo customizado, precisamos ter certeza de n\u00e3o reinventar a roda.

    C\u00f3digo fonte

    "},{"location":"pt-br/#estruturas-de-controle","title":"Estruturas de Controle","text":""},{"location":"pt-br/#ignorando-que-os-elementos-sao-copiados-em-loops-de-range-30","title":"Ignorando que os elementos s\u00e3o copiados em loops de range (#30)","text":"TL;DR

    O elemento de valor em um loop de range \u00e9 uma c\u00f3pia. Portanto, para modificar uma struct, por exemplo, acesse-a atrav\u00e9s de seu \u00edndice ou atrav\u00e9s de um loop for cl\u00e1ssico (a menos que o elemento ou campo que voc\u00ea deseja modificar seja um ponteiro).

    Um range loop permite iterar em diferentes estruturas de dados:

    Comparado a um for loop cl\u00e1ssico, um loop range \u00e9 uma maneira conveniente de iterar todos os elementos de uma dessas estruturas de dados, gra\u00e7as \u00e0 sua sintaxe concisa.

    Ainda assim, devemos lembrar que o elemento de valor em um range loop \u00e9 uma c\u00f3pia. Portanto, se o valor for uma estrutura que precisamos sofrer muta\u00e7\u00e3o, atualizaremos apenas a c\u00f3pia, n\u00e3o o elemento em si, a menos que o valor ou campo que modificamos seja um ponteiro. As op\u00e7\u00f5es preferidas s\u00e3o acessar o elemento atrav\u00e9s do \u00edndice usando um range loop ou um loop for cl\u00e1ssico.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-como-os-argumentos-sao-avaliados-em-range-loops-canais-e-arrays-31","title":"Ignorando como os argumentos s\u00e3o avaliados em range loops (canais e arrays) (#31)","text":"TL;DR

    Entender que a express\u00e3o passada ao operador range \u00e9 avaliada apenas uma vez antes do in\u00edcio do loop pode ajudar a evitar erros comuns, como atribui\u00e7\u00e3o ineficiente em canal ou itera\u00e7\u00e3o de slice.

    O range loop avalia a express\u00e3o fornecida apenas uma vez, antes do in\u00edcio do loop, fazendo uma c\u00f3pia (independentemente do tipo). Devemos lembrar deste comportamento para evitar erros comuns que podem, por exemplo, nos levar a acessar o elemento errado. Por exemplo:

    a := [3]int{0, 1, 2}\nfor i, v := range a {\n    a[2] = 10\n    if i == 2 {\n        fmt.Println(v)\n    }\n}\n

    Este c\u00f3digo atualiza o \u00faltimo \u00edndice para 10. No entanto, se executarmos este c\u00f3digo, ele n\u00e3o imprimir\u00e1 10; imprime 2.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-os-impactos-do-uso-de-elementos-ponteiros-em-range-loops-32","title":"Ignorando os impactos do uso de elementos ponteiros em range loops (#32)","text":"Warning

    Este erro n\u00e3o \u00e9 mais relevante no Go 1.22 (detalhes).

    "},{"location":"pt-br/#fazendo-suposicoes-erradas-durante-as-iteracoes-de-maps-ordenacao-e-insercao-do-mapa-durante-a-iteracao-33","title":"Fazendo suposi\u00e7\u00f5es erradas durante as itera\u00e7\u00f5es de maps (ordena\u00e7\u00e3o e inser\u00e7\u00e3o do mapa durante a itera\u00e7\u00e3o) (#33)","text":"TL;DR

    Para garantir resultados previs\u00edveis ao usar maps, lembre-se de que uma estrutura de dados de mapa:

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-como-a-declaracao-break-funciona-34","title":"Ignorando como a declara\u00e7\u00e3o break funciona (#34)","text":"TL;DR

    Usar break ou continue com um r\u00f3tulo imp\u00f5e a quebra de uma instru\u00e7\u00e3o espec\u00edfica. Isso pode ser \u00fatil com instru\u00e7\u00f5es switch ou select dentro de loops.

    Uma instru\u00e7\u00e3o break \u00e9 comumente usada para encerrar a execu\u00e7\u00e3o de um loop. Quando loops s\u00e3o usados \u200b\u200bem conjunto com switch ou select, os desenvolvedores frequentemente cometem o erro de quebrar a instru\u00e7\u00e3o errada. Por exemplo:

    for i := 0; i < 5; i++ {\n    fmt.Printf(\"%d \", i)\n\n    switch i {\n    default:\n    case 2:\n        break\n    }\n}\n

    A instru\u00e7\u00e3o break n\u00e3o encerra o loop for: em vez disso, ela encerra a instru\u00e7\u00e3o switch. Portanto, em vez de iterar de 0 a 2, este c\u00f3digo itera de 0 a 4: 0 1 2 3 4.

    Uma regra essencial a ter em mente \u00e9 que uma instru\u00e7\u00e3o break encerra a execu\u00e7\u00e3o da instru\u00e7\u00e3o for, switch, ou mais interna select. No exemplo anterior, ele encerra a instru\u00e7\u00e3o switch.

    Para quebrar o loop em vez da instru\u00e7\u00e3o switch, a maneira mais idiom\u00e1tica \u00e9 usar um r\u00f3tulo:

    loop:\n    for i := 0; i < 5; i++ {\n        fmt.Printf(\"%d \", i)\n\n        switch i {\n        default:\n        case 2:\n            break loop\n        }\n    }\n

    Aqui, associamos o loopr\u00f3tulo ao for loop. Ent\u00e3o, como fornecemos o loop r\u00f3tulo para a instru\u00e7\u00e3o break, ela interrompe o loop, n\u00e3o a op\u00e7\u00e3o. Portanto, esta nova vers\u00e3o ser\u00e1 impressa 0 1 2, como esper\u00e1vamos.

    C\u00f3digo fonte

    "},{"location":"pt-br/#usando-defer-dentro-de-um-loop-35","title":"Usando defer dentro de um loop (#35)","text":"TL;DR

    Extrair a l\u00f3gica do loop dentro de uma fun\u00e7\u00e3o leva \u00e0 execu\u00e7\u00e3o de uma instru\u00e7\u00e3o defer no final de cada itera\u00e7\u00e3o.

    A instru\u00e7\u00e3o defer atrasa a execu\u00e7\u00e3o de uma chamada at\u00e9 que a fun\u00e7\u00e3o circundante retorne. \u00c9 usado principalmente para reduzir o c\u00f3digo padr\u00e3o. Por exemplo, se um recurso precisar ser fechado eventualmente, podemos usar defer para evitar a repeti\u00e7\u00e3o das chamadas de fechamento antes de cada return.

    Um erro comum com defer \u00e9 esquecer que ele agenda uma chamada de fun\u00e7\u00e3o quando a fun\u00e7\u00e3o circundante retorna. Por exemplo:

    func readFiles(ch <-chan string) error {\n    for path := range ch {\n        file, err := os.Open(path)\n        if err != nil {\n            return err\n        }\n\n        defer file.Close()\n\n        // Do something with file\n    }\n    return nil\n}\n

    As chamadas defer n\u00e3o s\u00e3o executadas durante cada itera\u00e7\u00e3o do loop, mas quando a fun\u00e7\u00e3o readFiles retorna. Se readFiles n\u00e3o retornar, os descritores de arquivos ficar\u00e3o abertos para sempre, causando vazamentos.

    Uma op\u00e7\u00e3o comum para corrigir esse problema \u00e9 criar uma fun\u00e7\u00e3o circundante ap\u00f3s defer, chamada durante cada itera\u00e7\u00e3o:

    func readFiles(ch <-chan string) error {\n    for path := range ch {\n        if err := readFile(path); err != nil {\n            return err\n        }\n    }\n    return nil\n}\n\nfunc readFile(path string) error {\n    file, err := os.Open(path)\n    if err != nil {\n        return err\n    }\n\n    defer file.Close()\n\n    // Do something with file\n    return nil\n}\n

    Outra solu\u00e7\u00e3o \u00e9 tornar a fun\u00e7\u00e3o readFile um encerramento, mas intrinsecamente, esta permanece a mesma solu\u00e7\u00e3o: adicionar outra fun\u00e7\u00e3o circundante para executar as chamadas defer durante cada itera\u00e7\u00e3o.

    C\u00f3digo fonte

    "},{"location":"pt-br/#strings","title":"Strings","text":""},{"location":"pt-br/#nao-entendendo-o-conceito-de-rune-36","title":"N\u00e3o entendendo o conceito de rune (#36)","text":"TL;DR

    Entender que uma runa corresponde ao conceito de um ponto de c\u00f3digo Unicode e que pode ser composta de m\u00faltiplos bytes deve fazer parte do conhecimento b\u00e1sico do desenvolvedor Go para trabalhar com precis\u00e3o com strings.

    Como as runas est\u00e3o por toda parte no Go, \u00e9 importante entender o seguinte:

    C\u00f3digo fonte

    "},{"location":"pt-br/#iteracao-de-string-imprecisa-37","title":"Itera\u00e7\u00e3o de string imprecisa (#37)","text":"TL;DR

    Iterar em uma string com o operador range itera nas runas com o \u00edndice correspondente ao \u00edndice inicial da sequ\u00eancia de bytes da runa. Para acessar um \u00edndice de runa espec\u00edfico (como a terceira runa), converta a string em um arquivo []rune.

    Iterar em uma string \u00e9 uma opera\u00e7\u00e3o comum para desenvolvedores. Talvez queiramos realizar uma opera\u00e7\u00e3o para cada runa na string ou implementar uma fun\u00e7\u00e3o personalizada para procurar uma substring espec\u00edfica. Em ambos os casos, temos que iterar nas diferentes runas de uma string. Mas \u00e9 f\u00e1cil ficar confuso sobre como funciona a itera\u00e7\u00e3o.

    For example, consider the following example:

    s := \"h\u00eallo\"\nfor i := range s {\n    fmt.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

    Vamos destacar tr\u00eas pontos que podem ser confusos:

    Vamos come\u00e7ar com a \u00faltima observa\u00e7\u00e3o. J\u00e1 mencionamos que len retorna o n\u00famero de bytes em uma string, n\u00e3o o n\u00famero de runas. Como atribu\u00edmos uma string literal a s, s \u00e9 uma string UTF-8. Enquanto isso, o caractere especial \u201c\u00ea\u201d n\u00e3o \u00e9 codificado em um \u00fanico byte; requer 2 bytes. Portanto, chamar len(s) retorna 6.

    Enquanto isso, no exemplo anterior, temos que entender que n\u00e3o repetimos cada runa; em vez disso, iteramos sobre cada \u00edndice inicial de uma runa:

    Imprimir s[i] n\u00e3o imprime a i-\u00e9sima runa; imprime a representa\u00e7\u00e3o UTF-8 do byte em index i. Portanto, imprimimos \"h\u00c3llo\" em vez de \"h\u00eallo\".

    Se quisermos imprimir todas as diferentes runas, podemos usar o elemento value do operador range:

    s := \"h\u00eallo\"\nfor i, r := range s {\n    fmt.Printf(\"position %d: %c\\n\", i, r)\n}\n

    Ou podemos converter a string em uma fatia de runas e iterar sobre ela:

    s := \"h\u00eallo\"\nrunes := []rune(s)\nfor i, r := range runes {\n    fmt.Printf(\"position %d: %c\\n\", i, r)\n}\n

    Observe que esta solu\u00e7\u00e3o introduz uma sobrecarga de tempo de execu\u00e7\u00e3o em compara\u00e7\u00e3o com a anterior. Na verdade, converter uma string em uma fatia de runas requer a aloca\u00e7\u00e3o de uma fatia adicional e a convers\u00e3o dos bytes em runas: uma complexidade de tempo O(n) com n o n\u00famero de bytes na string. Portanto, se quisermos iterar todas as runas, devemos usar a primeira solu\u00e7\u00e3o.

    Por\u00e9m, se quisermos acessar a i-\u00e9sima runa de uma string com a primeira op\u00e7\u00e3o, n\u00e3o teremos acesso ao \u00edndice da runa; em vez disso, conhecemos o \u00edndice inicial de uma runa na sequ\u00eancia de bytes.

    s := \"h\u00eallo\"\nr := []rune(s)[4]\nfmt.Printf(\"%c\\n\", r) // o\n

    C\u00f3digo fonte

    "},{"location":"pt-br/#uso-indevido-de-funcoes-de-trim-38","title":"Uso indevido de fun\u00e7\u00f5es de trim (#38)","text":"TL;DR

    strings.TrimRight/strings.TrimLeft remove todas as runas finais/iniciais contidas em um determinado conjunto, enquanto strings.TrimSuffix/strings.TrimPrefix retorna uma string sem um sufixo/prefixo fornecido.

    Por exemplo:

    fmt.Println(strings.TrimRight(\"123oxo\", \"xo\"))\n

    O exemplo imprime 123:

    Por outro lado, strings.TrimLeft remove todas as runas principais contidas em um conjunto.

    Por outro lado, strings.TrimSuffix/strings.TrimPrefix retorna uma string sem o sufixo/prefixo final fornecido.

    C\u00f3digo fonte

    "},{"location":"pt-br/#concatenacao-de-strings-subotimizada-39","title":"Concatena\u00e7\u00e3o de strings subotimizada (#39)","text":"TL;DR

    A concatena\u00e7\u00e3o de uma lista de strings deve ser feita com strings.Builder para evitar a aloca\u00e7\u00e3o de uma nova string durante cada itera\u00e7\u00e3o.

    Vamos considerar uma fun\u00e7\u00e3o concat que concatena todos os elementos string de uma fatia usando o operador +=:

    func concat(values []string) string {\n    s := \"\"\n    for _, value := range values {\n        s += value\n    }\n    return s\n}\n

    Durante cada itera\u00e7\u00e3o, o operador += concatena com s a sequ\u00eancia de valores. \u00c0 primeira vista, esta fun\u00e7\u00e3o pode n\u00e3o parecer errada. Mas com esta implementa\u00e7\u00e3o, esquecemos uma das principais caracter\u00edsticas de uma string: a sua imutabilidade. Portanto, cada itera\u00e7\u00e3o n\u00e3o \u00e9 atualizada s; ele realoca uma nova string na mem\u00f3ria, o que impacta significativamente o desempenho desta fun\u00e7\u00e3o.

    Felizmente, existe uma solu\u00e7\u00e3o para lidar com esse problema, usando strings.Builder:

    func concat(values []string) string {\n    sb := strings.Builder{}\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return sb.String()\n}\n

    Durante cada itera\u00e7\u00e3o, constru\u00edmos a string resultante chamando o m\u00e9todo WriteString que anexa o conte\u00fado do valor ao seu buffer interno, minimizando assim a c\u00f3pia da mem\u00f3ria.

    Note

    WriteString retorna um erro como segunda sa\u00edda, mas n\u00f3s o ignoramos propositalmente. Na verdade, este m\u00e9todo nunca retornar\u00e1 um erro diferente de zero. Ent\u00e3o, qual \u00e9 o prop\u00f3sito deste m\u00e9todo retornar um erro como parte de sua assinatura? strings.Builder implementa a io.StringWriter interface, que cont\u00e9m um \u00fanico m\u00e9todo: WriteString(s string) (n int, err error). Portanto, para estar em conformidade com esta interface, WriteString deve retornar um erro.

    Internamente, strings.Builder cont\u00e9m uma fatia de bytes. Cada chamada para WriteString resulta em uma chamada para anexar nesta fatia. Existem dois impactos. Primeiro, esta estrutura n\u00e3o deve ser usada simultaneamente, pois as chamadas append levariam a condi\u00e7\u00f5es de corrida. O segundo impacto \u00e9 algo que vimos no mistake #21, \"Inicializa\u00e7\u00e3o de slice ineficiente\": se o comprimento futuro de uma slice j\u00e1 for conhecido, devemos pr\u00e9-aloc\u00e1-la. Para isso, strings.Builder exp\u00f5e um m\u00e9todo Grow(n int) para garantir espa\u00e7o para outros n bytes:

    func concat(values []string) string {\n    total := 0\n    for i := 0; i < len(values); i++ {\n        total += len(values[i])\n    }\n\n    sb := strings.Builder{}\n    sb.Grow(total) (2)\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return sb.String()\n}\n

    Vamos executar um benchmark para comparar as tr\u00eas vers\u00f5es (v1 usando +=; v2 usando strings.Builder{} sem pr\u00e9-aloca\u00e7\u00e3o; e v3 usando strings.Builder{} com pr\u00e9-aloca\u00e7\u00e3o). A slice de entrada cont\u00e9m 1.000 strings e cada string cont\u00e9m 1.000 bytes:

    BenchmarkConcatV1-4             16      72291485 ns/op\nBenchmarkConcatV2-4           1188        878962 ns/op\nBenchmarkConcatV3-4           5922        190340 ns/op\n

    Como podemos ver, a vers\u00e3o mais recente \u00e9 de longe a mais eficiente: 99% mais r\u00e1pida que a v1 e 78% mais r\u00e1pida que a v2.

    strings.Builder \u00e9 a solu\u00e7\u00e3o recomendada para concatenar uma lista de strings. Normalmente, esta solu\u00e7\u00e3o deve ser usada dentro de um loop. Na verdade, se precisarmos apenas concatenar algumas strings (como um nome e um sobrenome), o uso strings.Builder n\u00e3o \u00e9 recomendado, pois isso tornar\u00e1 o c\u00f3digo um pouco menos leg\u00edvel do que usar o operador += or fmt.Sprintf.

    C\u00f3digo fonte

    "},{"location":"pt-br/#conversoes-de-string-inuteis-40","title":"Convers\u00f5es de string in\u00fateis (#40)","text":"TL;DR

    Lembrar que o pacote bytes oferece as mesmas opera\u00e7\u00f5es que o pacote strings pode ajudar a evitar convers\u00f5es extras de bytes/string.

    Ao optar por trabalhar com uma string ou um []byte, a maioria dos programadores tende a preferir strings por conveni\u00eancia. Mas a maior parte da E/S \u00e9 realmente feita com []byte. Por exemplo, io.Reader, io.Writer e io.ReadAll trabalham com []byte, n\u00e3o com strings.

    Quando nos perguntamos se devemos trabalhar com strings ou []byte, lembremos que trabalhar com []byten\u00e3o \u00e9 necessariamente menos conveniente. Na verdade, todas as fun\u00e7\u00f5es exportadas do pacote strings tamb\u00e9m possuem alternativas no pacote bytes: Split, Count, Contains, Index e assim por diante. Portanto, estejamos fazendo I/O ou n\u00e3o, devemos primeiro verificar se poder\u00edamos implementar um fluxo de trabalho completo usando bytes em vez de strings e evitar o pre\u00e7o de convers\u00f5es adicionais.

    C\u00f3digo fonte

    "},{"location":"pt-br/#vazamentos-de-substring-e-memoria-41","title":"Vazamentos de substring e mem\u00f3ria (#41)","text":"TL;DR

    Usar c\u00f3pias em vez de substrings pode evitar vazamentos de mem\u00f3ria, pois a string retornada por uma opera\u00e7\u00e3o de substring ser\u00e1 apoiada pela mesma matriz de bytes.

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#functions-and-methods","title":"Functions and Methods","text":""},{"location":"pt-br/#nao-saber-que-tipo-de-receptor-usar-42","title":"N\u00e3o saber que tipo de receptor usar (#42)","text":"TL;DR

    A decis\u00e3o de usar um valor ou um receptor de ponteiro deve ser tomada com base em fatores como o tipo, se deve sofrer muta\u00e7\u00e3o, se cont\u00e9m um campo que n\u00e3o pode ser copiado e o tamanho do objeto. Em caso de d\u00favida, use um receptor de ponteiro.

    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\n\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

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nunca-usando-parametros-de-resultado-nomeados-43","title":"Nunca usando par\u00e2metros de resultado nomeados (#43)","text":"TL;DR

    Usar par\u00e2metros de resultado nomeados pode ser uma maneira eficiente de melhorar a legibilidade de uma fun\u00e7\u00e3o/m\u00e9todo, especialmente se v\u00e1rios par\u00e2metros de resultado tiverem o mesmo tipo. Em alguns casos, esta abordagem tamb\u00e9m pode ser conveniente porque os par\u00e2metros de resultado nomeados s\u00e3o inicializados com seu valor zero. Mas tenha cuidado com os poss\u00edveis efeitos colaterais.

    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) {\n    b = a\n    return\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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#efeitos-colaterais-nao-intencionais-com-parametros-de-resultado-nomeados-44","title":"Efeitos colaterais n\u00e3o intencionais com par\u00e2metros de resultado nomeados (#44)","text":"TL;DR

    Consulte #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) (\n    lat, lng float32, err error) {\n    isValid := l.validateAddress(address) (1)\n    if !isValid {\n        return 0, 0, errors.New(\"invalid address\")\n    }\n\n    if ctx.Err() != nil { (2)\n        return 0, 0, err\n    }\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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#retornando-um-receptor-nulo-45","title":"Retornando um receptor nulo (#45)","text":"TL;DR

    Ao retornar uma interface, tenha cuidado para n\u00e3o retornar um ponteiro nulo, mas um valor nulo expl\u00edcito. Caso contr\u00e1rio, poder\u00e3o ocorrer consequ\u00eancias n\u00e3o intencionais e o chamador receber\u00e1 um valor diferente de zero.

    C\u00f3digo fonte

    "},{"location":"pt-br/#usando-um-nome-de-arquivo-como-entrada-de-funcao-46","title":"Usando um nome de arquivo como entrada de fun\u00e7\u00e3o (#46)","text":"TL;DR

    Projetar fun\u00e7\u00f5es para receber tipos io.Reader em vez de nomes de arquivos melhora a capacidade de reutiliza\u00e7\u00e3o de uma fun\u00e7\u00e3o e facilita o teste.

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-como-argumentos-defer-e-receptores-sao-avaliados-avaliacao-de-argumentos-ponteiros-e-receptores-de-valor-47","title":"Ignorando como argumentos defer e receptores s\u00e3o avaliados (avalia\u00e7\u00e3o de argumentos, ponteiros e receptores de valor) (#47)","text":"TL;DR

    Passar um ponteiro para uma fun\u00e7\u00e3o defer e agrupar uma chamada dentro de um closure s\u00e3o duas solu\u00e7\u00f5es poss\u00edveis para superar a avalia\u00e7\u00e3o imediata de argumentos e receptores.

    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 (\n    StatusSuccess  = \"success\"\n    StatusErrorFoo = \"error_foo\"\n    StatusErrorBar = \"error_bar\"\n)\n\nfunc f() error {\n    var status string\n    defer notify(status)\n    defer incrementCounter(status)\n\n    if err := foo(); err != nil {\n        status = StatusErrorFoo\n        return err\n    }\n\n    if err := bar(); err != nil {\n        status = StatusErrorBar\n        return err\n    }\n\n    status = StatusSuccess <5>\n    return 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 {\n    var status string\n    defer notify(&status) \n    defer incrementCounter(&status)\n\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 {\n    var status string\n    defer func() {\n        notify(status)\n        incrementCounter(status)\n    }()\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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#error-management","title":"Error Management","text":""},{"location":"pt-br/#panico-48","title":"P\u00e2nico (#48)","text":"TL;DR

    Usar panic \u00e9 uma op\u00e7\u00e3o para lidar com erros no Go. No entanto, s\u00f3 deve ser usado com modera\u00e7\u00e3o em condi\u00e7\u00f5es irrecuper\u00e1veis: por exemplo, para sinalizar um erro do programador ou quando voc\u00ea n\u00e3o consegue carregar uma depend\u00eancia obrigat\u00f3ria.

    In Go, panic is a built-in function that stops the ordinary flow:

    func main() {\n    fmt.Println(\"a\")\n    panic(\"foo\")\n    fmt.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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-quando-embrulhar-um-erro-49","title":"Ignorando quando embrulhar um erro (#49)","text":"TL;DR

    Embrulhar um erro permite marcar um erro e/ou fornecer contexto adicional. No entanto, o agrupamento de erros cria um acoplamento potencial, pois disponibiliza o erro de origem para o chamador. Se voc\u00ea quiser evitar isso, n\u00e3o use a agrupamento autom\u00e1tico de erros.

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#comparando-um-tipo-de-erro-de-forma-imprecisa-50","title":"Comparando um tipo de erro de forma imprecisa (#50)","text":"TL;DR

    Se voc\u00ea usar o agrupamento de erros do Go 1.13 com a diretiva %w e fmt.Errorf, a compara\u00e7\u00e3o de um erro com um tipo dever\u00e1 ser feita usando errors.As. Caso contr\u00e1rio, se o erro retornado que voc\u00ea deseja verificar for embrulhado, as verifica\u00e7\u00f5es falhar\u00e3o.

    C\u00f3digo fonte

    "},{"location":"pt-br/#comparando-um-valor-de-erro-incorretamente-51","title":"Comparando um valor de erro incorretamente (#51)","text":"TL;DR

    Se voc\u00ea usar o agrupamento de erros do Go 1.13 com a diretiva %w e fmt.Errorf, a compara\u00e7\u00e3o de um erro ou de um valor dever\u00e1 ser feita usando errors.As. Caso contr\u00e1rio, se o erro retornado que voc\u00ea deseja verificar for embrulhado, as verifica\u00e7\u00f5es falhar\u00e3o.

    A sentinel error is an error defined as a global variable:

    import \"errors\"\n\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:

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#lidando-com-um-erro-duas-vezes-52","title":"Lidando com um erro duas vezes (#52)","text":"TL;DR

    Na maioria das situa\u00e7\u00f5es, um erro deve ser tratado apenas uma vez. Registrar um erro \u00e9 tratar um erro. Portanto, voc\u00ea deve escolher entre registrar ou retornar um erro. Em muitos casos, o embrulho autom\u00e1tico de erros \u00e9 a solu\u00e7\u00e3o, pois permite fornecer contexto adicional a um erro e retornar o erro de origem.

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-tratando-de-um-erro-53","title":"N\u00e3o tratando de um erro (#53)","text":"TL;DR

    Ignorar um erro, seja durante uma chamada de fun\u00e7\u00e3o ou em uma fun\u00e7\u00e3o defer, deve ser feito explicitamente usando o identificador em branco. Caso contr\u00e1rio, os futuros leitores poder\u00e3o ficar confusos sobre se foi intencional ou um erro.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-tratando-erros-de-defer-54","title":"N\u00e3o tratando erros de defer (#54)","text":"TL;DR

    Em muitos casos, voc\u00ea n\u00e3o deve ignorar um erro retornado por uma fun\u00e7\u00e3o defer. Manipule-o diretamente ou propague-o para o chamador, dependendo do contexto. Se voc\u00ea quiser ignor\u00e1-lo, use o identificador em branco.

    Consider the following code:

    func f() {\n  // ...\n  notify() // Error handling is omitted\n}\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

    C\u00f3digo fonte

    "},{"location":"pt-br/#concurrency-foundations","title":"Concurrency: Foundations","text":""},{"location":"pt-br/#misturando-simultaneidade-e-paralelismo-55","title":"Misturando simultaneidade e paralelismo (#55)","text":"TL;DR

    Compreender as diferen\u00e7as fundamentais entre simultaneidade e paralelismo \u00e9 a base do conhecimento do desenvolvedor Go. A simultaneidade tem a ver com estrutura, enquanto o paralelismo tem a ver com execu\u00e7\u00e3o.

    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":"pt-br/#pensar-que-a-simultaneidade-e-sempre-mais-rapida-56","title":"Pensar que a simultaneidade \u00e9 sempre mais r\u00e1pida (#56)","text":"TL;DR

    Para ser um desenvolvedor proficiente, voc\u00ea deve reconhecer que a simultaneidade nem sempre \u00e9 mais r\u00e1pida. As solu\u00e7\u00f5es que envolvem a paraleliza\u00e7\u00e3o de cargas de trabalho m\u00ednimas podem n\u00e3o ser necessariamente mais r\u00e1pidas do que uma implementa\u00e7\u00e3o sequencial. A avalia\u00e7\u00e3o comparativa de solu\u00e7\u00f5es sequenciais versus solu\u00e7\u00f5es simult\u00e2neas deve ser a forma de validar suposi\u00e7\u00f5es.

    Read the full section here.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ficar-confuso-sobre-quando-usar-canais-ou-mutexes-57","title":"Ficar confuso sobre quando usar canais ou mutexes (#57)","text":"TL;DR

    Estar ciente das intera\u00e7\u00f5es goroutine tamb\u00e9m pode ser \u00fatil ao decidir entre canais e mutexes. Em geral, goroutines paralelas requerem sincroniza\u00e7\u00e3o e, portanto, mutexes. Por outro lado, goroutines simult\u00e2neas geralmente requerem coordena\u00e7\u00e3o e orquestra\u00e7\u00e3o e, portanto, canais.

    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.

    "},{"location":"pt-br/#nao-entender-os-problemas-de-corrida-corridas-de-dados-vs-condicoes-de-corrida-e-o-modelo-de-memoria-go-58","title":"N\u00e3o entender os problemas de corrida (corridas de dados vs. condi\u00e7\u00f5es de corrida e o modelo de mem\u00f3ria Go) (#58)","text":"TL;DR

    Ser proficiente em simultaneidade tamb\u00e9m significa compreender que corridas de dados e condi\u00e7\u00f5es de corrida s\u00e3o conceitos diferentes. As corridas de dados ocorrem quando v\u00e1rias goroutines acessam simultaneamente o mesmo local de mem\u00f3ria e pelo menos uma delas est\u00e1 gravando. Enquanto isso, estar livre de disputa de dados n\u00e3o significa necessariamente execu\u00e7\u00e3o determin\u00edstica. Quando um comportamento depende da sequ\u00eancia ou do tempo de eventos que n\u00e3o podem ser controlados, esta \u00e9 uma condi\u00e7\u00e3o de corrida.

    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":"pt-br/#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:

    "},{"location":"pt-br/#race-condition","title":"Race Condition","text":"

    Depending 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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-compreender-os-impactos-de-simultaneidade-de-um-tipo-de-carga-de-trabalho-59","title":"N\u00e3o compreender os impactos de simultaneidade de um tipo de carga de trabalho (#59)","text":"TL;DR

    Ao criar um determinado n\u00famero de goroutines, considere o tipo de carga de trabalho. Criar goroutines vinculadas \u00e0 CPU significa limitar esse n\u00famero pr\u00f3ximo \u00e0 vari\u00e1vel GOMAXPROCS (baseado por padr\u00e3o no n\u00famero de n\u00facleos de CPU no host). A cria\u00e7\u00e3o de goroutines vinculadas a E/S depende de outros fatores, como o sistema externo.

    In programming, the execution time of a workload is limited by one of the following:

    Note

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#incompreensao-dos-contextos-go-60","title":"Incompreens\u00e3o dos contextos Go (#60)","text":"TL;DR

    Os contextos Go tamb\u00e9m s\u00e3o um dos pilares da simultaneidade em Go. Um contexto permite que voc\u00ea carregue um prazo, um sinal de cancelamento e/ou uma lista de valores-chave.

    https://pkg.go.dev/context

    A Context carries a deadline, a cancellation signal, and other values across API boundaries.

    "},{"location":"pt-br/#deadline","title":"Deadline","text":"

    A deadline refers to a specific point in time determined with one of the following:

    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":"pt-br/#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.

    "},{"location":"pt-br/#context-values","title":"Context values","text":"

    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":"pt-br/#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,

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#concurrency-practice","title":"Concurrency: Practice","text":""},{"location":"pt-br/#propagando-um-contexto-improprio-61","title":"Propagando um contexto impr\u00f3prio (#61)","text":"TL;DR

    Compreender as condi\u00e7\u00f5es em que um contexto pode ser cancelado deve ser importante ao propag\u00e1-lo: por exemplo, um manipulador HTTP cancelando o contexto quando a resposta for enviada.

    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) {\n    response, err := doSomeTask(r.Context(), r)\n    if err != nil {\n        http.Error(w, err.Error(), http.StatusInternalServerError)\n    return\n    }\n    go func() {\n        err := publish(r.Context(), response)\n        // Do something with err\n    }()\n    writeResponse(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.

    Note

    From 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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#iniciando-uma-goroutine-sem-saber-quando-interrompe-la-62","title":"Iniciando uma goroutine sem saber quando interromp\u00ea-la (#62)","text":"TL;DR

    Evitar vazamentos significa estar ciente de que sempre que uma goroutine for iniciada, voc\u00ea deve ter um plano para interromp\u00ea-la eventualmente.

    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() {\n    newWatcher()\n    // Run the application\n}\n\ntype watcher struct { /* Some resources */ }\n\nfunc newWatcher() {\n    w := watcher{}\n    go 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() {\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n    newWatcher(ctx)\n    // Run the application\n}\n\nfunc newWatcher(ctx context.Context) {\n    w := watcher{}\n    go 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() {\n    w := newWatcher()\n    defer w.close()\n    // Run the application\n}\n\nfunc newWatcher() watcher {\n    w := watcher{}\n    go w.watch()\n    return w\n}\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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-ter-cuidado-com-goroutines-e-variaveis-de-loop-63","title":"N\u00e3o ter cuidado com goroutines e vari\u00e1veis \u200b\u200bde loop (#63)","text":"Warning

    Este erro n\u00e3o \u00e9 mais relevante no Go 1.22 (detalhes).

    "},{"location":"pt-br/#esperando-um-comportamento-deterministico-usando-selecao-e-canais-64","title":"Esperando um comportamento determin\u00edstico usando sele\u00e7\u00e3o e canais (#64)","text":"TL;DR

    Compreender que com select v\u00e1rios canais escolhe o caso aleatoriamente se m\u00faltiplas op\u00e7\u00f5es forem poss\u00edveis evita fazer suposi\u00e7\u00f5es erradas que podem levar a erros sutis de simultaneidade.

    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() {\n  for i := 0; i < 10; i++ {\n      messageCh <- i\n    }\n    disconnectCh <- struct{}{}\n}()\n\nfor {\n    select {\n    case v := <-messageCh:\n        fmt.Println(v)\n    case <-disconnectCh:\n        fmt.Println(\"disconnection, return\")\n        return\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 statement 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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-usar-canais-de-notificacao-65","title":"N\u00e3o usar canais de notifica\u00e7\u00e3o (#65)","text":"TL;DR

    Envie notifica\u00e7\u00f5es usando um tipo chan struct{}.

    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{}.

    "},{"location":"pt-br/#nao-usar-canais-nulos-66","title":"N\u00e3o usar canais nulos (#66)","text":"TL;DR

    O uso de canais nulos deve fazer parte do seu conjunto de ferramentas de simultaneidade porque permite remover casos de instru\u00e7\u00f5es select, por exemplo.

    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 {\n    ch := make(chan int, 1)\n\n    go func() {\n        for ch1 != nil || ch2 != nil { // Continue if at least one channel isn\u2019t nil\n            select {\n            case v, open := <-ch1:\n                if !open {\n                    ch1 = nil // Assign ch1 to a nil channel once closed\n                    break\n                }\n                ch <- v\n            case v, open := <-ch2:\n                if !open {\n                    ch2 = nil // Assigns ch2 to a nil channel once closed\n                    break\n                }\n                ch <- v\n            }\n        }\n        close(ch)\n    }()\n\n    return 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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ficar-intrigado-com-o-tamanho-do-canal-67","title":"Ficar intrigado com o tamanho do canal (#67)","text":"TL;DR

    Decida cuidadosamente o tipo de canal correto a ser usado, considerando o problema. Somente canais sem buffer oferecem fortes garantias de sincroniza\u00e7\u00e3o. Para canais em buffer, voc\u00ea deve ter um bom motivo para especificar um tamanho de canal diferente de um.

    An unbuffered channel is a channel without any capacity. It can be created by either omitting the size or providing a 0 size:

    ch1 := make(chan int)\nch2 := make(chan int, 0)\n

    With an unbuffered channel (sometimes called a synchronous channel), the sender will block until the receiver receives data from the channel.

    Conversely, a buffered channel has a capacity, and it must be created with a size greater than or equal to 1:

    ch3 := make(chan int, 1)\n

    With a buffered channel, a sender can send messages while the channel isn\u2019t full. Once the channel is full, it will block until a receiver goroutine receives a message:

    ch3 := make(chan int, 1)\nch3 <-1 // Non-blocking\nch3 <-2 // Blocking\n

    The first send isn\u2019t blocking, whereas the second one is, as the channel is full at this stage.

    What's the main difference between unbuffered and buffered channels:

    If we need a buffered channel, what size should we provide?

    The default value we should use for buffered channels is its minimum: 1. So, we may approach the problem from this standpoint: is there any good reason not to use a value of 1? Here\u2019s a list of possible cases where we should use another size:

    If we are outside of these cases, using a different channel size should be done cautiously. Let\u2019s bear in mind that deciding about an accurate queue size isn\u2019t an easy problem:

    Martin Thompson

    Queues are typically always close to full or close to empty due to the differences in pace between consumers and producers. They very rarely operate in a balanced middle ground where the rate of production and consumption is evenly matched.

    "},{"location":"pt-br/#esquecendo-os-possiveis-efeitos-colaterais-da-formatacao-de-strings-68","title":"Esquecendo os poss\u00edveis efeitos colaterais da formata\u00e7\u00e3o de strings (#68)","text":"TL;DR

    Estar ciente de que a formata\u00e7\u00e3o de strings pode levar \u00e0 chamada de fun\u00e7\u00f5es existentes significa estar atento a poss\u00edveis impasses e outras disputas de dados.

    It\u2019s pretty easy to forget the potential side effects of string formatting while working in a concurrent application.

    "},{"location":"pt-br/#etcd-data-race","title":"etcd data race","text":"

    github.com/etcd-io/etcd/pull/7816 shows an example of an issue where a map's key was formatted based on a mutable values from a context.

    "},{"location":"pt-br/#deadlock","title":"Deadlock","text":"

    Can you see what the problem is in this code with a Customer struct exposing an UpdateAge method and implementing the fmt.Stringer interface?

    type Customer struct {\n    mutex sync.RWMutex // Uses a sync.RWMutex to protect concurrent accesses\n    id    string\n    age   int\n}\n\nfunc (c *Customer) UpdateAge(age int) error {\n    c.mutex.Lock() // Locks and defers unlock as we update Customer\n    defer c.mutex.Unlock()\n\n    if age < 0 { // Returns an error if age is negative\n        return fmt.Errorf(\"age should be positive for customer %v\", c)\n    }\n\n    c.age = age\n    return nil\n}\n\nfunc (c *Customer) String() string {\n    c.mutex.RLock() // Locks and defers unlock as we read Customer\n    defer c.mutex.RUnlock()\n    return fmt.Sprintf(\"id %s, age %d\", c.id, c.age)\n}\n

    The problem here may not be straightforward. If the provided age is negative, we return an error. Because the error is formatted, using the %s directive on the receiver, it will call the String method to format Customer. But because UpdateAge already acquires the mutex lock, the String method won\u2019t be able to acquire it. Hence, this leads to a deadlock situation. If all goroutines are also asleep, it leads to a panic.

    One possible solution is to restrict the scope of the mutex lock:

    func (c *Customer) UpdateAge(age int) error {\n    if age < 0 {\n        return fmt.Errorf(\"age should be positive for customer %v\", c)\n    }\n\n    c.mutex.Lock() <1>\n    defer c.mutex.Unlock()\n\n    c.age = age\n    return nil\n}\n

    Yet, such an approach isn't always possible. In these conditions, we have to be extremely careful with string formatting.

    Another approach is to access the id field directly:

    func (c *Customer) UpdateAge(age int) error {\n    c.mutex.Lock()\n    defer c.mutex.Unlock()\n\n    if age < 0 {\n        return fmt.Errorf(\"age should be positive for customer id %s\", c.id)\n    }\n\n    c.age = age\n    return nil\n}\n

    In concurrent applications, we should remain cautious about the possible side effects of string formatting.

    C\u00f3digo fonte

    "},{"location":"pt-br/#criando-corridas-de-dados-com-acrescimo-69","title":"Criando corridas de dados com acr\u00e9scimo (#69)","text":"TL;DR

    As chamadas append nem sempre s\u00e3o isentas de disputa de dados; portanto, n\u00e3o deve ser usado simultaneamente em uma slice compartilhada.

    Should adding an element to a slice using append is data-race-free? Spoiler: it depends.

    Do you believe this example has a data race?

    s := make([]int, 1)\n\ngo func() { // In a new goroutine, appends a new element on s\n    s1 := append(s, 1)\n    fmt.Println(s1)\n}()\n\ngo func() { // Same\n    s2 := append(s, 1)\n    fmt.Println(s2)\n}()\n

    The answer is no.

    In this example, we create a slice with make([]int, 1). The code creates a one-length, one-capacity slice. Thus, because the slice is full, using append in each goroutine returns a slice backed by a new array. It doesn\u2019t mutate the existing array; hence, it doesn\u2019t lead to a data race.

    Now, let\u2019s run the same example with a slight change in how we initialize s. Instead of creating a slice with a length of 1, we create it with a length of 0 but a capacity of 1. How about this new example? Does it contain a data race?

    s := make([]int, 0, 1)\n\ngo func() { \n    s1 := append(s, 1)\n    fmt.Println(s1)\n}()\n\ngo func() {\n    s2 := append(s, 1)\n    fmt.Println(s2)\n}()\n

    The answer is yes. We create a slice with make([]int, 0, 1). Therefore, the array isn\u2019t full. Both goroutines attempt to update the same index of the backing array (index 1), which is a data race.

    How can we prevent the data race if we want both goroutines to work on a slice containing the initial elements of s plus an extra element? One solution is to create a copy of s.

    We should remember that using append on a shared slice in concurrent applications can lead to a data race. Hence, it should be avoided.

    C\u00f3digo fonte

    "},{"location":"pt-br/#usando-mutexes-imprecisamente-com-slices-e-maps-70","title":"Usando mutexes imprecisamente com slices e maps (#70)","text":"TL;DR

    Lembrar que slices e maps s\u00e3o ponteiros pode evitar corridas comuns de dados.

    Let's implement a Cache struct used to handle caching for customer balances. This struct will contain a map of balances per customer ID and a mutex to protect concurrent accesses:

    type Cache struct {\n    mu       sync.RWMutex\n    balances map[string]float64\n}\n

    Next, we add an AddBalance method that mutates the balances map. The mutation is done in a critical section (within a mutex lock and a mutex unlock):

    func (c *Cache) AddBalance(id string, balance float64) {\n    c.mu.Lock()\n    c.balances[id] = balance\n    c.mu.Unlock()\n}\n

    Meanwhile, we have to implement a method to calculate the average balance for all the customers. One idea is to handle a minimal critical section this way:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    balances := c.balances // Creates a copy of the balances map\n    c.mu.RUnlock()\n\n    sum := 0.\n    for _, balance := range balances { // Iterates over the copy, outside of the critical section\n        sum += balance\n    }\n    return sum / float64(len(balances))\n}\n

    What's the problem with this code?

    If we run a test using the -race flag with two concurrent goroutines, one calling AddBalance (hence mutating balances) and another calling AverageBalance, a data race occurs. What\u2019s the problem here?

    Internally, a map is a runtime.hmap struct containing mostly metadata (for example, a counter) and a pointer referencing data buckets. So, balances := c.balances doesn\u2019t copy the actual data. Therefore, the two goroutines perform operations on the same data set, and one mutates it. Hence, it's a data race.

    One possible solution is to protect the whole AverageBalance function:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    defer c.mu.RUnlock() // Unlocks when the function returns\n\n    sum := 0.\n    for _, balance := range c.balances {\n        sum += balance\n    }\n    return sum / float64(len(c.balances))\n}\n

    Another option, if the iteration operation isn\u2019t lightweight, is to work on an actual copy of the data and protect only the copy:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    m := make(map[string]float64, len(c.balances)) // Copies the map\n    for k, v := range c.balances {\n        m[k] = v\n    }\n    c.mu.RUnlock()\n\n    sum := 0.\n    for _, balance := range m {\n        sum += balance\n    }\n    return sum / float64(len(m))\n}\n

    Once we have made a deep copy, we release the mutex. The iterations are done on the copy outside of the critical section.

    In summary, we have to be careful with the boundaries of a mutex lock. In this section, we have seen why assigning an existing map (or an existing slice) to a map isn\u2019t enough to protect against data races. The new variable, whether a map or a slice, is backed by the same data set. There are two leading solutions to prevent this: protect the whole function, or work on a copy of the actual data. In all cases, let\u2019s be cautious when designing critical sections and make sure the boundaries are accurately defined.

    C\u00f3digo fonte

    "},{"location":"pt-br/#uso-indevido-syncwaitgroup-71","title":"Uso indevido sync.WaitGroup (#71)","text":"TL;DR

    Para usar com precis\u00e3o sync.WaitGroup, chame o m\u00e9todo Add antes de ativar goroutines.

    C\u00f3digo fonte

    "},{"location":"pt-br/#esquecendo-synccond-72","title":"Esquecendo sync.Cond (#72)","text":"TL;DR

    Voc\u00ea pode enviar notifica\u00e7\u00f5es repetidas para v\u00e1rios goroutines com sync.Cond.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-usando-errgroup-73","title":"N\u00e3o usando errgroup (#73)","text":"TL;DR

    Voc\u00ea pode sincronizar um grupo de goroutines e lidar com erros e contextos com o pacote errgroup.

    C\u00f3digo fonte

    "},{"location":"pt-br/#copiando-um-tipo-sync-74","title":"Copiando um tipo sync (#74)","text":"TL;DR

    Tipos sync n\u00e3o devem ser copiados.

    C\u00f3digo fonte

    "},{"location":"pt-br/#standard-library","title":"Standard Library","text":""},{"location":"pt-br/#fornecendo-uma-duracao-de-tempo-errada-75","title":"Fornecendo uma dura\u00e7\u00e3o de tempo errada (#75)","text":"TL;DR

    Seja cauteloso com fun\u00e7\u00f5es que aceitam um arquivo time.Duration. Mesmo que a passagem de um n\u00famero inteiro seja permitida, tente usar a API time para evitar qualquer poss\u00edvel confus\u00e3o.

    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 {\n    select {\n    case <-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

    C\u00f3digo fonte

    "},{"location":"pt-br/#timeafter-e-vazamentos-de-memoria-76","title":"time.After e vazamentos de mem\u00f3ria (#76)","text":"TL;DR

    Evitar chamadas para fun\u00e7\u00f5es time.After repetidas (como loops ou manipuladores HTTP) pode evitar pico de consumo de mem\u00f3ria. Os recursos criados por time.After s\u00e3o liberados somente quando o cron\u00f4metro expira.

    Developers often use time.After in loops or HTTP handlers repeatedly to implement the timing function. But it can lead to unintended peak memory consumption due to the delayed release of resources, just like the following code:

    func consumer(ch <-chan Event) {\n    for {\n        select {\n        case event := <-ch:\n            handle(event)\n        case <-time.After(time.Hour):\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    The source code of the function time.After is as follows:

    func After(d Duration) <-chan Time {\n    return NewTimer(d).C\n}\n

    As we see, it returns receive-only channel.

    When time.After is used in a loop or repeated context, a new channel is created in each iteration. If these channels are not properly closed or if their associated timers are not stopped, they can accumulate and consume memory. The resources associated with each timer and channel are only released when the timer expires or the channel is closed.

    To avoid this happening, We can use context's timeout setting instead of time.After, like below:

    func consumer(ch <-chan Event) {\n    for {\n        ctx, cancel := context.WithTimeout(context.Background(), time.Hour)\n        select {\n        case event := <-ch:\n            cancel()\n            handle(event)\n        case <-ctx.Done():\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    We can also use time.NewTimer like so:

    func consumer(ch <-chan Event) {\n    timerDuration := 1 * time.Hour\n    timer := time.NewTimer(timerDuration)\n\n    for {\n        timer.Reset(timerDuration)\n        select {\n        case event := <-ch:\n            handle(event)\n        case <-timer.C:\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    C\u00f3digo fonte

    "},{"location":"pt-br/#lidando-com-erros-comuns-json-77","title":"Lidando com erros comuns JSON (#77)","text":"

    Tenha cuidado ao usar campos incorporados em estruturas Go. Fazer isso pode levar a bugs sorrateiros, como um campo time.Time incorporado que implementa a interface json.Marshaler, substituindo assim o comportamento de empacotamento padr\u00e3o.

    C\u00f3digo fonte

    Ao comparar duas estruturas time.Time, lembre-se de que time.Time cont\u00e9m um rel\u00f3gio de parede e um rel\u00f3gio monot\u00f4nico, e a compara\u00e7\u00e3o usando o operador == \u00e9 feita em ambos os rel\u00f3gios.

    C\u00f3digo fonte

    Para evitar suposi\u00e7\u00f5es erradas ao fornecer um map ao desempacotar (unmarshaling) dados JSON, lembre-se de que os valores num\u00e9ricos s\u00e3o convertidos para float64 por padr\u00e3o.

    C\u00f3digo fonte

    "},{"location":"pt-br/#erros-comuns-de-sql-78","title":"Erros comuns de SQL (#78)","text":"

    Esquecer sql.Open n\u00e3o necessariamente estabelece conex\u00f5es com um banco de dados Chame o m\u00e9todo Ping ou PingContext se precisar testar sua configura\u00e7\u00e3o e garantir que um banco de dados esteja acess\u00edvel.

    C\u00f3digo fonte

    Configure os par\u00e2metros de conex\u00e3o do banco de dados para aplicativos de n\u00edvel de produ\u00e7\u00e3o.

    O uso de instru\u00e7\u00f5es preparadas em SQL torna as consultas mais eficientes e seguras.

    C\u00f3digo fonte

    Lide com colunas anul\u00e1veis \u200b\u200bem tabelas usando ponteiros ou tipos sql.NullXXX.

    C\u00f3digo fonte

    Chame o m\u00e9todo Err de sql.Rows itera\u00e7\u00f5es posteriores \u00e0 linha para garantir que voc\u00ea n\u00e3o perdeu nenhum erro ao preparar a pr\u00f3xima linha.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-fechando-recursos-transitorios-body-http-sqlrows-e-osfile-79","title":"N\u00e3o fechando recursos transit\u00f3rios (body HTTP, sql.Rows e os.File) (#79)","text":"TL;DR

    Eventualmente feche todas as estruturas implementadas io.Closer para evitar poss\u00edveis vazamentos.

    C\u00f3digo fonte

    "},{"location":"pt-br/#esquecendo-a-instrucao-return-apos-responder-a-uma-solicitacao-http-80","title":"Esquecendo a instru\u00e7\u00e3o return ap\u00f3s responder a uma solicita\u00e7\u00e3o HTTP (#80)","text":"TL;DR

    Para evitar comportamentos inesperados nas implementa\u00e7\u00f5es do manipulador HTTP, certifique-se de n\u00e3o perder a instru\u00e7\u00e3o return se quiser que um manipulador pare ap\u00f3s http.Error.

    Consider the following HTTP handler that handles an error from foo using http.Error:

    func handler(w http.ResponseWriter, req *http.Request) {\n    err := foo(req)\n    if err != nil {\n        http.Error(w, \"foo\", http.StatusInternalServerError)\n    }\n\n    _, _ = w.Write([]byte(\"all good\"))\n    w.WriteHeader(http.StatusCreated)\n}\n

    If we run this code and err != nil, the HTTP response would be:

    foo\nall good\n

    The response contains both the error and success messages, and also the first HTTP status code, 500. There would also be a warning log indicating that we attempted to write the status code multiple times:

    2023/10/10 16:45:33 http: superfluous response.WriteHeader call from main.handler (main.go:20)\n

    The mistake in this code is that http.Error does not stop the handler's execution, which means the success message and status code get written in addition to the error. Beyond an incorrect response, failing to return after writing an error can lead to the unwanted execution of code and unexpected side-effects. The following code adds the return statement following the http.Error and exhibits the desired behavior when ran:

    func handler(w http.ResponseWriter, req *http.Request) {\n    err := foo(req)\n    if err != nil {\n        http.Error(w, \"foo\", http.StatusInternalServerError)\n        return // Adds the return statement\n    }\n\n    _, _ = w.Write([]byte(\"all good\"))\n    w.WriteHeader(http.StatusCreated)\n}\n

    C\u00f3digo fonte

    "},{"location":"pt-br/#usando-o-cliente-e-servidor-http-padrao-81","title":"Usando o cliente e servidor HTTP padr\u00e3o (#81)","text":"TL;DR

    Para aplicativos de n\u00edvel de produ\u00e7\u00e3o, n\u00e3o use as implementa\u00e7\u00f5es de cliente e servidor HTTP padr\u00e3o. Essas implementa\u00e7\u00f5es n\u00e3o possuem tempos limite e comportamentos que deveriam ser obrigat\u00f3rios na produ\u00e7\u00e3o.

    C\u00f3digo fonte

    "},{"location":"pt-br/#teste","title":"Teste","text":""},{"location":"pt-br/#nao-categorizar-testes-tags-de-construcao-variaveis-de-ambiente-e-modo-abreviado-82","title":"N\u00e3o categorizar testes (tags de constru\u00e7\u00e3o, vari\u00e1veis \u200b\u200bde ambiente e modo abreviado) (#82)","text":"TL;DR

    Categorizar testes usando sinalizadores de constru\u00e7\u00e3o, vari\u00e1veis \u200b\u200bde ambiente ou modo curto torna o processo de teste mais eficiente. Voc\u00ea pode criar categorias de teste usando sinalizadores de constru\u00e7\u00e3o ou vari\u00e1veis \u200b\u200bde ambiente (por exemplo, testes de unidade versus testes de integra\u00e7\u00e3o) e diferenciar testes curtos de testes de longa dura\u00e7\u00e3o para decidir quais tipos de testes executar.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-habilitando-a-bandeira-de-corrida-83","title":"N\u00e3o habilitando a bandeira de corrida (#83)","text":"TL;DR

    A ativa\u00e7\u00e3o do sinalizador -race \u00e9 altamente recomendada ao escrever aplicativos simult\u00e2neos. Isso permite que voc\u00ea detecte poss\u00edveis corridas de dados que podem levar a bugs de software.

    In Go, the race detector isn\u2019t a static analysis tool used during compilation; instead, it\u2019s a tool to find data races that occur at runtime. To enable it, we have to enable the -race flag while compiling or running a test. For example:

    go test -race ./...\n

    Once the race detector is enabled, the compiler instruments the code to detect data races. Instrumentation refers to a compiler adding extra instructions: here, tracking all memory accesses and recording when and how they occur.

    Enabling the race detector adds an overhead in terms of memory and execution time; hence, it's generally recommended to enable it only during local testing or continuous integration, not production.

    If a race is detected, Go raises a warning. For example:

    package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    i := 0\n    go func() { i++ }()\n    fmt.Println(i)\n}\n

    Runnig this code with the -race logs the following warning:

    ==================\nWARNING: DATA RACE\nWrite at 0x00c000026078 by goroutine 7: # (1)\n  main.main.func1()\n      /tmp/app/main.go:9 +0x4e\n\nPrevious read at 0x00c000026078 by main goroutine: # (2)\n  main.main()\n      /tmp/app/main.go:10 +0x88\n\nGoroutine 7 (running) created at: # (3)\n  main.main()\n      /tmp/app/main.go:9 +0x7a\n==================\n
    1. Indicates that goroutine 7 was writing
    2. Indicates that the main goroutine was reading
    3. Indicates when the goroutine 7 was created

    Let\u2019s make sure we are comfortable reading these messages. Go always logs the following:

    In addition, if a specific file contains tests that lead to data races, we can exclude it from race detection using the !race build tag:

    //go:build !race\n\npackage main\n\nimport (\n    \"testing\"\n)\n\nfunc TestFoo(t *testing.T) {\n    // ...\n}\n
    "},{"location":"pt-br/#nao-usar-modos-de-execucao-de-teste-paralelo-e-aleatorio-84","title":"N\u00e3o usar modos de execu\u00e7\u00e3o de teste (paralelo e aleat\u00f3rio) (#84)","text":"TL;DR

    Usar o sinalizador -parallel \u00e9 uma forma eficiente de acelerar testes, especialmente os de longa dura\u00e7\u00e3o. Use o sinalizador -shuffle para ajudar a garantir que um conjunto de testes n\u00e3o se baseie em suposi\u00e7\u00f5es erradas que possam ocultar bugs.

    "},{"location":"pt-br/#nao-usar-testes-baseados-em-tabela-85","title":"N\u00e3o usar testes baseados em tabela (#85)","text":"TL;DR

    Os testes baseados em tabelas s\u00e3o uma maneira eficiente de agrupar um conjunto de testes semelhantes para evitar a duplica\u00e7\u00e3o de c\u00f3digo e facilitar o manuseio de atualiza\u00e7\u00f5es futuras.

    C\u00f3digo fonte

    "},{"location":"pt-br/#dormindo-em-testes-unitarios-86","title":"Dormindo em testes unit\u00e1rios (#86)","text":"TL;DR

    Evite interrup\u00e7\u00f5es usando a sincroniza\u00e7\u00e3o para tornar o teste menos inst\u00e1vel e mais robusto. Se a sincroniza\u00e7\u00e3o n\u00e3o for poss\u00edvel, considere uma abordagem de nova tentativa.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-lidar-com-a-api-de-tempo-de-forma-eficiente-87","title":"N\u00e3o lidar com a API de tempo de forma eficiente (#87)","text":"TL;DR

    Entender como lidar com fun\u00e7\u00f5es usando a API time \u00e9 outra maneira de tornar um teste menos complicado. Voc\u00ea pode usar t\u00e9cnicas padr\u00e3o, como lidar com o tempo como parte de uma depend\u00eancia oculta ou solicitar que os clientes o forne\u00e7am.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-usar-pacotes-de-utilitarios-de-teste-httptest-e-iotest-88","title":"N\u00e3o usar pacotes de utilit\u00e1rios de teste ( httptest e iotest) (#88)","text":"

    C\u00f3digo fonte

    C\u00f3digo fonte

    "},{"location":"pt-br/#escrevendo-benchmarks-imprecisos-89","title":"Escrevendo benchmarks imprecisos (#89)","text":"TL;DR

    Regarding benchmarks:

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-explorando-todos-os-recursos-de-teste-do-go-90","title":"N\u00e3o explorando todos os recursos de teste do Go (#90)","text":"

    Use a cobertura de c\u00f3digo com o sinalizador -coverprofile para ver rapidamente qual parte do c\u00f3digo precisa de mais aten\u00e7\u00e3o.

    Coloque os testes unit\u00e1rios em um pacote diferente para impor testes de escrita que se concentrem em um comportamento exposto, n\u00e3o em internos.

    C\u00f3digo fonte

    O tratamento de erros usando a vari\u00e1vel *testing.T em vez do cl\u00e1ssico if err != nil torna o c\u00f3digo mais curto e f\u00e1cil de ler.

    C\u00f3digo fonte

    Voc\u00ea pode usar fun\u00e7\u00f5es de setup e teardown para configurar um ambiente complexo, como no caso de testes de integra\u00e7\u00e3o.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-usar-fuzzing-erro-da-comunidade","title":"N\u00e3o usar fuzzing (erro da comunidade)","text":"TL;DR

    Fuzzing \u00e9 uma estrat\u00e9gia eficiente para detectar entradas aleat\u00f3rias, inesperadas ou malformadas em fun\u00e7\u00f5es e m\u00e9todos complexos, a fim de descobrir vulnerabilidades, bugs ou at\u00e9 mesmo travamentos potenciais.

    Credits: @jeromedoucet

    "},{"location":"pt-br/#otimizacoes","title":"Otimiza\u00e7\u00f5es","text":""},{"location":"pt-br/#nao-entendendo-os-caches-da-cpu-91","title":"N\u00e3o entendendo os caches da CPU (#91)","text":"

    Compreender como usar caches de CPU \u00e9 importante para otimizar aplicativos vinculados \u00e0 CPU porque o cache L1 \u00e9 cerca de 50 a 100 vezes mais r\u00e1pido que a mem\u00f3ria principal.

    Estar consciente do conceito de linha de cache \u00e9 fundamental para entender como organizar dados em aplicativos com uso intensivo de dados. Uma CPU n\u00e3o busca mem\u00f3ria palavra por palavra; em vez disso, geralmente copia um bloco de mem\u00f3ria para uma linha de cache de 64 bytes. Para aproveitar ao m\u00e1ximo cada linha de cache individual, imponha a localidade espacial.

    C\u00f3digo fonte

    C\u00f3digo fonte

    Tornar o c\u00f3digo previs\u00edvel para a CPU tamb\u00e9m pode ser uma forma eficiente de otimizar certas fun\u00e7\u00f5es. Por exemplo, uma passada unit\u00e1ria ou constante \u00e9 previs\u00edvel para a CPU, mas uma passada n\u00e3o unit\u00e1ria (por exemplo, uma lista vinculada) n\u00e3o \u00e9 previs\u00edvel.

    C\u00f3digo fonte

    Para evitar um avan\u00e7o cr\u00edtico e, portanto, utilizar apenas uma pequena parte do cache, esteja ciente de que os caches s\u00e3o particionados.

    "},{"location":"pt-br/#escrevendo-codigo-simultaneo-que-leva-a-compartilhamento-falso-92","title":"Escrevendo c\u00f3digo simult\u00e2neo que leva a compartilhamento falso (#92)","text":"TL;DR

    Saber que n\u00edveis mais baixos de caches de CPU n\u00e3o s\u00e3o compartilhados entre todos os n\u00facleos ajuda a evitar padr\u00f5es que degradam o desempenho, como compartilhamento falso ao escrever c\u00f3digo de simultaneidade. Compartilhar mem\u00f3ria \u00e9 uma ilus\u00e3o.

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-levando-em-consideracao-o-paralelismo-no-nivel-de-instrucao-93","title":"N\u00e3o levando em considera\u00e7\u00e3o o paralelismo no n\u00edvel de instru\u00e7\u00e3o (#93)","text":"TL;DR

    Use o ILP para otimizar partes espec\u00edficas do seu c\u00f3digo para permitir que uma CPU execute tantas instru\u00e7\u00f5es paralelas quanto poss\u00edvel. Identificar perigos nos dados \u00e9 uma das etapas principais.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-estar-ciente-do-alinhamento-dos-dados-94","title":"N\u00e3o estar ciente do alinhamento dos dados (#94)","text":"TL;DR

    Voc\u00ea pode evitar erros comuns lembrando que no Go os tipos b\u00e1sicos s\u00e3o alinhados com seu pr\u00f3prio tamanho. Por exemplo, tenha em mente que reorganizar os campos de uma estrutura por tamanho em ordem decrescente pode levar a estruturas mais compactas (menos aloca\u00e7\u00e3o de mem\u00f3ria e potencialmente uma melhor localidade espacial).

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-entendendo-stack-vs-heap-95","title":"N\u00e3o entendendo stack vs. heap (#95)","text":"TL;DR

    Compreender as diferen\u00e7as fundamentais entre heap e pilha tamb\u00e9m deve fazer parte do seu conhecimento b\u00e1sico ao otimizar um aplicativo Go. As aloca\u00e7\u00f5es de pilha s\u00e3o quase gratuitas, enquanto as aloca\u00e7\u00f5es de heap s\u00e3o mais lentas e dependem do GC para limpar a mem\u00f3ria.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-saber-como-reduzir-alocacoes-mudanca-de-api-otimizacoes-de-compilador-e-syncpool-96","title":"N\u00e3o saber como reduzir aloca\u00e7\u00f5es (mudan\u00e7a de API, otimiza\u00e7\u00f5es de compilador e sync.Pool) (#96)","text":"TL;DR

    A redu\u00e7\u00e3o das aloca\u00e7\u00f5es tamb\u00e9m \u00e9 um aspecto essencial da otimiza\u00e7\u00e3o de um aplicativo Go. Isso pode ser feito de diferentes maneiras, como projetar a API cuidadosamente para evitar compartilhamento, compreender as otimiza\u00e7\u00f5es comuns do compilador Go e usar sync.Pool.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-dependendo-do-inlining-97","title":"N\u00e3o dependendo do inlining (#97)","text":"TL;DR

    Use a t\u00e9cnica de inlining de caminho r\u00e1pido para reduzir com efici\u00eancia o tempo amortizado para chamar uma fun\u00e7\u00e3o.

    "},{"location":"pt-br/#nao-usar-ferramentas-de-diagnostico-go-98","title":"N\u00e3o usar ferramentas de diagn\u00f3stico Go (#98)","text":"TL;DR

    Confie na cria\u00e7\u00e3o de perfil e no rastreador de execu\u00e7\u00e3o para entender o desempenho de um aplicativo e as partes a serem otimizadas.

    Leia a se\u00e7\u00e3o completa aqui.

    "},{"location":"pt-br/#nao-entendendo-como-funciona-o-gc-99","title":"N\u00e3o entendendo como funciona o GC (#99)","text":"TL;DR

    Compreender como ajustar o GC pode levar a v\u00e1rios benef\u00edcios, como lidar com aumentos repentinos de carga com mais efici\u00eancia.

    "},{"location":"pt-br/#nao-entendendo-os-impactos-da-execucao-do-go-no-docker-e-kubernetes-100","title":"N\u00e3o entendendo os impactos da execu\u00e7\u00e3o do Go no Docker e Kubernetes (#100)","text":"TL;DR

    Para ajudar a evitar a limita\u00e7\u00e3o da CPU quando implantado no Docker e no Kubernetes, lembre-se de que Go n\u00e3o reconhece CFS.

    By default, GOMAXPROCS is set to the number of OS-apparent logical CPU cores.

    When running some Go code inside Docker and Kubernetes, we must know that Go isn't CFS-aware (github.com/golang/go/issues/33803). Therefore, GOMAXPROCS isn't automatically set to the value of spec.containers.resources.limits.cpu (see Kubernetes Resource Management for Pods and Containers); instead, it's set to the number of logical cores on the host machine. The main implication is that it can lead to an increased tail latency in some specific situations.

    One solution is to rely on uber-go/automaxprocs that automatically set GOMAXPROCS to match the Linux container CPU quota.

    "},{"location":"pt-br/#community","title":"Community","text":"

    Thanks to all the contributors:

    "},{"location":"zh/","title":"100\u4e2aGo\u5e38\u89c1\u9519\u8bef\u53ca\u5982\u4f55\u907f\u514d","text":"Jobs

    Is your company hiring? Sponsor the Chinese version of this repository and let a significant audience of Go developers (~1k unique visitors per week) know about your opportunities in this section.

    "},{"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

    "},{"location":"zh/#9","title":"\u56f0\u60d1\u4f55\u65f6\u8be5\u7528\u8303\u578b (#9)","text":"

    \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

    "},{"location":"zh/#14","title":"\u5ffd\u7565\u4e86\u5305\u540d\u51b2\u7a81 (#14)","text":"

    \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

    "},{"location":"zh/#15","title":"\u4ee3\u7801\u7f3a\u5c11\u6587\u6863 (#15)","text":"

    \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

    "},{"location":"zh/#slice-append-25","title":"slice append\u5e26\u6765\u7684\u9884\u671f\u4e4b\u5916\u7684\u526f\u4f5c\u7528 (#25)","text":"

    \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

    "},{"location":"zh/#_3","title":"\u63a7\u5236\u7ed3\u6784","text":""},{"location":"zh/#range-30","title":"\u5ffd\u7565\u4e86 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

    "},{"location":"zh/#range-channels-arrays-31","title":"\u5ffd\u7565\u4e86 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

    "},{"location":"zh/#range-range-loops-32","title":"\u5ffd\u7565\u4e86 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

    "},{"location":"zh/#map-33","title":"map\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u7684\u9519\u8bef\u5047\u8bbe\uff08\u904d\u5386\u987a\u5e8f \u548c \u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u63d2\u5165\uff09(#33)","text":"

    \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\u4e86 break \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

    "},{"location":"zh/#defer-35","title":"\u5728\u5faa\u73af\u4e2d\u4f7f\u7528 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

    "},{"location":"zh/#trim-38","title":"\u8bef\u7528trim\u51fd\u6570 (#38)","text":"

    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

    "},{"location":"zh/#39","title":"\u4e0d\u7ecf\u4f18\u5316\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u64cd\u4f5c (#39)","text":"

    \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

    "},{"location":"zh/#40","title":"\u65e0\u7528\u7684\u5b57\u7b26\u4e32\u8f6c\u6362 (#40)","text":"

    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

    "},{"location":"zh/#41","title":"\u5b50\u5b57\u7b26\u4e32\u548c\u5185\u5b58\u6cc4\u6f0f (#41)","text":"

    \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

    "},{"location":"zh/#nil-45","title":"\u8fd4\u56de\u4e00\u4e2anil\u63a5\u6536\u5668 (#45)","text":"

    \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

    "},{"location":"zh/#defer-value-47","title":"\u5ffd\u7565 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

    "},{"location":"zh/#_6","title":"\u9519\u8bef\u7ba1\u7406","text":""},{"location":"zh/#panicking-48","title":"Panicking (#48)","text":"

    \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

    "},{"location":"zh/#error-49","title":"\u672a\u8003\u8651\u4f55\u65f6\u624d\u5e94\u8be5\u5305\u88c5error (#49)","text":"

    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

    "},{"location":"zh/#51","title":"\u4e0d\u6b63\u786e\u7684\u9519\u8bef\u5bf9\u8c61\u503c\u6bd4\u8f83 (#51)","text":"

    \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

    "},{"location":"zh/#52","title":"\u5904\u7406\u540c\u4e00\u4e2a\u9519\u8bef\u4e24\u6b21 (#52)","text":"

    \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

    "},{"location":"zh/#defer-54","title":"\u4e0d\u5904\u7406 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

    "},{"location":"zh/#_7","title":"\u5e76\u53d1\u7f16\u7a0b: \u57fa\u7840","text":""},{"location":"zh/#55","title":"\u6df7\u6dc6\u5e76\u53d1\u548c\u5e76\u884c (#55)","text":"

    \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

    "},{"location":"zh/#go-contexts-60","title":"\u8bef\u89e3\u4e86Go contexts (#60)","text":"

    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

    "},{"location":"zh/#channels-65","title":"\u4e0d\u6b63\u786e\u4f7f\u7528\u901a\u77e5 channels (#65)","text":"

    \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

    "},{"location":"zh/#channel-size-67","title":"\u4e0d\u6e05\u695a\u8be5\u5982\u4f55\u786e\u5b9a channel size (#67)","text":"

    \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

    "},{"location":"zh/#append-69","title":"\u4f7f\u7528 append \u4e0d\u5f53\u5bfc\u81f4\u6570\u636e\u7ade\u4e89 (#69)","text":"

    \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

    "},{"location":"zh/#mutexes-slicesmaps-70","title":"\u8bef\u7528 mutexes \u548c slices\u3001maps (#70)","text":"

    \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\u7528 sync.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

    "},{"location":"zh/#synccond-72","title":"\u5fd8\u8bb0\u4f7f\u7528 sync.Cond (#72)","text":"

    \u4f60\u53ef\u4ee5\u4f7f\u7528 sync.Cond \u5411\u591a\u4e2a goroutines \u53d1\u9001\u91cd\u590d\u7684\u901a\u77e5\u3002

    "},{"location":"zh/#errgroup-73","title":"\u4e0d\u4f7f\u7528 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

    "},{"location":"zh/#sync-74","title":"\u62f7\u8d1d\u4e00\u4e2a sync \u4e0b\u7684\u7c7b\u578b (#74)","text":"

    sync \u5305\u4e0b\u7684\u7c7b\u578b\u4e0d\u5e94\u8be5\u88ab\u62f7\u8d1d\u3002

    "},{"location":"zh/#_9","title":"\u6807\u51c6\u5e93","text":""},{"location":"zh/#timeduration-75","title":"\u4f7f\u7528\u4e86\u9519\u8bef\u7684 time.Duration (#75)","text":"

    \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

    "},{"location":"zh/#json-77","title":"JSON\u5904\u7406\u4e2d\u7684\u5e38\u89c1\u9519\u8bef (#77)","text":"

    \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

    \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

    "},{"location":"zh/#sql-78","title":"\u5e38\u89c1\u7684 SQL \u9519\u8bef (#78)","text":"

    \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

    "},{"location":"zh/#http-sqlrows-osfile-79","title":"\u4e0d\u5173\u95ed\u4e34\u65f6\u8d44\u6e90\uff08HTTP \u8bf7\u6c42\u4f53\u3001sql.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

    "},{"location":"zh/#http-80","title":"\u54cd\u5e94HTTP\u8bf7\u6c42\u540e\u6ca1\u6709\u8fd4\u56de\u8bed\u53e5 (#80)","text":"

    \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

    "},{"location":"zh/#http-clientserver-81","title":"\u76f4\u63a5\u4f7f\u7528\u9ed8\u8ba4\u7684HTTP client\u548cserver (#81)","text":"

    \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

    "},{"location":"zh/#race-83","title":"\u4e0d\u6253\u5f00 race \u5f00\u5173 (#83)","text":"

    \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

    "},{"location":"zh/#parallel-shuffle-84","title":"\u4e0d\u6253\u5f00\u6d4b\u8bd5\u7684\u6267\u884c\u6a21\u5f0f\u5f00\u5173 (parallel \u548c shuffle) (#84)","text":"

    \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

    "},{"location":"zh/#85","title":"\u4e0d\u4f7f\u7528\u8868\u9a71\u52a8\u7684\u6d4b\u8bd5 (#85)","text":"

    \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

    "},{"location":"zh/#89","title":"\u4e0d\u6b63\u786e\u7684\u57fa\u51c6\u6d4b\u8bd5 (#89)","text":"

    \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

    \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, and sync.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

    "},{"location":"zh/#97","title":"\u4e0d\u6ce8\u610f\u4f7f\u7528\u5185\u8054 (#97)","text":"

    \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 the mistakes in the 100 Go Mistakes and How to Avoid Them book. Meanwhile, it's also open to the community. If you believe that a common Go mistake should be added, please create an issue.

    Support

    This website is 100% free as I wanted to provide to the Go community as much free content as possible from my book. If you find the content useful and want to support my work, feel free to contribute \ud83d\ude42:

    Beta

    You're viewing a beta version enriched with significantly more content. However, this version is not yet complete, and I'm looking for volunteers to help me summarize the remaining mistakes (GitHub issue #43).

    Progress:

    "},{"location":"#code-and-project-organization","title":"Code and Project Organization","text":""},{"location":"#unintended-variable-shadowing-1","title":"Unintended variable shadowing (#1)","text":"TL;DR

    Avoiding 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;DR

    Avoiding 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 foo() {\n    // ...\n    return true\n} else {\n    // ...\n}\n

    Instead, we omit the else block like this:

    if foo() {\n    // ...\n    return true\n}\n// ...\n
    if s != \"\" {\n    // ...\n} else {\n    return 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 == \"\" {\n    return 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;DR

    When 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;DR

    Forcing 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;DR

    Abstractions 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.

    Read the full section here.

    Source code

    "},{"location":"#interface-on-the-producer-side-6","title":"Interface on the producer side (#6)","text":"TL;DR

    Keeping 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;DR

    To 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;DR

    Relying 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;DR

    Using 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 {\n    Bar // Embedded field\n}\n\ntype Bar struct {\n    Baz 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:

    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;DR

    To 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 options struct {\n  port *int\n}\n\ntype Option func(options *options) error\n\nfunc WithPort(port int) Option {\n  return func(options *options) error {\n    if port < 0 {\n    return errors.New(\"port should be positive\")\n  }\n  options.port = &port\n  return nil\n  }\n}\n\nfunc NewServer(addr string, opts ...Option) ( *http.Server, error) {\n  var options options\n  for _, opt := range opts {\n    err := opt(&options)\n    if err != nil {\n      return nil, err\n    }\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\n  var port int\n  if options.port == nil {\n    port = defaultHTTPPort\n  } else {\n      if *options.port == 0 {\n      port = randomPort()\n    } else {\n      port = *options.port\n    }\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.

    Note

    In 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;DR

    Naming 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;DR

    To 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;DR

    To 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;DR

    To 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:

    Besides linters, we should also use code formatters to fix code style. Here is a list of some code formatters for you to try:

    Meanwhile, we should also look at golangci-lint (https://github.com/golangci/golangci-lint). It\u2019s a linting tool that provides a facade on top of many useful linters and formatters. Also, it allows running the linters in parallel to improve analysis speed, which is quite handy.

    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;DR

    When 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:

    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;DR

    Because 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;DR

    Making 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;DR

    Understanding 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;DR

    When 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;DR

    To 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

    The 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;DR

    To 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;DR

    To 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;DR

    Using 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.

    Note

    s[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;DR

    Working 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;DR

    When 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;DR

    A 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;DR

    To 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:

    Note

    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 in range 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 in range 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 {\n    a[2] = 10\n    if i == 2 {\n        fmt.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 in range loops (#32)","text":"Warning

    This mistake isn't relevant anymore from Go 1.22 (details).

    "},{"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;DR

    To 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 the break 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++ {\n    fmt.Printf(\"%d \", i)\n\n    switch i {\n    default:\n    case 2:\n        break\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:\n    for i := 0; i < 5; i++ {\n        fmt.Printf(\"%d \", i)\n\n        switch i {\n        default:\n        case 2:\n            break 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":"Using defer 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 {\n    for path := range ch {\n        file, err := os.Open(path)\n        if err != nil {\n            return err\n        }\n\n        defer file.Close()\n\n        // Do something with file\n    }\n    return 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 {\n    for path := range ch {\n        if err := readFile(path); err != nil {\n            return err\n        }\n    }\n    return nil\n}\n\nfunc readFile(path string) error {\n    file, err := os.Open(path)\n    if err != nil {\n        return err\n    }\n\n    defer file.Close()\n\n    // Do something with file\n    return 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;DR

    Understanding 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:

    Source code

    "},{"location":"#inaccurate-string-iteration-37","title":"Inaccurate string iteration (#37)","text":"TL;DR

    Iterating 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 {\n    fmt.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 {\n    fmt.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 {\n    fmt.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;DR

    strings.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;DR

    Concatenating 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 {\n    s := \"\"\n    for _, value := range values {\n        s += value\n    }\n    return 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 {\n    sb := strings.Builder{}\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return 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.

    Note

    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 {\n    total := 0\n    for i := 0; i < len(values); i++ {\n        total += len(values[i])\n    }\n\n    sb := strings.Builder{}\n    sb.Grow(total) (2)\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return 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;DR

    Remembering 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;DR

    Using 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;DR

    The 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\n\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

    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;DR

    Using 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) {\n    b = a\n    return\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;DR

    See #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) (\n    lat, lng float32, err error) {\n    isValid := l.validateAddress(address) (1)\n    if !isValid {\n        return 0, 0, errors.New(\"invalid address\")\n    }\n\n    if ctx.Err() != nil { (2)\n        return 0, 0, err\n    }\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;DR

    When 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;DR

    Designing 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 how defer 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 (\n    StatusSuccess  = \"success\"\n    StatusErrorFoo = \"error_foo\"\n    StatusErrorBar = \"error_bar\"\n)\n\nfunc f() error {\n    var status string\n    defer notify(status)\n    defer incrementCounter(status)\n\n    if err := foo(); err != nil {\n        status = StatusErrorFoo\n        return err\n    }\n\n    if err := bar(); err != nil {\n        status = StatusErrorBar\n        return err\n    }\n\n    status = StatusSuccess\n    return 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 {\n    var status string\n    defer notify(&status) \n    defer incrementCounter(&status)\n\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 {\n    var status string\n    defer func() {\n        notify(status)\n        incrementCounter(status)\n    }()\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;DR

    Using 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() {\n    fmt.Println(\"a\")\n    panic(\"foo\")\n    fmt.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;DR

    Wrapping 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;DR

    If 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;DR

    If 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\"\n\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:

    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;DR

    In 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;DR

    Ignoring 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 handling defer 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  // ...\n  notify() // Error handling is omitted\n}\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;DR

    Understanding 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;DR

    To 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;DR

    Being 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.

    "},{"location":"#not-understanding-race-problems-data-races-vs-race-conditions-and-the-go-memory-model-58","title":"Not understanding race problems (data races vs. race conditions and the Go memory model) (#58)","text":"TL;DR

    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:

    "},{"location":"#race-condition","title":"Race Condition","text":"

    Depending 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;DR

    When 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:

    Note

    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;DR

    Go 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:

    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.

    "},{"location":"#context-values","title":"Context values","text":"

    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,

    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;DR

    Understanding 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) {\n    response, err := doSomeTask(r.Context(), r)\n    if err != nil {\n        http.Error(w, err.Error(), http.StatusInternalServerError)\n    return\n    }\n    go func() {\n        err := publish(r.Context(), response)\n        // Do something with err\n    }()\n    writeResponse(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.

    Note

    From 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;DR

    Avoiding 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() {\n    newWatcher()\n    // Run the application\n}\n\ntype watcher struct { /* Some resources */ }\n\nfunc newWatcher() {\n    w := watcher{}\n    go 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() {\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n    newWatcher(ctx)\n    // Run the application\n}\n\nfunc newWatcher(ctx context.Context) {\n    w := watcher{}\n    go 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() {\n    w := newWatcher()\n    defer w.close()\n    // Run the application\n}\n\nfunc newWatcher() watcher {\n    w := watcher{}\n    go w.watch()\n    return w\n}\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":"Warning

    This mistake isn't relevant anymore from Go 1.22 (details).

    "},{"location":"#expecting-a-deterministic-behavior-using-select-and-channels-64","title":"Expecting a deterministic behavior using select and channels (#64)","text":"TL;DR

    Understanding 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() {\n  for i := 0; i < 10; i++ {\n      messageCh <- i\n    }\n    disconnectCh <- struct{}{}\n}()\n\nfor {\n    select {\n    case v := <-messageCh:\n        fmt.Println(v)\n    case <-disconnectCh:\n        fmt.Println(\"disconnection, return\")\n        return\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 statement 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;DR

    Send 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{}.

    "},{"location":"#not-using-nil-channels-66","title":"Not using nil channels (#66)","text":"TL;DR

    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 {\n    ch := make(chan int, 1)\n\n    go func() {\n        for ch1 != nil || ch2 != nil { // Continue if at least one channel isn\u2019t nil\n            select {\n            case v, open := <-ch1:\n                if !open {\n                    ch1 = nil // Assign ch1 to a nil channel once closed\n                    break\n                }\n                ch <- v\n            case v, open := <-ch2:\n                if !open {\n                    ch2 = nil // Assigns ch2 to a nil channel once closed\n                    break\n                }\n                ch <- v\n            }\n        }\n        close(ch)\n    }()\n\n    return 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;DR

    Carefully decide on the right channel type to use, given a problem. Only unbuffered channels provide strong synchronization guarantees. For buffered channels, you should have a good reason to specify a channel size other than one.

    An unbuffered channel is a channel without any capacity. It can be created by either omitting the size or providing a 0 size:

    ch1 := make(chan int)\nch2 := make(chan int, 0)\n

    With an unbuffered channel (sometimes called a synchronous channel), the sender will block until the receiver receives data from the channel.

    Conversely, a buffered channel has a capacity, and it must be created with a size greater than or equal to 1:

    ch3 := make(chan int, 1)\n

    With a buffered channel, a sender can send messages while the channel isn\u2019t full. Once the channel is full, it will block until a receiver goroutine receives a message:

    ch3 := make(chan int, 1)\nch3 <-1 // Non-blocking\nch3 <-2 // Blocking\n

    The first send isn\u2019t blocking, whereas the second one is, as the channel is full at this stage.

    What's the main difference between unbuffered and buffered channels:

    If we need a buffered channel, what size should we provide?

    The default value we should use for buffered channels is its minimum: 1. So, we may approach the problem from this standpoint: is there any good reason not to use a value of 1? Here\u2019s a list of possible cases where we should use another size:

    If we are outside of these cases, using a different channel size should be done cautiously. Let\u2019s bear in mind that deciding about an accurate queue size isn\u2019t an easy problem:

    Martin Thompson

    Queues are typically always close to full or close to empty due to the differences in pace between consumers and producers. They very rarely operate in a balanced middle ground where the rate of production and consumption is evenly matched.

    "},{"location":"#forgetting-about-possible-side-effects-with-string-formatting-68","title":"Forgetting about possible side effects with string formatting (#68)","text":"TL;DR

    Being aware that string formatting may lead to calling existing functions means watching out for possible deadlocks and other data races.

    It\u2019s pretty easy to forget the potential side effects of string formatting while working in a concurrent application.

    "},{"location":"#etcd-data-race","title":"etcd data race","text":"

    github.com/etcd-io/etcd/pull/7816 shows an example of an issue where a map's key was formatted based on a mutable values from a context.

    "},{"location":"#deadlock","title":"Deadlock","text":"

    Can you see what the problem is in this code with a Customer struct exposing an UpdateAge method and implementing the fmt.Stringer interface?

    type Customer struct {\n    mutex sync.RWMutex // Uses a sync.RWMutex to protect concurrent accesses\n    id    string\n    age   int\n}\n\nfunc (c *Customer) UpdateAge(age int) error {\n    c.mutex.Lock() // Locks and defers unlock as we update Customer\n    defer c.mutex.Unlock()\n\n    if age < 0 { // Returns an error if age is negative\n        return fmt.Errorf(\"age should be positive for customer %v\", c)\n    }\n\n    c.age = age\n    return nil\n}\n\nfunc (c *Customer) String() string {\n    c.mutex.RLock() // Locks and defers unlock as we read Customer\n    defer c.mutex.RUnlock()\n    return fmt.Sprintf(\"id %s, age %d\", c.id, c.age)\n}\n

    The problem here may not be straightforward. If the provided age is negative, we return an error. Because the error is formatted, using the %s directive on the receiver, it will call the String method to format Customer. But because UpdateAge already acquires the mutex lock, the String method won\u2019t be able to acquire it. Hence, this leads to a deadlock situation. If all goroutines are also asleep, it leads to a panic.

    One possible solution is to restrict the scope of the mutex lock:

    func (c *Customer) UpdateAge(age int) error {\n    if age < 0 {\n        return fmt.Errorf(\"age should be positive for customer %v\", c)\n    }\n\n    c.mutex.Lock()\n    defer c.mutex.Unlock()\n\n    c.age = age\n    return nil\n}\n

    Yet, such an approach isn't always possible. In these conditions, we have to be extremely careful with string formatting.

    Another approach is to access the id field directly:

    func (c *Customer) UpdateAge(age int) error {\n    c.mutex.Lock()\n    defer c.mutex.Unlock()\n\n    if age < 0 {\n        return fmt.Errorf(\"age should be positive for customer id %s\", c.id)\n    }\n\n    c.age = age\n    return nil\n}\n

    In concurrent applications, we should remain cautious about the possible side effects of string formatting.

    Source code

    "},{"location":"#creating-data-races-with-append-69","title":"Creating data races with append (#69)","text":"TL;DR

    Calling append isn\u2019t always data-race-free; hence, it shouldn\u2019t be used concurrently on a shared slice.

    Should adding an element to a slice using append is data-race-free? Spoiler: it depends.

    Do you believe this example has a data race?

    s := make([]int, 1)\n\ngo func() { // In a new goroutine, appends a new element on s\n    s1 := append(s, 1)\n    fmt.Println(s1)\n}()\n\ngo func() { // Same\n    s2 := append(s, 1)\n    fmt.Println(s2)\n}()\n

    The answer is no.

    In this example, we create a slice with make([]int, 1). The code creates a one-length, one-capacity slice. Thus, because the slice is full, using append in each goroutine returns a slice backed by a new array. It doesn\u2019t mutate the existing array; hence, it doesn\u2019t lead to a data race.

    Now, let\u2019s run the same example with a slight change in how we initialize s. Instead of creating a slice with a length of 1, we create it with a length of 0 but a capacity of 1. How about this new example? Does it contain a data race?

    s := make([]int, 0, 1)\n\ngo func() { \n    s1 := append(s, 1)\n    fmt.Println(s1)\n}()\n\ngo func() {\n    s2 := append(s, 1)\n    fmt.Println(s2)\n}()\n

    The answer is yes. We create a slice with make([]int, 0, 1). Therefore, the array isn\u2019t full. Both goroutines attempt to update the same index of the backing array (index 1), which is a data race.

    How can we prevent the data race if we want both goroutines to work on a slice containing the initial elements of s plus an extra element? One solution is to create a copy of s.

    We should remember that using append on a shared slice in concurrent applications can lead to a data race. Hence, it should be avoided.

    Source code

    "},{"location":"#using-mutexes-inaccurately-with-slices-and-maps-70","title":"Using mutexes inaccurately with slices and maps (#70)","text":"TL;DR

    Remembering that slices and maps are pointers can prevent common data races.

    Let's implement a Cache struct used to handle caching for customer balances. This struct will contain a map of balances per customer ID and a mutex to protect concurrent accesses:

    type Cache struct {\n    mu       sync.RWMutex\n    balances map[string]float64\n}\n

    Next, we add an AddBalance method that mutates the balances map. The mutation is done in a critical section (within a mutex lock and a mutex unlock):

    func (c *Cache) AddBalance(id string, balance float64) {\n    c.mu.Lock()\n    c.balances[id] = balance\n    c.mu.Unlock()\n}\n

    Meanwhile, we have to implement a method to calculate the average balance for all the customers. One idea is to handle a minimal critical section this way:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    balances := c.balances // Creates a copy of the balances map\n    c.mu.RUnlock()\n\n    sum := 0.\n    for _, balance := range balances { // Iterates over the copy, outside of the critical section\n        sum += balance\n    }\n    return sum / float64(len(balances))\n}\n

    What's the problem with this code?

    If we run a test using the -race flag with two concurrent goroutines, one calling AddBalance (hence mutating balances) and another calling AverageBalance, a data race occurs. What\u2019s the problem here?

    Internally, a map is a runtime.hmap struct containing mostly metadata (for example, a counter) and a pointer referencing data buckets. So, balances := c.balances doesn\u2019t copy the actual data. Therefore, the two goroutines perform operations on the same data set, and one mutates it. Hence, it's a data race.

    One possible solution is to protect the whole AverageBalance function:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    defer c.mu.RUnlock() // Unlocks when the function returns\n\n    sum := 0.\n    for _, balance := range c.balances {\n        sum += balance\n    }\n    return sum / float64(len(c.balances))\n}\n

    Another option, if the iteration operation isn\u2019t lightweight, is to work on an actual copy of the data and protect only the copy:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    m := make(map[string]float64, len(c.balances)) // Copies the map\n    for k, v := range c.balances {\n        m[k] = v\n    }\n    c.mu.RUnlock()\n\n    sum := 0.\n    for _, balance := range m {\n        sum += balance\n    }\n    return sum / float64(len(m))\n}\n

    Once we have made a deep copy, we release the mutex. The iterations are done on the copy outside of the critical section.

    In summary, we have to be careful with the boundaries of a mutex lock. In this section, we have seen why assigning an existing map (or an existing slice) to a map isn\u2019t enough to protect against data races. The new variable, whether a map or a slice, is backed by the same data set. There are two leading solutions to prevent this: protect the whole function, or work on a copy of the actual data. In all cases, let\u2019s be cautious when designing critical sections and make sure the boundaries are accurately defined.

    Source code

    "},{"location":"#misusing-syncwaitgroup-71","title":"Misusing sync.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 about sync.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 using errgroup (#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 a sync 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;DR

    Remain 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 {\n    select {\n    case <-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.

    Developers often use time.After in loops or HTTP handlers repeatedly to implement the timing function. But it can lead to unintended peak memory consumption due to the delayed release of resources, just like the following code:

    func consumer(ch <-chan Event) {\n    for {\n        select {\n        case event := <-ch:\n            handle(event)\n        case <-time.After(time.Hour):\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    The source code of the function time.After is as follows:

    func After(d Duration) <-chan Time {\n    return NewTimer(d).C\n}\n

    As we see, it returns receive-only channel.

    When time.After is used in a loop or repeated context, a new channel is created in each iteration. If these channels are not properly closed or if their associated timers are not stopped, they can accumulate and consume memory. The resources associated with each timer and channel are only released when the timer expires or the channel is closed.

    To avoid this happening, We can use context's timeout setting instead of time.After, like below:

    func consumer(ch <-chan Event) {\n    for {\n        ctx, cancel := context.WithTimeout(context.Background(), time.Hour)\n        select {\n        case event := <-ch:\n            cancel()\n            handle(event)\n        case <-ctx.Done():\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    We can also use time.NewTimer like so:

    func consumer(ch <-chan Event) {\n    timerDuration := 1 * time.Hour\n    timer := time.NewTimer(timerDuration)\n\n    for {\n        timer.Reset(timerDuration)\n        select {\n        case event := <-ch:\n            handle(event)\n        case <-timer.C:\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    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

    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":"

    Call 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;DR

    To 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.

    Consider the following HTTP handler that handles an error from foo using http.Error:

    func handler(w http.ResponseWriter, req *http.Request) {\n    err := foo(req)\n    if err != nil {\n        http.Error(w, \"foo\", http.StatusInternalServerError)\n    }\n\n    _, _ = w.Write([]byte(\"all good\"))\n    w.WriteHeader(http.StatusCreated)\n}\n

    If we run this code and err != nil, the HTTP response would be:

    foo\nall good\n

    The response contains both the error and success messages, and also the first HTTP status code, 500. There would also be a warning log indicating that we attempted to write the status code multiple times:

    2023/10/10 16:45:33 http: superfluous response.WriteHeader call from main.handler (main.go:20)\n

    The mistake in this code is that http.Error does not stop the handler's execution, which means the success message and status code get written in addition to the error. Beyond an incorrect response, failing to return after writing an error can lead to the unwanted execution of code and unexpected side-effects. The following code adds the return statement following the http.Error and exhibits the desired behavior when ran:

    func handler(w http.ResponseWriter, req *http.Request) {\n    err := foo(req)\n    if err != nil {\n        http.Error(w, \"foo\", http.StatusInternalServerError)\n        return // Adds the return statement\n    }\n\n    _, _ = w.Write([]byte(\"all good\"))\n    w.WriteHeader(http.StatusCreated)\n}\n

    Source code

    "},{"location":"#using-the-default-http-client-and-server-81","title":"Using the default HTTP client and server (#81)","text":"TL;DR

    For 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;DR

    Categorizing 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;DR

    Enabling 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.

    In Go, the race detector isn\u2019t a static analysis tool used during compilation; instead, it\u2019s a tool to find data races that occur at runtime. To enable it, we have to enable the -race flag while compiling or running a test. For example:

    go test -race ./...\n

    Once the race detector is enabled, the compiler instruments the code to detect data races. Instrumentation refers to a compiler adding extra instructions: here, tracking all memory accesses and recording when and how they occur.

    Enabling the race detector adds an overhead in terms of memory and execution time; hence, it's generally recommended to enable it only during local testing or continuous integration, not production.

    If a race is detected, Go raises a warning. For example:

    package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    i := 0\n    go func() { i++ }()\n    fmt.Println(i)\n}\n

    Running this code with the -race logs the following warning:

    ==================\nWARNING: DATA RACE\nWrite at 0x00c000026078 by goroutine 7: # (1)\n  main.main.func1()\n      /tmp/app/main.go:9 +0x4e\n\nPrevious read at 0x00c000026078 by main goroutine: # (2)\n  main.main()\n      /tmp/app/main.go:10 +0x88\n\nGoroutine 7 (running) created at: # (3)\n  main.main()\n      /tmp/app/main.go:9 +0x7a\n==================\n
    1. Indicates that goroutine 7 was writing
    2. Indicates that the main goroutine was reading
    3. Indicates when the goroutine 7 was created

    Let\u2019s make sure we are comfortable reading these messages. Go always logs the following:

    In addition, if a specific file contains tests that lead to data races, we can exclude it from race detection using the !race build tag:

    //go:build !race\n\npackage main\n\nimport (\n    \"testing\"\n)\n\nfunc TestFoo(t *testing.T) {\n    // ...\n}\n
    "},{"location":"#not-using-test-execution-modes-parallel-and-shuffle-84","title":"Not using test execution modes (parallel and shuffle) (#84)","text":"TL;DR

    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.

    "},{"location":"#not-using-table-driven-tests-85","title":"Not using table-driven tests (#85)","text":"TL;DR

    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;DR

    Avoid 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;DR

    Understanding 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":"

    Source code

    Source code

    "},{"location":"#writing-inaccurate-benchmarks-89","title":"Writing inaccurate benchmarks (#89)","text":"TL;DR

    Regarding 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;DR

    Fuzzing 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;DR

    Knowing 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.

    Read the full section here.

    Source code

    "},{"location":"#not-taking-into-account-instruction-level-parallelism-93","title":"Not taking into account instruction-level parallelism (#93)","text":"TL;DR

    Use 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;DR

    You 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;DR

    Understanding 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, and sync.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;DR

    Use 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;DR

    Rely 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;DR

    Understanding 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;DR

    To help avoid CPU throttling when deployed in Docker and Kubernetes, keep in mind that Go isn\u2019t CFS-aware.

    By default, GOMAXPROCS is set to the number of OS-apparent logical CPU cores.

    When running some Go code inside Docker and Kubernetes, we must know that Go isn't CFS-aware (github.com/golang/go/issues/33803). Therefore, GOMAXPROCS isn't automatically set to the value of spec.containers.resources.limits.cpu (see Kubernetes Resource Management for Pods and Containers); instead, it's set to the number of logical cores on the host machine. The main implication is that it can lead to an increased tail latency in some specific situations.

    One solution is to rely on uber-go/automaxprocs that automatically set GOMAXPROCS to match the Linux container CPU quota.

    "},{"location":"#community","title":"Community","text":"

    Thanks to all the contributors:

    "},{"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.

    Note

    In 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:

    1. Allocate an empty map.
    2. Add 1 million elements.
    3. Remove all the elements, and run a Garbage Collection (GC).

    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() {\n    n := 1_000_000\n    m := make(map[int][128]byte)\n    printAlloc()\n\n    for i := 0; i < n; i++ { // Adds 1 million elements\n        m[i] = [128]byte{}\n    }\n    printAlloc()\n\n    for i := 0; i < n; i++ { // Deletes 1 million elements\n        delete(m, i)\n    }\n\n    runtime.GC() // Triggers a manual GC\n    printAlloc()\n    runtime.KeepAlive(m) // Keeps a reference to m so that the map isn\u2019t collected\n}\n\nfunc printAlloc() {\n    var m runtime.MemStats\n    runtime.ReadMemStats(&m)\n    fmt.Printf(\"%d MB\\n\", m.Alloc/(1024*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 {\n    B 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.

    Step map[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":"5-interface-pollution/","title":"Interface pollution","text":"

    Interfaces are one of the cornerstones of the Go language when designing and structuring our code. However, like many tools or concepts, abusing them is generally not a good idea. Interface pollution is about overwhelming our code with unnecessary abstractions, making it harder to understand. It\u2019s a common mistake made by developers coming from another language with different habits. Before delving into the topic, let\u2019s refresh our minds about Go\u2019s interfaces. Then, we will see when it\u2019s appropriate to use interfaces and when it may be considered pollution.

    "},{"location":"5-interface-pollution/#concepts","title":"Concepts","text":"

    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.

    To understand what makes interfaces so powerful, we will dig into two popular ones from the standard library: io.Reader and io.Writer. The io package provides abstractions for I/O primitives. Among these abstractions, io.Reader relates to reading data from a data source and io.Writer to writing data to a target, as represented in the next figure:

    The io.Reader contains a single Read method:

    type Reader interface {\n    Read(p []byte) (n int, err error)\n}\n

    Custom implementations of the io.Reader interface should accept a slice of bytes, filling it with its data and returning either the number of bytes read or an error.

    On the other hand, io.Writer defines a single method, Write:

    type Writer interface {\n    Write(p []byte) (n int, err error)\n}\n

    Custom implementations of io.Writer should write the data coming from a slice to a target and return either the number of bytes written or an error. Therefore, both interfaces provide fundamental abstractions:

    What is the rationale for having these two interfaces in the language? What is the point of creating these abstractions?

    Let\u2019s assume we need to implement a function that should copy the content of one file to another. We could create a specific function that would take as input two *os.File. Or, we can choose to create a more generic function using io.Reader and io.Writer abstractions:

    func copySourceToDest(source io.Reader, dest io.Writer) error {\n    // ...\n}\n

    This function would work with *os.File parameters (as *os.File implements both io.Reader and io.Writer) and any other type that would implement these interfaces. For example, we could create our own io.Writer that writes to a database, and the code would remain the same. It increases the genericity of the function; hence, its reusability.

    Furthermore, writing a unit test for this function is easier because, instead of having to handle files, we can use the strings and bytes packages that provide helpful implementations:

    func TestCopySourceToDest(t *testing.T) {\n    const input = \"foo\"\n    source := strings.NewReader(input) // Creates an io.Reader\n    dest := bytes.NewBuffer(make([]byte, 0)) // Creates an io.Writer\n\n    err := copySourceToDest(source, dest) // Calls copySourceToDest from a *strings.Reader and a *bytes.Buffer\n    if err != nil {\n        t.FailNow()\n    }\n\n    got := dest.String()\n    if got != input {\n        t.Errorf(\"expected: %s, got: %s\", input, got)\n    }\n}\n

    In the example, source is a *strings.Reader, whereas dest is a *bytes.Buffer. Here, we test the behavior of copySourceToDest without creating any files.

    While designing interfaces, the granularity (how many methods the interface contains) is also something to keep in mind. A known proverb in Go relates to how big an interface should be:

    Rob Pike

    The bigger the interface, the weaker the abstraction.

    Indeed, adding methods to an interface can decrease its level of reusability. io.Reader and io.Writer are powerful abstractions because they cannot get any simpler. Furthermore, we can also combine fine-grained interfaces to create higher-level abstractions. This is the case with io.ReadWriter, which combines the reader and writer behaviors:

    type ReadWriter interface {\n    Reader\n    Writer\n}\n
    Note

    As Einstein said, \u201cEverything should be made as simple as possible, but no simpler.\u201d Applied to interfaces, this denotes that finding the perfect granularity for an interface isn\u2019t necessarily a straightforward process.

    Let\u2019s now discuss common cases where interfaces are recommended.

    "},{"location":"5-interface-pollution/#when-to-use-interfaces","title":"When to use interfaces","text":"

    When should we create interfaces in Go? Let\u2019s look at three concrete use cases where interfaces are usually considered to bring value. Note that the goal isn\u2019t to be exhaustive because the more cases we add, the more they would depend on the context. However, these three cases should give us a general idea:

    "},{"location":"5-interface-pollution/#common-behavior","title":"Common behavior","text":"

    The first option we will discuss is to use interfaces when multiple types implement a common behavior. In such a case, we can factor out the behavior inside an interface. If we look at the standard library, we can find many examples of such a use case. For example, sorting a collection can be factored out via three methods:

    Hence, the following interface was added to the sort package:

    type Interface interface {\n    Len() int // Number of elements\n    Less(i, j int) bool // Checks two elements\n    Swap(i, j int) // Swaps two elements\n}\n

    This interface has a strong potential for reusability because it encompasses the common behavior to sort any collection that is index-based.

    Throughout the sort package, we can find dozens of implementations. If at some point we compute a collection of integers, for example, and we want to sort it, are we necessarily interested in the implementation type? Is it important whether the sorting algorithm is a merge sort or a quicksort? In many cases, we don\u2019t care. Hence, the sorting behavior can be abstracted, and we can depend on the sort.Interface.

    Finding the right abstraction to factor out a behavior can also bring many benefits. For example, the sort package provides utility functions that also rely on sort.Interface, such as checking whether a collection is already sorted. For instance:

    func IsSorted(data Interface) bool {\n    n := data.Len()\n    for i := n - 1; i > 0; i-- {\n        if data.Less(i, i-1) {\n            return false\n        }\n    }\n    return true\n}\n

    Because sort.Interface is the right level of abstraction, it makes it highly valuable.

    Let\u2019s now see another main use case when using interfaces.

    "},{"location":"5-interface-pollution/#decoupling","title":"Decoupling","text":"

    Another important use case is about decoupling our code from an implementation. If we rely on an abstraction instead of a concrete implementation, the implementation itself can be replaced with another without even having to change our code. This is the Liskov Substitution Principle (the L in Robert C. Martin\u2019s SOLID design principles).

    One benefit of decoupling can be related to unit testing. Let\u2019s assume we want to implement a CreateNewCustomer method that creates a new customer and stores it. We decide to rely on the concrete implementation directly (let\u2019s say a mysql.Store struct):

    type CustomerService struct {\n    store mysql.Store // Depends on the concrete implementation\n}\n\nfunc (cs CustomerService) CreateNewCustomer(id string) error {\n    customer := Customer{id: id}\n    return cs.store.StoreCustomer(customer)\n}\n

    Now, what if we want to test this method? Because customerService relies on the actual implementation to store a Customer, we are obliged to test it through integration tests, which requires spinning up a MySQL instance (unless we use an alternative technique such as go-sqlmock, but this isn\u2019t the scope of this section). Although integration tests are helpful, that\u2019s not always what we want to do. To give us more flexibility, we should decouple CustomerService from the actual implementation, which can be done via an interface like so:

    type customerStorer interface { // Creates a storage abstraction\n    StoreCustomer(Customer) error\n}\n\ntype CustomerService struct {\n    storer customerStorer // Decouples CustomerService from the actual implementation\n}\n\nfunc (cs CustomerService) CreateNewCustomer(id string) error {\n    customer := Customer{id: id}\n    return cs.storer.StoreCustomer(customer)\n}\n

    Because storing a customer is now done via an interface, this gives us more flexibility in how we want to test the method. For instance, we can:

    Let\u2019s now discuss another use case: to restrict a behavior.

    "},{"location":"5-interface-pollution/#restricting-behavior","title":"Restricting behavior","text":"

    The last use case we will discuss can be pretty counterintuitive at first sight. It\u2019s about restricting a type to a specific behavior. Let\u2019s imagine we implement a custom configuration package to deal with dynamic configuration. We create a specific container for int configurations via an IntConfig struct that also exposes two methods: Get and Set. Here\u2019s how that code would look:

    type IntConfig struct {\n    // ...\n}\n\nfunc (c *IntConfig) Get() int {\n    // Retrieve configuration\n}\n\nfunc (c *IntConfig) Set(value int) {\n    // Update configuration\n}\n

    Now, suppose we receive an IntConfig that holds some specific configuration, such as a threshold. Yet, in our code, we are only interested in retrieving the configuration value, and we want to prevent updating it. How can we enforce that, semantically, this configuration is read-only, if we don\u2019t want to change our configuration package? By creating an abstraction that restricts the behavior to retrieving only a config value:

    type intConfigGetter interface {\n    Get() int\n}\n

    Then, in our code, we can rely on intConfigGetter instead of the concrete implementation:

    type Foo struct {\n    threshold intConfigGetter\n}\n\nfunc NewFoo(threshold intConfigGetter) Foo { // Injects the configuration getter\n    return Foo{threshold: threshold}\n}\n\nfunc (f Foo) Bar()  {\n    threshold := f.threshold.Get() // Reads the configuration\n    // ...\n}\n

    In this example, the configuration getter is injected into the NewFoo factory method. It doesn\u2019t impact a client of this function because it can still pass an IntConfig struct as it implements intConfigGetter. Then, we can only read the configuration in the Bar method, not modify it. Therefore, we can also use interfaces to restrict a type to a specific behavior for various reasons, such as semantics enforcement.

    In this section, we saw three potential 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. Again, this list isn\u2019t exhaustive, but it should give us a general understanding of when interfaces are helpful in Go.

    Now, let\u2019s finish this section and discuss the problems with interface pollution.

    "},{"location":"5-interface-pollution/#interface-pollution_1","title":"Interface pollution","text":"

    It\u2019s fairly common to see interfaces being overused in Go projects. Perhaps the developer\u2019s background was C# or Java, and they found it natural to create interfaces before concrete types. However, this isn\u2019t how things should work in Go.

    As we discussed, 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?

    Note

    We may also experience performance overhead when calling a method through an interface. It requires a lookup in a hash table\u2019s data structure to find the concrete type an interface points to. But this isn\u2019t an issue in many contexts as the overhead is minimal.

    In summary, we should be cautious when creating abstractions in our code\u2014abstractions 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.

    "},{"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:

    Note

    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.

    Note

    Context 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) {\n    if len(s) <= 1 {\n        return\n    }\n\n    middle := len(s) / 2\n    sequentialMergesort(s[:middle]) // First half\n    sequentialMergesort(s[middle:]) // Second half\n    merge(s, middle) // Merges the two halves\n}\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) {\n    if len(s) <= 1 {\n        return\n    }\n\n    middle := len(s) / 2\n\n    var wg sync.WaitGroup\n    wg.Add(2)\n\n    go func() { // Spins up the first half of the work in a goroutine\n        defer wg.Done()\n        parallelMergesortV1(s[:middle])\n    }()\n\n    go func() { // Spins up the second half of the work in a goroutine\n        defer wg.Done()\n        parallelMergesortV1(s[middle:])\n    }()\n\n    wg.Wait()\n    merge(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\n\nfunc parallelMergesortV2(s []int) {\n    if len(s) <= 1 {\n        return\n    }\n\n    if len(s) <= max {\n        sequentialMergesort(s) // Calls our initial sequential version\n    } else { // If bigger than the threshold, keeps the parallel version\n        middle := len(s) / 2\n\n        var wg sync.WaitGroup\n        wg.Add(2)\n\n        go func() {\n            defer wg.Done()\n            parallelMergesortV2(s[:middle])\n        }()\n\n        go func() {\n            defer wg.Done()\n            parallelMergesortV2(s[middle:])\n        }()\n\n        wg.Wait()\n        merge(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.

    Note

    Why 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:

    "},{"location":"89-benchmarks/#general-concepts","title":"General concepts","text":"

    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) {\n    for i := 0; i < b.N; i++ {\n        foo()\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) {\n    expensiveSetup()\n    for i := 0; i < b.N; i++ {\n        functionUnderTest()\n    }\n}\n

    In this case, we can use the ResetTimer method before entering the loop:

    func BenchmarkFoo(b *testing.B) {\n    expensiveSetup()\n    b.ResetTimer() // Reset the benchmark timer\n    for i := 0; i < b.N; i++ {\n        functionUnderTest()\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) {\n    for i := 0; i < b.N; i++ {\n        expensiveSetup()\n        functionUnderTest()\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) {\n    for i := 0; i < b.N; i++ {\n        b.StopTimer() // Pause the benchmark timer\n        expensiveSetup()\n        b.StartTimer() // Resume the benchmark timer\n        functionUnderTest()\n    }\n}\n

    Here, we pause the benchmark timer to perform the expensive setup and then resume the timer.

    Note

    There\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) {\n    var v int32\n    for i := 0; i < b.N; i++ {\n        atomic.StoreInt32(&v, 1)\n    }\n}\n\nfunc BenchmarkAtomicStoreInt64(b *testing.B) {\n    var v int64\n    for i := 0; i < b.N; i++ {\n        atomic.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.

    Note

    We 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\n\nfunc popcnt(x uint64) uint64 {\n    x -= (x >> 1) & m1\n    x = (x & m2) + ((x >> 2) & m2)\n    x = (x + (x >> 4)) & m4\n    return (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) {\n    for i := 0; i < b.N; i++ {\n        popcnt(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) {\n    for 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:

    1. During each loop iteration, assign the result to a local variable (local in the context of the benchmark function).
    2. Assign the latest result to a global variable.

    In our case, we write the following benchmark:

    var global uint64 // Define a global variable\n\nfunc BenchmarkPopcnt2(b *testing.B) {\n    var v uint64 // Define a local variable\n    for i := 0; i < b.N; i++ {\n        v = popcnt(uint64(i)) // Assign the result to the local variable\n    }\n    global = 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.

    Note

    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 {\n    var sum int64\n    for i := 0; i < len(s); i++ { // Iterate over each row\n        for j := 0; j < 8; j++ { // Iterate over the first eight columns\n            sum += s[i][j] // Increment sum\n        }\n    }\n    return sum\n}\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\n\nvar res int64\n\nfunc BenchmarkCalculateSum512(b *testing.B) {\n    var sum int64\n    s := createMatrix512(rows) // Create a matrix of 512 columns\n    b.ResetTimer()\n    for i := 0; i < b.N; i++ {\n        sum = calculateSum512(s) // Create a matrix of 512 columns\n    }\n    res = sum\n}\n\nfunc BenchmarkCalculateSum513(b *testing.B) {\n    var sum int64\n    s := createMatrix513(rows) // Create a matrix of 513 columns\n    b.ResetTimer()\n    for i := 0; i < b.N; i++ {\n        sum = calculateSum513(s) // Calculate the sum\n    }\n    res = 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).

    Note

    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) {\n    var sum int64\n    for i := 0; i < b.N; i++ {\n        b.StopTimer()\n        s := createMatrix512(rows) // Create a new matrix during each loop iteration\n        b.StartTimer()\n        sum = calculateSum512(s)\n    }\n    res = 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 {\n    var keys []string\n    for k := range m {\n        keys = append(keys, k)\n    }\n    return 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) {\n    switch t := m.(type) {\n    default:\n        return nil, fmt.Errorf(\"unknown type: %T\", t)\n    case map[string]int:\n        var keys []any\n        for k := range t {\n            keys = append(keys, k)\n        }\n        return keys, nil\n    case map[int]string:\n        // Copy the extraction logic\n    }\n}\n

    We can start noticing a couple of issues:

    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 {\n  var keys []K\n  for k := range m {\n    keys = append(keys, k)\n  }\n  return 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\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\n   String() string\n}\n

    Using this constraint will restrict type arguments to custom types like this one:

    type customInt int\n\nfunc (i customInt) String() string {\n   return 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\n   Val  T\n   next *Node[T]\n}\n\nfunc (n *Node[T]) Add(next *Node[T]) { // Instantiate type receiver\n   n.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 {}\n\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
    type sliceFn[T any] struct { // Use type parameter\n   s       []T\n   compare func(T, T) bool // Compare two T elements\n}\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?

    func foo[T io.Writer](w T) {\n   b := getBytes()\n   _, _ = w.Write(b)\n}\n
    "},{"location":"9-generics/#conclusion","title":"Conclusion","text":"

    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":"92-false-sharing/","title":"Writing concurrent code that leads to false sharing","text":"

    In previous sections, we have discussed the fundamental concepts of CPU caching. We have seen that some specific caches (typically, L1 and L2) aren\u2019t shared among all the logical cores but are specific to a physical core. This specificity has some concrete impacts such as concurrency and the concept of false sharing, which can lead to a significant performance decrease. Let\u2019s look at what false sharing is via an example and then see how to prevent it.

    In this example, we use two structs, Input and Result:

    type Input struct {\n    a int64\n    b int64\n}\n\ntype Result struct {\n    sumA int64\n    sumB int64\n}\n

    The goal is to implement a count function that receives a slice of Input and computes the following:

    For the sake of the example, we implement a concurrent solution with one goroutine that computes sumA and another that computes sumB:

    func count(inputs []Input) Result {\n    wg := sync.WaitGroup{}\n    wg.Add(2)\n\n    result := Result{} // Init the result struct\n\n    go func() {\n        for i := 0; i < len(inputs); i++ {\n            result.sumA += inputs[i].a // Computes sumA\n        }\n        wg.Done()\n    }()\n\n    go func() {\n        for i := 0; i < len(inputs); i++ {\n            result.sumB += inputs[i].b // Computes sumB\n        }\n        wg.Done()\n    }()\n\n    wg.Wait()\n    return result\n}\n

    We spin up two goroutines: one that iterates over each a field and another that iterates over each b field. This example is fine from a concurrency perspective. For instance, it doesn\u2019t lead to a data race, because each goroutine increments its own variable. But this example illustrates the false sharing concept that degrades expected performance.

    Let\u2019s look at the main memory. Because sumA and sumB are allocated contiguously, in most cases (seven out of eight), both variables are allocated to the same memory block:

    In this example, sumA and sumB are part of the same memory block.

    Now, let\u2019s assume that the machine contains two cores. In most cases, we should eventually have two threads scheduled on different cores. So if the CPU decides to copy this memory block to a cache line, it is copied twice:

    Each block is copied to a cache line on both code 0 and core 1.

    Both cache lines are replicated because L1D (L1 data) is per core. Recall that in our example, each goroutine updates its own variable: sumA on one side, and sumB on the other side:

    Each goroutine updates its own variable.

    Because these cache lines are replicated, one of the goals of the CPU is to guarantee cache coherency. For example, if one goroutine updates sumA and another reads sumA (after some synchronization), we expect our application to get the latest value.

    However, our example doesn\u2019t do exactly this. Both goroutines access their own variables, not a shared one. We might expect the CPU to know about this and understand that it isn\u2019t a conflict, but this isn\u2019t the case. When we write a variable that\u2019s in a cache, the granularity tracked by the CPU isn\u2019t the variable: it\u2019s the cache line.

    When a cache line is shared across multiple cores and at least one goroutine is a writer, the entire cache line is invalidated. This happens even if the updates are logically independent (for example, sumA and sumB). This is the problem of false sharing, and it degrades performance.

    Note

    Internally, a CPU uses the MESI protocol to guarantee cache coherency. It tracks each cache line, marking it modified, exclusive, shared, or invalid (MESI).

    One of the most important aspects to understand about memory and caching is that sharing memory across cores isn\u2019t real\u2014it\u2019s an illusion. This understanding comes from the fact that we don\u2019t consider a machine a black box; instead, we try to have mechanical sympathy with underlying levels.

    So how do we solve false sharing? There are two main solutions.

    The first solution is to use the same approach we\u2019ve shown but ensure that sumA and sumB aren\u2019t part of the same cache line. For example, we can update the Result struct to add padding between the fields. Padding is a technique to allocate extra memory. Because an int64 requires an 8-byte allocation and a cache line 64 bytes long, we need 64 \u2013 8 = 56 bytes of padding:

    type Result struct {\n    sumA int64\n    _    [56]byte // Padding\n    sumB int64\n}\n

    The next figure shows a possible memory allocation. Using padding, sumA and sumB will always be part of different memory blocks and hence different cache lines.

    sumA and sumB are part of different memory blocks.

    If we benchmark both solutions (with and without padding), we see that the padding solution is significantly faster (about 40% on my machine). This is an important improvement that results from the addition of padding between the two fields to prevent false sharing.

    The second solution is to rework the structure of the algorithm. For example, instead of having both goroutines share the same struct, we can make them communicate their local result via channels. The result benchmark is roughly the same as with padding.

    In summary, we must remember that sharing memory across goroutines is an illusion at the lowest memory levels. False sharing occurs when a cache line is shared across two cores when at least one goroutine is a writer. If we need to optimize an application that relies on concurrency, we should check whether false sharing applies, because this pattern is known to degrade application performance. We can prevent false sharing with either padding or communication.

    "},{"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:

    Profiling 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.

    "},{"location":"98-profiling-execution-tracing/#enabling-pprof","title":"Enabling pprof","text":"

    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\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"net/http\"\n    _ \"net/http/pprof\" // Blank import to pprof\n)\n\nfunc main() {\n    // Exposes an HTTP endpoint\n    http.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n        fmt.Fprintf(w, \"\")\n    })\n    log.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.

    "},{"location":"98-profiling-execution-tracing/#cpu-profiling","title":"CPU Profiling","text":"

    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.

    Note

    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.

    Note

    We 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:

    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.

    Note

    We 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.

    "},{"location":"98-profiling-execution-tracing/#heap-profiling","title":"Heap Profiling","text":"

    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:

    Another very helpful capability with heap profiling is tracking memory leaks. With a GC-based language, the usual procedure is the following:

    1. Trigger a GC.
    2. Download heap data.
    3. Wait for a few seconds/minutes.
    4. Trigger another GC.
    5. Download another heap data.
    6. Compare.

    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:

    1. Go to /debug/pprof/heap?gc=1 (trigger the GC and download the heap profile).
    2. Wait for a few seconds/minutes.
    3. Go to /debug/pprof/heap?gc=1 again.
    4. Use go tool to compare both heap profiles:
    $ 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.

    "},{"location":"98-profiling-execution-tracing/#goroutine-profiling","title":"Goroutine Profiling","text":"

    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.

    Note

    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:

    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() {\n    v = fibonacci(10)\n})\nfibTask.End()\n\n// Creates a store task\nctx, fibStore := trace.NewTask(ctx, \"store\")\ntrace.WithRegion(ctx, \"main\", func() {\n    atomic.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?

    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.

    "},{"location":"book/","title":"100 Go Mistakes and How to Avoid Them","text":""},{"location":"book/#description","title":"Description","text":"

    If you're a Go developer looking to improve your skills, the 100 Go Mistakes and How to Avoid Them book is for you. With a focus on practical examples, this book 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 or the first chapter.

    "},{"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":"

    Covers (English, Japanese, Chinese, and Korean)"},{"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":"
    1. 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

    2. J. Metcalfe, \u201cLearning from Errors,\u201d Annual Review of Psychology, vol. 68, pp. 465\u2013489, Jan. 2017.\u00a0\u21a9

    3. 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

    "},{"location":"external/","title":"External Resources","text":""},{"location":"external/#english","title":"English","text":""},{"location":"external/#the-best-golang-book-prime-reacts","title":"The Best Golang Book | Prime Reacts","text":""},{"location":"external/#book-review-100-go-mistakes-and-how-to-avoid-them","title":"Book Review: 100 Go Mistakes (And How to Avoid Them)","text":"

    Post

    "},{"location":"external/#the-most-useful-book-for-a-go-programmer","title":"The Most Useful Book for a Go Programmer?","text":""},{"location":"external/#how-to-make-mistakes-in-go-go-time-190","title":"How to make mistakes in Go - Go Time #190","text":""},{"location":"external/#go-is-amazing","title":"Go is AMAZING","text":""},{"location":"external/#8lu-100-test-coverage","title":"8LU - 100% Test Coverage","text":""},{"location":"external/#some-tips-i-learned-from-100-mistakes-in-go","title":"Some Tips I learned from 100 Mistakes in Go","text":"

    Post

    "},{"location":"external/#what-can-be-summarized-from-100-go-mistakes","title":"What can be summarized from 100 Go Mistakes?","text":"

    Post

    "},{"location":"external/#book-review-100-go-mistakes-and-how-to-avoid-them_1","title":"Book review: 100 Go Mistakes and How to Avoid Them","text":"

    Post

    "},{"location":"external/#chinese","title":"Chinese","text":""},{"location":"external/#100-go-mistakes-and-how-to-avoid-them","title":"\u6df1\u5ea6\u9605\u8bfb\u4e4b\u300a100 Go Mistakes and How to Avoid Them","text":"

    Post

    "},{"location":"external/#100-go-mistakes","title":"100 Go Mistakes \u968f\u8bb0","text":"

    Post

    "},{"location":"external/#go","title":"\u6211\u4e3a\u4ec0\u4e48\u653e\u5f03Go\u8bed\u8a00\uff1f","text":"

    Post

    "},{"location":"external/#japanese","title":"Japanese","text":""},{"location":"external/#go100-go-mistakes-and-how-to-avoid-them","title":"\u6700\u8fd1\u8aad\u3093\u3060Go\u8a00\u8a9e\u306e\u672c\u306e\u7d39\u4ecb\uff1a100 Go Mistakes and How to Avoid Them","text":"

    Post

    "},{"location":"external/#100-go-mistakes-and-how-to-avoid-them_1","title":"\u300e100 Go Mistakes and How to Avoid Them\u300f\u3092\u8aad\u3080","text":"

    Post

    "},{"location":"external/#portuguese","title":"Portuguese","text":""},{"location":"external/#um-otimo-livro-para-programadores-go","title":"Um \u00d3TIMO livro para programadores Go","text":""},{"location":"ja/","title":"Go\u8a00\u8a9e\u3067\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044","text":"

    \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

    Jobs

    Is your company hiring? Sponsor the Japanese version of this repository and let a significant audience of Go developers (~1k unique visitors per week) know about your opportunities in this section.

    \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 foo() {\n    // ...\n    return 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    // ...\n    return true\n}\n// ...\n
    if s != \"\" {\n    // ...\n} else {\n    return 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 == \"\" {\n    return 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\u30b9\u30c6\u30fc\u30c8\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\u30b9\u30c6\u30fc\u30c8\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\u7279\u5b9a\u306e\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\u7d04

    Go\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\u5316\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\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\u60f3\u5916\u306e\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 {\n    Bar // \u57cb\u3081\u8fbc\u307f\u30d5\u30a3\u30fc\u30eb\u30c9\n}\n\ntype Bar struct {\n    Baz 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

    \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\u7d04

    API \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 options struct {\n  port *int\n}\n\ntype Option func(options *options) error\n\nfunc WithPort(port int) Option {\n  return func(options *options) error {\n    if port < 0 {\n    return errors.New(\"port should be positive\")\n  }\n  options.port = &port\n  return nil\n  }\n}\n\nfunc NewServer(addr string, opts ...Option) ( *http.Server, error) {\n  var options options\n  for _, opt := range opts { \n    err := opt(&options) \n    if err != nil {\n      return nil, err\n    }\n  }\n\n// \u3053\u306e\u6bb5\u968e\u3067\u3001options \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\n  var port int\n  if options.port == nil {\n    port = defaultHTTPPort\n  } else {\n      if *options.port == 0 {\n      port = randomPort()\n    } else {\n      port = *options.port\n    }\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\u8db3

    Go \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\u3092 2023 \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\u5229\u7528\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\u3046 2 \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\u305f\u3060\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\u3068\u3044\u3046\u3053\u3068\u306f\u7406\u89e3\u3057\u3001\u899a\u3048\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002

    \u30ea\u30f3\u30bf\u30fc\u306e\u307b\u304b\u306b\u3001\u30b3\u30fc\u30c9\u30b9\u30bf\u30a4\u30eb\u3092\u4fee\u6b63\u3059\u308b\u305f\u3081\u306b\u30b3\u30fc\u30c9\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u30fc\u3082\u4f7f\u7528\u3057\u307e\u3057\u3087\u3046\u3002\u4ee5\u4e0b\u306b\u3001\u3044\u304f\u3064\u304b\u306e\u30b3\u30fc\u30c9\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u30fc\u3092\u793a\u3057\u307e\u3059\u3002

    \u307b\u304b\u306b golangci-lint (https://github.com/golangci/golangci-lint) \u3068\u3044\u3046\u3082\u306e\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u591a\u304f\u306e\u4fbf\u5229\u306a\u30ea\u30f3\u30bf\u30fc\u3084\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u30fc\u306e\u4e0a\u306b\u30d5\u30a1\u30b5\u30fc\u30c9\u3092\u63d0\u4f9b\u3059\u308b\u30ea\u30f3\u30c6\u30a3\u30f3\u30b0\u30c4\u30fc\u30eb\u3067\u3059\u3002\u307e\u305f\u3001\u30ea\u30f3\u30bf\u30fc\u3092\u4e26\u5217\u5b9f\u884c\u3057\u3066\u5206\u6790\u901f\u5ea6\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u975e\u5e38\u306b\u4fbf\u5229\u3067\u3059\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\u3044\u3066\u3057\u307e\u3046 (#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\u304c 8 \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\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

    \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\u3092\u7121\u8996\u3057\u3066\u3044\u308b (#18)","text":"\u8981\u7d04

    Go\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\u9664\u3044\u305f\u5834\u5408\uff09 float32 \u3068 float64 \u3068\u3044\u3046 2 \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\u60f3\u5916\u306e\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\u7d04

    Go \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\u3002

    \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\u7d04

    encoding/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

    \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\u60f3\u5916\u306e\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\u30de\u30c3\u30d7\u3067\u3082\u540c\u3058\u3067\u3059\u3002\u30de\u30c3\u30d7\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\u306e\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\u304c 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\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/#append-25","title":"append \u306e\u4f7f\u7528\u306b\u3088\u308b\u4e88\u60f3\u5916\u306e\u526f\u4f5c\u7528 (#25)","text":"\u8981\u7d04

    2\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\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\u306e\u30b3\u30d4\u30fc\u3060\u3051\u3067\u3059\u3002

    \u30b9\u30e9\u30a4\u30b9\u3092\u4f7f\u7528\u3059\u308b\u3068\u304d\u306f\u3001\u4e88\u60f3\u5916\u306e\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\u306e\u30b3\u30d4\u30fc\u307e\u305f\u306f\u5b8c\u5168\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

    \u88dc\u8db3

    s[low:high:max]\uff08\u5b8c\u5168\u30b9\u30e9\u30a4\u30b9\u5f0f\uff09\u2015\u2015\u3053\u306e\u547d\u4ee4\u6587\u306f\u3001\u5bb9\u91cf\u304c max - low \u306b\u7b49\u3057\u3044\u3053\u3068\u3092\u9664\u3051\u3070\u3001s[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

    \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\u306e\u30b3\u30d4\u30fc\u3092\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","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\u30de\u30c3\u30d7\u306e\u521d\u671f\u5316 (#27)","text":"\u8981\u7d04

    \u30de\u30c3\u30d7\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

    \u30de\u30c3\u30d7\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\u30de\u30c3\u30d7\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

    \u30de\u30c3\u30d7\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\u30de\u30c3\u30d7\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":"\u30de\u30c3\u30d7\u3068\u30e1\u30e2\u30ea\u30ea\u30fc\u30af (#28)","text":"\u8981\u7d04

    \u30de\u30c3\u30d7\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\u30de\u30c3\u30d7\u3092\u5f37\u5236\u7684\u306b\u518d\u751f\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\u3002

    \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\u7d04

    Go\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\u507d\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

    \u88dc\u8db3

    ? \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\u30de\u30c3\u30d7\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\u578b\u306b\u52a0\u3048\u3066\u3001\u914d\u5217\u3001\u69cb\u9020\u4f53\u3001\u30b9\u30e9\u30a4\u30b9\u3001\u30de\u30c3\u30d7\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\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":"\u5236\u5fa1\u69cb\u9020","text":""},{"location":"ja/#range-30","title":"\u8981\u7d20\u304c range \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 {\n    a[2] = 10\n    if i == 2 {\n        fmt.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":"\u30de\u30c3\u30d7\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\u30de\u30c3\u30d7\u306e\u633f\u5165\uff09 (#33)","text":"\u8981\u7d04

    \u30de\u30c3\u30d7\u3092\u4f7f\u7528\u3059\u308b\u3068\u304d\u306b\u4e88\u6e2c\u53ef\u80fd\u306a\u51fa\u529b\u3092\u4fdd\u8a3c\u3059\u308b\u306b\u306f\u3001\u30de\u30c3\u30d7\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++ {\n    fmt.Printf(\"%d \", i)\n\n    switch i {\n    default:\n    case 2:\n        break\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:\n    for i := 0; i < 5; i++ {\n        fmt.Printf(\"%d \", i)\n\n        switch i {\n        default:\n        case 2:\n            break 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\u3067 defer \u3092\u4f7f\u7528\u3059\u308b (#35)","text":"\u8981\u7d04

    \u95a2\u6570\u5185\u306e\u30eb\u30fc\u30d7\u30ed\u30b8\u30c3\u30af\u306e\u62bd\u51fa\u306f\u3001\u5404\u53cd\u5fa9\u306e\u6700\u5f8c\u3067\u306e defer \u6587\u306e\u5b9f\u884c\u306b\u3064\u306a\u304c\u308a\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 {\n    for path := range ch {\n        file, err := os.Open(path)\n        if err != nil {\n            return err\n        }\n\n        defer file.Close()\n\n        // \u30d5\u30a1\u30a4\u30eb\u306e\u51e6\u7406\u3092\u3059\u308b\n    }\n    return 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 {\n    for path := range ch {\n        if err := readFile(path); err != nil {\n            return err\n        }\n    }\n    return nil\n}\n\nfunc readFile(path string) error {\n    file, err := os.Open(path)\n    if err != nil {\n        return err\n    }\n\n    defer file.Close()\n\n    // \u30d5\u30a1\u30a4\u30eb\u306e\u51e6\u7406\u3092\u3059\u308b\n    return 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

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#37","title":"\u6587\u5b57\u5217\u306b\u5bfe\u3059\u308b\u4e0d\u6b63\u306a\u53cd\u5fa9\u51e6\u7406 (#37)","text":"\u8981\u7d04

    range \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\u51e6\u7406\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 {\n    fmt.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\u308b 3 \u70b9\u3092\u53d6\u308a\u4e0a\u3052\u307e\u3057\u3087\u3046\u3002

    \u7d50\u679c\u306e\u6700\u5f8c\u304b\u3089\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002len \u306f\u30eb\u30fc\u30f3\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 {\n    fmt.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 {\n    fmt.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\u7d04

    strings.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 {\n    s := \"\"\n    for _, value := range values {\n        s += value\n    }\n    return 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 {\n    sb := strings.Builder{}\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return 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

    \u88dc\u8db3

    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 {\n    total := 0\n    for i := 0; i < len(values); i++ {\n        total += len(values[i])\n    }\n\n    sb := strings.Builder{}\n    sb.Grow(total) (2)\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return sb.String()\n}\n

    \u30d9\u30f3\u30c1\u30de\u30fc\u30af\u3092\u5b9f\u884c\u3057\u3066 3 \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\u7d04

    bytes \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\u30b3\u30b9\u30c8\u3092\u56de\u907f\u3067\u304d\u308b\u304b\u3069\u3046\u304b\u3092\u6700\u521d\u306b\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\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\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\u3063\u3066\u3044\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\n\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

    \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) {\n    b = a\n    return\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\u60f3\u5916\u306e\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) (\n    lat, lng float32, err error) {\n    isValid := l.validateAddress(address) (1)\n    if !isValid {\n        return 0, 0, errors.New(\"invalid address\")\n    }\n\n    if ctx.Err() != nil { (2)\n        return 0, 0, err\n    }\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\u308b\u3079\u304d\u3067\u3059\uff08 os.Open \u306a\u3069\u306e\u7279\u5b9a\u306e\u95a2\u6570\u3092\u9664\u304f\uff09\u3002\u8907\u6570\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u306b\u306b\u306a\u308b\u304b\u3082\u3057\u308c\u305a\u3001\u5358\u4f53\u30c6\u30b9\u30c8\u304c\u3088\u308a\u8907\u96d1\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u304b\u3089\u3067\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 (\n    StatusSuccess  = \"success\"\n    StatusErrorFoo = \"error_foo\"\n    StatusErrorBar = \"error_bar\"\n)\n\nfunc f() error {\n    var status string\n    defer notify(status)\n    defer incrementCounter(status)\n\n    if err := foo(); err != nil {\n        status = StatusErrorFoo\n        return err\n    }\n\n    if err := bar(); err != nil {\n        status = StatusErrorBar\n        return err\n    }\n\n    status = StatusSuccess\n    return 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 {\n    var status string\n    defer notify(&status) \n    defer incrementCounter(&status)\n\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 {\n    var status string\n    defer func() {\n        notify(status)\n        incrementCounter(status)\n    }()\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\u7d04

    panic \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() {\n    fmt.Println(\"a\")\n    panic(\"foo\")\n    fmt.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\u751f\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\u306a\u6bd4\u8f03 (#50)","text":"\u8981\u7d04

    Go 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\u306a\u6bd4\u8f03 (#51)","text":"\u8981\u7d04

    Go 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\"\n\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

    \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  // ...\n  notify() // \u30a8\u30e9\u30fc\u51e6\u7406\u306f\u7701\u7565\u3055\u308c\u3066\u3044\u307e\u3059\n}\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\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\u3002

    \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\u30b9\u30c6\u30fc\u30c8\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\u30eb\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

    "},{"location":"ja/#go-58","title":"\u7af6\u5408\u554f\u984c\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044\uff08\u30c7\u30fc\u30bf\u7af6\u5408\u3068\u7af6\u5408\u72b6\u614b\u3001\u305d\u3057\u3066Go\u8a00\u8a9e\u306e\u30e1\u30e2\u30ea\u30e2\u30c7\u30eb\uff09 (#58)","text":"\u8981\u7d04

    \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

    "},{"location":"ja/#_11","title":"\u7af6\u5408\u72b6\u614b","text":"

    \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\u60f3\u5916\u306e\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\u3002I/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

    \u88dc\u8db3

    \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 3 \u3064\u76ee\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\u7d04

    Go 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

    \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

    "},{"location":"ja/#context-value","title":"Context Value","text":"

    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\u30b5\u30d6\u95a2\u6570\u306e\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

    \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/#_14","title":"\u4e26\u884c\u51e6\u7406\uff1a\u5b9f\u8df5","text":""},{"location":"ja/#context-61","title":"\u4e0d\u9069\u5207\u306a Context \u3092\u5e83\u3081\u3066\u3057\u307e\u3046 (#61)","text":"\u8981\u7d04

    Context \u3092\u4f1d\u64ad\u3059\u308b\u969b\u306b\u306f\u3001Context \u3092\u30ad\u30e3\u30f3\u30bb\u30eb\u3067\u304d\u308b\u6761\u4ef6\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u9001\u4fe1\u3055\u308c\u305f\u969b\u306b HTTP \u30cf\u30f3\u30c9\u30e9\u304c Context \u3092\u30ad\u30e3\u30f3\u30bb\u30eb\u3059\u308b\u3068\u304d\u306a\u3069\u3067\u3059\u3002

    \u591a\u304f\u306e\u72b6\u6cc1\u3067\u306f\u3001Go Context \u3092\u4f1d\u64ad\u3059\u308b\u3053\u3068\u304c\u63a8\u5968\u3055\u308c\u307e\u3059\u3002\u305f\u3060\u3057\u3001Context \u306e\u4f1d\u64ad\u306b\u3088\u3063\u3066\u8efd\u5fae\u306a\u30d0\u30b0\u304c\u767a\u751f\u3057\u3001\u30b5\u30d6\u95a2\u6570\u304c\u6b63\u3057\u304f\u5b9f\u884c\u3055\u308c\u306a\u304f\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002

    \u6b21\u306e\u4f8b\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u3044\u304f\u3064\u304b\u306e\u30bf\u30b9\u30af\u3092\u5b9f\u884c\u3057\u3066\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059 HTTP \u30cf\u30f3\u30c9\u30e9\u3092\u516c\u958b\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\u76f4\u524d\u306b\u3001\u305d\u308c\u3092 Kafka \u30c8\u30d4\u30c3\u30af\u306b\u9001\u4fe1\u3057\u305f\u3044\u3068\u601d\u3063\u3066\u3044\u307e\u3059\u3002HTTP \u30b3\u30f3\u30b7\u30e5\u30fc\u30de\u306b\u30ec\u30a4\u30c6\u30f3\u30b7\u306e\u70b9\u3067\u30da\u30ca\u30eb\u30c6\u30a3\u3092\u8ab2\u3057\u305f\u304f\u306a\u3044\u306e\u3067\u3001publish \u30a2\u30af\u30b7\u30e7\u30f3\u3092\u65b0\u3057\u3044\u30b4\u30eb\u30fc\u30c1\u30f3\u5185\u3067\u975e\u540c\u671f\u306b\u51e6\u7406\u3057\u305f\u3044\u3068\u8003\u3048\u3066\u3044\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001Context \u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u305f\u5834\u5408\u306b\u30e1\u30c3\u30bb\u30fc\u30b8\u306e publish \u30a2\u30af\u30b7\u30e7\u30f3\u3092\u4e2d\u65ad\u3067\u304d\u308b\u3088\u3046\u306b\u3001Context \u3092\u53d7\u3051\u5165\u308c\u308b publish \u95a2\u6570\u3092\u81ea\u7531\u306b\u4f7f\u3048\u308b\u3068\u3057\u307e\u3059\u3002\u53ef\u80fd\u306a\u5b9f\u88c5\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002

    func handler(w http.ResponseWriter, r *http.Request) {\n    response, err := doSomeTask(r.Context(), r)\n    if err != nil {\n        http.Error(w, err.Error(), http.StatusInternalServerError)\n    return\n    }\n    go func() {\n        err := publish(r.Context(), response)\n        // err \u306e\u51e6\u7406\u3092\u3059\u308b\n    }()\n    writeResponse(response)\n}\n

    \u3053\u306e\u30b3\u30fc\u30c9\u306e\u4f55\u304c\u554f\u984c\u306a\u306e\u3067\u3057\u3087\u3046\u304b\u3002HTTP \u30ea\u30af\u30a8\u30b9\u30c8\u306b\u4ed8\u3055\u308c\u305f Context \u306f\u3001\u3055\u307e\u3056\u307e\u306a\u72b6\u6cc1\u3067\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u77e5\u3063\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

    \u6700\u521d\u306e 2 \u3064\u306e\u30b1\u30fc\u30b9\u3067\u306f\u3001\u51e6\u7406\u306f\u304a\u305d\u3089\u304f\u6b63\u3057\u304f\u884c\u308f\u308c\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001doSomeTask \u304b\u3089\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d7\u3051\u53d6\u3063\u305f\u3082\u306e\u306e\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u63a5\u7d9a\u3092\u9589\u3058\u305f\u5834\u5408\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u304c publish \u3055\u308c\u306a\u3044\u3088\u3046\u306b\u3001Context \u304c\u65e2\u306b\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u305f\u72b6\u614b\u3067 publish \u3092\u547c\u3073\u51fa\u3057\u3066\u3082\u554f\u984c\u306f\u304a\u305d\u3089\u304f\u8d77\u304d\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u6700\u5f8c\u306e\u30b1\u30fc\u30b9\u306f\u3069\u3046\u3067\u3057\u3087\u3046\u304b\u3002

    \u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u66f8\u304d\u8fbc\u307e\u308c\u308b\u3068\u3001\u8981\u6c42\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f Context \u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u7af6\u5408\u72b6\u614b\u306b\u76f4\u9762\u3057\u307e\u3059\u3002

    \u5f8c\u8005\u306e\u5834\u5408\u3001HTTP \u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u3059\u3050\u306b\u8fd4\u3059\u306e\u3067\u3001publish \u3092\u547c\u3073\u51fa\u3059\u3068\u30a8\u30e9\u30fc\u304c\u8fd4\u3055\u308c\u307e\u3059\u3002

    \u88dc\u8db3

    Go 1.21 \u304b\u3089\u306f\u3001\u30ad\u30e3\u30f3\u30bb\u30eb\u305b\u305a\u306b\u65b0\u3057\u3044 Context \u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3057\u305f\u3002 context.WithoutCancel \u306f\u3001\u89aa\u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u305f\u3068\u304d\u306b\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u3066\u3044\u306a\u3044\u89aa\u306e\u30b3\u30d4\u30fc\u3092\u8fd4\u3057\u307e\u3059\u3002

    \u307e\u3068\u3081\u308b\u3068\u3001Context \u306e\u4f1d\u64ad\u306f\u614e\u91cd\u306b\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#62","title":"\u505c\u6b62\u3059\u3079\u304d\u3068\u304d\u3092\u77e5\u3089\u305a\u306b\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u958b\u59cb\u3057\u3066\u3057\u307e\u3046 (#62)","text":"\u8981\u7d04

    \u30ea\u30fc\u30af\u3092\u907f\u3051\u308b\u3053\u3068\u306f\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u958b\u59cb\u3055\u308c\u308b\u305f\u3073\u306b\u3001\u6700\u7d42\u7684\u306b\u505c\u6b62\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

    \u30b4\u30eb\u30fc\u30c1\u30f3\u306f\u7c21\u5358\u306b\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u975e\u5e38\u306b\u7c21\u5358\u3067\u3042\u308b\u305f\u3081\u3001\u65b0\u3057\u3044\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u3044\u3064\u505c\u6b62\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u306e\u8a08\u753b\u3092\u5fc5\u305a\u3057\u3082\u7acb\u3066\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u30ea\u30fc\u30af\u306b\u3064\u306a\u304c\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u3044\u3064\u505c\u6b62\u3059\u308c\u3070\u3088\u3044\u304b\u308f\u304b\u3089\u306a\u3044\u306e\u306f\u3001Go\u8a00\u8a9e\u3067\u3088\u304f\u3042\u308b\u8a2d\u8a08\u4e0a\u306e\u554f\u984c\u3067\u3042\u308a\u3001\u4e26\u884c\u51e6\u7406\u306b\u304a\u3051\u308b\u30df\u30b9\u3067\u3059\u3002

    \u5177\u4f53\u7684\u306a\u4f8b\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u307e\u3057\u3087\u3046\u3002\u5916\u90e8\u8a2d\u5b9a\uff08\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u306a\u3069\u3092\u4f7f\u7528\u3057\u305f\u3082\u306e\u306a\u3069\uff09\u3092\u76e3\u8996\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u8a2d\u8a08\u3057\u307e\u3059\u3002\u307e\u305a\u3001\u6b21\u306e\u3088\u3046\u306a\u5b9f\u88c5\u3092\u3057\u3066\u307f\u307e\u3059\u3002

    func main() {\n    newWatcher()\n    // \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\n}\n\ntype watcher struct { /* \u3044\u304f\u3064\u304b\u306e\u30ea\u30bd\u30fc\u30b9 */ }\n\nfunc newWatcher() {\n    w := watcher{}\n    go w.watch() // \u5916\u90e8\u8a2d\u5b9a\u3092\u76e3\u8996\u3059\u308b\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u4f5c\u6210\u3059\u308b\n}\n

    \u3053\u306e\u30b3\u30fc\u30c9\u306e\u554f\u984c\u306f\u3001\u30e1\u30a4\u30f3\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u7d42\u4e86\u3059\u308b\u3068\uff08\u304a\u305d\u3089\u304f OS \u30b7\u30b0\u30ca\u30eb\u307e\u305f\u306f\u6709\u9650\u306e\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u306e\u305f\u3081\uff09\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u505c\u6b62\u3057\u3066\u3057\u307e\u3046\u3053\u3068\u3067\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u30a6\u30a9\u30c3\u30c1\u30e3\u30fc\u306b\u3088\u3063\u3066\u4f5c\u6210\u3055\u308c\u305f\u30ea\u30bd\u30fc\u30b9\u306f\u6b63\u5e38\u306b\u9589\u3058\u3089\u308c\u307e\u305b\u3093\u3002\u3053\u308c\u3092\u9632\u3050\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u3067\u3057\u3087\u3046\u304b\u3002

    1 \u3064\u306e\u65b9\u6cd5\u3068\u3057\u3066\u306f\u3001main \u304c\u623b\u3063\u305f\u3068\u304d\u306b\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u308b Context \u3092 newWatcher \u306b\u6e21\u3059\u3053\u3068\u304c\u6319\u3052\u3089\u308c\u307e\u3059\u3002

    func main() {\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n    newWatcher(ctx)\n    // \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\n}\n\nfunc newWatcher(ctx context.Context) {\n    w := watcher{}\n    go w.watch(ctx)\n}\n

    \u4f5c\u6210\u3057\u305f Context \u3092 watch \u30e1\u30bd\u30c3\u30c9\u306b\u4f1d\u64ad\u3057\u307e\u3059\u3002Context \u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u308b\u3068\u3001\u30a6\u30a9\u30c3\u30c1\u30e3\u30fc\u69cb\u9020\u4f53\u306f\u305d\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u9589\u3058\u307e\u3059\u3002\u3057\u304b\u3057\u3001watch \u304c\u305d\u308c\u3092\u884c\u3046\u6642\u9593\u304c\u78ba\u5b9f\u306b\u3042\u308b\u3068\u306f\u3044\u3048\u307e\u305b\u3093\u3002\u3053\u308c\u306f\u8a2d\u8a08\u4e0a\u306e\u6b20\u9665\u3067\u3059\u3002

    \u554f\u984c\u306f\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u505c\u6b62\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u3092\u4f1d\u3048\u308b\u305f\u3081\u306b\u30b7\u30b0\u30ca\u30eb\u3092\u4f7f\u7528\u3057\u305f\u3053\u3068\u3067\u3059\u3002\u30ea\u30bd\u30fc\u30b9\u304c\u9589\u3058\u3089\u308c\u308b\u307e\u3067\u3001\u89aa\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u30d6\u30ed\u30c3\u30af\u3057\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u305d\u3046\u306a\u3089\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002

    func main() {\n    w := newWatcher()\n    defer w.close()\n    // \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\n}\n\nfunc newWatcher() watcher {\n    w := watcher{}\n    go w.watch()\n    return w\n}\n\nfunc (w watcher) close() {\n    // \u30ea\u30bd\u30fc\u30b9\u3092\u9589\u3058\u308b\n}\n

    \u30ea\u30bd\u30fc\u30b9\u3092\u9589\u3058\u308b\u6642\u9593\u306b\u306a\u3063\u305f\u3053\u3068\u3092 watcher \u306b\u901a\u77e5\u3059\u308b\u4ee3\u308f\u308a\u306b\u3001 defer \u3092\u4f7f\u7528\u3057\u3066\u3053\u306e\u3000close \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u7d42\u4e86\u3059\u308b\u524d\u306b\u30ea\u30bd\u30fc\u30b9\u304c\u78ba\u5b9f\u306b\u9589\u3058\u3089\u308c\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002

    \u307e\u3068\u3081\u308b\u3068\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u306f\u4ed6\u306e\u30ea\u30bd\u30fc\u30b9\u3068\u540c\u69d8\u3001\u30e1\u30e2\u30ea\u3084\u4ed6\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u89e3\u653e\u3059\u308b\u305f\u3081\u306b\u6700\u7d42\u7684\u306b\u9589\u3058\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u3044\u3064\u505c\u6b62\u3059\u308b\u304b\u3092\u77e5\u3089\u305a\u306b\u958b\u59cb\u3059\u308b\u306e\u306f\u8a2d\u8a08\u4e0a\u306e\u554f\u984c\u3067\u3059\u3002\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u958b\u59cb\u3055\u308c\u308b\u3068\u304d\u306f\u5e38\u306b\u3001\u3044\u3064\u505c\u6b62\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u660e\u78ba\u306a\u8a08\u753b\u3092\u7acb\u3066\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u6700\u5f8c\u306b\u306a\u308a\u307e\u3057\u305f\u304c\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u30ea\u30bd\u30fc\u30b9\u3092\u4f5c\u6210\u3057\u3001\u305d\u306e\u6709\u52b9\u671f\u9593\u304c\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u5b58\u7d9a\u671f\u9593\u306b\u30d0\u30a4\u30f3\u30c9\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u7d42\u4e86\u3059\u308b\u524d\u306b\u305d\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u304c\u5b8c\u4e86\u3059\u308b\u306e\u3092\u5f85\u3063\u305f\u65b9\u304c\u304a\u305d\u3089\u304f\u78ba\u5b9f\u3067\u3059\u3002\u305d\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u30ea\u30bd\u30fc\u30b9\u3092\u9593\u9055\u3044\u306a\u304f\u89e3\u653e\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#63","title":"\u30b4\u30eb\u30fc\u30c1\u30f3\u3068\u30eb\u30fc\u30d7\u5909\u6570\u306b\u6ce8\u610f\u3057\u306a\u3044 (#63)","text":"\u6ce8\u610f

    \u3053\u306e\u30df\u30b9\u306f Go\u30001.22 \u304b\u3089\u306f\u6c17\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\uff08\u8a73\u7d30\uff09\u3002

    "},{"location":"ja/#select-64","title":"select \u3068\u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3057\u3066\u6c7a\u5b9a\u7684\u52d5\u4f5c\u3092\u671f\u5f85\u3059\u308b (#64)","text":"\u8981\u7d04

    \u8907\u6570\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u53ef\u80fd\u306a\u5834\u5408\u3001\u8907\u6570\u306e\u30c1\u30e3\u30cd\u30eb\u3067 select \u3059\u308b\u3068\u30b1\u30fc\u30b9\u304c\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u629e\u3055\u308c\u308b\u3053\u3068\u3092\u7406\u89e3\u3059\u308b\u3068\u3001\u4e26\u884c\u51e6\u7406\u306b\u304a\u3051\u308b\u8efd\u5fae\u306a\u30d0\u30b0\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u306e\u3042\u308b\u8aa4\u3063\u305f\u4eee\u5b9a\u3092\u7acb\u3066\u308b\u3053\u3068\u304c\u306a\u304f\u306a\u308a\u307e\u3059\u3002

    Go \u958b\u767a\u8005\u304c\u30c1\u30e3\u30cd\u30eb\u3092\u64cd\u4f5c\u3059\u308b\u3068\u304d\u306b\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u306e 1 \u3064\u306f\u3001select \u304c\u8907\u6570\u306e\u30c1\u30e3\u30cd\u30eb\u3067\u3069\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u8aa4\u3063\u305f\u7406\u89e3\u3092\u3059\u308b\u3053\u3068\u3067\u3059\u3002

    \u305f\u3068\u3048\u3070\u3001\u6b21\u306e\u5834\u5408\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\uff08 disconnectCh \u306f\u30d0\u30c3\u30d5\u30a1\u306a\u3057\u30c1\u30e3\u30cd\u30eb\u3067\u3059\uff09\u3002

    go func() {\n  for i := 0; i < 10; i++ {\n      messageCh <- i\n    }\n    disconnectCh <- struct{}{}\n}()\n\nfor {\n    select {\n    case v := <-messageCh:\n        fmt.Println(v)\n    case <-disconnectCh:\n        fmt.Println(\"disconnection, return\")\n        return\n    }\n}\n

    \u3053\u306e\u4f8b\u3092\u8907\u6570\u56de\u5b9f\u884c\u3057\u305f\u5834\u5408\u3001\u7d50\u679c\u306f\u30e9\u30f3\u30c0\u30e0\u306b\u306a\u308a\u307e\u3059\u3002

    0\n1\n2\ndisconnection, return\n\n0\ndisconnection, return\n

    \u3069\u3046\u3044\u3046\u308f\u3051\u304b 10 \u901a\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u6d88\u8cbb\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u305d\u306e\u3046\u3061\u306e\u6570\u901a\u3060\u3051\u3092\u53d7\u4fe1\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u306f\u3001\u8907\u6570\u306e\u30c1\u30e3\u30cd\u30eb\u3068\u4f75\u7528\u3057\u305f\u5834\u5408\u306e select \u6587\u306e\u4ed5\u69d8\u306b\u3088\u308b\u3082\u306e\u3067\u3059\uff08https:// go.dev/ref/spec\uff09\u3002

    Quote

    1 \u3064\u4ee5\u4e0a\u306e\u901a\u4fe1\u3092\u7d9a\u884c\u3067\u304d\u308b\u5834\u5408\u3001\u5747\u4e00\u306e\u64ec\u4f3c\u30e9\u30f3\u30c0\u30e0\u9078\u629e\u306b\u3088\u3063\u3066\u3001\u7d9a\u884c\u3067\u304d\u308b 1 \u3064\u306e\u901a\u4fe1\u304c\u9078\u629e\u3055\u308c\u307e\u3059\u3002

    \u6700\u521d\u306b\u4e00\u81f4\u3057\u305f\u30b1\u30fc\u30b9\u304c\u512a\u5148\u3055\u308c\u308b switch \u6587\u3068\u306f\u7570\u306a\u308a\u3001select \u6587\u306f\u8907\u6570\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u53ef\u80fd\u306a\u5834\u5408\u306b\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u629e\u3057\u307e\u3059\u3002

    \u3053\u306e\u52d5\u4f5c\u306f\u6700\u521d\u306f\u5947\u5999\u306b\u601d\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u3053\u308c\u306f\u30b9\u30bf\u30d9\u30fc\u30b7\u30e7\u30f3\u3092\u9632\u3050\u3068\u3044\u3046\u7406\u7531\u304c\u3042\u3063\u3066\u306e\u3053\u3068\u3067\u3059\u3002\u6700\u521d\u306b\u9078\u629e\u3055\u308c\u305f\u901a\u4fe1\u304c\u30bd\u30fc\u30b9\u306e\u9806\u5e8f\u306b\u57fa\u3065\u3044\u3066\u3044\u308b\u3068\u3057\u307e\u3059\u3002\u305d\u306e\u5834\u5408\u3001\u9001\u4fe1\u901f\u5ea6\u304c\u901f\u3044\u305f\u3081\u306b\u3001\u305f\u3068\u3048\u3070 1 \u3064\u306e\u30c1\u30e3\u30cd\u30eb\u304b\u3089\u3057\u304b\u53d7\u4fe1\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u72b6\u6cc1\u306b\u9665\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u9632\u3050\u305f\u3081\u306b\u3001Go\u8a00\u8a9e\u306e\u8a2d\u8a08\u8005\u306f\u30e9\u30f3\u30c0\u30e0\u9078\u629e\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002

    \u8907\u6570\u306e\u30c1\u30e3\u30cd\u30eb\u3067 select \u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u8907\u6570\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u3042\u308b\u306a\u3089\u3001\u30bd\u30fc\u30b9\u9806\u5e8f\u306e\u6700\u521d\u306e\u30b1\u30fc\u30b9\u304c\u81ea\u52d5\u7684\u306b\u512a\u5148\u3055\u308c\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee3\u308f\u308a\u306b\u3001Go\u8a00\u8a9e\u306f\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u629e\u3059\u308b\u305f\u3081\u3001\u3069\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u9078\u629e\u3055\u308c\u308b\u304b\u306f\u4fdd\u8a3c\u3055\u308c\u307e\u305b\u3093\u3002\u3053\u306e\u52d5\u4f5c\u3092\u514b\u670d\u3059\u308b\u306b\u306f\u3001\u5358\u4e00\u306e\u751f\u7523\u8005\u30b4\u30eb\u30fc\u30c1\u30f3\u306e\u5834\u5408\u3001\u30d0\u30c3\u30d5\u30a1\u306a\u3057\u306e\u30c1\u30e3\u30cd\u30eb\u307e\u305f\u306f\u5358\u4e00\u306e\u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#65","title":"\u901a\u77e5\u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#65)","text":"\u8981\u7d04

    chan struct{} \u578b\u3092\u4f7f\u7528\u3057\u3066\u901a\u77e5\u3092\u9001\u4fe1\u3057\u307e\u3057\u3087\u3046\u3002

    \u30c1\u30e3\u30cd\u30eb\u306f\u3001\u30b7\u30b0\u30ca\u30eb\u3092\u4ecb\u3057\u3066\u30b4\u30eb\u30fc\u30c1\u30f3\u9593\u3067\u901a\u4fe1\u3059\u308b\u305f\u3081\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u3067\u3059\u3002\u30b7\u30b0\u30ca\u30eb\u306b\u306f\u30c7\u30fc\u30bf\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u306f\u95a2\u4fc2\u3042\u308a\u307e\u305b\u3093\u3002

    \u5177\u4f53\u7684\u306a\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u901a\u4fe1\u306e\u5207\u65ad\u304c\u767a\u751f\u3059\u308b\u305f\u3073\u306b\u305d\u308c\u3092\u901a\u77e5\u3059\u308b\u30c1\u30e3\u30cd\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 1 \u3064\u306e\u65b9\u6cd5\u3068\u3057\u3066\u3001\u3053\u308c\u3092 chan bool \u3068\u3057\u3066\u6271\u3046\u3053\u3068\u304c\u6319\u3052\u3089\u308c\u307e\u3059\u3002

    disconnectCh := make(chan bool)\n

    \u3053\u3053\u3067\u3001\u305d\u306e\u3088\u3046\u306a\u30c1\u30e3\u30cd\u30eb\u3092\u63d0\u4f9b\u3059\u308b API \u3068\u5bfe\u8a71\u3059\u308b\u3068\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u771f\u507d\u5024\u306e\u30c1\u30e3\u30cd\u30eb\u3067\u3042\u308b\u305f\u3081\u3001true \u307e\u305f\u306f false \u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u53d7\u4fe1\u3067\u304d\u307e\u3059\u3002true \u304c\u4f55\u3092\u4f1d\u3048\u3066\u3044\u308b\u304b\u306f\u304a\u305d\u3089\u304f\u660e\u3089\u304b\u3067\u3057\u3087\u3046\u3002\u3057\u304b\u3057\u3001 false \u3068\u306f\u4f55\u3092\u610f\u5473\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002\u901a\u4fe1\u304c\u5207\u65ad\u3055\u308c\u3066\u3044\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3057\u3087\u3046\u304b\u3002\u305d\u306e\u5834\u5408\u3001\u3069\u308c\u304f\u3089\u3044\u306e\u983b\u5ea6\u3067\u305d\u306e\u3088\u3046\u306a\u30b7\u30b0\u30ca\u30eb\u3092\u53d7\u4fe1\u3059\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002\u3042\u308b\u3044\u306f\u518d\u63a5\u7d9a\u3057\u305f\u3068\u3044\u3046\u3053\u3068\u3067\u3057\u3087\u3046\u304b\u3002\u305d\u3082\u305d\u3082 false \u3092\u53d7\u3051\u53d6\u308b\u3053\u3068\u3092\u671f\u5f85\u3059\u3079\u304d\u306a\u306e\u3067\u3057\u3087\u3046\u304b\u3002\u304a\u305d\u3089\u304f true \u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u53d7\u3051\u53d6\u308b\u3053\u3068\u3060\u3051\u3092\u671f\u5f85\u3059\u3079\u304d\u3067\u3057\u3087\u3046\u3002

    \u305d\u306e\u5834\u5408\u3001\u60c5\u5831\u3092\u4f1d\u3048\u308b\u305f\u3081\u306b\u7279\u5b9a\u306e\u5024\u306f\u5fc5\u8981\u306a\u3044\u3053\u3068\u3092\u610f\u5473\u3057\u3001\u30c7\u30fc\u30bf\u306e \u306a\u3044 \u30c1\u30e3\u30cd\u30eb\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u51e6\u7406\u3059\u308b\u6163\u7528\u7684\u306a\u65b9\u6cd5\u306f\u3001\u7a7a\u306e\u69cb\u9020\u4f53\u306e\u30c1\u30e3\u30cd\u30eb\u2015\u2015 chan struct{}\u2015\u2015\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3059\u3002

    "},{"location":"ja/#nil-66","title":"nil \u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#66)","text":"\u8981\u7d04

    nil \u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u305f\u3068\u3048\u3070 select \u6587\u304b\u3089\u30b1\u30fc\u30b9\u3092 \u524a\u9664 \u3067\u304d\u308b\u305f\u3081\u3001\u4e26\u884c\u51e6\u7406\u3092\u884c\u3046\u969b\u306e\u9053\u5177\u306e\u4e00\u3064\u3068\u3057\u3066\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u3079\u304d\u3067\u3059\u3002

    \u6b21\u306e\u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066\u4f55\u304c\u884c\u308f\u308c\u308b\u3067\u3057\u3087\u3046\u304b\u3002

    var ch chan int\n<-ch\n

    ch \u306f chan int \u578b\u3067\u3059\u3002\u30c1\u30e3\u30cd\u30eb\u306e\u30bc\u30ed\u5024\u306f nil \u3067\u3042\u308b\u306e\u3067\u3001 ch \u306f nil \u3067\u3059\u3002\u30b4\u30eb\u30fc\u30c1\u30f3\u306f panic \u3092\u8d77\u3053\u3057\u307e\u305b\u3093\u3002\u305f\u3060\u3057\u3001\u6c38\u4e45\u306b\u30d6\u30ed\u30c3\u30af\u3057\u307e\u3059\u3002

    nil \u30c1\u30e3\u30cd\u30eb\u306b\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u9001\u4fe1\u3059\u308b\u5834\u5408\u3082\u539f\u7406\u306f\u540c\u3058\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u306f\u6c38\u4e45\u306b\u30d6\u30ed\u30c3\u30af\u3057\u307e\u3059\u3002

    var ch chan int\nch <- 0\n

    \u3067\u306f\u3001Go\u8a00\u8a9e\u304c nil \u30c1\u30e3\u30cd\u30eb\u3068\u306e\u9593\u3067\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u9001\u53d7\u4fe1\u3092\u8a31\u53ef\u3059\u308b\u76ee\u7684\u306f\u4f55\u3067\u3057\u3087\u3046\u304b\u3002\u305f\u3068\u3048\u3070\u30012 \u3064\u306e\u30c1\u30e3\u30cd\u30eb\u3092\u30de\u30fc\u30b8\u3059\u308b\u6163\u7528\u7684\u306a\u65b9\u6cd5\u3092\u5b9f\u88c5\u3059\u308b\u306e\u306b\u3001 nil \u30c1\u30e3\u30cd\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

    func merge(ch1, ch2 <-chan int) <-chan int {\n    ch := make(chan int, 1)\n\n    go func() {\n        for ch1 != nil || ch2 != nil { // \u6700\u4f4e\u3067\u3082\u4e00\u3064\u306e\u30c1\u30e3\u30cd\u30eb\u304c nil \u3067\u306a\u3051\u308c\u3070\u7d9a\u884c\u3059\u308b\n            select {\n            case v, open := <-ch1:\n                if !open {\n                    ch1 = nil // \u9589\u3058\u305f\u3089 ch1 \u3092 nil \u30c1\u30e3\u30cd\u30eb\u306b\u5272\u308a\u5f53\u3066\u308b\n                    break\n                }\n                ch <- v\n            case v, open := <-ch2:\n                if !open {\n                    ch2 = nil // \u9589\u3058\u305f\u3089 ch2 \u3092 nil \u30c1\u30e3\u30cd\u30eb\u306b\u5272\u308a\u5f53\u3066\u308b\n                    break\n                }\n                ch <- v\n            }\n        }\n        close(ch)\n    }()\n\n    return ch\n}\n

    \u3053\u306e\u6d17\u7df4\u3055\u308c\u305f\u89e3\u6c7a\u7b56\u306f\u3001nil \u30c1\u30e3\u30cd\u30eb\u3092\u5229\u7528\u3057\u3066\u3001\u4f55\u3089\u304b\u306e\u65b9\u6cd5\u3067 select \u6587\u304b\u3089 1 \u3064\u306e\u30b1\u30fc\u30b9\u3092 \u524a\u9664 \u3057\u307e\u3059\u3002

    nil \u30c1\u30e3\u30cd\u30eb\u306f\u72b6\u6cc1\u306b\u3088\u3063\u3066\u306f\u4fbf\u5229\u3067\u3042\u308a\u3001Go \u958b\u767a\u8005\u306f\u4e26\u884c\u51e6\u7406\u3092\u6271\u3046\u969b\u306b\u4f7f\u3044\u3053\u306a\u305b\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u304a\u304f\u3079\u304d\u3067\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#67","title":"\u30c1\u30e3\u30cd\u30eb\u306e\u5bb9\u91cf\u306b\u3064\u3044\u3066\u56f0\u60d1\u3057\u3066\u3044\u308b (#67)","text":"\u8981\u7d04

    \u554f\u984c\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306f\u3001\u4f7f\u7528\u3059\u308b\u30c1\u30e3\u30cd\u30eb\u306e\u578b\u3092\u614e\u91cd\u306b\u6c7a\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u540c\u671f\u3092\u5f37\u529b\u306b\u4fdd\u8a3c\u3057\u3066\u304f\u308c\u308b\u306e\u306f\u30d0\u30c3\u30d5\u30a1\u306a\u3057\u30c1\u30e3\u30cd\u30eb\u306e\u307f\u3067\u3059\u3002

    \u30d0\u30c3\u30d5\u30a1\u3042\u308a\u30c1\u30e3\u30cd\u30eb\u4ee5\u5916\u306e\u30c1\u30e3\u30cd\u30eb\u306e\u5bb9\u91cf\u3092\u6307\u5b9a\u3059\u308b\u306b\u306f\u6b63\u5f53\u306a\u7406\u7531\u304c\u3042\u308b\u3079\u304d\u3067\u3059\u3002

    "},{"location":"ja/#etcd-68","title":"\u6587\u5b57\u5217\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3067\u8d77\u3053\u308a\u5f97\u308b\u526f\u4f5c\u7528\u3092\u5fd8\u308c\u3066\u3057\u307e\u3046\uff08 etcd \u30c7\u30fc\u30bf\u7af6\u5408\u306e\u4f8b\u3068\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\uff09 (#68)","text":"\u8981\u7d04

    \u6587\u5b57\u5217\u306e\u66f8\u5f0f\u8a2d\u5b9a\u304c\u65e2\u5b58\u306e\u95a2\u6570\u304c\u547c\u3073\u51fa\u3059\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u8a8d\u8b58\u3059\u308b\u3053\u3068\u306f\u3001\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u3084\u305d\u306e\u4ed6\u306e\u30c7\u30fc\u30bf\u7af6\u5408\u306e\u53ef\u80fd\u6027\u306b\u6ce8\u610f\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#append-69","title":"append \u3067\u30c7\u30fc\u30bf\u7af6\u5408\u3092\u8d77\u3053\u3057\u3066\u3057\u307e\u3046 (#69)","text":"\u8981\u7d04

    append \u306e\u547c\u3073\u51fa\u3057\u306f\u5fc5\u305a\u3057\u3082\u30c7\u30fc\u30bf\u7af6\u5408\u304c\u306a\u3044\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3086\u3048\u306b\u3001\u5171\u6709\u30b9\u30e9\u30a4\u30b9\u4e0a\u3067\u540c\u6642\u306b\u4f7f\u7528\u3057\u3066\u306f\u3044\u3051\u307e\u305b\u3093\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#70","title":"\u30b9\u30e9\u30a4\u30b9\u3068\u30de\u30c3\u30d7\u3067\u30df\u30e5\u30fc\u30c6\u30c3\u30af\u30b9\u3092\u6b63\u3057\u304f\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#70)","text":"\u8981\u7d04

    \u30b9\u30e9\u30a4\u30b9\u3068\u30de\u30c3\u30d7\u306f\u30dd\u30a4\u30f3\u30bf\u3067\u3042\u308b\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304f\u3068\u3001\u5178\u578b\u7684\u306a\u30c7\u30fc\u30bf\u7af6\u5408\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#syncwaitgroup-71","title":"sync.WaitGroup \u3092\u6b63\u3057\u304f\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#71)","text":"\u8981\u7d04

    sync.WaitGroup \u3092\u6b63\u3057\u304f\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u3092\u8d77\u52d5\u3059\u308b\u524d\u306b Add \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#synccond-72","title":"sync.Cond \u306b\u3064\u3044\u3066\u5fd8\u308c\u3066\u3057\u307e\u3046 (#72)","text":"\u8981\u7d04

    sync.Cond \u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u8907\u6570\u306e\u30b4\u30eb\u30fc\u30c1\u30f3\u306b\u7e70\u308a\u8fd4\u3057\u901a\u77e5\u3092\u9001\u4fe1\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#errgroup-73","title":"errgroup \u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#73)","text":"\u8981\u7d04

    errgroup \u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u7528\u3057\u3066\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u306e\u30b0\u30eb\u30fc\u30d7\u3092\u540c\u671f\u3057\u3001\u30a8\u30e9\u30fc\u3068 Context \u3092\u51e6\u7406\u3067\u304d\u307e\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/#_15","title":"\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea","text":""},{"location":"ja/#75","title":"\u9593\u9055\u3063\u305f\u6642\u9593\u3092\u6307\u5b9a\u3059\u308b (#75)","text":"\u8981\u7d04

    time.Duration \u3092\u53d7\u3051\u5165\u308c\u308b\u95a2\u6570\u306b\u306f\u6ce8\u610f\u3092\u6255\u3063\u3066\u304f\u3060\u3055\u3044\u3002\u6574\u6570\u3092\u6e21\u3059\u3053\u3068\u306f\u8a31\u53ef\u3055\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u6df7\u4e71\u3092\u62db\u304b\u306a\u3044\u3088\u3046\u306b time API \u3092\u4f7f\u7528\u3059\u308b\u3088\u3046\u52aa\u3081\u3066\u304f\u3060\u3055\u3044\u3002

    \u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u591a\u304f\u306e\u95a2\u6570\u306f\u3001int64 \u578b\u306e\u30a8\u30a4\u30ea\u30a2\u30b9\u3067\u3042\u308b time.Duration \u3092\u53d7\u3051\u5165\u308c\u307e\u3059\u3002\u305f\u3060\u3057\u30011 \u5358\u4f4d\u306e time.Duration \u306f\u3001\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u4e00\u822c\u7684\u306b\u898b\u3089\u308c\u308b 1 \u30df\u30ea\u79d2\u3067\u306f\u306a\u304f\u30011 \u30ca\u30ce\u79d2\u3092\u8868\u3057\u307e\u3059\u3002\u305d\u306e\u7d50\u679c\u3001time.Duration API \u3092\u4f7f\u7528\u3059\u308b\u4ee3\u308f\u308a\u306b\u6570\u5024\u578b\u3092\u6e21\u3059\u3068\u3001\u4e88\u60f3\u5916\u306e\u52d5\u4f5c\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002

    \u4ed6\u8a00\u8a9e\u3092\u4f7f\u7528\u3057\u305f\u3053\u3068\u306e\u3042\u308b\u958b\u767a\u8005\u306e\u65b9\u306f\u3001\u6b21\u306e\u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066 1 \u79d2\u5468\u671f\u306e\u65b0\u3057\u3044 time.Ticker \u304c\u751f\u6210\u3055\u308c\u308b\u3068\u8003\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002

    ticker := time.NewTicker(1000)\nfor {\n    select {\n    case <-ticker.C:\n        // \u51e6\u7406\u3092\u3059\u308b\n    }\n}\n

    \u3057\u304b\u3057\u306a\u304c\u3089\u30011,000 time.Duration = 1,000 \u30ca\u30ce\u79d2\u3067\u3042\u308b\u305f\u3081\u3001\u60f3\u5b9a\u3055\u308c\u3066\u3044\u308b 1\u79d2 \u3067\u306f\u306a\u304f\u30011,000 \u30ca\u30ce\u79d2 = 1 \u30de\u30a4\u30af\u30ed\u79d2\u306e\u5468\u671f\u306b\u306a\u308a\u307e\u3059\u3002

    \u6df7\u4e71\u3084\u4e88\u60f3\u5916\u306e\u52d5\u4f5c\u3092\u62db\u304b\u306a\u3044\u3088\u3046\u3001\u3044\u3064\u3082 time.Duration API \u3092\u4f7f\u7528\u3059\u308b\u3079\u304d\u3067\u3059\u3002

    ticker = time.NewTicker(time.Microsecond)\n// \u3082\u3057\u304f\u306f\nticker = time.NewTicker(1000 * time.Nanosecond)\n

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#timeafter-76","title":"time.After \u3068\u30e1\u30e2\u30ea\u30ea\u30fc\u30af (#76)","text":"\u8981\u7d04

    \u7e70\u308a\u8fd4\u3055\u308c\u308b\u95a2\u6570\uff08\u30eb\u30fc\u30d7\u3084 HTTP \u30cf\u30f3\u30c9\u30e9\u306a\u3069\uff09\u3067 time.After \u306e\u547c\u3073\u51fa\u3057\u3092\u56de\u907f\u3059\u308b\u3068\u3001\u30d4\u30fc\u30af\u6642\u306e\u30e1\u30e2\u30ea\u6d88\u8cbb\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002time.After \u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u305f\u30ea\u30bd\u30fc\u30b9\u306f\u3001 timer \u304c\u7d42\u4e86\u3057\u305f\u3068\u304d\u306b\u306e\u307f\u89e3\u653e\u3055\u308c\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#json-77","title":"JSON \u51e6\u7406\u3067\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044 (#77)","text":"

    Go \u69cb\u9020\u4f53\u3067\u57cb\u3081\u8fbc\u307f\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002 \u306a\u305c\u306a\u3089 json.Marshaler \u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u5b9f\u88c5\u3059\u308b time.Time \u57cb\u3081\u8fbc\u307f\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u3088\u3046\u306a\u3084\u3063\u304b\u3044\u306a\u30d0\u30b0\u304c\u767a\u751f\u3057\u3066\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30de\u30fc\u30b7\u30e3\u30ea\u30f3\u30b0\u52d5\u4f5c\u304c\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    2 \u3064\u306e time.Time \u69cb\u9020\u4f53\u3092\u6bd4\u8f03\u3059\u308b\u5834\u5408\u3001time.Time \u306b\u306f wall clock \u3068 monotonic clock \u306e\u4e21\u65b9\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u3001== \u6f14\u7b97\u5b50\u3092\u4f7f\u7528\u3057\u305f\u6bd4\u8f03\u306f\u4e21\u65b9\u306e clock \u306b\u5bfe\u3057\u3066\u884c\u308f\u308c\u308b\u3053\u3068\u3092\u601d\u3044\u51fa\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    JSON \u30c7\u30fc\u30bf\u306e\u30a2\u30f3\u30de\u30fc\u30b7\u30e3\u30ea\u30f3\u30b0\u4e2d\u306b\u30de\u30c3\u30d7\u3092\u63d0\u4f9b\u3059\u308b\u3068\u304d\u306b\u9593\u9055\u3044\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u6570\u5024\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067 float64 \u306b\u5909\u63db\u3055\u308c\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#sql-78","title":"SQL \u3067\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044 (#78)","text":"

    \u8a2d\u5b9a\u3092\u8a66\u3057\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u3001 Ping \u307e\u305f\u306f PingContext \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u5b9f\u904b\u7528\u6c34\u6e96\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u8a2d\u5b9a\u3057\u307e\u3057\u3087\u3046\u3002

    SQL \u306e\u30d7\u30ea\u30da\u30a2\u30c9\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30af\u30a8\u30ea\u304c\u3088\u308a\u52b9\u7387\u7684\u304b\u3064\u78ba\u5b9f\u306b\u306a\u308a\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u30c6\u30fc\u30d6\u30eb\u5185\u306e null \u304c\u8a31\u5bb9\u3055\u308c\u3066\u3044\u308b\u5217\u306f\u3001\u30dd\u30a4\u30f3\u30bf\u307e\u305f\u306f sql.NullXXX \u578b\u3092\u4f7f\u7528\u3057\u3066\u51e6\u7406\u3057\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u884c\u306e\u53cd\u5fa9\u51e6\u7406\u306e\u5f8c\u306b sql.Rows \u306e Err \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u3001\u6b21\u306e\u884c\u306e\u6e96\u5099\u4e2d\u306b\u30a8\u30e9\u30fc\u3092\u898b\u9003\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#http-bodysqlrows-osfile-79","title":"\u4e00\u6642\u7684\u306a\u30ea\u30bd\u30fc\u30b9\uff08 HTTP body\u3001sql.Rows\u3001\u304a\u3088\u3073 os.File \uff09\u3092\u9589\u3058\u3066\u3044\u306a\u3044 (#79)","text":"\u8981\u7d04

    \u30ea\u30fc\u30af\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001 io.Closer \u3092\u5b9f\u88c5\u3057\u3066\u3044\u308b\u3059\u3079\u3066\u306e\u69cb\u9020\u4f53\u3092\u6700\u5f8c\u306b\u306f\u9589\u3058\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#http-return-80","title":"HTTP \u30ea\u30af\u30a8\u30b9\u30c8\u306b\u5fdc\u7b54\u3057\u305f\u5f8c\u306e return \u6587\u3092\u5fd8\u308c\u3066\u3057\u307e\u3046 (#80)","text":"\u8981\u7d04

    HTTP \u30cf\u30f3\u30c9\u30e9\u306e\u5b9f\u88c5\u3067\u306e\u4e88\u60f3\u5916\u306e\u52d5\u4f5c\u3092\u907f\u3051\u308b\u305f\u3081\u3001http.Error \u306e\u5f8c\u306b\u30cf\u30f3\u30c9\u30e9\u3092\u505c\u6b62\u3057\u305f\u3044\u5834\u5408\u306f\u3001return \u6587\u3092\u5fd8\u308c\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#http-81","title":"\u6a19\u6e96\u306e HTTP \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3068\u30b5\u30fc\u30d0\u30fc\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b (#81)","text":"\u8981\u7d04

    \u5b9f\u904b\u7528\u6c34\u6e96\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u6c42\u3081\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u6a19\u6e96\u306e HTTP \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3068\u30b5\u30fc\u30d0\u30fc\u306e\u5b9f\u88c5\u3092\u4f7f\u7528\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u3089\u306e\u5b9f\u88c5\u306b\u306f\u3001\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u3084\u7a3c\u50cd\u74b0\u5883\u3067\u5fc5\u9808\u3067\u3042\u308b\u3079\u304d\u52d5\u4f5c\u304c\u6b20\u843d\u3057\u3066\u3044\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#_16","title":"\u30c6\u30b9\u30c8","text":""},{"location":"ja/#82","title":"\u30c6\u30b9\u30c8\u3092\u5206\u985e\u3057\u3066\u3044\u306a\u3044\uff08\u30d3\u30eb\u30c9\u30bf\u30b0\u3001\u74b0\u5883\u5909\u6570\u3001\u30b7\u30e7\u30fc\u30c8\u30e2\u30fc\u30c9\uff09 (#82)","text":"\u8981\u7d04

    \u30d3\u30eb\u30c9\u30d5\u30e9\u30b0\u3001\u74b0\u5883\u5909\u6570\u3001\u307e\u305f\u306f\u30b7\u30e7\u30fc\u30c8\u30e2\u30fc\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30c6\u30b9\u30c8\u3092\u5206\u985e\u3059\u308b\u3068\u3001\u30c6\u30b9\u30c8\u30d7\u30ed\u30bb\u30b9\u304c\u3088\u308a\u52b9\u7387\u7684\u306b\u306a\u308a\u307e\u3059\u3002\u30d3\u30eb\u30c9\u30d5\u30e9\u30b0\u307e\u305f\u306f\u74b0\u5883\u5909\u6570\u3092\u4f7f\u7528\u3057\u3066\u30c6\u30b9\u30c8\u30ab\u30c6\u30b4\u30ea\uff08\u305f\u3068\u3048\u3070\u3001\u5358\u4f53\u30c6\u30b9\u30c8\u3068\u7d71\u5408\u30c6\u30b9\u30c8\uff09\u3092\u4f5c\u6210\u3057\u3001\u77ed\u671f\u9593\u306e\u30c6\u30b9\u30c8\u3068\u9577\u6642\u9593\u306e\u30c6\u30b9\u30c8\u3092\u533a\u5225\u3059\u308b\u3053\u3068\u3067\u3001\u5b9f\u884c\u3059\u308b\u30c6\u30b9\u30c8\u306e\u7a2e\u985e\u3092\u6c7a\u5b9a\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#-race-83","title":"-race \u30d5\u30e9\u30b0\u3092\u6709\u52b9\u306b\u3057\u3066\u3044\u306a\u3044 (#83)","text":"\u8981\u7d04

    \u4e26\u884c\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u5834\u5408\u306f\u3001 -race \u30d5\u30e9\u30b0\u3092\u6709\u52b9\u306b\u3059\u308b\u3053\u3068\u3092\u5f37\u304f\u304a\u52e7\u3081\u3057\u307e\u3059\u3002\u305d\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306e\u30d0\u30b0\u306b\u3064\u306a\u304c\u308b\u53ef\u80fd\u6027\u306e\u3042\u308b\u6f5c\u5728\u7684\u306a\u30c7\u30fc\u30bf\u7af6\u5408\u3092\u767a\u898b\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002

    "},{"location":"ja/#-parallel-shuffle-84","title":"\u30c6\u30b9\u30c8\u5b9f\u884c\u30e2\u30fc\u30c9\uff08 -parallel \u304a\u3088\u3073 -shuffle \uff09\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#84)","text":"\u8981\u7d04

    -parallel \u30d5\u30e9\u30b0\u3092\u4f7f\u7528\u3059\u308b\u306e\u306f\u3001\u7279\u306b\u9577\u6642\u9593\u5b9f\u884c\u3055\u308c\u308b\u30c6\u30b9\u30c8\u3092\u9ad8\u901f\u5316\u3059\u308b\u306e\u306b\u52b9\u679c\u7684\u3067\u3059\u3002 -shuffle \u30d5\u30e9\u30b0\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30c6\u30b9\u30c8\u30b9\u30a4\u30fc\u30c8\u304c\u30d0\u30b0\u3092\u96a0\u3059\u53ef\u80fd\u6027\u306e\u3042\u308b\u9593\u9055\u3063\u305f\u4eee\u5b9a\u306b\u4f9d\u5b58\u3057\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

    "},{"location":"ja/#85","title":"\u30c6\u30fc\u30d6\u30eb\u99c6\u52d5\u30c6\u30b9\u30c8\u3092\u4f7f\u7528\u3057\u306a\u3044 (#85)","text":"\u8981\u7d04

    \u30c6\u30fc\u30d6\u30eb\u99c6\u52d5\u30c6\u30b9\u30c8\u306f\u3001\u30b3\u30fc\u30c9\u306e\u91cd\u8907\u3092\u9632\u304e\u3001\u5c06\u6765\u306e\u66f4\u65b0\u306e\u51e6\u7406\u3092\u5bb9\u6613\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u4e00\u9023\u306e\u985e\u4f3c\u3057\u305f\u30c6\u30b9\u30c8\u3092\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b\u52b9\u7387\u7684\u306a\u65b9\u6cd5\u3067\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#86","title":"\u5358\u4f53\u30c6\u30b9\u30c8\u3067\u306e\u30b9\u30ea\u30fc\u30d7 (#86)","text":"\u8981\u7d04

    \u30c6\u30b9\u30c8\u306e\u4e0d\u5b89\u5b9a\u3055\u3092\u306a\u304f\u3057\u3001\u3088\u308a\u5805\u7262\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u540c\u671f\u3092\u4f7f\u7528\u3057\u3066\u30b9\u30ea\u30fc\u30d7\u3092\u56de\u907f\u3057\u307e\u3057\u3087\u3046\u3002\u540c\u671f\u304c\u4e0d\u53ef\u80fd\u306a\u5834\u5408\u306f\u3001\u30ea\u30c8\u30e9\u30a4\u624b\u6cd5\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#time-api-87","title":"time API \u3092\u52b9\u7387\u7684\u306b\u51e6\u7406\u3067\u304d\u3066\u3044\u306a\u3044 (#87)","text":"\u8981\u7d04

    time API \u3092\u4f7f\u7528\u3057\u3066\u95a2\u6570\u3092\u51e6\u7406\u3059\u308b\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u30c6\u30b9\u30c8\u306e\u4e0d\u5b89\u5b9a\u3055\u3092\u8efd\u6e1b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u96a0\u308c\u305f\u4f9d\u5b58\u95a2\u4fc2\u306e\u4e00\u90e8\u3068\u3057\u3066 time \u3092\u51e6\u7406\u3057\u305f\u308a\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b time \u3092\u63d0\u4f9b\u3059\u308b\u3088\u3046\u306b\u8981\u6c42\u3057\u305f\u308a\u3059\u308b\u306a\u3069\u3001\u6a19\u6e96\u7684\u306a\u624b\u6bb5\u3092\u5229\u7528\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#httptest-iotest-88","title":"\u30c6\u30b9\u30c8\u306b\u95a2\u3059\u308b\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u30d1\u30c3\u30b1\u30fc\u30b8\uff08 httptest \u304a\u3088\u3073 iotest \uff09\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044 (#88)","text":"

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#89","title":"\u4e0d\u6b63\u78ba\u306a\u30d9\u30f3\u30c1\u30de\u30fc\u30af\u306e\u4f5c\u6210 (#89)","text":"\u8981\u7d04

    \u30d9\u30f3\u30c1\u30de\u30fc\u30af\u306b\u3064\u3044\u3066

    \u30bb\u30af\u30b7\u30e7\u30f3\u5168\u6587\u306f\u3053\u3061\u3089\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#go-90","title":"Go\u8a00\u8a9e\u306e\u30c6\u30b9\u30c8\u6a5f\u80fd\u3092\u3059\u3079\u3066\u8a66\u3057\u3066\u3044\u306a\u3044 (#90)","text":"

    \u30b3\u30fc\u30c9\u306e\u3069\u306e\u90e8\u5206\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u304b\u3092\u3059\u3050\u306b\u78ba\u8a8d\u3059\u308b\u305f\u3081\u306b\u3001-coverprofile \u30d5\u30e9\u30b0\u3092\u6307\u5b9a\u3057\u3066\u30b3\u30fc\u30c9\u30ab\u30d0\u30ec\u30c3\u30b8\u3092\u4f7f\u7528\u3057\u307e\u3057\u3087\u3046\u3002

    \u5185\u90e8\u3067\u306f\u306a\u304f\u516c\u958b\u3055\u308c\u305f\u52d5\u4f5c\u306b\u7126\u70b9\u3092\u5f53\u3066\u305f\u30c6\u30b9\u30c8\u306e\u4f5c\u6210\u3092\u5f37\u5236\u3059\u308b\u305f\u3081\u306b\u3001\u5358\u4f53\u30c6\u30b9\u30c8\u306f\u5225\u3005\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u914d\u7f6e\u3057\u307e\u3057\u3087\u3046\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u5f93\u6765\u306e if err != nil \u306e\u4ee3\u308f\u308a\u306b *testing.T \u5909\u6570\u3092\u4f7f\u7528\u3057\u3066\u30a8\u30e9\u30fc\u3092\u51e6\u7406\u3059\u308b\u3068\u3001\u30b3\u30fc\u30c9\u304c\u77ed\u304f\u3001\u8aad\u307f\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    setup \u304a\u3088\u3073 teardown \u6a5f\u80fd\u3092\u5229\u7528\u3057\u3066\u3001\u7d71\u5408\u30c6\u30b9\u30c8\u306e\u5834\u5408\u306a\u3069\u3001\u8907\u96d1\u306a\u74b0\u5883\u3092\u69cb\u6210\u3067\u304d\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#community-mistake","title":"\u30d5\u30a1\u30b8\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044\uff08community mistake\uff09","text":"\u8981\u7d04

    \u30d5\u30a1\u30b8\u30f3\u30b0\u306f\u3001\u8907\u96d1\u306a\u95a2\u6570\u3084\u30e1\u30bd\u30c3\u30c9\u3078\u306e\u30e9\u30f3\u30c0\u30e0\u306a\u3001\u4e88\u60f3\u5916\u306e\u3001\u307e\u305f\u306f\u4e0d\u6b63\u306a\u5165\u529b\u3092\u691c\u51fa\u3057\u3001\u8106\u5f31\u6027\u3001\u30d0\u30b0\u3001\u3055\u3089\u306b\u306f\u6f5c\u5728\u7684\u306a\u30af\u30e9\u30c3\u30b7\u30e5\u3092\u767a\u898b\u3059\u308b\u306e\u306b\u52b9\u7387\u7684\u3067\u3059\u3002

    @jeromedoucet \u3055\u3093\u306e\u3054\u5354\u529b\u306b\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\u3002

    "},{"location":"ja/#_17","title":"\u6700\u9069\u5316","text":""},{"location":"ja/#cpu-91","title":"CPU \u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#91)","text":"

    L1 \u30ad\u30e3\u30c3\u30b7\u30e5\u306f\u30e1\u30a4\u30f3\u30e1\u30e2\u30ea\u3088\u308a\u3082\u7d04 50 \uff5e 100 \u500d\u9ad8\u901f\u3067\u3042\u308b\u305f\u3081\u3001CPU \u30d0\u30a6\u30f3\u30c9\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u6700\u9069\u5316\u3059\u308b\u306b\u306f\u3001CPU \u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u4f7f\u7528\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002

    \u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\u306e\u6982\u5ff5\u3092\u610f\u8b58\u3059\u308b\u3053\u3068\u306f\u3001\u30c7\u30fc\u30bf\u96c6\u7d04\u578b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u30c7\u30fc\u30bf\u3092\u6574\u7406\u3059\u308b\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u306e\u306b\u91cd\u8981\u3067\u3059\u3002CPU \u306f\u30e1\u30e2\u30ea\u3092\u30ef\u30fc\u30c9\u3054\u3068\u306b\u30d5\u30a7\u30c3\u30c1\u3057\u307e\u305b\u3093\u3002\u4ee3\u308f\u308a\u306b\u3001\u901a\u5e38\u306f\u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u3092 64 \u30d0\u30a4\u30c8\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\u306b\u30b3\u30d4\u30fc\u3057\u307e\u3059\u3002\u500b\u3005\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u30e9\u30a4\u30f3\u3092\u6700\u5927\u9650\u306b\u6d3b\u7528\u3059\u308b\u306b\u306f\u3001\u7a7a\u9593\u7684\u5c40\u6240\u6027\u3092\u5f37\u5236\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    CPU \u306b\u3068\u3063\u3066\u4e88\u6e2c\u53ef\u80fd\u306a\u30b3\u30fc\u30c9\u306b\u3059\u308b\u3053\u3068\u306f\u3001\u7279\u5b9a\u306e\u95a2\u6570\u3092\u6700\u9069\u5316\u3059\u308b\u52b9\u7387\u7684\u306a\u65b9\u6cd5\u3067\u3082\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u30e6\u30cb\u30c3\u30c8\u307e\u305f\u306f\u5b9a\u6570\u30b9\u30c8\u30e9\u30a4\u30c9\u306f CPU \u306b\u3068\u3063\u3066\u4e88\u6e2c\u53ef\u80fd\u3067\u3059\u304c\u3001\u975e\u30e6\u30cb\u30c3\u30c8\u30b9\u30c8\u30e9\u30a4\u30c9\uff08\u9023\u7d50\u30ea\u30b9\u30c8\u306a\u3069\uff09\u306f\u4e88\u6e2c\u3067\u304d\u307e\u305b\u3093\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    \u30ad\u30e3\u30c3\u30b7\u30e5\u304c\u30d1\u30fc\u30c6\u30a3\u30b7\u30e7\u30f3\u5316\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u8a8d\u8b58\u3059\u308b\u3053\u3068\u3067\u3001\u91cd\u5927\u306a\u30b9\u30c8\u30e9\u30a4\u30c9\u3092\u56de\u907f\u3057\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u3054\u304f\u4e00\u90e8\u306e\u307f\u3092\u4f7f\u7528\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002

    "},{"location":"ja/#92","title":"\u8aa4\u3063\u305f\u5171\u6709\u3092\u5f15\u304d\u8d77\u3053\u3059\u4e26\u884c\u51e6\u7406(#92)","text":"\u8981\u7d04

    \u4e0b\u4f4d\u30ec\u30d9\u30eb\u306e CPU \u30ad\u30e3\u30c3\u30b7\u30e5\u304c\u3059\u3079\u3066\u306e\u30b3\u30a2\u3067\u5171\u6709\u3055\u308c\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3053\u3068\u3092\u77e5\u3063\u3066\u304a\u304f\u3068\u3001\u4e26\u884c\u51e6\u7406\u306b\u304a\u3051\u308b\u306e\u8aa4\u3063\u305f\u5171\u6709\u306a\u3069\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u4f4e\u4e0b\u3055\u305b\u3066\u3057\u307e\u3046\u3053\u3068\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002\u30e1\u30e2\u30ea\u306e\u5171\u6709\u306f\u3042\u308a\u3048\u306a\u3044\u306e\u3067\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#93","title":"\u547d\u4ee4\u30ec\u30d9\u30eb\u306e\u4e26\u5217\u6027\u3092\u8003\u616e\u3057\u306a\u3044 (#93)","text":"\u8981\u7d04

    \u547d\u4ee4\u30ec\u30d9\u30eb\u306e\u4e26\u5217\u6027\uff08ILP\uff09\u3092\u4f7f\u7528\u3057\u3066\u30b3\u30fc\u30c9\u306e\u7279\u5b9a\u306e\u90e8\u5206\u3092\u6700\u9069\u5316\u3057\u3001CPU \u304c\u3067\u304d\u308b\u3060\u3051\u591a\u304f\u306e\u547d\u4ee4\u3092\u4e26\u5217\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002\u4e3b\u306a\u624b\u9806\u306e 1 \u3064\u306b\u30c7\u30fc\u30bf\u30cf\u30b6\u30fc\u30c9\u306e\u7279\u5b9a\u304c\u3042\u308a\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#94","title":"\u30c7\u30fc\u30bf\u306e\u914d\u7f6e\u3092\u610f\u8b58\u3057\u3066\u3044\u306a\u3044 (#94)","text":"\u8981\u7d04

    Go\u8a00\u8a9e\u3067\u306f\u3001\u57fa\u672c\u578b\u306f\u5404\u3005\u306e\u30b5\u30a4\u30ba\u306b\u5408\u308f\u305b\u3066\u914d\u7f6e\u3055\u308c\u308b\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u304f\u3053\u3068\u3067\u3001\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u69cb\u9020\u4f53\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u30b5\u30a4\u30ba\u3067\u964d\u9806\u306b\u518d\u7de8\u6210\u3059\u308b\u3068\u3001\u69cb\u9020\u4f53\u304c\u3088\u308a\u30b3\u30f3\u30d1\u30af\u30c8\u306b\u306a\u308b\uff08\u30e1\u30e2\u30ea\u5272\u308a\u5f53\u3066\u304c\u5c11\u306a\u304f\u306a\u308a\u3001\u7a7a\u9593\u7684\u5c40\u6240\u6027\u304c\u5411\u4e0a\u3059\u308b\uff09\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u306b\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#95","title":"\u30d2\u30fc\u30d7\u3068\u30b9\u30bf\u30c3\u30af\u306e\u9055\u3044\u3092\u7406\u89e3\u3057\u3066\u3044\u306a\u3044 (#95)","text":"\u8981\u7d04

    \u30d2\u30fc\u30d7\u3068\u30b9\u30bf\u30c3\u30af\u306e\u57fa\u672c\u7684\u306a\u9055\u3044\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3082\u3001Go \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u6700\u9069\u5316\u3059\u308b\u969b\u306b\u306f\u5927\u5207\u3067\u3059\u3002\u30b9\u30bf\u30c3\u30af\u5272\u308a\u5f53\u3066\u306f\u5bb9\u6613\u306a\u306e\u306b\u5bfe\u3057\u3066\u3001\u30d2\u30fc\u30d7\u5272\u308a\u5f53\u3066\u306f\u9045\u304f\u3001\u30e1\u30e2\u30ea\u306e\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u306b GC \u3092\u5229\u7528\u3057\u307e\u3059\u3002

    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9

    "},{"location":"ja/#api-syncpool-96","title":"\u5272\u308a\u5f53\u3066\u3092\u6e1b\u3089\u3059\u65b9\u6cd5\u304c\u308f\u304b\u3063\u3066\u3044\u306a\u3044\uff08 API \u306e\u5909\u66f4\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u6700\u9069\u5316\u3001\u304a\u3088\u3073 sync.Pool\uff09 (#96)","text":"\u8981\u7d04

    \u5272\u308a\u5f53\u3066\u3092\u6e1b\u3089\u3059\u3053\u3068\u3082\u3001Go \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u6700\u9069\u5316\u3059\u308b\u4e0a\u3067\u91cd\u8981\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u5171\u6709\u3092\u9632\u3050\u305f\u3081\u306b API \u3092\u614e\u91cd\u306b\u8a2d\u8a08\u3059\u308b\u3001\u4e00\u822c\u7684\u306a Go \u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u6700\u9069\u5316\u3092\u7406\u89e3\u3059\u308b\u3001sync.Pool \u3092\u4f7f\u7528\u3059\u308b\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u65b9\u6cd5\u3067\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\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\u3057\u3087\u3046\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\u7d04

    GC \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\u7d04

    Docker \u3068 Kubernetes \u306b\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u969b\u306e CPU \u30b9\u30ed\u30c3\u30c8\u30ea\u30f3\u30b0\u3092\u56de\u907f\u3059\u308b\u306b\u306f\u3001Go\u8a00\u8a9e\u304c CFS \u5bfe\u5fdc\u3067\u306f\u306a\u3044\u3053\u3068\u306b\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002

    "},{"location":"pt-br/","title":"Erros comuns de Go","text":"

    Esta p\u00e1gina \u00e9 um resumo dos erros do 100 Go Mistakes and How to Avoid Them book. Enquanto isso, tamb\u00e9m est\u00e1 aberto \u00e0 comunidade. Se voc\u00ea acredita que um erro comum do Go deve ser adicionado, crie uma issue.

    Jobs

    Sua empresa est\u00e1 contratando? Patrocine este reposit\u00f3rio e informe um p\u00fablico significativo de desenvolvedores Go (cerca de 1 mil visitantes \u00fanicos por semana) sobre suas oportunidades nesta se\u00e7\u00e3o.

    Beta

    Voc\u00ea est\u00e1 visualizando uma vers\u00e3o beta enriquecida com muito mais conte\u00fado. No entanto, esta vers\u00e3o ainda n\u00e3o est\u00e1 completa e estou procurando volunt\u00e1rios para me ajudar a resumir os erros restantes (GitHub issue #43).

    Progresso:

    "},{"location":"pt-br/#codigo-e-organizacao-do-projeto","title":"C\u00f3digo e Organiza\u00e7\u00e3o do Projeto","text":""},{"location":"pt-br/#sombreamento-nao-intencional-de-variavel-1","title":"Sombreamento n\u00e3o intencional de vari\u00e1vel (#1)","text":"TL;DR

    Evitar vari\u00e1veis \u200b\u200bsombreadas pode ajudar a evitar erros, como fazer refer\u00eancia \u00e0 vari\u00e1vel errada ou confundir os desenvolvedores.

    O sombreamento de vari\u00e1vel ocorre quando um nome de vari\u00e1vel \u00e9 redeclarado em um bloco interno, mas essa pr\u00e1tica est\u00e1 sujeita a erros. A imposi\u00e7\u00e3o de uma regra para proibir vari\u00e1veis \u200b\u200bobscuras depende do gosto pessoal. Por exemplo, \u00e0s vezes pode ser conveniente reutilizar um nome de vari\u00e1vel existente, como err no caso de erros. Por\u00e9m, em geral, devemos ser cautelosos porque agora sabemos que podemos enfrentar um cen\u00e1rio onde o c\u00f3digo compila, mas a vari\u00e1vel que recebe o valor n\u00e3o \u00e9 a esperada.

    C\u00f3digo fonte

    "},{"location":"pt-br/#codigo-aninhado-desnecessario-2","title":"C\u00f3digo aninhado desnecess\u00e1rio (#2)","text":"TL;DR

    Evitar n\u00edveis aninhados e manter o caminho feliz alinhado \u00e0 esquerda facilita a constru\u00e7\u00e3o de um modelo de c\u00f3digo mental.

    Em geral, quanto mais n\u00edveis aninhados uma fun\u00e7\u00e3o exigir, mais complexa ser\u00e1 sua leitura e compreens\u00e3o. Vamos ver algumas aplica\u00e7\u00f5es diferentes desta regra para otimizar a legibilidade do nosso c\u00f3digo:

    if foo() {\n    // ...\n    return true\n} else {\n    // ...\n}\n

    Em vez disso, omitimos o bloco else assim:

    if foo() {\n    // ...\n    return true\n}\n// ...\n
    if s != \"\" {\n    // ...\n} else {\n    return errors.New(\"empty string\")\n}\n

    Aqui, um s vazio representa o caminho n\u00e3o feliz. Portanto, devemos inverter a condi\u00e7\u00e3o assim:

    if s == \"\" {\n    return errors.New(\"empty string\")\n}\n// ...\n

    Escrever c\u00f3digo leg\u00edvel \u00e9 um desafio importante para todo desenvolvedor. Esfor\u00e7ar-se para reduzir o n\u00famero de blocos aninhados, alinhar o caminho feliz \u00e0 esquerda e retornar o mais cedo poss\u00edvel s\u00e3o meios concretos para melhorar a legibilidade do nosso c\u00f3digo.

    C\u00f3digo fonte

    "},{"location":"pt-br/#uso-indevido-de-funcoes-init-3","title":"Uso indevido de fun\u00e7\u00f5es init (#3)","text":"TL;DR

    Ao inicializar vari\u00e1veis, lembre-se de que as fun\u00e7\u00f5es init t\u00eam tratamento de erros limitado e tornam o tratamento de estado e os testes mais complexos. Na maioria dos casos, as inicializa\u00e7\u00f5es devem ser tratadas como fun\u00e7\u00f5es espec\u00edficas.

    Uma fun\u00e7\u00e3o init \u00e9 uma fun\u00e7\u00e3o usada para inicializar o estado de um aplicativo. N\u00e3o aceita argumentos e n\u00e3o retorna nenhum resultado (uma fun\u00e7\u00e3o func()). Quando um pacote \u00e9 inicializado, todas as declara\u00e7\u00f5es de constantes e vari\u00e1veis \u200b\u200bdo pacote s\u00e3o avaliadas. Ent\u00e3o, as fun\u00e7\u00f5es init s\u00e3o executadas.

    As fun\u00e7\u00f5es de inicializa\u00e7\u00e3o podem levar a alguns problemas:

    Devemos ser cautelosos com as fun\u00e7\u00f5es init. No entanto, elas podem ser \u00fateis em algumas situa\u00e7\u00f5es, como na defini\u00e7\u00e3o de configura\u00e7\u00e3o est\u00e1tica. Caso contr\u00e1rio, e na maioria dos casos, devemos tratar as inicializa\u00e7\u00f5es atrav\u00e9s de fun\u00e7\u00f5es ad hoc.

    C\u00f3digo fonte

    "},{"location":"pt-br/#uso-excessivo-de-getters-e-setters-4","title":"Uso excessivo de getters e setters (#4)","text":"TL;DR

    Forcing 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.

    O encapsulamento de dados refere-se a ocultar os valores ou o estado de um objeto. Getters e setters s\u00e3o meios de habilitar o encapsulamento, fornecendo m\u00e9todos exportados sobre campos de objetos n\u00e3o exportados.

    No Go, n\u00e3o h\u00e1 suporte autom\u00e1tico para getters e setters como vemos em algumas linguagens. Tamb\u00e9m n\u00e3o \u00e9 considerado obrigat\u00f3rio nem idiom\u00e1tico o uso de getters e setters para acessar campos struct. N\u00e3o devemos sobrecarregar nosso c\u00f3digo com getters e setters em structs se eles n\u00e3o trouxerem nenhum valor. Dever\u00edamos ser pragm\u00e1ticos e nos esfor\u00e7ar para encontrar o equil\u00edbrio certo entre efici\u00eancia e seguir express\u00f5es que \u00e0s vezes s\u00e3o consideradas indiscut\u00edveis em outros paradigmas de programa\u00e7\u00e3o.

    Lembre-se de que Go \u00e9 uma linguagem \u00fanica projetada para muitas caracter\u00edsticas, incluindo simplicidade. No entanto, se encontrarmos necessidade de getters e setters ou, como mencionado, prevermos uma necessidade futura e ao mesmo tempo garantirmos a compatibilidade futura, n\u00e3o h\u00e1 nada de errado em us\u00e1-los.

    "},{"location":"pt-br/#interface-poluidas-5","title":"Interface poluidas (#5)","text":"TL;DR

    Abstra\u00e7\u00f5es devem ser descobertas, n\u00e3o criadas. Para evitar complexidade desnecess\u00e1ria, crie uma interface quando precisar dela e n\u00e3o quando voc\u00ea prev\u00ear que ser\u00e1 necess\u00e1ria, ou se puder pelo menos provar que a abstra\u00e7\u00e3o \u00e9 v\u00e1lida.

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#interface-do-lado-do-producer-6","title":"Interface do lado do producer (#6)","text":"TL;DR

    Manter interfaces no lado do cliente evita abstra\u00e7\u00f5es desnecess\u00e1rias.

    As interfaces s\u00e3o satisfeitas implicitamente em Go, o que tende a ser um divisor de \u00e1guas em compara\u00e7\u00e3o com linguagens com implementa\u00e7\u00e3o expl\u00edcita. Na maioria dos casos, a abordagem a seguir \u00e9 semelhante \u00e0 que descrevemos na se\u00e7\u00e3o anterior: as abstra\u00e7\u00f5es devem ser descobertas, n\u00e3o criadas. Isso significa que n\u00e3o cabe ao producer for\u00e7ar uma determinada abstra\u00e7\u00e3o para todos os clientes. Em vez disso, cabe ao cliente decidir se precisa de alguma forma de abstra\u00e7\u00e3o e ent\u00e3o determinar o melhor n\u00edvel de abstra\u00e7\u00e3o para suas necessidades.

    Uma interface deve residir no lado do consumidor na maioria dos casos. Contudo, em contextos espec\u00edficos (por exemplo, quando sabemos \u2013 e n\u00e3o prevemos \u2013 que uma abstra\u00e7\u00e3o ser\u00e1 \u00fatil para os consumidores), podemos querer t\u00ea-la do lado do procuder. Se o fizermos, devemos nos esfor\u00e7ar para mant\u00ea-lo o m\u00ednimo poss\u00edvel, aumentando o seu potencial de reutiliza\u00e7\u00e3o e tornando-o mais facilmente combin\u00e1vel.

    C\u00f3digo fonte

    "},{"location":"pt-br/#interfaces-de-retorno-7","title":"Interfaces de retorno (#7)","text":"TL;DR

    Para evitar restri\u00e7\u00f5es em termos de flexibilidade, uma fun\u00e7\u00e3o n\u00e3o deve retornar interfaces, mas implementa\u00e7\u00f5es concretas na maioria dos casos. Por outro lado, uma fun\u00e7\u00e3o deve aceitar interfaces sempre que poss\u00edvel.

    Na maioria dos casos, n\u00e3o devemos retornar interfaces, mas implementa\u00e7\u00f5es concretas. Caso contr\u00e1rio, isso pode tornar nosso design mais complexo devido \u00e0s depend\u00eancias do pacote e pode restringir a flexibilidade porque todos os clientes teriam que contar com a mesma abstra\u00e7\u00e3o. Novamente, a conclus\u00e3o \u00e9 semelhante \u00e0s se\u00e7\u00f5es anteriores: se sabemos (n\u00e3o prevemos) que uma abstra\u00e7\u00e3o ser\u00e1 \u00fatil para os clientes, podemos considerar o retorno de uma interface. Caso contr\u00e1rio, n\u00e3o dever\u00edamos for\u00e7ar abstra\u00e7\u00f5es; eles devem ser descobertas pelos clientes. Se um cliente precisar abstrair uma implementa\u00e7\u00e3o por qualquer motivo, ele ainda poder\u00e1 fazer isso do lado do cliente.

    "},{"location":"pt-br/#any-nao-diz-nada-8","title":"any n\u00e3o diz nada (#8)","text":"TL;DR

    Use apenas any se precisar aceitar ou retornar qualquer tipo poss\u00edvel, como json.Marshal. Caso contr\u00e1rio, any n\u00e3o fornece informa\u00e7\u00f5es significativas e pode levar a problemas de tempo de compila\u00e7\u00e3o, permitindo que um chamador chame m\u00e9todos com qualquer tipo de dados.

    O tipo any pode ser \u00fatil se houver uma necessidade genu\u00edna de aceitar ou retornar qualquer tipo poss\u00edvel (por exemplo, quando se trata de empacotamento ou formata\u00e7\u00e3o). Em geral, devemos evitar a todo custo generalizar demais o c\u00f3digo que escrevemos. Talvez um pouco de c\u00f3digo duplicado possa ocasionalmente ser melhor se melhorar outros aspectos, como a expressividade do c\u00f3digo.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ficar-confuso-sobre-quando-usar-genericos-9","title":"Ficar confuso sobre quando usar gen\u00e9ricos (#9)","text":"TL;DR

    Depender de par\u00e2metros gen\u00e9ricos e de tipo pode impedir a grava\u00e7\u00e3o de c\u00f3digo clich\u00ea (boilerplate) para fatorar elementos ou comportamentos. No entanto, n\u00e3o use par\u00e2metros de tipo prematuramente, mas somente quando voc\u00ea perceber uma necessidade concreta deles. Caso contr\u00e1rio, introduzem abstra\u00e7\u00f5es e complexidade desnecess\u00e1rias.

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-estar-ciente-dos-possiveis-problemas-com-a-incorporacao-de-tipos-10","title":"N\u00e3o estar ciente dos poss\u00edveis problemas com a incorpora\u00e7\u00e3o de tipos (#10)","text":"TL;DR

    Usar a incorpora\u00e7\u00e3o de tipo (type embedding) tamb\u00e9m pode ajudar a evitar c\u00f3digo clich\u00ea (boilerplate); no entanto, certifique-se de que isso n\u00e3o leve a problemas de visibilidade onde alguns campos deveriam ter permanecido ocultos.

    Ao criar uma struct, Go oferece a op\u00e7\u00e3o de incorporar tipos. Mas isso \u00e0s vezes pode levar a comportamentos inesperados se n\u00e3o compreendermos todas as implica\u00e7\u00f5es da incorpora\u00e7\u00e3o de tipos. Ao longo desta se\u00e7\u00e3o, veremos como incorporar tipos, o que eles trazem e os poss\u00edveis problemas.

    No Go, um campo struct \u00e9 chamado de incorporado se for declarado sem nome. Por exemplo,

    type Foo struct {\n    Bar // Embedded field\n}\n\ntype Bar struct {\n    Baz int\n}\n

    Na estrutura Foo, o tipo Bar \u00e9 declarado sem nome associado; portanto, \u00e9 um campo incorporado.

    Usamos incorpora\u00e7\u00e3o para promover os campos e m\u00e9todos de um tipo incorporado. Como Bar cont\u00e9m um campo Baz, esse campo \u00e9 promovido para Foo. Portanto, Baz fica dispon\u00edvel a partir de Foo.

    O que podemos dizer sobre a incorpora\u00e7\u00e3o de tipos? Primeiro, observemos que raramente \u00e9 uma necessidade e significa que, qualquer que seja o caso de uso, provavelmente tamb\u00e9m poderemos resolv\u00ea-lo sem incorpora\u00e7\u00e3o de tipo. A incorpora\u00e7\u00e3o de tipos \u00e9 usada principalmente por conveni\u00eancia: na maioria dos casos, para promover comportamentos.

    Se decidirmos usar incorpora\u00e7\u00e3o de tipo, precisamos ter em mente duas restri\u00e7\u00f5es principais:

    Usar a incorpora\u00e7\u00e3o de tipo de forma consciente, mantendo essas restri\u00e7\u00f5es em mente, pode ajudar a evitar c\u00f3digo clich\u00ea (boilerplate) com m\u00e9todos de encaminhamento adicionais. No entanto, vamos garantir que n\u00e3o o fazemos apenas por motivos cosm\u00e9ticos e n\u00e3o promovemos elementos que deveriam permanecer ocultos.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-usar-o-padrao-de-opcoes-funcionais-functional-options-pattern-11","title":"N\u00e3o usar o padr\u00e3o de op\u00e7\u00f5es funcionais (functional options pattern) (#11)","text":"TL;DR

    Para lidar com op\u00e7\u00f5es de maneira conveniente e amig\u00e1vel \u00e0 API, use o padr\u00e3o de op\u00e7\u00f5es funcionais.

    Embora existam diferentes implementa\u00e7\u00f5es com pequenas varia\u00e7\u00f5es, a ideia principal \u00e9 a seguinte:

    type options struct {\n  port *int\n}\n\ntype Option func(options *options) error\n\nfunc WithPort(port int) Option {\n  return func(options *options) error {\n    if port < 0 {\n    return errors.New(\"port should be positive\")\n  }\n  options.port = &port\n  return nil\n  }\n}\n\nfunc NewServer(addr string, opts ...Option) ( *http.Server, error) {\n  var options options\n  for _, opt := range opts {\n    err := opt(&options)\n    if err != nil {\n      return nil, err\n    }\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\n  var port int\n  if options.port == nil {\n    port = defaultHTTPPort\n  } else {\n      if *options.port == 0 {\n      port = randomPort()\n    } else {\n      port = *options.port\n    }\n  }\n\n  // ...\n}\n

    O padr\u00e3o de op\u00e7\u00f5es funcionais fornece uma maneira pr\u00e1tica e amig\u00e1vel \u00e0 API de lidar com op\u00e7\u00f5es. Embora o padr\u00e3o do construtor possa ser uma op\u00e7\u00e3o v\u00e1lida, ele tem algumas desvantagens menores (ter que passar uma estrutura de configura\u00e7\u00e3o que pode estar vazia ou uma maneira menos pr\u00e1tica de lidar com o gerenciamento de erros) que tendem a tornar o padr\u00e3o de op\u00e7\u00f5es funcionais a maneira idiom\u00e1tica de lidar com esse tipo de problema no Go.

    C\u00f3digo fonte

    "},{"location":"pt-br/#desorganizacao-do-projeto-estrutura-do-projeto-e-organizacao-do-pacote-12","title":"Desorganiza\u00e7\u00e3o do projeto (estrutura do projeto e organiza\u00e7\u00e3o do pacote) (#12)","text":"

    No que diz respeito \u00e0 organiza\u00e7\u00e3o geral, existem diferentes escolas de pensamento. Por exemplo, devemos organizar a nossa aplica\u00e7\u00e3o por contexto ou por camada? Depende de nossas prefer\u00eancias. Podemos preferir agrupar o c\u00f3digo por contexto (como o contexto do cliente, o contexto do contrato, etc.), ou podemos preferir seguir os princ\u00edpios da arquitetura hexagonal e agrupar por camada t\u00e9cnica. Se a decis\u00e3o que tomarmos se adequar ao nosso caso de uso, n\u00e3o pode ser uma decis\u00e3o errada, desde que permane\u00e7amos consistentes com ela.

    Em rela\u00e7\u00e3o aos pacotes, existem v\u00e1rias pr\u00e1ticas recomendadas que devemos seguir. Primeiro, devemos evitar pacotes prematuros porque podem complicar demais um projeto. \u00c0s vezes, \u00e9 melhor usar uma organiza\u00e7\u00e3o simples e fazer nosso projeto evoluir quando entendemos o que ele cont\u00e9m, em vez de nos for\u00e7armos a fazer a estrutura perfeita desde o in\u00edcio. A granularidade \u00e9 outra coisa essencial a considerar. Devemos evitar dezenas de pacotes nano contendo apenas um ou dois arquivos. Se o fizermos, \u00e9 porque provavelmente perdemos algumas conex\u00f5es l\u00f3gicas entre esses pacotes, tornando nosso projeto mais dif\u00edcil de ser compreendido pelos leitores. Por outro lado, tamb\u00e9m devemos evitar pacotes grandes que diluem o significado do nome de um pacote.

    A nomenclatura dos pacotes tamb\u00e9m deve ser considerada com cuidado. Como todos sabemos (como desenvolvedores), nomear \u00e9 dif\u00edcil. Para ajudar os clientes a entender um projeto Go, devemos nomear nossos pacotes de acordo com o que eles fornecem, n\u00e3o com o que cont\u00eam. Al\u00e9m disso, a nomenclatura deve ser significativa. Portanto, o nome de um pacote deve ser curto, conciso, expressivo e, por conven\u00e7\u00e3o, uma \u00fanica palavra min\u00fascula.

    Quanto ao que exportar, a regra \u00e9 bastante simples. Devemos minimizar o que deve ser exportado tanto quanto poss\u00edvel para reduzir o acoplamento entre pacotes e manter ocultos os elementos exportados desnecess\u00e1rios. Se n\u00e3o tivermos certeza se devemos ou n\u00e3o exportar um elemento, devemos optar por n\u00e3o export\u00e1-lo. Mais tarde, se descobrirmos que precisamos export\u00e1-lo, poderemos ajustar nosso c\u00f3digo. Vamos tamb\u00e9m ter em mente algumas exce\u00e7\u00f5es, como fazer com que os campos sejam exportados para que uma estrutura possa ser desempacotada com encoding/json.

    Organizar um projeto n\u00e3o \u00e9 simples, mas seguir essas regras deve ajudar a facilitar sua manuten\u00e7\u00e3o. No entanto, lembre-se de que a consist\u00eancia tamb\u00e9m \u00e9 vital para facilitar a manuten\u00e7\u00e3o. Portanto, vamos nos certificar de manter as coisas o mais consistentes poss\u00edvel dentro de uma base de c\u00f3digo.

    Note

    Em 2023, a equipe Go publicou uma diretriz oficial para organizar/estruturar um projeto Go: go.dev/doc/modules/layout

    "},{"location":"pt-br/#criando-pacotes-de-utilitarios-13","title":"Criando pacotes de utilit\u00e1rios (#13)","text":"TL;DR

    A nomenclatura \u00e9 uma parte cr\u00edtica do design do aplicativo. Criar pacotes como common, util e shared n\u00e3o traz muito valor para o leitor. Refatore esses pacotes em nomes de pacotes significativos e espec\u00edficos.

    Al\u00e9m disso, tenha em mente que nomear um pacote com base no que ele fornece e n\u00e3o no que ele cont\u00e9m pode ser uma forma eficiente de aumentar sua expressividade.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-colisoes-de-nomes-de-pacotes-14","title":"Ignorando colis\u00f5es de nomes de pacotes (#14)","text":"TL;DR

    Para evitar colis\u00f5es de nomes entre vari\u00e1veis \u200b\u200be pacotes, levando a confus\u00e3o ou talvez at\u00e9 bugs, use nomes exclusivos para cada um. Se isso n\u00e3o for vi\u00e1vel, use um alias de importa\u00e7\u00e3o para alterar o qualificador para diferenciar o nome do pacote do nome da vari\u00e1vel ou pense em um nome melhor.

    As colis\u00f5es de pacotes ocorrem quando um nome de vari\u00e1vel colide com um nome de pacote existente, impedindo que o pacote seja reutilizado. Devemos evitar colis\u00f5es de nomes de vari\u00e1veis \u200b\u200bpara evitar ambiguidade. Se enfrentarmos uma colis\u00e3o, devemos encontrar outro nome significativo ou usar um alias de importa\u00e7\u00e3o.

    "},{"location":"pt-br/#documentacao-de-codigo-ausente-15","title":"Documenta\u00e7\u00e3o de c\u00f3digo ausente (#15)","text":"TL;DR

    Para ajudar clientes e mantenedores a entender a finalidade do seu c\u00f3digo, documente os elementos exportados.

    A documenta\u00e7\u00e3o \u00e9 um aspecto importante da programa\u00e7\u00e3o. Simplifica como os clientes podem consumir uma API, mas tamb\u00e9m pode ajudar na manuten\u00e7\u00e3o de um projeto. No Go, devemos seguir algumas regras para tornar nosso c\u00f3digo idiom\u00e1tico:

    Primeiro, cada elemento exportado deve ser documentado. Seja uma estrutura, uma interface, uma fun\u00e7\u00e3o ou qualquer outra coisa, se for exportado deve ser documentado. A conven\u00e7\u00e3o \u00e9 adicionar coment\u00e1rios, come\u00e7ando com o nome do elemento exportado.

    Por conven\u00e7\u00e3o, cada coment\u00e1rio deve ser uma frase completa que termina com pontua\u00e7\u00e3o. Tenha tamb\u00e9m em mente que quando documentamos uma fun\u00e7\u00e3o (ou um m\u00e9todo), devemos destacar o que a fun\u00e7\u00e3o pretende fazer, n\u00e3o como o faz; isso pertence ao n\u00facleo de uma fun\u00e7\u00e3o e coment\u00e1rios, n\u00e3o \u00e0 documenta\u00e7\u00e3o. Al\u00e9m disso, o ideal \u00e9 que a documenta\u00e7\u00e3o forne\u00e7a informa\u00e7\u00f5es suficientes para que o consumidor n\u00e3o precise olhar nosso c\u00f3digo para entender como usar um elemento exportado.

    Quando se trata de documentar uma vari\u00e1vel ou constante, podemos estar interessados \u200b\u200bem transmitir dois aspectos: sua finalidade e seu conte\u00fado. O primeiro deve funcionar como documenta\u00e7\u00e3o de c\u00f3digo para ser \u00fatil para clientes externos. Este \u00faltimo, por\u00e9m, n\u00e3o deveria ser necessariamente p\u00fablico.

    Para ajudar clientes e mantenedores a entender o escopo de um pacote, devemos tamb\u00e9m documentar cada pacote. A conven\u00e7\u00e3o \u00e9 iniciar o coment\u00e1rio com // Package seguido do nome do pacote. A primeira linha de um coment\u00e1rio de pacote deve ser concisa. Isso porque ele aparecer\u00e1 no pacote. Ent\u00e3o, podemos fornecer todas as informa\u00e7\u00f5es que precisamos nas linhas seguintes.

    Documentar nosso c\u00f3digo n\u00e3o deve ser uma restri\u00e7\u00e3o. Devemos aproveitar a oportunidade para garantir que isso ajude os clientes e mantenedores a entender o prop\u00f3sito do nosso c\u00f3digo.

    "},{"location":"pt-br/#nao-usando-linters-16","title":"N\u00e3o usando linters (#16)","text":"TL;DR

    Para melhorar a qualidade e consist\u00eancia do c\u00f3digo, use linters e formatadores.

    Um linter \u00e9 uma ferramenta autom\u00e1tica para analisar c\u00f3digo e detectar erros. O escopo desta se\u00e7\u00e3o n\u00e3o \u00e9 fornecer uma lista exaustiva dos linters existentes; caso contr\u00e1rio, ele ficar\u00e1 obsoleto rapidamente. Mas devemos entender e lembrar por que os linters s\u00e3o essenciais para a maioria dos projetos Go.

    No entanto, se voc\u00ea n\u00e3o \u00e9 um usu\u00e1rio regular de linters, aqui est\u00e1 uma lista que voc\u00ea pode usar diariamente:

    Al\u00e9m dos linters, tamb\u00e9m devemos usar formatadores de c\u00f3digo para corrigir o estilo do c\u00f3digo. Aqui est\u00e1 uma lista de alguns formatadores de c\u00f3digo para voc\u00ea experimentar:

    Enquanto isso, devemos tamb\u00e9m dar uma olhada em golangci-lint (https://github.com/golangci/golangci-lint). \u00c9 uma ferramenta de linting que fornece uma fachada sobre muitos linters e formatadores \u00fateis. Al\u00e9m disso, permite executar os linters em paralelo para melhorar a velocidade de an\u00e1lise, o que \u00e9 bastante \u00fatil.

    Linters e formatadores s\u00e3o uma forma poderosa de melhorar a qualidade e consist\u00eancia de nossa base de c\u00f3digo. Vamos dedicar um tempo para entender qual deles devemos usar e garantir que automatizamos sua execu\u00e7\u00e3o (como um precommit hook de CI ou Git).

    "},{"location":"pt-br/#tipos-de-dados","title":"Tipos de dados","text":""},{"location":"pt-br/#criando-confusao-com-literais-octais-17","title":"Criando confus\u00e3o com literais octais (#17)","text":"TL;DR

    Ao ler o c\u00f3digo existente, lembre-se de que literais inteiros come\u00e7ando com 0 s\u00e3o n\u00fameros octais. Al\u00e9m disso, para melhorar a legibilidade, torne os inteiros octais expl\u00edcitos prefixando-os com 0o.

    Os n\u00fameros octais come\u00e7am com 0 (por exemplo, 010 \u00e9 igual a 8 na base 10). Para melhorar a legibilidade e evitar poss\u00edveis erros para futuros leitores de c\u00f3digo, devemos tornar os n\u00fameros octais expl\u00edcitos usando o prefixo 0o (por exemplo, 0o10).

    Devemos tamb\u00e9m observar as outras representa\u00e7\u00f5es literais inteiras:

    Tamb\u00e9m podemos usar um caractere de sublinhado (_) como separador para facilitar a leitura. Por exemplo, podemos escrever 1 bilh\u00e3o desta forma: 1_000_000_000. Tamb\u00e9m podemos usar o caractere sublinhado com outras representa\u00e7\u00f5es (por exemplo, 0b00_00_01).

    C\u00f3digo fonte

    "},{"location":"pt-br/#negligenciando-estouros-de-numero-inteiro-18","title":"Negligenciando estouros de n\u00famero inteiro (#18)","text":"TL;DR

    Como os overflows e underflows de n\u00fameros inteiros s\u00e3o tratados silenciosamente no Go, voc\u00ea pode implementar suas pr\u00f3prias fun\u00e7\u00f5es para captur\u00e1-los.

    No Go, um estouro de n\u00famero inteiro que pode ser detectado em tempo de compila\u00e7\u00e3o gera um erro de compila\u00e7\u00e3o. Por exemplo,

    var counter int32 = math.MaxInt32 + 1\n
    constant 2147483648 overflows int32\n

    No entanto, em tempo de execu\u00e7\u00e3o, um overflow ou underflow de inteiro \u00e9 silencioso; isso n\u00e3o leva ao p\u00e2nico do aplicativo. \u00c9 essencial ter esse comportamento em mente, pois ele pode levar a bugs sorrateiros (por exemplo, um incremento de n\u00famero inteiro ou adi\u00e7\u00e3o de n\u00fameros inteiros positivos que leva a um resultado negativo).

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-entendendo-os-pontos-flutuantes-19","title":"N\u00e3o entendendo os pontos flutuantes (#19)","text":"TL;DR

    Fazer compara\u00e7\u00f5es de ponto flutuante dentro de um determinado delta pode garantir que seu c\u00f3digo seja port\u00e1til. Ao realizar adi\u00e7\u00e3o ou subtra\u00e7\u00e3o, agrupe as opera\u00e7\u00f5es com ordem de grandeza semelhante para favorecer a precis\u00e3o. Al\u00e9m disso, execute multiplica\u00e7\u00e3o e divis\u00e3o antes da adi\u00e7\u00e3o e subtra\u00e7\u00e3o.

    Em Go, existem dois tipos de ponto flutuante (se omitirmos os n\u00fameros imagin\u00e1rios): float32 e float64. O conceito de ponto flutuante foi inventado para resolver o principal problema dos n\u00fameros inteiros: sua incapacidade de representar valores fracion\u00e1rios. Para evitar surpresas desagrad\u00e1veis, precisamos saber que a aritm\u00e9tica de ponto flutuante \u00e9 uma aproxima\u00e7\u00e3o da aritm\u00e9tica real.

    Para isso, veremos um exemplo de multiplica\u00e7\u00e3o:

    var n float32 = 1.0001\nfmt.Println(n * n)\n

    Podemos esperar que este c\u00f3digo imprima o resultado de 1.0001 * 1.0001 = 1,00020001, certo? No entanto, execut\u00e1-lo na maioria dos processadores x86 imprime 1.0002.

    Como os tipos float32 e float64 em Go s\u00e3o aproxima\u00e7\u00f5es, temos que ter algumas regras em mente:

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-entendendo-o-comprimento-e-a-capacidade-de-slice-20","title":"N\u00e3o entendendo o comprimento e a capacidade de slice (#20)","text":"TL;DR

    Compreender a diferen\u00e7a entre comprimento e capacidade da slice deve fazer parte do conhecimento b\u00e1sico de um desenvolvedor Go. O comprimento de slice \u00e9 o n\u00famero de elementos dispon\u00edveis na slice, enquanto a capacidade de slice \u00e9 o n\u00famero de elementos na matriz de apoio.

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#inicializacao-de-slice-ineficiente-21","title":"Inicializa\u00e7\u00e3o de slice ineficiente (#21)","text":"TL;DR

    Ao criar uma fatia, inicialize-a com um determinado comprimento ou capacidade se o seu comprimento j\u00e1 for conhecido. Isso reduz o n\u00famero de aloca\u00e7\u00f5es e melhora o desempenho.

    Ao inicializar uma fatia usando make, podemos fornecer um comprimento e uma capacidade opcional. Esquecer de passar um valor apropriado para ambos os par\u00e2metros quando faz sentido \u00e9 um erro generalizado. Na verdade, isso pode levar a m\u00faltiplas c\u00f3pias e esfor\u00e7o adicional para o GC limpar as matrizes de apoio tempor\u00e1rias. Em termos de desempenho, n\u00e3o h\u00e1 uma boa raz\u00e3o para n\u00e3o ajudar o tempo de execu\u00e7\u00e3o do Go.

    Nossas op\u00e7\u00f5es s\u00e3o alocar uma fatia com determinada capacidade ou comprimento. Destas duas solu\u00e7\u00f5es, vimos que a segunda tende a ser um pouco mais r\u00e1pida. Mas usar uma determinada capacidade e anexar pode ser mais f\u00e1cil de implementar e ler em alguns contextos.

    C\u00f3digo fonte

    "},{"location":"pt-br/#estar-confuso-sobre-slice-nula-vs-slice-vazia-22","title":"Estar confuso sobre slice nula vs. slice vazia (#22)","text":"TL;DR

    To 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.

    No Go, h\u00e1 uma distin\u00e7\u00e3o entre slices nulas e vazias. Uma slice nula \u00e9 igual a nil, enquanto uma slice vazia tem comprimento zero. Uma slice nula est\u00e1 vazia, mas uma slice vazia n\u00e3o \u00e9 necessariamente nil. Enquanto isso, uma slice nula n\u00e3o requer nenhuma aloca\u00e7\u00e3o. Vimos ao longo desta se\u00e7\u00e3o como inicializar uma slice dependendo do contexto usando

    A \u00faltima op\u00e7\u00e3o, []string{} deve ser evitada se inicializarmos a fatia sem elementos. Finalmente, vamos verificar se as bibliotecas que usamos fazem distin\u00e7\u00f5es entre fatias nulas e vazias para evitar comportamentos inesperados.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-verificar-corretamente-se-um-slice-esta-vazio-23","title":"N\u00e3o verificar corretamente se um slice est\u00e1 vazio (#23)","text":"TL;DR

    Para verificar se uma fatia n\u00e3o cont\u00e9m nenhum elemento, verifique seu comprimento. Esta verifica\u00e7\u00e3o funciona independentemente de o slice estar nil ou vazio. O mesmo vale para maps. Para projetar APIs inequ\u00edvocas, voc\u00ea n\u00e3o deve distinguir entre slice nulos e vazios.

    Para determinar se um slice possui elementos, podemos faz\u00ea-lo verificando se o slice \u00e9 nulo ou se seu comprimento \u00e9 igual a 0. Verificar o comprimento \u00e9 a melhor op\u00e7\u00e3o a seguir, pois cobrir\u00e1 ambos se o slice estiver vazio ou se o slice \u00e9 nulo.

    Enquanto isso, ao projetar interfaces, devemos evitar distinguir slices nulos e vazios, o que leva a erros sutis de programa\u00e7\u00e3o. Ao retornar slices, n\u00e3o deve haver diferen\u00e7a sem\u00e2ntica nem t\u00e9cnica se retornarmos um slice nulo ou vazio. Ambos devem significar a mesma coisa para quem liga. Este princ\u00edpio \u00e9 o mesmo com maps. Para verificar se um map est\u00e1 vazio, verifique seu comprimento, n\u00e3o se \u00e9 nulo.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-fazer-copias-de-slcies-corretamente-24","title":"N\u00e3o fazer c\u00f3pias de slcies corretamente (#24)","text":"TL;DR

    Para copiar um slice para outro usando a fun\u00e7\u00e3o copy, lembre-se que o n\u00famero de elementos copiados corresponde ao m\u00ednimo entre os comprimentos dos dois slices.

    Copiar elementos de um slice para outro \u00e9 uma opera\u00e7\u00e3o razoavelmente frequente. Ao utilizar a c\u00f3pia, devemos lembrar que o n\u00famero de elementos copiados para o destino corresponde ao m\u00ednimo entre os comprimentos dos dois slices. Tenha tamb\u00e9m em mente que existem outras alternativas para copiar um slice, por isso n\u00e3o devemos nos surpreender se as encontrarmos em uma base de c\u00f3digo.

    C\u00f3digo fonte

    "},{"location":"pt-br/#efeitos-colaterais-inesperados-usando-o-slice-append-25","title":"Efeitos colaterais inesperados usando o slice append (#25)","text":"TL;DR

    Usar copy ou a express\u00e3o de slice completa \u00e9 uma forma de evitar que append crie conflitos se duas fun\u00e7\u00f5es diferentes usarem slices apoiados pela mesmo array. No entanto, apenas uma c\u00f3pia de slice evita vazamentos de mem\u00f3ria se voc\u00ea quiser reduzir um slice grande.

    Ao usar o slicing, devemos lembrar que podemos enfrentar uma situa\u00e7\u00e3o que leva a efeitos colaterais n\u00e3o intencionais. Se o slice resultante tiver um comprimento menor que sua capacidade, o acr\u00e9scimo poder\u00e1 alterar o slice original. Se quisermos restringir a gama de poss\u00edveis efeitos colaterais, podemos usar uma c\u00f3pia de slice ou a express\u00e3o de slice completa, o que nos impede de fazer uma c\u00f3pia.

    Note

    s[low:high:max](express\u00e3o de slice completo): Esta instru\u00e7\u00e3o cria um slice semelhante \u00e0quele criado com s[low:high], exceto que a capacidade de slice resultante \u00e9 igual a max - low.

    C\u00f3digo fonte

    "},{"location":"pt-br/#slices-e-vazamentos-de-memoria-26","title":"Slices e vazamentos de mem\u00f3ria (#26)","text":"TL;DR

    Trabalhando com um slice de ponteiros ou estruturas com campos de ponteiro, voc\u00ea pode evitar vazamentos de mem\u00f3ria marcando como nulos os elementos exclu\u00eddos por uma opera\u00e7\u00e3o de fatiamento.

    "},{"location":"pt-br/#vazamento-de-capacidade","title":"Vazamento de capacidade","text":"

    Lembre-se de que fatiar um slice ou array grande pode levar a um potencial alto consumo de mem\u00f3ria. O espa\u00e7o restante n\u00e3o ser\u00e1 recuperado pelo GC e podemos manter um grande array de apoio, apesar de usarmos apenas alguns elementos. Usar uma c\u00f3pia em slice \u00e9 a solu\u00e7\u00e3o para evitar tal caso.

    C\u00f3digo fonte

    "},{"location":"pt-br/#slice-e-ponteiros","title":"Slice e ponteiros","text":"

    Quando usamos a opera\u00e7\u00e3o de fatiamento com ponteiros ou estruturas com campos de ponteiro, precisamos saber que o GC n\u00e3o recuperar\u00e1 esses elementos. Nesse caso, as duas op\u00e7\u00f5es s\u00e3o realizar uma c\u00f3pia ou marcar explicitamente os elementos restantes ou seus campos como nil.

    C\u00f3digo fonte

    "},{"location":"pt-br/#inicializacao-ineficiente-do-mapa-27","title":"Inicializa\u00e7\u00e3o ineficiente do mapa (#27)","text":"TL;DR

    Ao criar um mapa, inicialize-o com um determinado comprimento se o seu comprimento j\u00e1 for conhecido. Isso reduz o n\u00famero de aloca\u00e7\u00f5es e melhora o desempenho.

    Um mapa fornece uma cole\u00e7\u00e3o n\u00e3o ordenada de pares chave-valor em que todas as chaves s\u00e3o distintas. No Go, um mapa \u00e9 baseado na estrutura de dados da tabela hash. Internamente, uma tabela hash \u00e9 uma matriz de intervalos e cada intervalo \u00e9 um ponteiro para uma matriz de pares de valores-chave.

    Se soubermos de antem\u00e3o o n\u00famero de elementos que um mapa conter\u00e1, devemos cri\u00e1-lo fornecendo um tamanho inicial. Fazer isso evita o crescimento potencial do mapa, o que \u00e9 bastante pesado em termos de computa\u00e7\u00e3o porque requer a realoca\u00e7\u00e3o de espa\u00e7o suficiente e o reequil\u00edbrio de todos os elementos.

    C\u00f3digo fonte

    "},{"location":"pt-br/#mapas-e-vazamentos-de-memoria-28","title":"Mapas e vazamentos de mem\u00f3ria (#28)","text":"TL;DR

    Um mapa sempre pode crescer na mem\u00f3ria, mas nunca diminui. Portanto, se isso causar alguns problemas de mem\u00f3ria, voc\u00ea pode tentar diferentes op\u00e7\u00f5es, como for\u00e7ar Go a recriar o mapa ou usar ponteiros.

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#comparando-valores-incorretamente-29","title":"Comparando valores incorretamente (#29)","text":"TL;DR

    Para comparar tipos em Go, voc\u00ea pode usar os operadores == e != se dois tipos forem compar\u00e1veis: booleanos, numerais, strings, ponteiros, canais e estruturas s\u00e3o compostos inteiramente de tipos compar\u00e1veis. Caso contr\u00e1rio, voc\u00ea pode usar reflect.DeepEquale pagar o pre\u00e7o da reflex\u00e3o ou usar implementa\u00e7\u00f5es e bibliotecas personalizadas.

    \u00c9 essencial entender como usar == e != para fazer compara\u00e7\u00f5es de forma eficaz. Podemos usar esses operadores em operandos compar\u00e1veis:

    Note

    Tamb\u00e9m podemos usar os operadores ?, >=, < e > com tipos num\u00e9ricos para comparar valores e com strings para comparar sua ordem lexical.

    Se os operandos n\u00e3o forem compar\u00e1veis \u200b\u200b(por exemplo, slices e mapas), teremos que usar outras op\u00e7\u00f5es, como reflex\u00e3o. A reflex\u00e3o \u00e9 uma forma de metaprograma\u00e7\u00e3o e se refere \u00e0 capacidade de um aplicativo de introspectar e modificar sua estrutura e comportamento. Por exemplo, em Go, podemos usar reflect.DeepEqual. Esta fun\u00e7\u00e3o informa se dois elementos s\u00e3o profundamente iguais percorrendo recursivamente dois valores. Os elementos que ele aceita s\u00e3o tipos b\u00e1sicos mais arrays, estruturas, slices, mapas, ponteiros, interfaces e fun\u00e7\u00f5es. No entanto, o principal problema \u00e9 a penalidade de desempenho.

    Se o desempenho for crucial em tempo de execu\u00e7\u00e3o, implementar nosso m\u00e9todo customizado pode ser a melhor solu\u00e7\u00e3o. Uma observa\u00e7\u00e3o adicional: devemos lembrar que a biblioteca padr\u00e3o possui alguns m\u00e9todos de compara\u00e7\u00e3o existentes. Por exemplo, podemos usar a fun\u00e7\u00e3o bytes.Compare otimizada para comparar duas slices de bytes. Antes de implementar um m\u00e9todo customizado, precisamos ter certeza de n\u00e3o reinventar a roda.

    C\u00f3digo fonte

    "},{"location":"pt-br/#estruturas-de-controle","title":"Estruturas de Controle","text":""},{"location":"pt-br/#ignorando-que-os-elementos-sao-copiados-em-loops-de-range-30","title":"Ignorando que os elementos s\u00e3o copiados em loops de range (#30)","text":"TL;DR

    O elemento de valor em um loop de range \u00e9 uma c\u00f3pia. Portanto, para modificar uma struct, por exemplo, acesse-a atrav\u00e9s de seu \u00edndice ou atrav\u00e9s de um loop for cl\u00e1ssico (a menos que o elemento ou campo que voc\u00ea deseja modificar seja um ponteiro).

    Um range loop permite iterar em diferentes estruturas de dados:

    Comparado a um for loop cl\u00e1ssico, um loop range \u00e9 uma maneira conveniente de iterar todos os elementos de uma dessas estruturas de dados, gra\u00e7as \u00e0 sua sintaxe concisa.

    Ainda assim, devemos lembrar que o elemento de valor em um range loop \u00e9 uma c\u00f3pia. Portanto, se o valor for uma estrutura que precisamos sofrer muta\u00e7\u00e3o, atualizaremos apenas a c\u00f3pia, n\u00e3o o elemento em si, a menos que o valor ou campo que modificamos seja um ponteiro. As op\u00e7\u00f5es preferidas s\u00e3o acessar o elemento atrav\u00e9s do \u00edndice usando um range loop ou um loop for cl\u00e1ssico.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-como-os-argumentos-sao-avaliados-em-range-loops-canais-e-arrays-31","title":"Ignorando como os argumentos s\u00e3o avaliados em range loops (canais e arrays) (#31)","text":"TL;DR

    Entender que a express\u00e3o passada ao operador range \u00e9 avaliada apenas uma vez antes do in\u00edcio do loop pode ajudar a evitar erros comuns, como atribui\u00e7\u00e3o ineficiente em canal ou itera\u00e7\u00e3o de slice.

    O range loop avalia a express\u00e3o fornecida apenas uma vez, antes do in\u00edcio do loop, fazendo uma c\u00f3pia (independentemente do tipo). Devemos lembrar deste comportamento para evitar erros comuns que podem, por exemplo, nos levar a acessar o elemento errado. Por exemplo:

    a := [3]int{0, 1, 2}\nfor i, v := range a {\n    a[2] = 10\n    if i == 2 {\n        fmt.Println(v)\n    }\n}\n

    Este c\u00f3digo atualiza o \u00faltimo \u00edndice para 10. No entanto, se executarmos este c\u00f3digo, ele n\u00e3o imprimir\u00e1 10; imprime 2.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-os-impactos-do-uso-de-elementos-ponteiros-em-range-loops-32","title":"Ignorando os impactos do uso de elementos ponteiros em range loops (#32)","text":"Warning

    Este erro n\u00e3o \u00e9 mais relevante no Go 1.22 (detalhes).

    "},{"location":"pt-br/#fazendo-suposicoes-erradas-durante-as-iteracoes-de-maps-ordenacao-e-insercao-do-mapa-durante-a-iteracao-33","title":"Fazendo suposi\u00e7\u00f5es erradas durante as itera\u00e7\u00f5es de maps (ordena\u00e7\u00e3o e inser\u00e7\u00e3o do mapa durante a itera\u00e7\u00e3o) (#33)","text":"TL;DR

    Para garantir resultados previs\u00edveis ao usar maps, lembre-se de que uma estrutura de dados de mapa:

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-como-a-declaracao-break-funciona-34","title":"Ignorando como a declara\u00e7\u00e3o break funciona (#34)","text":"TL;DR

    Usar break ou continue com um r\u00f3tulo imp\u00f5e a quebra de uma instru\u00e7\u00e3o espec\u00edfica. Isso pode ser \u00fatil com instru\u00e7\u00f5es switch ou select dentro de loops.

    Uma instru\u00e7\u00e3o break \u00e9 comumente usada para encerrar a execu\u00e7\u00e3o de um loop. Quando loops s\u00e3o usados \u200b\u200bem conjunto com switch ou select, os desenvolvedores frequentemente cometem o erro de quebrar a instru\u00e7\u00e3o errada. Por exemplo:

    for i := 0; i < 5; i++ {\n    fmt.Printf(\"%d \", i)\n\n    switch i {\n    default:\n    case 2:\n        break\n    }\n}\n

    A instru\u00e7\u00e3o break n\u00e3o encerra o loop for: em vez disso, ela encerra a instru\u00e7\u00e3o switch. Portanto, em vez de iterar de 0 a 2, este c\u00f3digo itera de 0 a 4: 0 1 2 3 4.

    Uma regra essencial a ter em mente \u00e9 que uma instru\u00e7\u00e3o break encerra a execu\u00e7\u00e3o da instru\u00e7\u00e3o for, switch, ou mais interna select. No exemplo anterior, ele encerra a instru\u00e7\u00e3o switch.

    Para quebrar o loop em vez da instru\u00e7\u00e3o switch, a maneira mais idiom\u00e1tica \u00e9 usar um r\u00f3tulo:

    loop:\n    for i := 0; i < 5; i++ {\n        fmt.Printf(\"%d \", i)\n\n        switch i {\n        default:\n        case 2:\n            break loop\n        }\n    }\n

    Aqui, associamos o loopr\u00f3tulo ao for loop. Ent\u00e3o, como fornecemos o loop r\u00f3tulo para a instru\u00e7\u00e3o break, ela interrompe o loop, n\u00e3o a op\u00e7\u00e3o. Portanto, esta nova vers\u00e3o ser\u00e1 impressa 0 1 2, como esper\u00e1vamos.

    C\u00f3digo fonte

    "},{"location":"pt-br/#usando-defer-dentro-de-um-loop-35","title":"Usando defer dentro de um loop (#35)","text":"TL;DR

    Extrair a l\u00f3gica do loop dentro de uma fun\u00e7\u00e3o leva \u00e0 execu\u00e7\u00e3o de uma instru\u00e7\u00e3o defer no final de cada itera\u00e7\u00e3o.

    A instru\u00e7\u00e3o defer atrasa a execu\u00e7\u00e3o de uma chamada at\u00e9 que a fun\u00e7\u00e3o circundante retorne. \u00c9 usado principalmente para reduzir o c\u00f3digo padr\u00e3o. Por exemplo, se um recurso precisar ser fechado eventualmente, podemos usar defer para evitar a repeti\u00e7\u00e3o das chamadas de fechamento antes de cada return.

    Um erro comum com defer \u00e9 esquecer que ele agenda uma chamada de fun\u00e7\u00e3o quando a fun\u00e7\u00e3o circundante retorna. Por exemplo:

    func readFiles(ch <-chan string) error {\n    for path := range ch {\n        file, err := os.Open(path)\n        if err != nil {\n            return err\n        }\n\n        defer file.Close()\n\n        // Do something with file\n    }\n    return nil\n}\n

    As chamadas defer n\u00e3o s\u00e3o executadas durante cada itera\u00e7\u00e3o do loop, mas quando a fun\u00e7\u00e3o readFiles retorna. Se readFiles n\u00e3o retornar, os descritores de arquivos ficar\u00e3o abertos para sempre, causando vazamentos.

    Uma op\u00e7\u00e3o comum para corrigir esse problema \u00e9 criar uma fun\u00e7\u00e3o circundante ap\u00f3s defer, chamada durante cada itera\u00e7\u00e3o:

    func readFiles(ch <-chan string) error {\n    for path := range ch {\n        if err := readFile(path); err != nil {\n            return err\n        }\n    }\n    return nil\n}\n\nfunc readFile(path string) error {\n    file, err := os.Open(path)\n    if err != nil {\n        return err\n    }\n\n    defer file.Close()\n\n    // Do something with file\n    return nil\n}\n

    Outra solu\u00e7\u00e3o \u00e9 tornar a fun\u00e7\u00e3o readFile um encerramento, mas intrinsecamente, esta permanece a mesma solu\u00e7\u00e3o: adicionar outra fun\u00e7\u00e3o circundante para executar as chamadas defer durante cada itera\u00e7\u00e3o.

    C\u00f3digo fonte

    "},{"location":"pt-br/#strings","title":"Strings","text":""},{"location":"pt-br/#nao-entendendo-o-conceito-de-rune-36","title":"N\u00e3o entendendo o conceito de rune (#36)","text":"TL;DR

    Entender que uma runa corresponde ao conceito de um ponto de c\u00f3digo Unicode e que pode ser composta de m\u00faltiplos bytes deve fazer parte do conhecimento b\u00e1sico do desenvolvedor Go para trabalhar com precis\u00e3o com strings.

    Como as runas est\u00e3o por toda parte no Go, \u00e9 importante entender o seguinte:

    C\u00f3digo fonte

    "},{"location":"pt-br/#iteracao-de-string-imprecisa-37","title":"Itera\u00e7\u00e3o de string imprecisa (#37)","text":"TL;DR

    Iterar em uma string com o operador range itera nas runas com o \u00edndice correspondente ao \u00edndice inicial da sequ\u00eancia de bytes da runa. Para acessar um \u00edndice de runa espec\u00edfico (como a terceira runa), converta a string em um arquivo []rune.

    Iterar em uma string \u00e9 uma opera\u00e7\u00e3o comum para desenvolvedores. Talvez queiramos realizar uma opera\u00e7\u00e3o para cada runa na string ou implementar uma fun\u00e7\u00e3o personalizada para procurar uma substring espec\u00edfica. Em ambos os casos, temos que iterar nas diferentes runas de uma string. Mas \u00e9 f\u00e1cil ficar confuso sobre como funciona a itera\u00e7\u00e3o.

    For example, consider the following example:

    s := \"h\u00eallo\"\nfor i := range s {\n    fmt.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

    Vamos destacar tr\u00eas pontos que podem ser confusos:

    Vamos come\u00e7ar com a \u00faltima observa\u00e7\u00e3o. J\u00e1 mencionamos que len retorna o n\u00famero de bytes em uma string, n\u00e3o o n\u00famero de runas. Como atribu\u00edmos uma string literal a s, s \u00e9 uma string UTF-8. Enquanto isso, o caractere especial \u201c\u00ea\u201d n\u00e3o \u00e9 codificado em um \u00fanico byte; requer 2 bytes. Portanto, chamar len(s) retorna 6.

    Enquanto isso, no exemplo anterior, temos que entender que n\u00e3o repetimos cada runa; em vez disso, iteramos sobre cada \u00edndice inicial de uma runa:

    Imprimir s[i] n\u00e3o imprime a i-\u00e9sima runa; imprime a representa\u00e7\u00e3o UTF-8 do byte em index i. Portanto, imprimimos \"h\u00c3llo\" em vez de \"h\u00eallo\".

    Se quisermos imprimir todas as diferentes runas, podemos usar o elemento value do operador range:

    s := \"h\u00eallo\"\nfor i, r := range s {\n    fmt.Printf(\"position %d: %c\\n\", i, r)\n}\n

    Ou podemos converter a string em uma fatia de runas e iterar sobre ela:

    s := \"h\u00eallo\"\nrunes := []rune(s)\nfor i, r := range runes {\n    fmt.Printf(\"position %d: %c\\n\", i, r)\n}\n

    Observe que esta solu\u00e7\u00e3o introduz uma sobrecarga de tempo de execu\u00e7\u00e3o em compara\u00e7\u00e3o com a anterior. Na verdade, converter uma string em uma fatia de runas requer a aloca\u00e7\u00e3o de uma fatia adicional e a convers\u00e3o dos bytes em runas: uma complexidade de tempo O(n) com n o n\u00famero de bytes na string. Portanto, se quisermos iterar todas as runas, devemos usar a primeira solu\u00e7\u00e3o.

    Por\u00e9m, se quisermos acessar a i-\u00e9sima runa de uma string com a primeira op\u00e7\u00e3o, n\u00e3o teremos acesso ao \u00edndice da runa; em vez disso, conhecemos o \u00edndice inicial de uma runa na sequ\u00eancia de bytes.

    s := \"h\u00eallo\"\nr := []rune(s)[4]\nfmt.Printf(\"%c\\n\", r) // o\n

    C\u00f3digo fonte

    "},{"location":"pt-br/#uso-indevido-de-funcoes-de-trim-38","title":"Uso indevido de fun\u00e7\u00f5es de trim (#38)","text":"TL;DR

    strings.TrimRight/strings.TrimLeft remove todas as runas finais/iniciais contidas em um determinado conjunto, enquanto strings.TrimSuffix/strings.TrimPrefix retorna uma string sem um sufixo/prefixo fornecido.

    Por exemplo:

    fmt.Println(strings.TrimRight(\"123oxo\", \"xo\"))\n

    O exemplo imprime 123:

    Por outro lado, strings.TrimLeft remove todas as runas principais contidas em um conjunto.

    Por outro lado, strings.TrimSuffix/strings.TrimPrefix retorna uma string sem o sufixo/prefixo final fornecido.

    C\u00f3digo fonte

    "},{"location":"pt-br/#concatenacao-de-strings-subotimizada-39","title":"Concatena\u00e7\u00e3o de strings subotimizada (#39)","text":"TL;DR

    A concatena\u00e7\u00e3o de uma lista de strings deve ser feita com strings.Builder para evitar a aloca\u00e7\u00e3o de uma nova string durante cada itera\u00e7\u00e3o.

    Vamos considerar uma fun\u00e7\u00e3o concat que concatena todos os elementos string de uma fatia usando o operador +=:

    func concat(values []string) string {\n    s := \"\"\n    for _, value := range values {\n        s += value\n    }\n    return s\n}\n

    Durante cada itera\u00e7\u00e3o, o operador += concatena com s a sequ\u00eancia de valores. \u00c0 primeira vista, esta fun\u00e7\u00e3o pode n\u00e3o parecer errada. Mas com esta implementa\u00e7\u00e3o, esquecemos uma das principais caracter\u00edsticas de uma string: a sua imutabilidade. Portanto, cada itera\u00e7\u00e3o n\u00e3o \u00e9 atualizada s; ele realoca uma nova string na mem\u00f3ria, o que impacta significativamente o desempenho desta fun\u00e7\u00e3o.

    Felizmente, existe uma solu\u00e7\u00e3o para lidar com esse problema, usando strings.Builder:

    func concat(values []string) string {\n    sb := strings.Builder{}\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return sb.String()\n}\n

    Durante cada itera\u00e7\u00e3o, constru\u00edmos a string resultante chamando o m\u00e9todo WriteString que anexa o conte\u00fado do valor ao seu buffer interno, minimizando assim a c\u00f3pia da mem\u00f3ria.

    Note

    WriteString retorna um erro como segunda sa\u00edda, mas n\u00f3s o ignoramos propositalmente. Na verdade, este m\u00e9todo nunca retornar\u00e1 um erro diferente de zero. Ent\u00e3o, qual \u00e9 o prop\u00f3sito deste m\u00e9todo retornar um erro como parte de sua assinatura? strings.Builder implementa a io.StringWriter interface, que cont\u00e9m um \u00fanico m\u00e9todo: WriteString(s string) (n int, err error). Portanto, para estar em conformidade com esta interface, WriteString deve retornar um erro.

    Internamente, strings.Builder cont\u00e9m uma fatia de bytes. Cada chamada para WriteString resulta em uma chamada para anexar nesta fatia. Existem dois impactos. Primeiro, esta estrutura n\u00e3o deve ser usada simultaneamente, pois as chamadas append levariam a condi\u00e7\u00f5es de corrida. O segundo impacto \u00e9 algo que vimos no mistake #21, \"Inicializa\u00e7\u00e3o de slice ineficiente\": se o comprimento futuro de uma slice j\u00e1 for conhecido, devemos pr\u00e9-aloc\u00e1-la. Para isso, strings.Builder exp\u00f5e um m\u00e9todo Grow(n int) para garantir espa\u00e7o para outros n bytes:

    func concat(values []string) string {\n    total := 0\n    for i := 0; i < len(values); i++ {\n        total += len(values[i])\n    }\n\n    sb := strings.Builder{}\n    sb.Grow(total) (2)\n    for _, value := range values {\n        _, _ = sb.WriteString(value)\n    }\n    return sb.String()\n}\n

    Vamos executar um benchmark para comparar as tr\u00eas vers\u00f5es (v1 usando +=; v2 usando strings.Builder{} sem pr\u00e9-aloca\u00e7\u00e3o; e v3 usando strings.Builder{} com pr\u00e9-aloca\u00e7\u00e3o). A slice de entrada cont\u00e9m 1.000 strings e cada string cont\u00e9m 1.000 bytes:

    BenchmarkConcatV1-4             16      72291485 ns/op\nBenchmarkConcatV2-4           1188        878962 ns/op\nBenchmarkConcatV3-4           5922        190340 ns/op\n

    Como podemos ver, a vers\u00e3o mais recente \u00e9 de longe a mais eficiente: 99% mais r\u00e1pida que a v1 e 78% mais r\u00e1pida que a v2.

    strings.Builder \u00e9 a solu\u00e7\u00e3o recomendada para concatenar uma lista de strings. Normalmente, esta solu\u00e7\u00e3o deve ser usada dentro de um loop. Na verdade, se precisarmos apenas concatenar algumas strings (como um nome e um sobrenome), o uso strings.Builder n\u00e3o \u00e9 recomendado, pois isso tornar\u00e1 o c\u00f3digo um pouco menos leg\u00edvel do que usar o operador += or fmt.Sprintf.

    C\u00f3digo fonte

    "},{"location":"pt-br/#conversoes-de-string-inuteis-40","title":"Convers\u00f5es de string in\u00fateis (#40)","text":"TL;DR

    Lembrar que o pacote bytes oferece as mesmas opera\u00e7\u00f5es que o pacote strings pode ajudar a evitar convers\u00f5es extras de bytes/string.

    Ao optar por trabalhar com uma string ou um []byte, a maioria dos programadores tende a preferir strings por conveni\u00eancia. Mas a maior parte da E/S \u00e9 realmente feita com []byte. Por exemplo, io.Reader, io.Writer e io.ReadAll trabalham com []byte, n\u00e3o com strings.

    Quando nos perguntamos se devemos trabalhar com strings ou []byte, lembremos que trabalhar com []byten\u00e3o \u00e9 necessariamente menos conveniente. Na verdade, todas as fun\u00e7\u00f5es exportadas do pacote strings tamb\u00e9m possuem alternativas no pacote bytes: Split, Count, Contains, Index e assim por diante. Portanto, estejamos fazendo I/O ou n\u00e3o, devemos primeiro verificar se poder\u00edamos implementar um fluxo de trabalho completo usando bytes em vez de strings e evitar o pre\u00e7o de convers\u00f5es adicionais.

    C\u00f3digo fonte

    "},{"location":"pt-br/#vazamentos-de-substring-e-memoria-41","title":"Vazamentos de substring e mem\u00f3ria (#41)","text":"TL;DR

    Usar c\u00f3pias em vez de substrings pode evitar vazamentos de mem\u00f3ria, pois a string retornada por uma opera\u00e7\u00e3o de substring ser\u00e1 apoiada pela mesma matriz de bytes.

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#functions-and-methods","title":"Functions and Methods","text":""},{"location":"pt-br/#nao-saber-que-tipo-de-receptor-usar-42","title":"N\u00e3o saber que tipo de receptor usar (#42)","text":"TL;DR

    A decis\u00e3o de usar um valor ou um receptor de ponteiro deve ser tomada com base em fatores como o tipo, se deve sofrer muta\u00e7\u00e3o, se cont\u00e9m um campo que n\u00e3o pode ser copiado e o tamanho do objeto. Em caso de d\u00favida, use um receptor de ponteiro.

    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\n\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

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nunca-usando-parametros-de-resultado-nomeados-43","title":"Nunca usando par\u00e2metros de resultado nomeados (#43)","text":"TL;DR

    Usar par\u00e2metros de resultado nomeados pode ser uma maneira eficiente de melhorar a legibilidade de uma fun\u00e7\u00e3o/m\u00e9todo, especialmente se v\u00e1rios par\u00e2metros de resultado tiverem o mesmo tipo. Em alguns casos, esta abordagem tamb\u00e9m pode ser conveniente porque os par\u00e2metros de resultado nomeados s\u00e3o inicializados com seu valor zero. Mas tenha cuidado com os poss\u00edveis efeitos colaterais.

    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) {\n    b = a\n    return\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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#efeitos-colaterais-nao-intencionais-com-parametros-de-resultado-nomeados-44","title":"Efeitos colaterais n\u00e3o intencionais com par\u00e2metros de resultado nomeados (#44)","text":"TL;DR

    Consulte #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) (\n    lat, lng float32, err error) {\n    isValid := l.validateAddress(address) (1)\n    if !isValid {\n        return 0, 0, errors.New(\"invalid address\")\n    }\n\n    if ctx.Err() != nil { (2)\n        return 0, 0, err\n    }\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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#retornando-um-receptor-nulo-45","title":"Retornando um receptor nulo (#45)","text":"TL;DR

    Ao retornar uma interface, tenha cuidado para n\u00e3o retornar um ponteiro nulo, mas um valor nulo expl\u00edcito. Caso contr\u00e1rio, poder\u00e3o ocorrer consequ\u00eancias n\u00e3o intencionais e o chamador receber\u00e1 um valor diferente de zero.

    C\u00f3digo fonte

    "},{"location":"pt-br/#usando-um-nome-de-arquivo-como-entrada-de-funcao-46","title":"Usando um nome de arquivo como entrada de fun\u00e7\u00e3o (#46)","text":"TL;DR

    Projetar fun\u00e7\u00f5es para receber tipos io.Reader em vez de nomes de arquivos melhora a capacidade de reutiliza\u00e7\u00e3o de uma fun\u00e7\u00e3o e facilita o teste.

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-como-argumentos-defer-e-receptores-sao-avaliados-avaliacao-de-argumentos-ponteiros-e-receptores-de-valor-47","title":"Ignorando como argumentos defer e receptores s\u00e3o avaliados (avalia\u00e7\u00e3o de argumentos, ponteiros e receptores de valor) (#47)","text":"TL;DR

    Passar um ponteiro para uma fun\u00e7\u00e3o defer e agrupar uma chamada dentro de um closure s\u00e3o duas solu\u00e7\u00f5es poss\u00edveis para superar a avalia\u00e7\u00e3o imediata de argumentos e receptores.

    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 (\n    StatusSuccess  = \"success\"\n    StatusErrorFoo = \"error_foo\"\n    StatusErrorBar = \"error_bar\"\n)\n\nfunc f() error {\n    var status string\n    defer notify(status)\n    defer incrementCounter(status)\n\n    if err := foo(); err != nil {\n        status = StatusErrorFoo\n        return err\n    }\n\n    if err := bar(); err != nil {\n        status = StatusErrorBar\n        return err\n    }\n\n    status = StatusSuccess <5>\n    return 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 {\n    var status string\n    defer notify(&status) \n    defer incrementCounter(&status)\n\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 {\n    var status string\n    defer func() {\n        notify(status)\n        incrementCounter(status)\n    }()\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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#error-management","title":"Error Management","text":""},{"location":"pt-br/#panico-48","title":"P\u00e2nico (#48)","text":"TL;DR

    Usar panic \u00e9 uma op\u00e7\u00e3o para lidar com erros no Go. No entanto, s\u00f3 deve ser usado com modera\u00e7\u00e3o em condi\u00e7\u00f5es irrecuper\u00e1veis: por exemplo, para sinalizar um erro do programador ou quando voc\u00ea n\u00e3o consegue carregar uma depend\u00eancia obrigat\u00f3ria.

    In Go, panic is a built-in function that stops the ordinary flow:

    func main() {\n    fmt.Println(\"a\")\n    panic(\"foo\")\n    fmt.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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ignorando-quando-embrulhar-um-erro-49","title":"Ignorando quando embrulhar um erro (#49)","text":"TL;DR

    Embrulhar um erro permite marcar um erro e/ou fornecer contexto adicional. No entanto, o agrupamento de erros cria um acoplamento potencial, pois disponibiliza o erro de origem para o chamador. Se voc\u00ea quiser evitar isso, n\u00e3o use a agrupamento autom\u00e1tico de erros.

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#comparando-um-tipo-de-erro-de-forma-imprecisa-50","title":"Comparando um tipo de erro de forma imprecisa (#50)","text":"TL;DR

    Se voc\u00ea usar o agrupamento de erros do Go 1.13 com a diretiva %w e fmt.Errorf, a compara\u00e7\u00e3o de um erro com um tipo dever\u00e1 ser feita usando errors.As. Caso contr\u00e1rio, se o erro retornado que voc\u00ea deseja verificar for embrulhado, as verifica\u00e7\u00f5es falhar\u00e3o.

    C\u00f3digo fonte

    "},{"location":"pt-br/#comparando-um-valor-de-erro-incorretamente-51","title":"Comparando um valor de erro incorretamente (#51)","text":"TL;DR

    Se voc\u00ea usar o agrupamento de erros do Go 1.13 com a diretiva %w e fmt.Errorf, a compara\u00e7\u00e3o de um erro ou de um valor dever\u00e1 ser feita usando errors.As. Caso contr\u00e1rio, se o erro retornado que voc\u00ea deseja verificar for embrulhado, as verifica\u00e7\u00f5es falhar\u00e3o.

    A sentinel error is an error defined as a global variable:

    import \"errors\"\n\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:

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#lidando-com-um-erro-duas-vezes-52","title":"Lidando com um erro duas vezes (#52)","text":"TL;DR

    Na maioria das situa\u00e7\u00f5es, um erro deve ser tratado apenas uma vez. Registrar um erro \u00e9 tratar um erro. Portanto, voc\u00ea deve escolher entre registrar ou retornar um erro. Em muitos casos, o embrulho autom\u00e1tico de erros \u00e9 a solu\u00e7\u00e3o, pois permite fornecer contexto adicional a um erro e retornar o erro de origem.

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-tratando-de-um-erro-53","title":"N\u00e3o tratando de um erro (#53)","text":"TL;DR

    Ignorar um erro, seja durante uma chamada de fun\u00e7\u00e3o ou em uma fun\u00e7\u00e3o defer, deve ser feito explicitamente usando o identificador em branco. Caso contr\u00e1rio, os futuros leitores poder\u00e3o ficar confusos sobre se foi intencional ou um erro.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-tratando-erros-de-defer-54","title":"N\u00e3o tratando erros de defer (#54)","text":"TL;DR

    Em muitos casos, voc\u00ea n\u00e3o deve ignorar um erro retornado por uma fun\u00e7\u00e3o defer. Manipule-o diretamente ou propague-o para o chamador, dependendo do contexto. Se voc\u00ea quiser ignor\u00e1-lo, use o identificador em branco.

    Consider the following code:

    func f() {\n  // ...\n  notify() // Error handling is omitted\n}\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

    C\u00f3digo fonte

    "},{"location":"pt-br/#concurrency-foundations","title":"Concurrency: Foundations","text":""},{"location":"pt-br/#misturando-simultaneidade-e-paralelismo-55","title":"Misturando simultaneidade e paralelismo (#55)","text":"TL;DR

    Compreender as diferen\u00e7as fundamentais entre simultaneidade e paralelismo \u00e9 a base do conhecimento do desenvolvedor Go. A simultaneidade tem a ver com estrutura, enquanto o paralelismo tem a ver com execu\u00e7\u00e3o.

    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":"pt-br/#pensar-que-a-simultaneidade-e-sempre-mais-rapida-56","title":"Pensar que a simultaneidade \u00e9 sempre mais r\u00e1pida (#56)","text":"TL;DR

    Para ser um desenvolvedor proficiente, voc\u00ea deve reconhecer que a simultaneidade nem sempre \u00e9 mais r\u00e1pida. As solu\u00e7\u00f5es que envolvem a paraleliza\u00e7\u00e3o de cargas de trabalho m\u00ednimas podem n\u00e3o ser necessariamente mais r\u00e1pidas do que uma implementa\u00e7\u00e3o sequencial. A avalia\u00e7\u00e3o comparativa de solu\u00e7\u00f5es sequenciais versus solu\u00e7\u00f5es simult\u00e2neas deve ser a forma de validar suposi\u00e7\u00f5es.

    Read the full section here.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ficar-confuso-sobre-quando-usar-canais-ou-mutexes-57","title":"Ficar confuso sobre quando usar canais ou mutexes (#57)","text":"TL;DR

    Estar ciente das intera\u00e7\u00f5es goroutine tamb\u00e9m pode ser \u00fatil ao decidir entre canais e mutexes. Em geral, goroutines paralelas requerem sincroniza\u00e7\u00e3o e, portanto, mutexes. Por outro lado, goroutines simult\u00e2neas geralmente requerem coordena\u00e7\u00e3o e orquestra\u00e7\u00e3o e, portanto, canais.

    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.

    "},{"location":"pt-br/#nao-entender-os-problemas-de-corrida-corridas-de-dados-vs-condicoes-de-corrida-e-o-modelo-de-memoria-go-58","title":"N\u00e3o entender os problemas de corrida (corridas de dados vs. condi\u00e7\u00f5es de corrida e o modelo de mem\u00f3ria Go) (#58)","text":"TL;DR

    Ser proficiente em simultaneidade tamb\u00e9m significa compreender que corridas de dados e condi\u00e7\u00f5es de corrida s\u00e3o conceitos diferentes. As corridas de dados ocorrem quando v\u00e1rias goroutines acessam simultaneamente o mesmo local de mem\u00f3ria e pelo menos uma delas est\u00e1 gravando. Enquanto isso, estar livre de disputa de dados n\u00e3o significa necessariamente execu\u00e7\u00e3o determin\u00edstica. Quando um comportamento depende da sequ\u00eancia ou do tempo de eventos que n\u00e3o podem ser controlados, esta \u00e9 uma condi\u00e7\u00e3o de corrida.

    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":"pt-br/#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:

    "},{"location":"pt-br/#race-condition","title":"Race Condition","text":"

    Depending 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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-compreender-os-impactos-de-simultaneidade-de-um-tipo-de-carga-de-trabalho-59","title":"N\u00e3o compreender os impactos de simultaneidade de um tipo de carga de trabalho (#59)","text":"TL;DR

    Ao criar um determinado n\u00famero de goroutines, considere o tipo de carga de trabalho. Criar goroutines vinculadas \u00e0 CPU significa limitar esse n\u00famero pr\u00f3ximo \u00e0 vari\u00e1vel GOMAXPROCS (baseado por padr\u00e3o no n\u00famero de n\u00facleos de CPU no host). A cria\u00e7\u00e3o de goroutines vinculadas a E/S depende de outros fatores, como o sistema externo.

    In programming, the execution time of a workload is limited by one of the following:

    Note

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#incompreensao-dos-contextos-go-60","title":"Incompreens\u00e3o dos contextos Go (#60)","text":"TL;DR

    Os contextos Go tamb\u00e9m s\u00e3o um dos pilares da simultaneidade em Go. Um contexto permite que voc\u00ea carregue um prazo, um sinal de cancelamento e/ou uma lista de valores-chave.

    https://pkg.go.dev/context

    A Context carries a deadline, a cancellation signal, and other values across API boundaries.

    "},{"location":"pt-br/#deadline","title":"Deadline","text":"

    A deadline refers to a specific point in time determined with one of the following:

    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":"pt-br/#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.

    "},{"location":"pt-br/#context-values","title":"Context values","text":"

    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":"pt-br/#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,

    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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#concurrency-practice","title":"Concurrency: Practice","text":""},{"location":"pt-br/#propagando-um-contexto-improprio-61","title":"Propagando um contexto impr\u00f3prio (#61)","text":"TL;DR

    Compreender as condi\u00e7\u00f5es em que um contexto pode ser cancelado deve ser importante ao propag\u00e1-lo: por exemplo, um manipulador HTTP cancelando o contexto quando a resposta for enviada.

    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) {\n    response, err := doSomeTask(r.Context(), r)\n    if err != nil {\n        http.Error(w, err.Error(), http.StatusInternalServerError)\n    return\n    }\n    go func() {\n        err := publish(r.Context(), response)\n        // Do something with err\n    }()\n    writeResponse(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.

    Note

    From 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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#iniciando-uma-goroutine-sem-saber-quando-interrompe-la-62","title":"Iniciando uma goroutine sem saber quando interromp\u00ea-la (#62)","text":"TL;DR

    Evitar vazamentos significa estar ciente de que sempre que uma goroutine for iniciada, voc\u00ea deve ter um plano para interromp\u00ea-la eventualmente.

    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() {\n    newWatcher()\n    // Run the application\n}\n\ntype watcher struct { /* Some resources */ }\n\nfunc newWatcher() {\n    w := watcher{}\n    go 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() {\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n    newWatcher(ctx)\n    // Run the application\n}\n\nfunc newWatcher(ctx context.Context) {\n    w := watcher{}\n    go 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() {\n    w := newWatcher()\n    defer w.close()\n    // Run the application\n}\n\nfunc newWatcher() watcher {\n    w := watcher{}\n    go w.watch()\n    return w\n}\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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-ter-cuidado-com-goroutines-e-variaveis-de-loop-63","title":"N\u00e3o ter cuidado com goroutines e vari\u00e1veis \u200b\u200bde loop (#63)","text":"Warning

    Este erro n\u00e3o \u00e9 mais relevante no Go 1.22 (detalhes).

    "},{"location":"pt-br/#esperando-um-comportamento-deterministico-usando-selecao-e-canais-64","title":"Esperando um comportamento determin\u00edstico usando sele\u00e7\u00e3o e canais (#64)","text":"TL;DR

    Compreender que com select v\u00e1rios canais escolhe o caso aleatoriamente se m\u00faltiplas op\u00e7\u00f5es forem poss\u00edveis evita fazer suposi\u00e7\u00f5es erradas que podem levar a erros sutis de simultaneidade.

    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() {\n  for i := 0; i < 10; i++ {\n      messageCh <- i\n    }\n    disconnectCh <- struct{}{}\n}()\n\nfor {\n    select {\n    case v := <-messageCh:\n        fmt.Println(v)\n    case <-disconnectCh:\n        fmt.Println(\"disconnection, return\")\n        return\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 statement 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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-usar-canais-de-notificacao-65","title":"N\u00e3o usar canais de notifica\u00e7\u00e3o (#65)","text":"TL;DR

    Envie notifica\u00e7\u00f5es usando um tipo chan struct{}.

    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{}.

    "},{"location":"pt-br/#nao-usar-canais-nulos-66","title":"N\u00e3o usar canais nulos (#66)","text":"TL;DR

    O uso de canais nulos deve fazer parte do seu conjunto de ferramentas de simultaneidade porque permite remover casos de instru\u00e7\u00f5es select, por exemplo.

    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 {\n    ch := make(chan int, 1)\n\n    go func() {\n        for ch1 != nil || ch2 != nil { // Continue if at least one channel isn\u2019t nil\n            select {\n            case v, open := <-ch1:\n                if !open {\n                    ch1 = nil // Assign ch1 to a nil channel once closed\n                    break\n                }\n                ch <- v\n            case v, open := <-ch2:\n                if !open {\n                    ch2 = nil // Assigns ch2 to a nil channel once closed\n                    break\n                }\n                ch <- v\n            }\n        }\n        close(ch)\n    }()\n\n    return 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.

    C\u00f3digo fonte

    "},{"location":"pt-br/#ficar-intrigado-com-o-tamanho-do-canal-67","title":"Ficar intrigado com o tamanho do canal (#67)","text":"TL;DR

    Decida cuidadosamente o tipo de canal correto a ser usado, considerando o problema. Somente canais sem buffer oferecem fortes garantias de sincroniza\u00e7\u00e3o. Para canais em buffer, voc\u00ea deve ter um bom motivo para especificar um tamanho de canal diferente de um.

    An unbuffered channel is a channel without any capacity. It can be created by either omitting the size or providing a 0 size:

    ch1 := make(chan int)\nch2 := make(chan int, 0)\n

    With an unbuffered channel (sometimes called a synchronous channel), the sender will block until the receiver receives data from the channel.

    Conversely, a buffered channel has a capacity, and it must be created with a size greater than or equal to 1:

    ch3 := make(chan int, 1)\n

    With a buffered channel, a sender can send messages while the channel isn\u2019t full. Once the channel is full, it will block until a receiver goroutine receives a message:

    ch3 := make(chan int, 1)\nch3 <-1 // Non-blocking\nch3 <-2 // Blocking\n

    The first send isn\u2019t blocking, whereas the second one is, as the channel is full at this stage.

    What's the main difference between unbuffered and buffered channels:

    If we need a buffered channel, what size should we provide?

    The default value we should use for buffered channels is its minimum: 1. So, we may approach the problem from this standpoint: is there any good reason not to use a value of 1? Here\u2019s a list of possible cases where we should use another size:

    If we are outside of these cases, using a different channel size should be done cautiously. Let\u2019s bear in mind that deciding about an accurate queue size isn\u2019t an easy problem:

    Martin Thompson

    Queues are typically always close to full or close to empty due to the differences in pace between consumers and producers. They very rarely operate in a balanced middle ground where the rate of production and consumption is evenly matched.

    "},{"location":"pt-br/#esquecendo-os-possiveis-efeitos-colaterais-da-formatacao-de-strings-68","title":"Esquecendo os poss\u00edveis efeitos colaterais da formata\u00e7\u00e3o de strings (#68)","text":"TL;DR

    Estar ciente de que a formata\u00e7\u00e3o de strings pode levar \u00e0 chamada de fun\u00e7\u00f5es existentes significa estar atento a poss\u00edveis impasses e outras disputas de dados.

    It\u2019s pretty easy to forget the potential side effects of string formatting while working in a concurrent application.

    "},{"location":"pt-br/#etcd-data-race","title":"etcd data race","text":"

    github.com/etcd-io/etcd/pull/7816 shows an example of an issue where a map's key was formatted based on a mutable values from a context.

    "},{"location":"pt-br/#deadlock","title":"Deadlock","text":"

    Can you see what the problem is in this code with a Customer struct exposing an UpdateAge method and implementing the fmt.Stringer interface?

    type Customer struct {\n    mutex sync.RWMutex // Uses a sync.RWMutex to protect concurrent accesses\n    id    string\n    age   int\n}\n\nfunc (c *Customer) UpdateAge(age int) error {\n    c.mutex.Lock() // Locks and defers unlock as we update Customer\n    defer c.mutex.Unlock()\n\n    if age < 0 { // Returns an error if age is negative\n        return fmt.Errorf(\"age should be positive for customer %v\", c)\n    }\n\n    c.age = age\n    return nil\n}\n\nfunc (c *Customer) String() string {\n    c.mutex.RLock() // Locks and defers unlock as we read Customer\n    defer c.mutex.RUnlock()\n    return fmt.Sprintf(\"id %s, age %d\", c.id, c.age)\n}\n

    The problem here may not be straightforward. If the provided age is negative, we return an error. Because the error is formatted, using the %s directive on the receiver, it will call the String method to format Customer. But because UpdateAge already acquires the mutex lock, the String method won\u2019t be able to acquire it. Hence, this leads to a deadlock situation. If all goroutines are also asleep, it leads to a panic.

    One possible solution is to restrict the scope of the mutex lock:

    func (c *Customer) UpdateAge(age int) error {\n    if age < 0 {\n        return fmt.Errorf(\"age should be positive for customer %v\", c)\n    }\n\n    c.mutex.Lock() <1>\n    defer c.mutex.Unlock()\n\n    c.age = age\n    return nil\n}\n

    Yet, such an approach isn't always possible. In these conditions, we have to be extremely careful with string formatting.

    Another approach is to access the id field directly:

    func (c *Customer) UpdateAge(age int) error {\n    c.mutex.Lock()\n    defer c.mutex.Unlock()\n\n    if age < 0 {\n        return fmt.Errorf(\"age should be positive for customer id %s\", c.id)\n    }\n\n    c.age = age\n    return nil\n}\n

    In concurrent applications, we should remain cautious about the possible side effects of string formatting.

    C\u00f3digo fonte

    "},{"location":"pt-br/#criando-corridas-de-dados-com-acrescimo-69","title":"Criando corridas de dados com acr\u00e9scimo (#69)","text":"TL;DR

    As chamadas append nem sempre s\u00e3o isentas de disputa de dados; portanto, n\u00e3o deve ser usado simultaneamente em uma slice compartilhada.

    Should adding an element to a slice using append is data-race-free? Spoiler: it depends.

    Do you believe this example has a data race?

    s := make([]int, 1)\n\ngo func() { // In a new goroutine, appends a new element on s\n    s1 := append(s, 1)\n    fmt.Println(s1)\n}()\n\ngo func() { // Same\n    s2 := append(s, 1)\n    fmt.Println(s2)\n}()\n

    The answer is no.

    In this example, we create a slice with make([]int, 1). The code creates a one-length, one-capacity slice. Thus, because the slice is full, using append in each goroutine returns a slice backed by a new array. It doesn\u2019t mutate the existing array; hence, it doesn\u2019t lead to a data race.

    Now, let\u2019s run the same example with a slight change in how we initialize s. Instead of creating a slice with a length of 1, we create it with a length of 0 but a capacity of 1. How about this new example? Does it contain a data race?

    s := make([]int, 0, 1)\n\ngo func() { \n    s1 := append(s, 1)\n    fmt.Println(s1)\n}()\n\ngo func() {\n    s2 := append(s, 1)\n    fmt.Println(s2)\n}()\n

    The answer is yes. We create a slice with make([]int, 0, 1). Therefore, the array isn\u2019t full. Both goroutines attempt to update the same index of the backing array (index 1), which is a data race.

    How can we prevent the data race if we want both goroutines to work on a slice containing the initial elements of s plus an extra element? One solution is to create a copy of s.

    We should remember that using append on a shared slice in concurrent applications can lead to a data race. Hence, it should be avoided.

    C\u00f3digo fonte

    "},{"location":"pt-br/#usando-mutexes-imprecisamente-com-slices-e-maps-70","title":"Usando mutexes imprecisamente com slices e maps (#70)","text":"TL;DR

    Lembrar que slices e maps s\u00e3o ponteiros pode evitar corridas comuns de dados.

    Let's implement a Cache struct used to handle caching for customer balances. This struct will contain a map of balances per customer ID and a mutex to protect concurrent accesses:

    type Cache struct {\n    mu       sync.RWMutex\n    balances map[string]float64\n}\n

    Next, we add an AddBalance method that mutates the balances map. The mutation is done in a critical section (within a mutex lock and a mutex unlock):

    func (c *Cache) AddBalance(id string, balance float64) {\n    c.mu.Lock()\n    c.balances[id] = balance\n    c.mu.Unlock()\n}\n

    Meanwhile, we have to implement a method to calculate the average balance for all the customers. One idea is to handle a minimal critical section this way:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    balances := c.balances // Creates a copy of the balances map\n    c.mu.RUnlock()\n\n    sum := 0.\n    for _, balance := range balances { // Iterates over the copy, outside of the critical section\n        sum += balance\n    }\n    return sum / float64(len(balances))\n}\n

    What's the problem with this code?

    If we run a test using the -race flag with two concurrent goroutines, one calling AddBalance (hence mutating balances) and another calling AverageBalance, a data race occurs. What\u2019s the problem here?

    Internally, a map is a runtime.hmap struct containing mostly metadata (for example, a counter) and a pointer referencing data buckets. So, balances := c.balances doesn\u2019t copy the actual data. Therefore, the two goroutines perform operations on the same data set, and one mutates it. Hence, it's a data race.

    One possible solution is to protect the whole AverageBalance function:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    defer c.mu.RUnlock() // Unlocks when the function returns\n\n    sum := 0.\n    for _, balance := range c.balances {\n        sum += balance\n    }\n    return sum / float64(len(c.balances))\n}\n

    Another option, if the iteration operation isn\u2019t lightweight, is to work on an actual copy of the data and protect only the copy:

    func (c *Cache) AverageBalance() float64 {\n    c.mu.RLock()\n    m := make(map[string]float64, len(c.balances)) // Copies the map\n    for k, v := range c.balances {\n        m[k] = v\n    }\n    c.mu.RUnlock()\n\n    sum := 0.\n    for _, balance := range m {\n        sum += balance\n    }\n    return sum / float64(len(m))\n}\n

    Once we have made a deep copy, we release the mutex. The iterations are done on the copy outside of the critical section.

    In summary, we have to be careful with the boundaries of a mutex lock. In this section, we have seen why assigning an existing map (or an existing slice) to a map isn\u2019t enough to protect against data races. The new variable, whether a map or a slice, is backed by the same data set. There are two leading solutions to prevent this: protect the whole function, or work on a copy of the actual data. In all cases, let\u2019s be cautious when designing critical sections and make sure the boundaries are accurately defined.

    C\u00f3digo fonte

    "},{"location":"pt-br/#uso-indevido-syncwaitgroup-71","title":"Uso indevido sync.WaitGroup (#71)","text":"TL;DR

    Para usar com precis\u00e3o sync.WaitGroup, chame o m\u00e9todo Add antes de ativar goroutines.

    C\u00f3digo fonte

    "},{"location":"pt-br/#esquecendo-synccond-72","title":"Esquecendo sync.Cond (#72)","text":"TL;DR

    Voc\u00ea pode enviar notifica\u00e7\u00f5es repetidas para v\u00e1rios goroutines com sync.Cond.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-usando-errgroup-73","title":"N\u00e3o usando errgroup (#73)","text":"TL;DR

    Voc\u00ea pode sincronizar um grupo de goroutines e lidar com erros e contextos com o pacote errgroup.

    C\u00f3digo fonte

    "},{"location":"pt-br/#copiando-um-tipo-sync-74","title":"Copiando um tipo sync (#74)","text":"TL;DR

    Tipos sync n\u00e3o devem ser copiados.

    C\u00f3digo fonte

    "},{"location":"pt-br/#standard-library","title":"Standard Library","text":""},{"location":"pt-br/#fornecendo-uma-duracao-de-tempo-errada-75","title":"Fornecendo uma dura\u00e7\u00e3o de tempo errada (#75)","text":"TL;DR

    Seja cauteloso com fun\u00e7\u00f5es que aceitam um arquivo time.Duration. Mesmo que a passagem de um n\u00famero inteiro seja permitida, tente usar a API time para evitar qualquer poss\u00edvel confus\u00e3o.

    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 {\n    select {\n    case <-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

    C\u00f3digo fonte

    "},{"location":"pt-br/#timeafter-e-vazamentos-de-memoria-76","title":"time.After e vazamentos de mem\u00f3ria (#76)","text":"TL;DR

    Evitar chamadas para fun\u00e7\u00f5es time.After repetidas (como loops ou manipuladores HTTP) pode evitar pico de consumo de mem\u00f3ria. Os recursos criados por time.After s\u00e3o liberados somente quando o cron\u00f4metro expira.

    Developers often use time.After in loops or HTTP handlers repeatedly to implement the timing function. But it can lead to unintended peak memory consumption due to the delayed release of resources, just like the following code:

    func consumer(ch <-chan Event) {\n    for {\n        select {\n        case event := <-ch:\n            handle(event)\n        case <-time.After(time.Hour):\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    The source code of the function time.After is as follows:

    func After(d Duration) <-chan Time {\n    return NewTimer(d).C\n}\n

    As we see, it returns receive-only channel.

    When time.After is used in a loop or repeated context, a new channel is created in each iteration. If these channels are not properly closed or if their associated timers are not stopped, they can accumulate and consume memory. The resources associated with each timer and channel are only released when the timer expires or the channel is closed.

    To avoid this happening, We can use context's timeout setting instead of time.After, like below:

    func consumer(ch <-chan Event) {\n    for {\n        ctx, cancel := context.WithTimeout(context.Background(), time.Hour)\n        select {\n        case event := <-ch:\n            cancel()\n            handle(event)\n        case <-ctx.Done():\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    We can also use time.NewTimer like so:

    func consumer(ch <-chan Event) {\n    timerDuration := 1 * time.Hour\n    timer := time.NewTimer(timerDuration)\n\n    for {\n        timer.Reset(timerDuration)\n        select {\n        case event := <-ch:\n            handle(event)\n        case <-timer.C:\n            log.Println(\"warning: no messages received\")\n        }\n    }\n}\n

    C\u00f3digo fonte

    "},{"location":"pt-br/#lidando-com-erros-comuns-json-77","title":"Lidando com erros comuns JSON (#77)","text":"

    Tenha cuidado ao usar campos incorporados em estruturas Go. Fazer isso pode levar a bugs sorrateiros, como um campo time.Time incorporado que implementa a interface json.Marshaler, substituindo assim o comportamento de empacotamento padr\u00e3o.

    C\u00f3digo fonte

    Ao comparar duas estruturas time.Time, lembre-se de que time.Time cont\u00e9m um rel\u00f3gio de parede e um rel\u00f3gio monot\u00f4nico, e a compara\u00e7\u00e3o usando o operador == \u00e9 feita em ambos os rel\u00f3gios.

    C\u00f3digo fonte

    Para evitar suposi\u00e7\u00f5es erradas ao fornecer um map ao desempacotar (unmarshaling) dados JSON, lembre-se de que os valores num\u00e9ricos s\u00e3o convertidos para float64 por padr\u00e3o.

    C\u00f3digo fonte

    "},{"location":"pt-br/#erros-comuns-de-sql-78","title":"Erros comuns de SQL (#78)","text":"

    Esquecer sql.Open n\u00e3o necessariamente estabelece conex\u00f5es com um banco de dados Chame o m\u00e9todo Ping ou PingContext se precisar testar sua configura\u00e7\u00e3o e garantir que um banco de dados esteja acess\u00edvel.

    C\u00f3digo fonte

    Configure os par\u00e2metros de conex\u00e3o do banco de dados para aplicativos de n\u00edvel de produ\u00e7\u00e3o.

    O uso de instru\u00e7\u00f5es preparadas em SQL torna as consultas mais eficientes e seguras.

    C\u00f3digo fonte

    Lide com colunas anul\u00e1veis \u200b\u200bem tabelas usando ponteiros ou tipos sql.NullXXX.

    C\u00f3digo fonte

    Chame o m\u00e9todo Err de sql.Rows itera\u00e7\u00f5es posteriores \u00e0 linha para garantir que voc\u00ea n\u00e3o perdeu nenhum erro ao preparar a pr\u00f3xima linha.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-fechando-recursos-transitorios-body-http-sqlrows-e-osfile-79","title":"N\u00e3o fechando recursos transit\u00f3rios (body HTTP, sql.Rows e os.File) (#79)","text":"TL;DR

    Eventualmente feche todas as estruturas implementadas io.Closer para evitar poss\u00edveis vazamentos.

    C\u00f3digo fonte

    "},{"location":"pt-br/#esquecendo-a-instrucao-return-apos-responder-a-uma-solicitacao-http-80","title":"Esquecendo a instru\u00e7\u00e3o return ap\u00f3s responder a uma solicita\u00e7\u00e3o HTTP (#80)","text":"TL;DR

    Para evitar comportamentos inesperados nas implementa\u00e7\u00f5es do manipulador HTTP, certifique-se de n\u00e3o perder a instru\u00e7\u00e3o return se quiser que um manipulador pare ap\u00f3s http.Error.

    Consider the following HTTP handler that handles an error from foo using http.Error:

    func handler(w http.ResponseWriter, req *http.Request) {\n    err := foo(req)\n    if err != nil {\n        http.Error(w, \"foo\", http.StatusInternalServerError)\n    }\n\n    _, _ = w.Write([]byte(\"all good\"))\n    w.WriteHeader(http.StatusCreated)\n}\n

    If we run this code and err != nil, the HTTP response would be:

    foo\nall good\n

    The response contains both the error and success messages, and also the first HTTP status code, 500. There would also be a warning log indicating that we attempted to write the status code multiple times:

    2023/10/10 16:45:33 http: superfluous response.WriteHeader call from main.handler (main.go:20)\n

    The mistake in this code is that http.Error does not stop the handler's execution, which means the success message and status code get written in addition to the error. Beyond an incorrect response, failing to return after writing an error can lead to the unwanted execution of code and unexpected side-effects. The following code adds the return statement following the http.Error and exhibits the desired behavior when ran:

    func handler(w http.ResponseWriter, req *http.Request) {\n    err := foo(req)\n    if err != nil {\n        http.Error(w, \"foo\", http.StatusInternalServerError)\n        return // Adds the return statement\n    }\n\n    _, _ = w.Write([]byte(\"all good\"))\n    w.WriteHeader(http.StatusCreated)\n}\n

    C\u00f3digo fonte

    "},{"location":"pt-br/#usando-o-cliente-e-servidor-http-padrao-81","title":"Usando o cliente e servidor HTTP padr\u00e3o (#81)","text":"TL;DR

    Para aplicativos de n\u00edvel de produ\u00e7\u00e3o, n\u00e3o use as implementa\u00e7\u00f5es de cliente e servidor HTTP padr\u00e3o. Essas implementa\u00e7\u00f5es n\u00e3o possuem tempos limite e comportamentos que deveriam ser obrigat\u00f3rios na produ\u00e7\u00e3o.

    C\u00f3digo fonte

    "},{"location":"pt-br/#teste","title":"Teste","text":""},{"location":"pt-br/#nao-categorizar-testes-tags-de-construcao-variaveis-de-ambiente-e-modo-abreviado-82","title":"N\u00e3o categorizar testes (tags de constru\u00e7\u00e3o, vari\u00e1veis \u200b\u200bde ambiente e modo abreviado) (#82)","text":"TL;DR

    Categorizar testes usando sinalizadores de constru\u00e7\u00e3o, vari\u00e1veis \u200b\u200bde ambiente ou modo curto torna o processo de teste mais eficiente. Voc\u00ea pode criar categorias de teste usando sinalizadores de constru\u00e7\u00e3o ou vari\u00e1veis \u200b\u200bde ambiente (por exemplo, testes de unidade versus testes de integra\u00e7\u00e3o) e diferenciar testes curtos de testes de longa dura\u00e7\u00e3o para decidir quais tipos de testes executar.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-habilitando-a-bandeira-de-corrida-83","title":"N\u00e3o habilitando a bandeira de corrida (#83)","text":"TL;DR

    A ativa\u00e7\u00e3o do sinalizador -race \u00e9 altamente recomendada ao escrever aplicativos simult\u00e2neos. Isso permite que voc\u00ea detecte poss\u00edveis corridas de dados que podem levar a bugs de software.

    In Go, the race detector isn\u2019t a static analysis tool used during compilation; instead, it\u2019s a tool to find data races that occur at runtime. To enable it, we have to enable the -race flag while compiling or running a test. For example:

    go test -race ./...\n

    Once the race detector is enabled, the compiler instruments the code to detect data races. Instrumentation refers to a compiler adding extra instructions: here, tracking all memory accesses and recording when and how they occur.

    Enabling the race detector adds an overhead in terms of memory and execution time; hence, it's generally recommended to enable it only during local testing or continuous integration, not production.

    If a race is detected, Go raises a warning. For example:

    package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    i := 0\n    go func() { i++ }()\n    fmt.Println(i)\n}\n

    Runnig this code with the -race logs the following warning:

    ==================\nWARNING: DATA RACE\nWrite at 0x00c000026078 by goroutine 7: # (1)\n  main.main.func1()\n      /tmp/app/main.go:9 +0x4e\n\nPrevious read at 0x00c000026078 by main goroutine: # (2)\n  main.main()\n      /tmp/app/main.go:10 +0x88\n\nGoroutine 7 (running) created at: # (3)\n  main.main()\n      /tmp/app/main.go:9 +0x7a\n==================\n
    1. Indicates that goroutine 7 was writing
    2. Indicates that the main goroutine was reading
    3. Indicates when the goroutine 7 was created

    Let\u2019s make sure we are comfortable reading these messages. Go always logs the following:

    In addition, if a specific file contains tests that lead to data races, we can exclude it from race detection using the !race build tag:

    //go:build !race\n\npackage main\n\nimport (\n    \"testing\"\n)\n\nfunc TestFoo(t *testing.T) {\n    // ...\n}\n
    "},{"location":"pt-br/#nao-usar-modos-de-execucao-de-teste-paralelo-e-aleatorio-84","title":"N\u00e3o usar modos de execu\u00e7\u00e3o de teste (paralelo e aleat\u00f3rio) (#84)","text":"TL;DR

    Usar o sinalizador -parallel \u00e9 uma forma eficiente de acelerar testes, especialmente os de longa dura\u00e7\u00e3o. Use o sinalizador -shuffle para ajudar a garantir que um conjunto de testes n\u00e3o se baseie em suposi\u00e7\u00f5es erradas que possam ocultar bugs.

    "},{"location":"pt-br/#nao-usar-testes-baseados-em-tabela-85","title":"N\u00e3o usar testes baseados em tabela (#85)","text":"TL;DR

    Os testes baseados em tabelas s\u00e3o uma maneira eficiente de agrupar um conjunto de testes semelhantes para evitar a duplica\u00e7\u00e3o de c\u00f3digo e facilitar o manuseio de atualiza\u00e7\u00f5es futuras.

    C\u00f3digo fonte

    "},{"location":"pt-br/#dormindo-em-testes-unitarios-86","title":"Dormindo em testes unit\u00e1rios (#86)","text":"TL;DR

    Evite interrup\u00e7\u00f5es usando a sincroniza\u00e7\u00e3o para tornar o teste menos inst\u00e1vel e mais robusto. Se a sincroniza\u00e7\u00e3o n\u00e3o for poss\u00edvel, considere uma abordagem de nova tentativa.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-lidar-com-a-api-de-tempo-de-forma-eficiente-87","title":"N\u00e3o lidar com a API de tempo de forma eficiente (#87)","text":"TL;DR

    Entender como lidar com fun\u00e7\u00f5es usando a API time \u00e9 outra maneira de tornar um teste menos complicado. Voc\u00ea pode usar t\u00e9cnicas padr\u00e3o, como lidar com o tempo como parte de uma depend\u00eancia oculta ou solicitar que os clientes o forne\u00e7am.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-usar-pacotes-de-utilitarios-de-teste-httptest-e-iotest-88","title":"N\u00e3o usar pacotes de utilit\u00e1rios de teste ( httptest e iotest) (#88)","text":"

    C\u00f3digo fonte

    C\u00f3digo fonte

    "},{"location":"pt-br/#escrevendo-benchmarks-imprecisos-89","title":"Escrevendo benchmarks imprecisos (#89)","text":"TL;DR

    Regarding benchmarks:

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-explorando-todos-os-recursos-de-teste-do-go-90","title":"N\u00e3o explorando todos os recursos de teste do Go (#90)","text":"

    Use a cobertura de c\u00f3digo com o sinalizador -coverprofile para ver rapidamente qual parte do c\u00f3digo precisa de mais aten\u00e7\u00e3o.

    Coloque os testes unit\u00e1rios em um pacote diferente para impor testes de escrita que se concentrem em um comportamento exposto, n\u00e3o em internos.

    C\u00f3digo fonte

    O tratamento de erros usando a vari\u00e1vel *testing.T em vez do cl\u00e1ssico if err != nil torna o c\u00f3digo mais curto e f\u00e1cil de ler.

    C\u00f3digo fonte

    Voc\u00ea pode usar fun\u00e7\u00f5es de setup e teardown para configurar um ambiente complexo, como no caso de testes de integra\u00e7\u00e3o.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-usar-fuzzing-erro-da-comunidade","title":"N\u00e3o usar fuzzing (erro da comunidade)","text":"TL;DR

    Fuzzing \u00e9 uma estrat\u00e9gia eficiente para detectar entradas aleat\u00f3rias, inesperadas ou malformadas em fun\u00e7\u00f5es e m\u00e9todos complexos, a fim de descobrir vulnerabilidades, bugs ou at\u00e9 mesmo travamentos potenciais.

    Credits: @jeromedoucet

    "},{"location":"pt-br/#otimizacoes","title":"Otimiza\u00e7\u00f5es","text":""},{"location":"pt-br/#nao-entendendo-os-caches-da-cpu-91","title":"N\u00e3o entendendo os caches da CPU (#91)","text":"

    Compreender como usar caches de CPU \u00e9 importante para otimizar aplicativos vinculados \u00e0 CPU porque o cache L1 \u00e9 cerca de 50 a 100 vezes mais r\u00e1pido que a mem\u00f3ria principal.

    Estar consciente do conceito de linha de cache \u00e9 fundamental para entender como organizar dados em aplicativos com uso intensivo de dados. Uma CPU n\u00e3o busca mem\u00f3ria palavra por palavra; em vez disso, geralmente copia um bloco de mem\u00f3ria para uma linha de cache de 64 bytes. Para aproveitar ao m\u00e1ximo cada linha de cache individual, imponha a localidade espacial.

    C\u00f3digo fonte

    C\u00f3digo fonte

    Tornar o c\u00f3digo previs\u00edvel para a CPU tamb\u00e9m pode ser uma forma eficiente de otimizar certas fun\u00e7\u00f5es. Por exemplo, uma passada unit\u00e1ria ou constante \u00e9 previs\u00edvel para a CPU, mas uma passada n\u00e3o unit\u00e1ria (por exemplo, uma lista vinculada) n\u00e3o \u00e9 previs\u00edvel.

    C\u00f3digo fonte

    Para evitar um avan\u00e7o cr\u00edtico e, portanto, utilizar apenas uma pequena parte do cache, esteja ciente de que os caches s\u00e3o particionados.

    "},{"location":"pt-br/#escrevendo-codigo-simultaneo-que-leva-a-compartilhamento-falso-92","title":"Escrevendo c\u00f3digo simult\u00e2neo que leva a compartilhamento falso (#92)","text":"TL;DR

    Saber que n\u00edveis mais baixos de caches de CPU n\u00e3o s\u00e3o compartilhados entre todos os n\u00facleos ajuda a evitar padr\u00f5es que degradam o desempenho, como compartilhamento falso ao escrever c\u00f3digo de simultaneidade. Compartilhar mem\u00f3ria \u00e9 uma ilus\u00e3o.

    Leia a se\u00e7\u00e3o completa aqui.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-levando-em-consideracao-o-paralelismo-no-nivel-de-instrucao-93","title":"N\u00e3o levando em considera\u00e7\u00e3o o paralelismo no n\u00edvel de instru\u00e7\u00e3o (#93)","text":"TL;DR

    Use o ILP para otimizar partes espec\u00edficas do seu c\u00f3digo para permitir que uma CPU execute tantas instru\u00e7\u00f5es paralelas quanto poss\u00edvel. Identificar perigos nos dados \u00e9 uma das etapas principais.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-estar-ciente-do-alinhamento-dos-dados-94","title":"N\u00e3o estar ciente do alinhamento dos dados (#94)","text":"TL;DR

    Voc\u00ea pode evitar erros comuns lembrando que no Go os tipos b\u00e1sicos s\u00e3o alinhados com seu pr\u00f3prio tamanho. Por exemplo, tenha em mente que reorganizar os campos de uma estrutura por tamanho em ordem decrescente pode levar a estruturas mais compactas (menos aloca\u00e7\u00e3o de mem\u00f3ria e potencialmente uma melhor localidade espacial).

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-entendendo-stack-vs-heap-95","title":"N\u00e3o entendendo stack vs. heap (#95)","text":"TL;DR

    Compreender as diferen\u00e7as fundamentais entre heap e pilha tamb\u00e9m deve fazer parte do seu conhecimento b\u00e1sico ao otimizar um aplicativo Go. As aloca\u00e7\u00f5es de pilha s\u00e3o quase gratuitas, enquanto as aloca\u00e7\u00f5es de heap s\u00e3o mais lentas e dependem do GC para limpar a mem\u00f3ria.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-saber-como-reduzir-alocacoes-mudanca-de-api-otimizacoes-de-compilador-e-syncpool-96","title":"N\u00e3o saber como reduzir aloca\u00e7\u00f5es (mudan\u00e7a de API, otimiza\u00e7\u00f5es de compilador e sync.Pool) (#96)","text":"TL;DR

    A redu\u00e7\u00e3o das aloca\u00e7\u00f5es tamb\u00e9m \u00e9 um aspecto essencial da otimiza\u00e7\u00e3o de um aplicativo Go. Isso pode ser feito de diferentes maneiras, como projetar a API cuidadosamente para evitar compartilhamento, compreender as otimiza\u00e7\u00f5es comuns do compilador Go e usar sync.Pool.

    C\u00f3digo fonte

    "},{"location":"pt-br/#nao-dependendo-do-inlining-97","title":"N\u00e3o dependendo do inlining (#97)","text":"TL;DR

    Use a t\u00e9cnica de inlining de caminho r\u00e1pido para reduzir com efici\u00eancia o tempo amortizado para chamar uma fun\u00e7\u00e3o.

    "},{"location":"pt-br/#nao-usar-ferramentas-de-diagnostico-go-98","title":"N\u00e3o usar ferramentas de diagn\u00f3stico Go (#98)","text":"TL;DR

    Confie na cria\u00e7\u00e3o de perfil e no rastreador de execu\u00e7\u00e3o para entender o desempenho de um aplicativo e as partes a serem otimizadas.

    Leia a se\u00e7\u00e3o completa aqui.

    "},{"location":"pt-br/#nao-entendendo-como-funciona-o-gc-99","title":"N\u00e3o entendendo como funciona o GC (#99)","text":"TL;DR

    Compreender como ajustar o GC pode levar a v\u00e1rios benef\u00edcios, como lidar com aumentos repentinos de carga com mais efici\u00eancia.

    "},{"location":"pt-br/#nao-entendendo-os-impactos-da-execucao-do-go-no-docker-e-kubernetes-100","title":"N\u00e3o entendendo os impactos da execu\u00e7\u00e3o do Go no Docker e Kubernetes (#100)","text":"TL;DR

    Para ajudar a evitar a limita\u00e7\u00e3o da CPU quando implantado no Docker e no Kubernetes, lembre-se de que Go n\u00e3o reconhece CFS.

    By default, GOMAXPROCS is set to the number of OS-apparent logical CPU cores.

    When running some Go code inside Docker and Kubernetes, we must know that Go isn't CFS-aware (github.com/golang/go/issues/33803). Therefore, GOMAXPROCS isn't automatically set to the value of spec.containers.resources.limits.cpu (see Kubernetes Resource Management for Pods and Containers); instead, it's set to the number of logical cores on the host machine. The main implication is that it can lead to an increased tail latency in some specific situations.

    One solution is to rely on uber-go/automaxprocs that automatically set GOMAXPROCS to match the Linux container CPU quota.

    "},{"location":"pt-br/#community","title":"Community","text":"

    Thanks to all the contributors:

    "},{"location":"zh/","title":"100 \u4e2a Go \u5e38\u89c1\u9519\u8bef\u53ca\u5982\u4f55\u907f\u514d","text":"Jobs

    Is your company hiring? Sponsor the Chinese version of this repository and let a significant audience of Go developers (~1k unique visitors per week) know about your opportunities in this section.

    "},{"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\u7528 init \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\u7528 getters/setters (#4)","text":"

    \u5728 Go \u8bed\u8a00\u4e2d\uff0c\u5f3a\u5236\u4f7f\u7528 getter \u548c setter \u65b9\u6cd5\u5e76\u4e0d\u7b26\u5408 Go \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

    "},{"location":"zh/#9","title":"\u56f0\u60d1\u4f55\u65f6\u8be5\u7528\u8303\u578b (#9)","text":"

    \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\u9732\u3002

    "},{"location":"zh/#function-option-11","title":"\u4e0d\u4f7f\u7528 function option \u6a21\u5f0f (#11)","text":"

    \u4e3a\u4e86\u8bbe\u8ba1\u5e76\u63d0\u4f9b\u66f4\u53cb\u597d\u7684 API\uff08\u53ef\u9009\u53c2\u6570\uff09\uff0c\u4e3a\u4e86\u66f4\u597d\u5730\u5904\u7406\u9009\u9879\uff0c\u5e94\u8be5\u4f7f\u7528 function 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\u7ec7 Go \u5de5\u7a0b\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u65b9\u6cd5\uff0c\u5c24\u5176\u662f\u4f60\u6b63\u5728\u5bfb\u627e\u4e00\u4e9b\u7c7b\u4f3c\u7684\u7ecf\u9a8c\u3001\u60ef\u4f8b\u6765\u7ec4\u7ec7\u4e00\u4e2a\u65b0\u7684 Go \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\u5177\u4f53\u7684\u5305\u540d\u3002

    "},{"location":"zh/#14","title":"\u5ffd\u7565\u4e86\u5305\u540d\u51b2\u7a81 (#14)","text":"

    \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' \u4ee5\u533a\u5206\u5305\u540d\u548c\u53d8\u91cf\u540d\uff0c\u6216\u8005\u8003\u8651\u4e00\u4e2a\u66f4\u597d\u7684\u53d8\u91cf\u540d\u3002

    "},{"location":"zh/#15","title":"\u4ee3\u7801\u7f3a\u5c11\u6587\u6863 (#15)","text":"

    \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\u52a0\u6ce8\u91ca\u3002

    "},{"location":"zh/#linters-16","title":"\u4e0d\u4f7f\u7528 linters \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\u7528 linters \u548c formatters\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\u7684 delta \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\u89e3 slice \u7684\u957f\u5ea6\u548c\u5bb9\u91cf (#20)","text":"

    \u7406\u89e3 slice \u7684\u957f\u5ea6\u548c\u5bb9\u91cf\u7684\u533a\u522b\uff0c\u662f\u4e00\u4e2a Go \u5f00\u53d1\u8005\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\u4e4b\u4e00\u3002slice \u7684\u957f\u5ea6\u6307\u7684\u662f slice \u5df2\u7ecf\u5b58\u50a8\u7684\u5143\u7d20\u7684\u6570\u91cf\uff0c\u800c\u5bb9\u91cf\u6307\u7684\u662f slice \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\u7684 slice \u521d\u59cb\u5316 (#21)","text":"

    \u5f53\u521b\u5efa\u4e00\u4e2a slice \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\u671f append \u65f6\u4e00\u6b21\u6216\u8005\u591a\u6b21\u7684\u5185\u5b58\u5206\u914d\u64cd\u4f5c\uff0c\u4ece\u800c\u6539\u5584\u6027\u80fd\u3002\u5bf9\u4e8e map \u7684\u521d\u59cb\u5316\u4e5f\u662f\u5982\u6b64\u3002

    "},{"location":"zh/#nil-slice-22","title":"\u56f0\u60d1\u4e8e nil \u548c\u7a7a slice (#22)","text":"

    \u4e3a\u4e86\u907f\u514d\u5e38\u89c1\u7684\u5bf9 nil \u548c empty 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\u67e5 slice \u662f\u5426\u4e3a\u7a7a (#23)","text":"

    \u68c0\u67e5\u4e00\u4e2a slice \u662f\u5426\u5305\u542b\u4efb\u4f55\u5143\u7d20\uff0c\u53ef\u4ee5\u68c0\u67e5\u5176\u957f\u5ea6\uff0c\u4e0d\u7ba1 slice \u662f nil \u8fd8\u662f empty\uff0c\u68c0\u67e5\u957f\u5ea6\u90fd\u662f\u6709\u6548\u7684\u3002\u8fd9\u4e2a\u68c0\u67e5\u65b9\u6cd5\u4e5f\u9002\u7528\u4e8e map\u3002

    \u4e3a\u4e86\u8bbe\u8ba1\u66f4\u660e\u786e\u7684 API\uff0cAPI \u4e0d\u5e94\u533a\u5206 nil \u548c\u7a7a\u5207\u7247\u3002

    "},{"location":"zh/#slice-24","title":"\u6ca1\u6709\u6b63\u786e\u62f7\u8d1d slice (#24)","text":"

    \u4f7f\u7528 copy \u62f7\u8d1d\u4e00\u4e2a slice \u5143\u7d20\u5230\u53e6\u4e00\u4e2a slice \u65f6\uff0c\u9700\u8981\u8bb0\u5f97\uff0c\u5b9e\u9645\u62f7\u8d1d\u7684\u5143\u7d20\u6570\u91cf\u662f\u4e8c\u8005 slice \u957f\u5ea6\u4e2d\u7684\u8f83\u5c0f\u503c\u3002

    "},{"location":"zh/#slice-append-25","title":"slice append \u5e26\u6765\u7684\u9884\u671f\u4e4b\u5916\u7684\u526f\u4f5c\u7528 (#25)","text":"

    \u5982\u679c\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u64cd\u4f5c\u7684 slice \u590d\u7528\u4e86\u76f8\u540c\u7684\u5e95\u5c42\u6570\u7ec4\uff0c\u5b83\u4eec\u5bf9 slice \u6267\u884c append \u64cd\u4f5c\u65f6\u53ef\u80fd\u4f1a\u4ea7\u751f\u51b2\u7a81\u3002\u4f7f\u7528 copy \u6765\u5b8c\u6574\u590d\u5236\u4e00\u4e2a slice \u6216\u8005\u4f7f\u7528\u5b8c\u6574\u7684 slice \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\u5927 slice \u8fdb\u884c shrink \u64cd\u4f5c\u65f6\uff0c\u4e24\u79cd\u65b9\u5f0f\u4e2d\uff0c\u53ea\u6709 copy \u624d\u53ef\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002

    "},{"location":"zh/#slice-26","title":"slice \u548c\u5185\u5b58\u6cc4\u6f0f (#26)","text":"

    \u5bf9\u4e8e slice \u5143\u7d20\u4e3a\u6307\u9488\uff0c\u6216\u8005 slice \u5143\u7d20\u4e3a struct \u4f46\u662f\u8be5 struct \u542b\u6709\u6307\u9488\u5b57\u6bb5\uff0c\u5f53\u901a\u8fc7 slice[low:high] \u64cd\u4f5c\u53d6 subslice \u65f6\uff0c\u5bf9\u4e8e\u90a3\u4e9b\u4e0d\u53ef\u8bbf\u95ee\u7684\u5143\u7d20\u53ef\u4ee5\u663e\u5f0f\u8bbe\u7f6e\u4e3a nil \u6765\u907f\u514d\u5185\u5b58\u6cc4\u9732\u3002

    "},{"location":"zh/#map-27","title":"\u4e0d\u9ad8\u6548\u7684 map \u521d\u59cb\u5316 (#27)","text":"

    \u89c1 #21.

    "},{"location":"zh/#map-28","title":"map \u548c\u5185\u5b58\u6cc4\u6f0f (#28)","text":"

    \u4e00\u4e2a map \u7684 buckets \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\u65b9\u5f0f\u6765\u89e3\u51b3\uff0c\u6bd4\u5982\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a map \u4ee3\u66ff\u539f\u6765\u7684\uff08\u539f\u6765\u7684 map \u4f1a\u88ab GC \u6389\uff09\uff0c\u6216\u8005 map[keyType]valueType \u4e2d\u7684 valueType \u4f7f\u7528\u6307\u9488\u4ee3\u66ff\u957f\u5ea6\u56fa\u5b9a\u7684\u6570\u7ec4\u6216\u8005 sliceHeader \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\u7684 structs\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

    "},{"location":"zh/#_3","title":"\u63a7\u5236\u7ed3\u6784","text":""},{"location":"zh/#range-30","title":"\u5ffd\u7565\u4e86 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\u4e2a struct \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\u7684 for \u5faa\u73af+\u7d22\u5f15\u503c\u7684\u5199\u6cd5\uff08\u9664\u975e\u904d\u5386\u7684\u5143\u7d20\u662f\u4e00\u4e2a\u6307\u9488\uff09\u3002

    "},{"location":"zh/#range-channels-arrays-31","title":"\u5ffd\u7565\u4e86 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\u7684 channel \u8d4b\u503c\u64cd\u4f5c\u548c slice \u8fed\u4ee3\u64cd\u4f5c\u3002

    "},{"location":"zh/#range-32","title":"\u5ffd\u7565\u4e86 range \u5faa\u73af\u4e2d\u6307\u9488\u5143\u7d20\u7684\u5f71\u54cd (#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\u3002\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\u662f for \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\u7684 bug\u3002

    "},{"location":"zh/#map-33","title":"map \u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u7684\u9519\u8bef\u5047\u8bbe\uff08\u904d\u5386\u987a\u5e8f\u548c\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u63d2\u5165\uff09(#33)","text":"

    \u4f7f\u7528 map \u65f6\uff0c\u4e3a\u4e86\u80fd\u5f97\u5230\u786e\u5b9a\u4e00\u81f4\u7684\u7ed3\u679c\uff0c\u5e94\u8be5\u8bb0\u4f4f Go \u4e2d\u7684 map \u6570\u636e\u7ed3\u6784\uff1a * \u4e0d\u4f1a\u6309\u7167 key \u5bf9 data \u8fdb\u884c\u6392\u5e8f\uff0c\u904d\u5386\u65f6 key \u4e0d\u662f\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\u4e86 break \u8bed\u53e5\u662f\u5982\u4f55\u5de5\u4f5c\u7684 (#34)","text":"

    \u914d\u5408 label \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 \u548c select \u8bed\u53e5\u7684\u573a\u666f\u4e2d\u5c31\u6bd4\u8f83\u6709\u5e2e\u52a9\u3002

    "},{"location":"zh/#defer-35","title":"\u5728\u5faa\u73af\u4e2d\u4f7f\u7528 defer (#35)","text":"

    \u5728\u5faa\u73af\u4e2d\u4f7f\u7528 defer \u4e0d\u80fd\u5728\u6bcf\u8f6e\u8fed\u4ee3\u7ed3\u675f\u65f6\u6267\u884c defer \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\u89e3 rune (#36)","text":"

    \u7406\u89e3 rune \u7c7b\u578b\u5bf9\u5e94\u7684\u662f\u4e00\u4e2a unicode \u7801\u70b9\uff0c\u6bcf\u4e00\u4e2a unicode \u7801\u70b9\u5176\u5b9e\u662f\u4e00\u4e2a\u591a\u5b57\u8282\u7684\u5e8f\u5217\uff0c\u4e0d\u662f\u4e00\u4e2a byte\u3002\u8fd9\u5e94\u8be5\u662f Go \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\u4e2a string \u8fdb\u884c\u904d\u5386\u5b9e\u9645\u4e0a\u662f\u5bf9 string \u5bf9\u5e94\u7684 []rune \u8fdb\u884c\u904d\u5386\uff0c\u8fed\u4ee3\u53d8\u91cf\u4e2d\u7684\u7d22\u5f15\u503c\uff0c\u8868\u793a\u7684\u5f53\u524d rune \u5bf9\u5e94\u7684 []rune \u5728\u6574\u4e2a []rune(string) \u4e2d\u7684\u8d77\u59cb\u7d22\u5f15\u3002\u5982\u679c\u8981\u8bbf\u95ee string \u4e2d\u7684\u67d0\u4e00\u4e2a rune\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

    "},{"location":"zh/#trim-38","title":"\u8bef\u7528 trim \u51fd\u6570 (#38)","text":"

    strings.TrimRight/strings.TrimLeft \u79fb\u9664\u5728\u5b57\u7b26\u4e32\u5c3e\u90e8\u6216\u8005\u5f00\u5934\u51fa\u73b0\u7684\u4e00\u4e9b runes\uff0c\u51fd\u6570\u4f1a\u6307\u5b9a\u4e00\u4e2a rune \u96c6\u5408\uff0c\u51fa\u73b0\u5728\u96c6\u5408\u4e2d\u7684 rune \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

    "},{"location":"zh/#39","title":"\u4e0d\u7ecf\u4f18\u5316\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u64cd\u4f5c (#39)","text":"

    \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\u7684 string \u5bf9\u8c61\u51fa\u6765\u3002

    "},{"location":"zh/#40","title":"\u65e0\u7528\u7684\u5b57\u7b26\u4e32\u8f6c\u6362 (#40)","text":"

    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

    "},{"location":"zh/#41","title":"\u5b50\u5b57\u7b26\u4e32\u548c\u5185\u5b58\u6cc4\u6f0f (#41)","text":"

    \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\u7684 s[low:high] \u64cd\u4f5c\u8fd4\u56de\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5176\u4f7f\u7528\u4e86\u548c\u539f\u5b57\u7b26\u4e32 s \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\u7528 value \u7c7b\u578b\u8fd8\u662f pointer \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\u7528 pointer \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

    "},{"location":"zh/#nil-45","title":"\u8fd4\u56de\u4e00\u4e2a nil \u63a5\u6536\u5668 (#45)","text":"

    \u5f53\u8fd4\u56de\u4e00\u4e2a interface \u53c2\u6570\u65f6\uff0c\u9700\u8981\u5c0f\u5fc3\uff0c\u4e0d\u8981\u8fd4\u56de\u4e00\u4e2a nil \u6307\u9488\uff0c\u800c\u662f\u5e94\u8be5\u663e\u5f0f\u8fd4\u56de\u4e00\u4e2a nil \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\u975e nil \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

    "},{"location":"zh/#defer-value-47","title":"\u5ffd\u7565 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\u5bf9 defer \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

    "},{"location":"zh/#_6","title":"\u9519\u8bef\u7ba1\u7406","text":""},{"location":"zh/#panicking-48","title":"Panicking (#48)","text":"

    \u4f7f\u7528 panic \u662f Go \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

    "},{"location":"zh/#error-49","title":"\u672a\u8003\u8651\u4f55\u65f6\u624d\u5e94\u8be5\u5305\u88c5 error (#49)","text":"

    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\u7684 error \u5bf9\u8c61\u5c31\u9700\u8981\u7528 errors.Is\u3002

    "},{"location":"zh/#51","title":"\u4e0d\u6b63\u786e\u7684\u9519\u8bef\u5bf9\u8c61\u503c\u6bd4\u8f83 (#51)","text":"

    \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

    "},{"location":"zh/#52","title":"\u4e24\u6b21\u5904\u7406\u540c\u4e00\u4e2a\u9519\u8bef (#52)","text":"

    \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\u5f0f\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

    "},{"location":"zh/#defer-54","title":"\u4e0d\u5904\u7406 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\u5f0f\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\u5f0f\u4f7f\u7528 _ \u6765\u5ffd\u7565\u3002

    "},{"location":"zh/#_7","title":"\u5e76\u53d1\u7f16\u7a0b: \u57fa\u7840","text":""},{"location":"zh/#55","title":"\u6df7\u6dc6\u5e76\u53d1\u548c\u5e76\u884c (#55)","text":"

    \u7406\u89e3\u5e76\u53d1\uff08concurrency\uff09\u3001\u5e76\u884c\uff08parallelism\uff09\u4e4b\u95f4\u7684\u672c\u8d28\u533a\u522b\u662f Go \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\u884c benchmark \u6d4b\u8bd5\uff0c\u662f\u9a8c\u8bc1\u5047\u8bbe\u7684\u597d\u529e\u6cd5\u3002

    "},{"location":"zh/#channels-mutexes-57","title":"\u4e0d\u6e05\u695a\u4f55\u65f6\u4f7f\u7528 channels \u6216 mutexes (#57)","text":"

    \u4e86\u89e3 goroutine \u4e4b\u95f4\u7684\u4ea4\u4e92\u4e5f\u53ef\u4ee5\u5728\u9009\u62e9\u4f7f\u7528 channels \u6216 mutexes \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\u7528 mutexes\u3002\u76f8\u53cd\uff0c\u5e76\u53d1\u7684 goroutine \u901a\u5e38\u9700\u8981\u534f\u8c03\u548c\u7f16\u6392\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528 channels\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\u4e2a goroutines \u540c\u65f6\u8bbf\u95ee\u76f8\u540c\u5185\u5b58\u533a\u57df\u65f6\uff0c\u7f3a\u4e4f\u5fc5\u8981\u7684\u540c\u6b65\u63a7\u5236\uff0c\u4e14\u5176\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a goroutine \u6267\u884c\u7684\u662f\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\u4e2a goroutines \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\u7684 goroutines \u65f6\uff0c\u9700\u8981\u8003\u8651\u5de5\u4f5c\u8d1f\u8f7d\u7684\u7c7b\u578b\u3002\u5982\u679c\u5de5\u4f5c\u8d1f\u8f7d\u662f CPU \u5bc6\u96c6\u578b\u7684\uff0c\u90a3\u4e48 goroutines \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\u662f IO \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

    "},{"location":"zh/#go-contexts-60","title":"\u8bef\u89e3\u4e86 Go contexts (#60)","text":"

    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\u7684 context (#61)","text":"

    \u5f53\u6211\u4eec\u4f20\u9012\u4e86\u4e00\u4e2a context\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u8fd9\u4e2a context \u4ec0\u4e48\u65f6\u5019\u53ef\u4ee5\u88ab\u53d6\u6d88\uff0c\u8fd9\u70b9\u5f88\u91cd\u8981\uff0c\u4f8b\u5982\uff1a\u4e00\u4e2a HTTP \u8bf7\u6c42\u5904\u7406\u5668\u5728\u53d1\u9001\u5b8c\u54cd\u5e94\u540e\u53d6\u6d88 context\u3002

    ps: \u5b9e\u9645\u4e0a context \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\u4e2a goroutine \u4f46\u662f\u4e0d\u77e5\u9053\u5b83\u4f55\u65f6\u4f1a\u505c\u6b62 (#62)","text":"

    \u907f\u514d goroutine \u6cc4\u6f0f\uff0c\u8981\u6709\u8fd9\u79cd\u610f\u8bc6\uff0c\u5f53\u521b\u5efa\u5e76\u542f\u52a8\u4e00\u4e2a goroutine \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\u514d goroutines \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\u8005 goroutines \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\u66ff goroutines \u8c03\u7528\u95ed\u5305\u3002

    "},{"location":"zh/#select-channels-64","title":"\u4f7f\u7528 select + channels \u65f6\u8bef\u4ee5\u4e3a\u5206\u652f\u9009\u62e9\u987a\u5e8f\u662f\u786e\u5b9a\u7684 (#64)","text":"

    \u8981\u660e\u767d\uff0cselect \u591a\u4e2a channels \u65f6\uff0c\u5982\u679c\u591a\u4e2a channels \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\u7684 bug\u3002

    "},{"location":"zh/#channels-65","title":"\u4e0d\u6b63\u786e\u4f7f\u7528\u901a\u77e5 channels (#65)","text":"

    \u53d1\u9001\u901a\u77e5\u65f6\u4f7f\u7528 chan struct{} \u7c7b\u578b\u3002

    ps: \u5148\u660e\u767d\u4ec0\u4e48\u662f\u901a\u77e5 channels\uff0c\u4e00\u4e2a\u901a\u77e5 channels \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\u7684 channels\uff0c\u8fd9\u79cd\u79f0\u4e3a\u901a\u77e5 channels\u3002\u5176\u4e2d\u4f20\u9012\u7684\u6570\u636e\u7684\u7c7b\u578b\u4e3a struct{} \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

    "},{"location":"zh/#channel-size-67","title":"\u4e0d\u6e05\u695a\u8be5\u5982\u4f55\u786e\u5b9a channel size (#67)","text":"

    \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\u4e3a 1\uff0c\u5982\u679c\u6709\u5408\u7406\u7684\u7406\u7531\u518d\u53bb\u6307\u5b9a channels size\u3002

    ps: \u6839\u636e disruptor \u8fd9\u4e2a\u9ad8\u6027\u80fd\u5185\u5b58\u6d88\u606f\u961f\u5217\u7684\u5b9e\u8df5\uff0c\u5728\u67d0\u79cd\u8bfb\u5199 pacing \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\u4e2a placeholder \u5bf9\u5e94\u7684\u503c\u662f\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

    "},{"location":"zh/#append-69","title":"\u4f7f\u7528 append \u4e0d\u5f53\u5bfc\u81f4\u6570\u636e\u7ade\u4e89 (#69)","text":"

    \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

    "},{"location":"zh/#mutexes-slicesmaps-70","title":"\u8bef\u7528 mutexes \u548c slices\u3001maps (#70)","text":"

    \u8bf7\u8bb0\u4f4f slices \u548c maps \u662f\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\u5730\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\u7528 sync.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

    "},{"location":"zh/#synccond-72","title":"\u5fd8\u8bb0\u4f7f\u7528 sync.Cond (#72)","text":"

    \u4f60\u53ef\u4ee5\u4f7f\u7528 sync.Cond \u5411\u591a\u4e2a goroutines \u53d1\u9001\u91cd\u590d\u7684\u901a\u77e5\u3002

    "},{"location":"zh/#errgroup-73","title":"\u4e0d\u4f7f\u7528 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

    "},{"location":"zh/#sync-74","title":"\u62f7\u8d1d\u4e00\u4e2a sync \u4e0b\u7684\u7c7b\u578b (#74)","text":"

    sync \u5305\u4e0b\u7684\u7c7b\u578b\u4e0d\u5e94\u8be5\u88ab\u62f7\u8d1d\u3002

    "},{"location":"zh/#_9","title":"\u6807\u51c6\u5e93","text":""},{"location":"zh/#timeduration-75","title":"\u4f7f\u7528\u4e86\u9519\u8bef\u7684 time.Duration (#75)","text":"

    \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\u548c bug\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\u6216 HTTP \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

    "},{"location":"zh/#json-77","title":"JSON \u5904\u7406\u4e2d\u7684\u5e38\u89c1\u9519\u8bef (#77)","text":"

    \u8981\u5f53\u5fc3\u5728 Go \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\u7684 JSON \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

    \u5f53\u63d0\u4f9b\u4e00\u4e2a map \u7528\u6765 unmarshal JSON \u6570\u636e\u65f6\uff0c\u4e3a\u4e86\u907f\u514d\u4e0d\u786e\u5b9a\u7684 value \u7ed3\u6784\u6211\u4eec\u4f1a\u4f7f\u7528 any \u6765\u4f5c\u4e3a value \u7684\u7c7b\u578b\u800c\u4e0d\u662f\u5b9a\u4e49\u4e00\u4e2a struct\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u9700\u8981\u8bb0\u5f97\u6570\u503c\u9ed8\u8ba4\u4f1a\u88ab\u8f6c\u6362\u4e3a float64\u3002

    "},{"location":"zh/#sql-78","title":"\u5e38\u89c1\u7684 SQL \u9519\u8bef (#78)","text":"

    \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

    "},{"location":"zh/#http-sqlrows-osfile-79","title":"\u4e0d\u5173\u95ed\u4e34\u65f6\u8d44\u6e90\uff08HTTP \u8bf7\u6c42\u4f53\u3001sql.Rows \u548c os.File\uff09 (#79)","text":"

    \u6700\u7ec8\u8981\u6ce8\u610f\u5173\u95ed\u6240\u6709\u5b9e\u73b0 io.Closer \u63a5\u53e3\u7684\u7ed3\u6784\u4f53\uff0c\u4ee5\u907f\u514d\u53ef\u80fd\u7684\u6cc4\u6f0f\u3002

    "},{"location":"zh/#http-80","title":"\u54cd\u5e94 HTTP \u8bf7\u6c42\u540e\u6ca1\u6709\u8fd4\u56de\u8bed\u53e5 (#80)","text":"

    \u4e3a\u4e86\u907f\u514d\u5728 HTTP \u5904\u7406\u51fd\u6570\u4e2d\u51fa\u73b0\u67d0\u4e9b\u610f\u5916\u7684\u95ee\u9898\uff0c\u5982\u679c\u60f3\u5728\u53d1\u751f http.Error \u540e\u8ba9 HTTP \u5904\u7406\u51fd\u6570\u505c\u6b62\uff0c\u90a3\u4e48\u5c31\u4e0d\u8981\u5fd8\u8bb0\u4f7f\u7528 return \u8bed\u53e5\u6765\u963b\u6b62\u540e\u7eed\u4ee3\u7801\u7684\u6267\u884c\u3002

    "},{"location":"zh/#http-client-server-81","title":"\u76f4\u63a5\u4f7f\u7528\u9ed8\u8ba4\u7684 HTTP client \u548c server (#81)","text":"

    \u5bf9\u4e8e\u751f\u4ea7\u7ea7\u522b\u7684\u5e94\u7528\uff0c\u4e0d\u8981\u4f7f\u7528\u9ed8\u8ba4\u7684 HTTP client \u548c server \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\u4e0b go build tags\uff0c\u4ee5\u53ca go test -short\u3002

    "},{"location":"zh/#race-83","title":"\u4e0d\u6253\u5f00 race \u5f00\u5173 (#83)","text":"

    \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\uff0c\u4ece\u800c\u907f\u514d\u8f6f\u4ef6 bug\u3002

    "},{"location":"zh/#parallel-shuffle-84","title":"\u4e0d\u6253\u5f00\u6d4b\u8bd5\u7684\u6267\u884c\u6a21\u5f0f\u5f00\u5173 (parallel \u548c shuffle) (#84)","text":"

    \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\u9732 bug\u3002

    "},{"location":"zh/#85","title":"\u4e0d\u4f7f\u7528\u8868\u9a71\u52a8\u7684\u6d4b\u8bd5 (#85)","text":"

    \u8868\u9a71\u52a8\u7684\u6d4b\u8bd5\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u4e00\u7ec4\u76f8\u4f3c\u7684\u6d4b\u8bd5\u5206\u7ec4\u5728\u4e00\u8d77\uff0c\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\u884c sleep \u64cd\u4f5c (#86)","text":"

    \u4f7f\u7528\u540c\u6b65\u7684\u65b9\u5f0f\u3001\u907f\u514d sleep\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\u7406 HTTP \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

    "},{"location":"zh/#89","title":"\u4e0d\u6b63\u786e\u7684\u57fa\u51c6\u6d4b\u8bd5 (#89)","text":"

    \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\uff0c\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\uff0c\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\u7d22 go 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\u9732\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\uff0c\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\u7528 setup \u548c teardown \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\u89e3 CPU cache (#91)","text":"

    \u7406\u89e3 CPU \u7f13\u5b58\u7684\u4f7f\u7528\u5bf9\u4e8e\u4f18\u5316 CPU \u5bc6\u96c6\u578b\u5e94\u7528\u5f88\u91cd\u8981\uff0c\u56e0\u4e3a L1 \u7f13\u5b58\u6bd4\u4e3b\u5b58\u5feb 50 \u5230 100 \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

    \u63d0\u9ad8 CPU \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\u5bf9 CPU \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\u81f4 false 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\u8bb8 CPU \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\u4f4f Go \u4e2d\u57fa\u672c\u7c7b\u578b\u4e0e\u5176\u81ea\u8eab\u5927\u5c0f\u5bf9\u9f50\uff0c\u4f8b\u5982\uff0c\u6309\u5927\u5c0f\u964d\u5e8f\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\u4e2a Go \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\u8d56 GC \u6765\u6e05\u7406\u5185\u5b58\u3002

    "},{"location":"zh/#api-syncpool-96","title":"\u4e0d\u77e5\u9053\u5982\u4f55\u51cf\u5c11\u5185\u5b58\u5206\u914d\u6b21\u6570\uff08API \u8c03\u6574\uff0c\u7f16\u8bd1\u5668\u4f18\u5316\u548c sync.Pool\uff09 (#96)","text":"

    \u51cf\u5c11\u5185\u5b58\u5206\u914d\u6b21\u6570\u4e5f\u662f\u4f18\u5316 Go \u5e94\u7528\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u6bd4\u5982\u4ed4\u7ec6\u8bbe\u8ba1 API \u6765\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u4ee5\u53ca\u4f7f\u7528 sync.Pool \u6765\u5bf9\u5206\u914d\u5bf9\u8c61\u8fdb\u884c\u6c60\u5316\u3002

    "},{"location":"zh/#97","title":"\u4e0d\u6ce8\u610f\u4f7f\u7528\u5185\u8054 (#97)","text":"

    \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\u7528 Go \u95ee\u9898\u8bca\u65ad\u5de5\u5177 (#98)","text":"

    \u4e86\u89e3 Go profiling \u5de5\u5177\u3001\u6267\u884c\u65f6 tracer \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\u89e3 GC \u662f\u5982\u4f55\u5de5\u4f5c\u7684 (#99)","text":"

    \u7406\u89e3\u5982\u4f55\u8c03\u4f18 GC \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/#docker-k8s-go-100","title":"\u4e0d\u4e86\u89e3 Docker \u6216\u8005 K8S \u5bf9\u8fd0\u884c\u7684 Go \u5e94\u7528\u7684\u6027\u80fd\u5f71\u54cd (#100)","text":"

    \u4e3a\u4e86\u907f\u514d CPU throttling\uff08CPU \u9650\u9891\uff09\u95ee\u9898\uff0c\u5f53\u6211\u4eec\u5728 Docker \u548c Kubernetes \u90e8\u7f72\u5e94\u7528\u65f6\uff0c\u8981\u77e5\u9053 Go \u8bed\u8a00\u5bf9 CFS\uff08\u5b8c\u5168\u516c\u5e73\u8c03\u5ea6\u5668\uff09\u65e0\u611f\u77e5\u3002

    "}]} \ No newline at end of file diff --git a/site/sitemap.xml b/site/sitemap.xml index 703fa98d..4fa83adc 100644 --- a/site/sitemap.xml +++ b/site/sitemap.xml @@ -2,77 +2,77 @@ https://100go.co/ - 2024-10-06 + 2024-10-09 daily https://100go.co/20-slice/ - 2024-10-06 + 2024-10-09 daily https://100go.co/28-maps-memory-leaks/ - 2024-10-06 + 2024-10-09 daily https://100go.co/5-interface-pollution/ - 2024-10-06 + 2024-10-09 daily https://100go.co/56-concurrency-faster/ - 2024-10-06 + 2024-10-09 daily https://100go.co/89-benchmarks/ - 2024-10-06 + 2024-10-09 daily https://100go.co/9-generics/ - 2024-10-06 + 2024-10-09 daily https://100go.co/92-false-sharing/ - 2024-10-06 + 2024-10-09 daily https://100go.co/98-profiling-execution-tracing/ - 2024-10-06 + 2024-10-09 daily https://100go.co/book/ - 2024-10-06 + 2024-10-09 daily https://100go.co/chapter-1/ - 2024-10-06 + 2024-10-09 daily https://100go.co/external/ - 2024-10-06 + 2024-10-09 daily https://100go.co/ja/ - 2024-10-06 + 2024-10-09 daily https://100go.co/pt-br/ - 2024-10-06 + 2024-10-09 daily https://100go.co/zh/ - 2024-10-06 + 2024-10-09 daily \ No newline at end of file diff --git a/site/sitemap.xml.gz b/site/sitemap.xml.gz index a58105f85a76203836de1c30714af6522dc00957..d2478330443ee896d5b3d18ad03f7dfd413019a4 100644 GIT binary patch delta 296 zcmV+@0oVTA0^9-zABzYGglYzn2Oob^GNtKlPp~}zrA|UiATxAr_w;kxYU**9N%#U- z#{PtVM7udpHv2*f#=CW17BbJ!X}>dWSmz(_Z&2r(Zq**i28p4?531*@P+oG6^r`ON`+%M`CV7Z z>Iq5-vSE|E?!iot$_>~Pes()$?4=bCnp=4B8~;xvm4sl8#*~n1Sl$^XoIG8?qB?|x z@SefAh}0{MFneo{kqas%z8BEm=_3)B_XT>6iIfmu-N2T~#z|39d2N#j0VFVPy+0&` u!}n+INGKCx!ZQ;&WfMZ$tAt?Y2-}oq==b>lFxQ2@LfsERnrJ0D2><|@DT;*v delta 296 zcmV+@0oVTA0^9-zABzYGNT>pl2Oob`Iwk3CPp~}zrB1?1ATxAr_w;kxYU**9N%#U- z#{PtVM6*3lHv2*f#=ABz3z=u=wBH*ywE6qnE7bY6TQx_r0b|C-F0}a=2xZB zi8!e_6rKhV^vH)G?n)txP2R0Cmc_wsca_F^-7Q`MyD6m{e4)K)#G-z*QXykbe&1EH zdV*4dT(ikt_h6<+<{#ON;FQ diff --git a/site/stylesheets/extra.css b/site/stylesheets/extra.css index 4180724a..a970fe22 100644 --- a/site/stylesheets/extra.css +++ b/site/stylesheets/extra.css @@ -6,3 +6,10 @@ .md-typeset details { font-size: 15px } + +.md-announce { + background-color: #f0f8ff; /* Light blue background */ + padding: 10px; + border-radius: 5px; + color: #333; +} diff --git a/site/zh/index.html b/site/zh/index.html index 4ed0268d..5601ce62 100644 --- a/site/zh/index.html +++ b/site/zh/index.html @@ -121,7 +121,7 @@
    - + Skip to content @@ -137,7 +137,7 @@ -📢 Two new full sections are now available: #5: Interface pollution and #92: Writing concurrent code that leads to false sharing. +📢 I have just released a daily newsletter for coders: The Coder Cafe ☕. Feel free to have a look at thecoder.cafe.
    @@ -841,7 +841,7 @@
  • - 误用init函数 (#3) + 误用 init 函数 (#3) @@ -850,7 +850,7 @@
  • - 滥用getters/setters (#4) + 滥用 getters/setters (#4) @@ -913,7 +913,7 @@
  • - 不使用function option模式 (#11) + 不使用 function option 模式 (#11) @@ -958,7 +958,7 @@
  • - 不使用linters检查 (#16) + 不使用 linters 检查 (#16) @@ -1009,7 +1009,7 @@
  • - 不理解slice的长度和容量 (#20) + 不理解 slice 的长度和容量 (#20) @@ -1018,16 +1018,16 @@
  • - 不高效的slice初始化 (#21) + 不高效的 slice 初始化 (#21)
  • - + - 困惑于nil和空slice (#22) + 困惑于 nil 和空 slice (#22) @@ -1036,7 +1036,7 @@
  • - 没有适当检查slice是否为空 (#23) + 没有适当检查 slice 是否为空 (#23) @@ -1045,7 +1045,7 @@
  • - 没有正确拷贝slice (#24) + 没有正确拷贝 slice (#24) @@ -1054,7 +1054,7 @@
  • - slice append带来的预期之外的副作用 (#25) + slice append 带来的预期之外的副作用 (#25) @@ -1063,7 +1063,7 @@
  • - slice和内存泄漏 (#26) + slice 和内存泄漏 (#26) @@ -1072,7 +1072,7 @@
  • - 不高效的map初始化 (#27) + 不高效的 map 初始化 (#27) @@ -1081,7 +1081,7 @@
  • - map和内存泄漏 (#28) + map 和内存泄漏 (#28) @@ -1130,9 +1130,9 @@
  • - + - 忽略了 range 循环中指针元素的影响 range loops (#32) + 忽略了 range 循环中指针元素的影响 (#32) @@ -1141,7 +1141,7 @@
  • - map迭代过程中的错误假设(遍历顺序 和 迭代过程中插入)(#33) + map 迭代过程中的错误假设(遍历顺序和迭代过程中插入)(#33) @@ -1183,7 +1183,7 @@
  • - 没有理解rune (#36) + 没有理解 rune (#36) @@ -1201,7 +1201,7 @@
  • - 误用trim函数 (#38) + 误用 trim 函数 (#38) @@ -1279,7 +1279,7 @@
  • - 返回一个nil接收器 (#45) + 返回一个 nil 接收器 (#45) @@ -1297,7 +1297,7 @@
  • - 忽略 defer 语句中参数、接收器值的计算方式 (参数值计算, 指针, 和 value类型接收器) (#47) + 忽略 defer 语句中参数、接收器值的计算方式 (参数值计算, 指针, 和 value 类型接收器) (#47) @@ -1330,7 +1330,7 @@
  • - 未考虑何时才应该包装error (#49) + 未考虑何时才应该包装 error (#49) @@ -1357,7 +1357,7 @@
  • - 处理同一个错误两次 (#52) + 两次处理同一个错误 (#52) @@ -1415,9 +1415,9 @@
  • - + - 不清楚何时使用channels或mutexes (#57) + 不清楚何时使用 channels 或 mutexes (#57) @@ -1426,7 +1426,7 @@
  • - 不明白竞态问题 (数据竞态 vs. 竞态条件 和 Go内存模型) (#58) + 不明白竞态问题 (数据竞态 vs. 竞态条件和 Go 内存模型) (#58) @@ -1444,7 +1444,7 @@
  • - 误解了Go contexts (#60) + 误解了 Go contexts (#60) @@ -1468,7 +1468,7 @@
  • - 传递不合适的context (#61) + 传递不合适的 context (#61) @@ -1477,7 +1477,7 @@
  • - 启动了一个goroutine但是不知道它何时会停止 (#62) + 启动了一个 goroutine 但是不知道它何时会停止 (#62) @@ -1486,7 +1486,7 @@
  • - 不注意处理 goroutines 和 循环中的迭代变量 (#63) + 不注意处理 goroutines 和循环中的迭代变量 (#63) @@ -1495,7 +1495,7 @@
  • - 使用select + channels 时误以为分支选择顺序是确定的 (#64) + 使用 select + channels 时误以为分支选择顺序是确定的 (#64) @@ -1627,7 +1627,7 @@
  • - JSON处理中的常见错误 (#77) + JSON 处理中的常见错误 (#77) @@ -1645,7 +1645,7 @@
  • - 不关闭临时资源(HTTP 请求体、sql.Rows 和 os.File) (#79) + 不关闭临时资源(HTTP 请求体、sql.Rows 和 os.File) (#79) @@ -1654,16 +1654,16 @@
  • - 响应HTTP请求后没有返回语句 (#80) + 响应 HTTP 请求后没有返回语句 (#80)
  • - + - 直接使用默认的HTTP client和server (#81) + 直接使用默认的 HTTP client 和 server (#81) @@ -1723,7 +1723,7 @@
  • - 在单元测试中执行sleep操作 (#86) + 在单元测试中执行 sleep 操作 (#86) @@ -1759,7 +1759,7 @@
  • - 没有去探索go test所有的特性 (#90) + 没有去探索 go test 所有的特性 (#90) @@ -1792,7 +1792,7 @@
  • - 不理解CPU cache (#91) + 不理解 CPU cache (#91) @@ -1801,7 +1801,7 @@
  • - 写的并发处理逻辑会导致false sharing (#92) + 写的并发处理逻辑会导致 false sharing (#92) @@ -1835,9 +1835,9 @@
  • - + - 不知道如何减少内存分配次数 (API调整, compiler optimizations, and sync.Pool) (#96) + 不知道如何减少内存分配次数(API 调整,编译器优化和 sync.Pool) (#96) @@ -1855,7 +1855,7 @@
  • - 不使用Go问题诊断工具 (#98) + 不使用 Go 问题诊断工具 (#98) @@ -1864,16 +1864,16 @@
  • - 不理解GC是如何工作的 (#99) + 不理解 GC 是如何工作的 (#99)
  • - + - 不了解Docker或者K8S对运行的Go应用的性能影响 (#100) + 不了解 Docker 或者 K8S 对运行的 Go 应用的性能影响 (#100) @@ -2119,7 +2119,7 @@
  • - 误用init函数 (#3) + 误用 init 函数 (#3) @@ -2128,7 +2128,7 @@
  • - 滥用getters/setters (#4) + 滥用 getters/setters (#4) @@ -2191,7 +2191,7 @@
  • - 不使用function option模式 (#11) + 不使用 function option 模式 (#11) @@ -2236,7 +2236,7 @@
  • - 不使用linters检查 (#16) + 不使用 linters 检查 (#16) @@ -2287,7 +2287,7 @@
  • - 不理解slice的长度和容量 (#20) + 不理解 slice 的长度和容量 (#20) @@ -2296,16 +2296,16 @@
  • - 不高效的slice初始化 (#21) + 不高效的 slice 初始化 (#21)
  • - + - 困惑于nil和空slice (#22) + 困惑于 nil 和空 slice (#22) @@ -2314,7 +2314,7 @@
  • - 没有适当检查slice是否为空 (#23) + 没有适当检查 slice 是否为空 (#23) @@ -2323,7 +2323,7 @@
  • - 没有正确拷贝slice (#24) + 没有正确拷贝 slice (#24) @@ -2332,7 +2332,7 @@
  • - slice append带来的预期之外的副作用 (#25) + slice append 带来的预期之外的副作用 (#25) @@ -2341,7 +2341,7 @@
  • - slice和内存泄漏 (#26) + slice 和内存泄漏 (#26) @@ -2350,7 +2350,7 @@
  • - 不高效的map初始化 (#27) + 不高效的 map 初始化 (#27) @@ -2359,7 +2359,7 @@
  • - map和内存泄漏 (#28) + map 和内存泄漏 (#28) @@ -2408,9 +2408,9 @@
  • - + - 忽略了 range 循环中指针元素的影响 range loops (#32) + 忽略了 range 循环中指针元素的影响 (#32) @@ -2419,7 +2419,7 @@
  • - map迭代过程中的错误假设(遍历顺序 和 迭代过程中插入)(#33) + map 迭代过程中的错误假设(遍历顺序和迭代过程中插入)(#33) @@ -2461,7 +2461,7 @@
  • - 没有理解rune (#36) + 没有理解 rune (#36) @@ -2479,7 +2479,7 @@
  • - 误用trim函数 (#38) + 误用 trim 函数 (#38) @@ -2557,7 +2557,7 @@
  • - 返回一个nil接收器 (#45) + 返回一个 nil 接收器 (#45) @@ -2575,7 +2575,7 @@
  • - 忽略 defer 语句中参数、接收器值的计算方式 (参数值计算, 指针, 和 value类型接收器) (#47) + 忽略 defer 语句中参数、接收器值的计算方式 (参数值计算, 指针, 和 value 类型接收器) (#47) @@ -2608,7 +2608,7 @@
  • - 未考虑何时才应该包装error (#49) + 未考虑何时才应该包装 error (#49) @@ -2635,7 +2635,7 @@
  • - 处理同一个错误两次 (#52) + 两次处理同一个错误 (#52) @@ -2693,9 +2693,9 @@
  • - + - 不清楚何时使用channels或mutexes (#57) + 不清楚何时使用 channels 或 mutexes (#57) @@ -2704,7 +2704,7 @@
  • - 不明白竞态问题 (数据竞态 vs. 竞态条件 和 Go内存模型) (#58) + 不明白竞态问题 (数据竞态 vs. 竞态条件和 Go 内存模型) (#58) @@ -2722,7 +2722,7 @@
  • - 误解了Go contexts (#60) + 误解了 Go contexts (#60) @@ -2746,7 +2746,7 @@
  • - 传递不合适的context (#61) + 传递不合适的 context (#61) @@ -2755,7 +2755,7 @@
  • - 启动了一个goroutine但是不知道它何时会停止 (#62) + 启动了一个 goroutine 但是不知道它何时会停止 (#62) @@ -2764,7 +2764,7 @@
  • - 不注意处理 goroutines 和 循环中的迭代变量 (#63) + 不注意处理 goroutines 和循环中的迭代变量 (#63) @@ -2773,7 +2773,7 @@
  • - 使用select + channels 时误以为分支选择顺序是确定的 (#64) + 使用 select + channels 时误以为分支选择顺序是确定的 (#64) @@ -2905,7 +2905,7 @@
  • - JSON处理中的常见错误 (#77) + JSON 处理中的常见错误 (#77) @@ -2923,7 +2923,7 @@
  • - 不关闭临时资源(HTTP 请求体、sql.Rows 和 os.File) (#79) + 不关闭临时资源(HTTP 请求体、sql.Rows 和 os.File) (#79) @@ -2932,16 +2932,16 @@
  • - 响应HTTP请求后没有返回语句 (#80) + 响应 HTTP 请求后没有返回语句 (#80)
  • - + - 直接使用默认的HTTP client和server (#81) + 直接使用默认的 HTTP client 和 server (#81) @@ -3001,7 +3001,7 @@
  • - 在单元测试中执行sleep操作 (#86) + 在单元测试中执行 sleep 操作 (#86) @@ -3037,7 +3037,7 @@
  • - 没有去探索go test所有的特性 (#90) + 没有去探索 go test 所有的特性 (#90) @@ -3070,7 +3070,7 @@
  • - 不理解CPU cache (#91) + 不理解 CPU cache (#91) @@ -3079,7 +3079,7 @@
  • - 写的并发处理逻辑会导致false sharing (#92) + 写的并发处理逻辑会导致 false sharing (#92) @@ -3113,9 +3113,9 @@
  • - + - 不知道如何减少内存分配次数 (API调整, compiler optimizations, and sync.Pool) (#96) + 不知道如何减少内存分配次数(API 调整,编译器优化和 sync.Pool) (#96) @@ -3133,7 +3133,7 @@
  • - 不使用Go问题诊断工具 (#98) + 不使用 Go 问题诊断工具 (#98) @@ -3142,16 +3142,16 @@
  • - 不理解GC是如何工作的 (#99) + 不理解 GC 是如何工作的 (#99)
  • - + - 不了解Docker或者K8S对运行的Go应用的性能影响 (#100) + 不了解 Docker 或者 K8S 对运行的 Go 应用的性能影响 (#100) @@ -3180,7 +3180,7 @@ -

    100个Go常见错误及如何避免

    +

    100 个 Go 常见错误及如何避免

    Jobs

    Is your company hiring? Sponsor the Chinese version of this repository and let a significant audience of Go developers (~1k unique visitors per week) know about your opportunities in this section.

    @@ -3191,10 +3191,10 @@

    意外的变量隐藏 (#1)

    避免变量隐藏(外部作用域变量被内部作用域同名变量隐藏),有助于避免变量引用错误,有助于他人阅读理解。

    不必要的代码嵌套 (#2)

    避免不必要的、过多的嵌套层次,并且让正常代码路径尽量左对齐(而不是放在分支路径中),有助于构建可读性更好的代码。

    -

    误用init函数 (#3)

    +

    误用 init 函数 (#3)

    初始化变量时,请记住 init 函数具有有限的错误处理能力,并且会使状态处理和测试变得更加复杂。在大多数情况下,初始化应该作为特定函数来处理。

    -

    滥用getters/setters (#4)

    -

    在Go语言中,强制使用getter和setter方法并不符合Go惯例。在实践中,应该找到效率和盲目遵循某些惯用法之间的平衡点。

    +

    滥用 getters/setters (#4)

    +

    在 Go 语言中,强制使用 getter 和 setter 方法并不符合 Go 惯例。在实践中,应该找到效率和盲目遵循某些惯用法之间的平衡点。

    接口污染 (#5)

    抽象应该被发现,而不是被创造。为了避免不必要的复杂性,需要时才创建接口,而不是预见到需要它,或者至少可以证明这种抽象是有价值的。

    将接口定义在实现方一侧 (#6)

    @@ -3206,161 +3206,161 @@

    any 没传递任何信息 (#8)

    困惑何时该用范型 (#9)

    使用泛型,可以通过类型参数分离具体的数据类型和行为,避免写很多重复度很高的代码。然而,不要过早地使用泛型、类型参数,只有在你看到真正需要时才使用。否则,它们会引入不必要的抽象和复杂性。

    未意识到类型嵌套的可能问题 (#10)

    -

    使用类型嵌套也可以避免写一些重复代码,然而,在使用时需要确保不会导致不合理的可见性问题,比如有些字段应该对外隐藏不应该被暴漏。

    -

    不使用function option模式 (#11)

    -

    为了设计并提供更友好的API(可选参数),为了更好地处理选项,应该使用function option模式。

    +

    使用类型嵌套也可以避免写一些重复代码,然而,在使用时需要确保不会导致不合理的可见性问题,比如有些字段应该对外隐藏不应该被暴露。

    +

    不使用 function option 模式 (#11)

    +

    为了设计并提供更友好的 API(可选参数),为了更好地处理选项,应该使用 function option 模式。

    工程组织不合理 (工程结构和包的组织) (#12)

    -

    遵循像 project-layout 的建议来组织Go工程是一个不错的方法,特别是你正在寻找一些类似的经验、惯例来组织一个新的Go工程的时候。

    +

    遵循像 project-layout 的建议来组织 Go 工程是一个不错的方法,尤其是你正在寻找一些类似的经验、惯例来组织一个新的 Go 工程的时候。

    创建工具包 (#13)

    -

    命名是软件设计开发中非常重要的一个部分,创建一些名如 commonutilshared 之类的包名并不会给读者带来太大价值,应该将这些包名重构为更清晰、更聚焦的包名。

    +

    命名是软件设计开发中非常重要的一个部分,创建一些名如 commonutilshared 之类的包名并不会给读者带来太大价值,应该将这些包名重构为更清晰、更具体的包名。

    忽略了包名冲突 (#14)

    -

    为了避免变量名和包名之间的冲突,导致混淆或甚至错误,应为每个变量和包使用唯一的名称。如果这不可行,可以考虑使用导入别名 import importAlias 'importPath',以区分包名和变量名,或者考虑一个更好的变量名。

    +

    为了避免变量名和包名之间的冲突,导致混淆或甚至错误,应为每个变量和包使用唯一的名称。如果这不可行,可以考虑使用导入别名 import importAlias 'importPath' 以区分包名和变量名,或者考虑一个更好的变量名。

    代码缺少文档 (#15)

    -

    为了让使用方、维护人员能更清晰地了解你的代码的意图,导出的元素(函数、类型、字段)需要添加godoc注释。

    -

    不使用linters检查 (#16)

    -

    为了改善代码质量、整体代码的一致性,应该使用linters、formatters。

    +

    为了让使用方、维护人员能更清晰地了解你的代码的意图,导出的元素(函数、类型、字段)需要添加注释。

    +

    不使用 linters 检查 (#16)

    +

    为了改善代码质量、整体代码的一致性,应该使用 linters 和 formatters。

    数据类型

    八进制字面量引发的困惑 (#17)

    在阅读现有代码时,请记住以 0 开头的整数字面量是八进制数。此外,为了提高可读性,可以通过在前面加上 0o 来显式地表示八进制整数。

    未注意可能的整数溢出 (#18)

    在 Go 中整数上溢出和下溢是静默处理的,所以你可以实现自己的函数来捕获它们。

    没有透彻理解浮点数 (#19)

    -

    比较浮点数时,通过比较二者的delta值是否介于一定的范围内,能让你写出可移植性更好的代码。

    +

    比较浮点数时,通过比较二者的 delta 值是否介于一定的范围内,能让你写出可移植性更好的代码。

    在进行加法或减法时,将具有相似数量级的操作分成同一组以提高精度 (过早指数对齐丢失精度)。此外,在进行加法和减法之前,应先进行乘法和除法 (加减法误差会被乘除放大)。

    -

    不理解slice的长度和容量 (#20)

    -

    理解slice的长度和容量的区别,是一个Go开发者的核心知识点之一。slice的长度指的是slice已经存储的元素的数量,而容量指的是slice当前底层开辟的数组最多能容纳的元素的数量。

    -

    不高效的slice初始化 (#21)

    -

    当创建一个slice时,如果其长度可以预先确定,那么可以在定义时指定它的长度和容量。这可以改善后期append时一次或者多次的内存分配操作,从而改善性能。对于map的初始化也是这样的。

    -

    困惑于nil和空slice (#22)

    -

    为了避免常见的对nil和empty slice处理行为的混淆,例如在使用 encoding/json 或 reflect 包时,你需要理解 nil 和 empty slice的区别。两者都是长度为零、容量为零的切片,但是 nil 切片不需要分配内存。

    -

    没有适当检查slice是否为空 (#23)

    -

    检查一个slice的是否包含任何元素,可以检查其长度,不管slice是nil还是empty,检查长度都是有效的。这个检查方法也适用于map。

    -

    为了设计更明确的API,API不应区分nil和空切片。

    -

    没有正确拷贝slice (#24)

    -

    使用 copy 拷贝一个slice元素到另一个slice时,需要记得,实际拷贝的元素数量是二者slice长度中的较小值。

    -

    slice append带来的预期之外的副作用 (#25)

    -

    如果两个不同的函数操作的slice复用了相同的底层数组,它们对slice执行append操作时可能会产生冲突。使用copy来完整复制一个slice或者使用完整的slice表达式[low:high:max]限制最大容量,有助于避免产生冲突。当想对一个大slice进行shrink操作时,两种方式中,只有copy才可以避免内存泄漏。

    -

    slice和内存泄漏 (#26)

    -

    对于slice元素为指针,或者slice元素为struct但是该struct含有指针字段,当通过slice[low:high]操作取subslice时,对于那些不可访问的元素可以显示设置为nil来避免内存泄露。

    -

    不高效的map初始化 (#27)

    +

    不理解 slice 的长度和容量 (#20)

    +

    理解 slice 的长度和容量的区别,是一个 Go 开发者的核心知识点之一。slice 的长度指的是 slice 已经存储的元素的数量,而容量指的是 slice 当前底层开辟的数组最多能容纳的元素的数量。

    +

    不高效的 slice 初始化 (#21)

    +

    当创建一个 slice 时,如果其长度可以预先确定,那么可以在定义时指定它的长度和容量。这可以改善后期 append 时一次或者多次的内存分配操作,从而改善性能。对于 map 的初始化也是如此。

    +

    困惑于 nil 和空 slice (#22)

    +

    为了避免常见的对 nil 和 empty slice 处理行为的混淆,例如在使用 encoding/json 或 reflect 包时,你需要理解 nil 和 empty slice 的区别。两者都是长度为零、容量为零的切片,但是 nil 切片不需要分配内存。

    +

    没有适当检查 slice 是否为空 (#23)

    +

    检查一个 slice 是否包含任何元素,可以检查其长度,不管 slice 是 nil 还是 empty,检查长度都是有效的。这个检查方法也适用于 map。

    +

    为了设计更明确的 API,API 不应区分 nil 和空切片。

    +

    没有正确拷贝 slice (#24)

    +

    使用 copy 拷贝一个 slice 元素到另一个 slice 时,需要记得,实际拷贝的元素数量是二者 slice 长度中的较小值。

    +

    slice append 带来的预期之外的副作用 (#25)

    +

    如果两个不同的函数操作的 slice 复用了相同的底层数组,它们对 slice 执行 append 操作时可能会产生冲突。使用 copy 来完整复制一个 slice 或者使用完整的 slice 表达式 [low:high:max] 限制最大容量,有助于避免产生冲突。当想对一个大 slice 进行 shrink 操作时,两种方式中,只有 copy 才可以避免内存泄漏。

    +

    slice 和内存泄漏 (#26)

    +

    对于 slice 元素为指针,或者 slice 元素为 struct 但是该 struct 含有指针字段,当通过 slice[low:high] 操作取 subslice 时,对于那些不可访问的元素可以显式设置为 nil 来避免内存泄露。

    +

    不高效的 map 初始化 (#27)

    #21.

    -

    map和内存泄漏 (#28)

    -

    一个map的buckets占用的内存只会增长,不会缩减。因此,如果它导致了一些内存占用的问题,你需要尝试不同的选项来解决,比如重新创建一个map代替原来的(原来的map会被GC掉),或者map[keyType]valueType中的valueType使用指针代替长度固定的数组或者sliceHeader来缓解过多的内存占用。

    +

    map 和内存泄漏 (#28)

    +

    一个 map 的 buckets 占用的内存只会增长,不会缩减。因此,如果它导致了一些内存占用的问题,你需要尝试不同的方式来解决,比如重新创建一个 map 代替原来的(原来的 map 会被 GC 掉),或者 map[keyType]valueType 中的 valueType 使用指针代替长度固定的数组或者 sliceHeader 来缓解过多的内存占用。

    不正确的值比较 (#29)

    -

    Go中比较两个类型值时,如果是可比较类型,那么可以使用 == 或者 != 运算符进行比较,比如:booleans、numerals、strings、pointers、channels,以及字段全部是可比较类型的structs。其他情况下,你可以使用 reflect.DeepEqual 来比较,用反射的话会牺牲一点性能,也可以使用自定义的实现和其他库来完成。

    +

    Go 中比较两个类型值时,如果是可比较类型,那么可以使用 == 或者 != 运算符进行比较,比如:booleans、numerals、strings、pointers、channels,以及字段全部是可比较类型的 structs。其他情况下,你可以使用 reflect.DeepEqual 来比较,用反射的话会牺牲一点性能,也可以使用自定义的实现和其他库来完成。

    控制结构

    忽略了 range 循环变量是一个拷贝 (#30)

    -

    range 循环中的循环变量是遍历容器中元素值的一个拷贝。因此,如果元素值是一个struct并且想在 range 中修改它,可以通过索引值来访问并修改它,或者使用经典的for循环+索引值的写法(除非遍历的元素是一个指针)。

    +

    range 循环中的循环变量是遍历容器中元素值的一个拷贝。因此,如果元素值是一个 struct 并且想在 range 中修改它,可以通过索引值来访问并修改它,或者使用经典的 for 循环+索引值的写法(除非遍历的元素是一个指针)。

    忽略了 range 循环中迭代目标值的计算方式 (channels 和 arrays) (#31)

    -

    传递给 range 操作的迭代目标对应的表达式的值,只会在循环执行前被计算一次,理解这个有助于避免犯一些常见的错误,例如不高效的channel赋值操作、slice迭代操作。

    -

    忽略了 range 循环中指针元素的影响 range loops (#32)

    -

    这里其实强调的是 range 迭代过程中,迭代变量实际上是一个拷贝,假设给另外一个容器元素(指针类型)赋值,且需要对迭代变量取地址转换成指针再赋值的话,这里潜藏着一个错误,就是for循环迭代变量是 per-variable-per-loop 而不是 per-variable-per-iteration。如果是通过局部变量(用迭代变量来初始化)或者使用索引值来直接引用迭代的元素,将有助于避免拷贝指针(迭代变量的地址)之类的bug。

    -

    map迭代过程中的错误假设(遍历顺序 和 迭代过程中插入)(#33)

    -

    使用map时,为了能得到确定一致的结果,应该记住Go中的map数据结构: -* 不会按照key对data进行排序,遍历时不是按key有序的; +

    传递给 range 操作的迭代目标对应的表达式的值,只会在循环执行前被计算一次,理解这个有助于避免犯一些常见的错误,例如不高效的 channel 赋值操作和 slice 迭代操作。

    +

    忽略了 range 循环中指针元素的影响 (#32)

    +

    这里其实强调的是 range 迭代过程中,迭代变量实际上是一个拷贝。假设给另外一个容器元素(指针类型)赋值,且需要对迭代变量取地址转换成指针再赋值的话,这里潜藏着一个错误,就是 for 循环迭代变量是 per-variable-per-loop 而不是 per-variable-per-iteration。如果是通过局部变量(用迭代变量来初始化)或者使用索引值来直接引用迭代的元素,将有助于避免拷贝指针(迭代变量的地址)之类的 bug。

    +

    map 迭代过程中的错误假设(遍历顺序和迭代过程中插入)(#33)

    +

    使用 map 时,为了能得到确定一致的结果,应该记住 Go 中的 map 数据结构: +* 不会按照 key 对 data 进行排序,遍历时 key 不是有序的; * 遍历时的顺序,也不是按照插入时的顺序; * 没有一个确定性的遍历顺序,每次遍历顺序是不同的; * 不能保证迭代过程中新插入的元素,在当前迭代中能够被遍历到;

    忽略了 break 语句是如何工作的 (#34)

    -

    配合label使用 breakcontinue,能够跳过一个特定的语句,在某些循环中存在 switchselect语句的场景中就比较有帮助。

    +

    配合 label 使用 breakcontinue,能够跳过一个特定的语句,在某些循环中存在 switchselect 语句的场景中就比较有帮助。

    在循环中使用 defer (#35)

    -

    在循环中使用defer不能在每轮迭代结束时执行defer语句,但是将循环逻辑提取到函数内部会在每次迭代结束时执行 defer 语句。

    +

    在循环中使用 defer 不能在每轮迭代结束时执行 defer 语句,但是将循环逻辑提取到函数内部会在每次迭代结束时执行 defer 语句。

    字符串

    -

    没有理解rune (#36)

    -

    理解rune类型对应的是一个unicode码点,每一个unicode码点其实是一个多字节的序列,不是一个byte。这应该是Go开发者的核心知识点之一,理解了这个有助于更准确地处理字符串。

    +

    没有理解 rune (#36)

    +

    理解 rune 类型对应的是一个 unicode 码点,每一个 unicode 码点其实是一个多字节的序列,不是一个 byte。这应该是 Go 开发者的核心知识点之一,理解了这个有助于更准确地处理字符串。

    不正确的字符串遍历 (#37)

    -

    使用 range 操作符对一个string进行遍历实际上是对string对应的 []rune 进行遍历,迭代变量中的索引值,表示的当前rune对应的 []byte 在整个 []byte(string) 中的起始索引。如果要访问string中的某一个rune(比如第三个),首先要将字符串转换为 []rune 然后再按索引值访问。

    -

    误用trim函数 (#38)

    -

    strings.TrimRight/strings.TrimLeft 移除在字符串尾部或者开头出现的一些runes,函数会指定一个rune集合,出现在集合中的rune将被从字符串移除。而 strings.TrimSuffix/strings.TrimPrefix 是移除字符串的一个后缀/前缀。

    +

    使用 range 操作符对一个 string 进行遍历实际上是对 string 对应的 []rune 进行遍历,迭代变量中的索引值,表示的当前 rune 对应的 []rune 在整个 []rune(string) 中的起始索引。如果要访问 string 中的某一个 rune(比如第三个),首先要将字符串转换为 []rune 然后再按索引值访问。

    +

    误用 trim 函数 (#38)

    +

    strings.TrimRight/strings.TrimLeft 移除在字符串尾部或者开头出现的一些 runes,函数会指定一个 rune 集合,出现在集合中的 rune 将被从字符串移除。而 strings.TrimSuffix/strings.TrimPrefix 是移除字符串的一个后缀/前缀。

    不经优化的字符串拼接操作 (#39)

    -

    对一个字符串列表进行遍历拼接操作,应该通过 strings.Builder 来完成,以避免每次迭代拼接时都分配一个新的string对象出来。

    +

    对一个字符串列表进行遍历拼接操作,应该通过 strings.Builder 来完成,以避免每次迭代拼接时都分配一个新的 string 对象出来。

    无用的字符串转换 (#40)

    bytes 包提供了一些和 strings 包相似的操作,可以帮助避免 []byte/string 之间的转换。

    子字符串和内存泄漏 (#41)

    -

    使用一个子字符串的拷贝,有助于避免内存泄漏,因为对一个字符串的s[low:high]操作返回的子字符串,其使用了和原字符串s相同的底层数组。

    +

    使用一个子字符串的拷贝,有助于避免内存泄漏,因为对一个字符串的 s[low:high] 操作返回的子字符串,其使用了和原字符串 s 相同的底层数组。

    函数和方法

    不知道使用哪种接收器类型 (#42)

    -

    对于接收器类型是采用value类型还是pointer类型,应该取决于下面这几种因素,比如:方法内是否会对它进行修改,它是否包含了一个不能被拷贝的字段,以及它表示的对象有多大。如果有疑问,接收器可以考虑使用pointer类型。

    +

    对于接收器类型是采用 value 类型还是 pointer 类型,应该取决于下面这几种因素,比如:方法内是否会对它进行修改,它是否包含了一个不能被拷贝的字段,以及它表示的对象有多大。如果有疑问,接收器可以考虑使用 pointer 类型。

    从不使用命名的返回值 (#43)

    使用命名的返回值,是一种有效改善函数、方法可读性的方法,特别是返回值列表中有多个类型相同的参数。另外,因为返回值列表中的参数是经过零值初始化过的,某些场景下也会简化函数、方法的实现。但是需要注意它的一些潜在副作用。

    使用命名的返回值时预期外的副作用 (#44)

    #43.

    使用命名的返回值,因为它已经被初始化了零值,需要注意在某些情况下异常返回时是否需要给它赋予一个不同的值,比如返回值列表定义了一个有名参数 err error,需要注意 return err 时是否正确地对 err 进行了赋值。

    -

    返回一个nil接收器 (#45)

    -

    当返回一个interface参数时,需要小心,不要返回一个nil指针,而是应该显示返回一个nil值。否则,可能会发生一些预期外的问题,因为调用方会收到一个非nil的值。

    +

    返回一个 nil 接收器 (#45)

    +

    当返回一个 interface 参数时,需要小心,不要返回一个 nil 指针,而是应该显式返回一个 nil 值。否则,可能会发生一些预期外的问题,因为调用方会收到一个非 nil 的值。

    使用文件名作为函数入参 (#46)

    设计函数时使用 io.Reader 类型作为入参,而不是文件名,将有助于改善函数的可复用性、易测试性。

    -

    忽略 defer 语句中参数、接收器值的计算方式 (参数值计算, 指针, 和 value类型接收器) (#47)

    -

    为了避免 defer 语句执行时就立即计算对defer要执行的函数的参数进行计算,可以考虑将要执行的函数放到闭包里面,然后通过指针传递参数给闭包内函数(或者通过闭包捕获外部变量),来解决这个问题。

    +

    忽略 defer 语句中参数、接收器值的计算方式 (参数值计算, 指针, 和 value 类型接收器) (#47)

    +

    为了避免 defer 语句执行时就立即对 defer 要执行的函数的参数进行计算,可以考虑将要执行的函数放到闭包里面,然后通过指针传递参数给闭包内函数(或者通过闭包捕获外部变量),来解决这个问题。

    错误管理

    Panicking (#48)

    -

    使用 panic 是Go中一种处理错误的方式,但是只能在遇到不可恢复的错误时使用,例如:通知开发人员一个强依赖的模块加载失败了。

    -

    未考虑何时才应该包装error (#49)

    +

    使用 panic 是 Go 中一种处理错误的方式,但是只能在遇到不可恢复的错误时使用,例如:通知开发人员一个强依赖的模块加载失败了。

    +

    未考虑何时才应该包装 error (#49)

    Wrapping(包装)错误允许您标记错误、提供额外的上下文信息。然而,包装错误会创建潜在的耦合,因为它使得原来的错误对调用者可见。如果您想要防止这种情况,请不要使用包装错误的方式。

    不正确的错误类型比较 (#50)

    -

    如果你使用 Go 1.13 引入的特性 fmt.Errorf + %w 来包装一个错误,当进行错误比较时,如果想判断该包装后的错误是不是指定的错误类型,就需要使用 errors.As,如果想判断是不是指定的error对象就需要用 errors.Is

    +

    如果你使用 Go 1.13 引入的特性 fmt.Errorf + %w 来包装一个错误,当进行错误比较时,如果想判断该包装后的错误是不是指定的错误类型,就需要使用 errors.As,如果想判断是不是指定的 error 对象就需要用 errors.Is

    不正确的错误对象值比较 (#51)

    #50.

    为了表达一个预期内的错误,请使用错误值的方式,并通过 == 或者 errors.Is 来比较。而对于意外错误,则应使用特定的错误类型(可以通过 errors.As 来比较)。

    -

    处理同一个错误两次 (#52)

    +

    两次处理同一个错误 (#52)

    大多数情况下,错误仅需要处理一次。打印错误日志也是一种错误处理。因此,当函数内发生错误时,应该在打印日志和返回错误中选择其中一种。包装错误也可以提供问题发生的额外上下文信息,也包括了原来的错误(可考虑交给调用方负责打日志)。

    不处理错误 (#53)

    -

    不管是在函数调用时,还是在一个 defer 函数执行时,如果想要忽略一个错误,应该显示地通过 _ 来忽略(可注明忽略的原因)。否则,将来的读者就会感觉到困惑,忽略这个错误是有意为之还是无意中漏掉了。

    +

    不管是在函数调用时,还是在一个 defer 函数执行时,如果想要忽略一个错误,应该显式地通过 _ 来忽略(可注明忽略的原因)。否则,将来的读者就会感觉到困惑,忽略这个错误是有意为之还是无意中漏掉了。

    不处理 defer 中的错误 (#54)

    -

    大多数情况下,你不应该忽略 defer 函数执行时返回的错误,或者显示处理它,或者将它传递给调用方处理,可以根据情景进行选择。如果你确定要忽略这个错误,请显示使用 _ 来忽略。

    +

    大多数情况下,你不应该忽略 defer 函数执行时返回的错误,或者显式处理它,或者将它传递给调用方处理,可以根据情景进行选择。如果你确定要忽略这个错误,请显式使用 _ 来忽略。

    并发编程: 基础

    混淆并发和并行 (#55)

    -

    理解并发(concurrency)、并行(parallelism)之间的本质区别是Go开发人员必须要掌握的。并发是关于结构设计上的,并行是关于具体执行上的。

    +

    理解并发(concurrency)、并行(parallelism)之间的本质区别是 Go 开发人员必须要掌握的。并发是关于结构设计上的,并行是关于具体执行上的。

    认为并发总是更快 (#56)

    -

    要成为一名熟练的开发人员,您必须意识到并非所有场景下都是并发的方案更快。对于任务中的最小工作负载部分,对它们进行并行化处理并不一定就有明显收益或者比串行化方案更快。对串行化、并发方案进行benchmark测试,是验证假设的好办法。

    -

    不清楚何时使用channels或mutexes (#57)

    -

    了解 goroutine 之间的交互也可以在选择使用channels或mutexes时有所帮助。一般来说,并行的 goroutine 需要同步,因此需要使用mutexes。相反,并发的 goroutine 通常需要协调和编排,因此需要使用channels。

    -

    不明白竞态问题 (数据竞态 vs. 竞态条件 和 Go内存模型) (#58)

    -

    掌握并发意味着要认识到数据竞争(data races)和竞态条件(race conditions)是两个不同的概念。数据竞争,指的是有多个goroutines同时访问相同内存区域时,没有必要的同步控制,且其中至少有一个goroutine是执行的写操作。同时要认识到,没有发生数据竞争不代表程序的执行是确定性的、没问题的。当在某个特定的操作顺序或者特定的事件发生顺序下,如果最终的行为是不可控的,这就是竞态条件。

    +

    要成为一名熟练的开发人员,您必须意识到并非所有场景下都是并发的方案更快。对于任务中的最小工作负载部分,对它们进行并行化处理并不一定就有明显收益或者比串行化方案更快。对串行化、并发方案进行 benchmark 测试,是验证假设的好办法。

    +

    不清楚何时使用 channels 或 mutexes (#57)

    +

    了解 goroutine 之间的交互也可以在选择使用 channels 或 mutexes 时有所帮助。一般来说,并行的 goroutine 需要同步,因此需要使用 mutexes。相反,并发的 goroutine 通常需要协调和编排,因此需要使用 channels。

    +

    不明白竞态问题 (数据竞态 vs. 竞态条件和 Go 内存模型) (#58)

    +

    掌握并发意味着要认识到数据竞争(data races)和竞态条件(race conditions)是两个不同的概念。数据竞争,指的是有多个 goroutines 同时访问相同内存区域时,缺乏必要的同步控制,且其中至少有一个 goroutine 执行的是写操作。同时要认识到,没有发生数据竞争不代表程序的执行是确定性的、没问题的。当在某个特定的操作顺序或者特定的事件发生顺序下,如果最终的行为是不可控的,这就是竞态条件。

    -

    ps:数据竞争是竞态条件的子集,竞态条件不仅局限于访存未同步,它可以发生在更高的层面。go test -race 检测的是数据竞争,需要同步来解决,而开发者还需要关注面更广的竞态条件,它需要对多个goroutines的执行进行编排。

    +

    ps:数据竞争是竞态条件的子集,竞态条件不仅局限于访存未同步,它可以发生在更高的层面。go test -race 检测的是数据竞争,需要同步来解决,而开发者还需要关注面更广的竞态条件,它需要对多个 goroutines 的执行进行编排。

    理解 Go 的内存模型以及有关顺序和同步的底层保证是防止可能的数据竞争和竞态条件的关键。

    不理解不同工作负载类型对并发的影响 (#59)

    -

    当创建一定数量的goroutines是,需要考虑工作负载的类型。如果工作负载是CPU密集型的,那么goroutines数量应该接近于 GOMAXPROCS 的值(该值取决于主机处理器核心数)。如果工作负载是IO密集型的,goroutines数量就需要考虑多种因素,比如外部系统(考虑请求、响应速率)。

    -

    误解了Go contexts (#60)

    +

    当创建一定数量的 goroutines 时,需要考虑工作负载的类型。如果工作负载是 CPU 密集型的,那么 goroutines 数量应该接近于 GOMAXPROCS 的值(该值取决于主机处理器核心数)。如果工作负载是 IO 密集型的,goroutines 数量就需要考虑多种因素,比如外部系统(考虑请求、响应速率)。

    +

    误解了 Go contexts (#60)

    Go 的上下文(context)也是 Go 并发编程的基石之一。上下文允许您携带截止时间、取消信号和键值列表。

    并发编程: 实践

    -

    传递不合适的context (#61)

    -

    当我们传递了一个context,我们需要知道这个context什么时候可以被取消,这点很重要,例如:一个HTTP请求处理器在发送完响应后取消context。

    +

    传递不合适的 context (#61)

    +

    当我们传递了一个 context,我们需要知道这个 context 什么时候可以被取消,这点很重要,例如:一个 HTTP 请求处理器在发送完响应后取消 context。

    -

    ps: 实际上context表达的是一个动作可以持续多久之后被停止。

    +

    ps: 实际上 context 表达的是一个动作可以持续多久之后被停止。

    -

    启动了一个goroutine但是不知道它何时会停止 (#62)

    -

    避免goroutine泄漏,要有这种意识,当创建并启动一个goroutine的时候,应该有对应的设计让它能正常退出。

    -

    不注意处理 goroutines 和 循环中的迭代变量 (#63)

    -

    为了避免goroutines和循环中的迭代变量问题,可以考虑创建局部变量并将迭代变量赋值给局部变量,或者goroutine调用带参数的函数,将迭代变量值作为参数值传入,来代替goroutine调用闭包。

    -

    使用select + channels 时误以为分支选择顺序是确定的 (#64)

    -

    要明白,select 多个channels时,如果多个channels上的操作都就绪,那么会随机选择一个 case 分支来执行,因此要避免有分支选择顺序是从上到下的这种错误预设,这可能会导致设计上的bug。

    +

    启动了一个 goroutine 但是不知道它何时会停止 (#62)

    +

    避免 goroutine 泄漏,要有这种意识,当创建并启动一个 goroutine 的时候,应该有对应的设计让它能正常退出。

    +

    不注意处理 goroutines 和循环中的迭代变量 (#63)

    +

    为了避免 goroutines 和循环中的迭代变量问题,可以考虑创建局部变量并将迭代变量赋值给局部变量,或者 goroutines 调用带参数的函数,将迭代变量值作为参数值传入,来代替 goroutines 调用闭包。

    +

    使用 select + channels 时误以为分支选择顺序是确定的 (#64)

    +

    要明白,select 多个 channels 时,如果多个 channels 上的操作都就绪,那么会随机选择一个 case 分支来执行,因此要避免有分支选择顺序是从上到下的这种错误预设,这可能会导致设计上的 bug。

    不正确使用通知 channels (#65)

    发送通知时使用 chan struct{} 类型。

    -

    ps: 先明白什么是通知channels,一个通知channels指的是只是用来做通知,而其中传递的数据没有意义,或者理解成不传递数据的channels,这种称为通知channels。其中传递的数据的类型struct{}更合适。

    +

    ps: 先明白什么是通知 channels,一个通知 channels 指的是只是用来做通知,而其中传递的数据没有意义,或者理解成不传递数据的 channels,这种称为通知 channels。其中传递的数据的类型为 struct{} 更合适。

    不使用 nil channels (#66)

    使用 nil channels 应该是并发处理方式中的一部分,例如,它能够帮助禁用 select 语句中的特定的分支。

    不清楚该如何确定 channel size (#67)

    根据指定的场景仔细评估应该使用哪一种 channel 类型(带缓冲的,不带缓冲的)。只有不带缓冲的 channels 可以提供强同步保证。

    -

    使用带缓冲的 channels 时如果不确定 size 该如何设置,可以先设为1,如果有合理的理由再去指定 channels size。

    +

    使用带缓冲的 channels 时如果不确定 size 该如何设置,可以先设为 1,如果有合理的理由再去指定 channels size。

    -

    ps: 根据disruptor这个高性能内存消息队列的实践,在某种读写pacing下,队列要么满要么空,不大可能处于某种介于中间的稳态。

    +

    ps: 根据 disruptor 这个高性能内存消息队列的实践,在某种读写 pacing 下,队列要么满要么空,不大可能处于某种介于中间的稳态。

    忘记了字符串格式化可能带来的副作用(例如 etcd 数据竞争和死锁)(#68)

    意识到字符串格式化可能会导致调用现有函数,这意味着需要注意可能的死锁和其他数据竞争问题。

    -

    ps: 核心是要关注 fmt.Sprintf + %v 进行字符串格式化时 %v 具体到不同的类型值时,实际上执行的操作是什么。比如 %v 这个placeholder对应的值时一个 context.Context,那么会就遍历其通过 context.WithValue 附加在其中的 values,这个过程可能涉及到数据竞争问题。书中提及的另一个导致死锁的案例本质上也是一样的问题,只不过又额外牵扯到了 sync.RWMutex 不可重入的问题。

    +

    ps: 核心是要关注 fmt.Sprintf + %v 进行字符串格式化时 %v 具体到不同的类型值时,实际上执行的操作是什么。比如 %v 这个 placeholder 对应的值是一个 context.Context,那么会就遍历其通过 context.WithValue 附加在其中的 values,这个过程可能涉及到数据竞争问题。书中提及的另一个导致死锁的案例本质上也是一样的问题,只不过又额外牵扯到了 sync.RWMutex 不可重入的问题。

    使用 append 不当导致数据竞争 (#69)

    调用 append 不总是没有数据竞争的,因此不要在一个共享的 slice 上并发地执行 append

    误用 mutexes 和 slices、maps (#70)

    -

    请记住 slices 和 maps 引用类型,有助于避免常见的数据竞争问题。

    +

    请记住 slices 和 maps 是引用类型,有助于避免常见的数据竞争问题。

    -

    ps: 这里实际是因为错误理解了 slices 和 maps,导致写出了错误的拷贝 slices 和 maps 的代码,进而导致锁保护无效、出现数据竞争问题。

    +

    ps: 这里实际是因为错误理解了 slices 和 maps,导致写出了错误地拷贝 slices 和 maps 的代码,进而导致锁保护无效、出现数据竞争问题。

    误用 sync.WaitGroup (#71)

    正确地使用 sync.WaitGroup 需要在启动 goroutines 之前先调用 Add 方法。

    @@ -3372,28 +3372,28 @@

    拷贝一个 sync 下的类型 (#74)

    sync 包下的类型不应该被拷贝。

    标准库

    使用了错误的 time.Duration (#75)

    -

    注意有些函数接收一个 time.Duration 类型的参数时,尽管直接传递一个整数是可以的,但最好还是使用 time API 中的方法来传递 duration,以避免可能造成的困惑、bug。

    +

    注意有些函数接收一个 time.Duration 类型的参数时,尽管直接传递一个整数是可以的,但最好还是使用 time API 中的方法来传递 duration,以避免可能造成的困惑和 bug。

    ps: 重点是注意 time.Duration 定义的是 nanoseconds 数。

    time.After 和内存泄漏 (#76)

    -

    避免在重复执行很多次的函数 (如循环中或HTTP处理函数)中调用 time.After,这可以避免内存峰值消耗。由 time.After 创建的资源仅在计时器超时才会被释放。

    -

    JSON处理中的常见错误 (#77)

    +

    避免在重复执行很多次的函数(如循环中或 HTTP 处理函数)中调用 time.After,这可以避免内存峰值消耗。由 time.After 创建的资源仅在计时器超时才会被释放。

    +

    JSON 处理中的常见错误 (#77)

    • 类型嵌套导致的预料外的行为
    -

    要当心在Go结构体中嵌入字段,这样做可能会导致诸如嵌入的 time.Time 字段实现 json.Marshaler 接口,从而覆盖默认的json序列。

    +

    要当心在 Go 结构体中嵌入字段,这样做可能会导致诸如嵌入的 time.Time 字段实现 json.Marshaler 接口,从而覆盖默认的 JSON 序列。

      -
    • JSON 和 单调时钟
    • +
    • JSON 和单调时钟

    当对两个 time.Time 类型值进行比较时,需要记住 time.Time 包含了一个墙上时钟(wall clock)和一个单调时钟 (monotonic clock),而使用 == 运算符进行比较时会同时比较这两个。

    • Map 键对应值为 any
    -

    当提供一个map用来unmarshal JSON数据时,为了避免不确定的value结构我们会使用 any 来作为value的类型而不是定义一个struct,这种情况下需要记得数值默认会被转换为 float64

    +

    当提供一个 map 用来 unmarshal JSON 数据时,为了避免不确定的 value 结构我们会使用 any 来作为 value 的类型而不是定义一个 struct,这种情况下需要记得数值默认会被转换为 float64

    常见的 SQL 错误 (#78)

      -
    • 忘记了 sql.Open 并没有与db服务器建立实际连接
    • +
    • 忘记了 sql.Open 并没有与 db 服务器建立实际连接

    需要调用 Ping 或者 PingContext 方法来测试配置并确保数据库是可达的。

      @@ -3412,42 +3412,42 @@

      常见的 SQL 错误 (#78)

    • 不处理行迭代时的错误

    调用 sql.RowsErr 方法来确保在准备下一个行时没有遗漏错误。

    -

    不关闭临时资源(HTTP 请求体、sql.Rowsos.File) (#79)

    -

    最终要注意关闭所有实现 io.Closer 接口的结构体,以避免可能的泄漏。

    -

    响应HTTP请求后没有返回语句 (#80)

    -

    为了避免在HTTP处理函数中出现某些意外的问题,如果想在发生 http.Error 后让HTTP处理函数停止,那么就不要忘记使用return语句来阻止后续代码的执行。

    -

    直接使用默认的HTTP client和server (#81)

    -

    对于生产级别的应用,不要使用默认的HTTP client和server实现。这些实现缺少超时和生产环境中应该强制使用的行为。

    +

    不关闭临时资源(HTTP 请求体、sql.Rowsos.File) (#79)

    +

    最终要注意关闭所有实现 io.Closer 接口的结构体,以避免可能的泄漏。

    +

    响应 HTTP 请求后没有返回语句 (#80)

    +

    为了避免在 HTTP 处理函数中出现某些意外的问题,如果想在发生 http.Error 后让 HTTP 处理函数停止,那么就不要忘记使用 return 语句来阻止后续代码的执行。

    +

    直接使用默认的 HTTP client 和 server (#81)

    +

    对于生产级别的应用,不要使用默认的 HTTP client 和 server 实现。这些实现缺少超时和生产环境中应该强制使用的行为。

    测试

    不对测试进行分类 (build tags, 环境变量,短模式)(#82)

    对测试进行必要的分类,可以借助 build tags、环境变量以及短模式,来使得测试过程更加高效。你可以使用 build tags 或环境变量来创建测试类别(例如单元测试与集成测试),并区分短测试与长时间测试,来决定执行哪种类型的。

    -

    ps: 了解下go build tags,以及 go test -short

    +

    ps: 了解下 go build tags,以及 go test -short

    不打开 race 开关 (#83)

    -

    打开 -race 开关在编写并发应用时非常重要。这能帮助你捕获可能的数据竞争,从而避免软件bug。

    +

    打开 -race 开关在编写并发应用时非常重要。这能帮助你捕获可能的数据竞争,从而避免软件 bug。

    不打开测试的执行模式开关 (parallel 和 shuffle) (#84)

    打开开关 -parallel 有助于加速测试的执行,特别是测试中包含一些需要长期运行的用例的时候。

    -

    打开开关 -shuffle 能够打乱测试用例执行的顺序,避免一个测试依赖于某些不符合真实情况的预设,有助于及早暴漏bug。

    +

    打开开关 -shuffle 能够打乱测试用例执行的顺序,避免一个测试依赖于某些不符合真实情况的预设,有助于及早暴露 bug。

    不使用表驱动的测试 (#85)

    -

    表驱动的测试是一种有效的方式,可以将一组相似的测试分组在一起,以避免代码重复和使未来的更新更容易处理。

    -

    在单元测试中执行sleep操作 (#86)

    -

    使用同步的方式、避免sleep,来尽量减少测试的不稳定性和提高鲁棒性。如果无法使用同步手段,可以考虑重试的方式。

    +

    表驱动的测试是一种有效的方式,可以将一组相似的测试分组在一起,以避免代码重复和使未来的更新更容易处理。

    +

    在单元测试中执行 sleep 操作 (#86)

    +

    使用同步的方式、避免 sleep,来尽量减少测试的不稳定性和提高鲁棒性。如果无法使用同步手段,可以考虑重试的方式。

    没有高效地处理 time API (#87)

    理解如何处理使用 time API 的函数,是使测试更加稳定的另一种方式。您可以使用标准技术,例如将时间作为隐藏依赖项的一部分来处理,或者要求客户端提供时间。

    不使用测试相关的工具包 (httptestiotest) (#88)

    -

    这个 httptest 包对处理HTTP应用程序很有帮助。它提供了一组实用程序来测试客户端和服务器。

    +

    这个 httptest 包对处理 HTTP 应用程序很有帮助。它提供了一组实用程序来测试客户端和服务器。

    这个 iotest 包有助于编写 io.Reader 并测试应用程序是否能够容忍错误。

    不正确的基准测试 (#89)

      -
    • 不要重置或者暂停timer
    • +
    • 不要重置或者暂停 timer

    使用 time 方法来保持基准测试的准确性。

    • 做出错误的微基准测试假设

    增加 benchtime 或者使用 benchstat 等工具可以有助于微基准测试。

    -

    小心微基准测试的结果,如果最终运行应用程序的系统与运行微基准测试的系统不同。

    +

    小心微基准测试的结果,如果最终运行应用程序的系统与运行微基准测试的系统不同。

    • 对编译期优化要足够小心
    @@ -3455,71 +3455,71 @@

    不正确的基准测试
  • 被观察者效应所欺骗
  • -

    为了避免被观察者效应欺骗,强制重新创建CPU密集型函数使用的数据。

    -

    没有去探索go test所有的特性 (#90)

    +

    为了避免被观察者效应欺骗,强制重新创建CPU密集型函数使用的数据。

    +

    没有去探索 go test 所有的特性 (#90)

    • 代码覆盖率

    使用 -coverprofile 参数可以快速查看代码的测试覆盖情况,方便快速查看哪个部分需要更多的关注。

      -
    • 在一个不同包中执行测试
    • +
    • 在不同的包中执行测试
    -

    单元测试组织到一个独立的包中,对于对外层暴漏的接口,需要写一些测试用例。测试应该关注公开的行为,而非内部实现细节。

    +

    单元测试组织到一个独立的包中,对于对外层暴露的接口,需要写一些测试用例。测试应该关注公开的行为,而非内部实现细节。

    • Utility 函数
    -

    处理错误时,使用 *testing.T 变量而不是经典的 if err != nil 可以让代码更加简洁易读。

    +

    处理错误时,使用 *testing.T 变量而不是经典的 if err != nil 可以让代码更加简洁易读。

    • 设置和销毁
    -

    你可以使用setup和teardown函数来配置一个复杂的环境,比如在集成测试的情况下。

    +

    你可以使用 setup 和 teardown 函数来配置一个复杂的环境,比如在集成测试的情况下。

    不使用模糊测试 (社区反馈错误)

    模糊测试是一种高效的策略,使用它能检测出随机、意料外的和一些恶意的数据输入,来完成一些复杂的操作。

    见: @jeromedoucet

    优化技术

    -

    不理解CPU cache (#91)

    +

    不理解 CPU cache (#91)

      -
    • CPU架构
    • +
    • CPU 架构
    -

    理解CPU缓存的使用对于优化CPU密集型应用很重要,因为L1缓存比主存快50到100倍。

    +

    理解 CPU 缓存的使用对于优化 CPU 密集型应用很重要,因为 L1 缓存比主存快 50 到 100 倍。

    • Cache line
    -

    意识到 cache line 概念对于理解如何在数据密集型应用中组织数据非常关键。CPU 并不是一个一个字来获取内存。相反,它通常复制一个 64字节长度的 cache line。为了获得每个 cache line 的最大效用,需要实施空间局部性。

    +

    意识到 cache line 概念对于理解如何在数据密集型应用中组织数据非常关键。CPU 并不是一个一个字来获取内存。相反,它通常复制一个 64 字节长度的 cache line。为了获得每个 cache line 的最大效用,需要实施空间局部性。

    • -

      一系列struct元素构成的slice vs. 多个slice字段构成的struct

      +

      一系列 struct 元素构成的 slice vs. 多个 slice 字段构成的 struct

    • 概率性的问题

    -

    提高CPU执行代码时的可预测性,也是优化某些函数的一个有效方法。比如,固定步长或连续访问对CPU来说是可预测的,但非连续访问(例如链表)就是不可预测的。

    +

    提高 CPU 执行代码时的可预测性,也是优化某些函数的一个有效方法。比如,固定步长或连续访问对 CPU 来说是可预测的,但非连续访问(例如链表)就是不可预测的。

      -
    • cache放置策略
    • +
    • cache 放置策略

    要注意现代缓存是分区的(set associative placement,组相连映射),要注意避免使用 critical stride,这种步长情况下只能利用 cache 的一小部分。

    critical stride,这种类型的步长,指的是内存访问的步长刚好等于 cache 大小。这种情况下,只有少部分 cacheline 被利用。

    -

    写的并发处理逻辑会导致false sharing (#92)

    +

    写的并发处理逻辑会导致 false sharing (#92)

    了解 CPU 缓存的较低层的 L1、L2 cache 不会在所有核间共享,编写并发处理逻辑时能避免写出一些降低性能的问题,比如伪共享(false sharing)。内存共享只是一种假象。

    没有考虑指令级的并行 (#93)

    -

    使用指令级并行(ILP)优化代码的特定部分,以允许CPU尽可能执行更多可以并行执行的指令。识别指令的数据依赖问题(data hazards)是主要步骤之一。

    +

    使用指令级并行(ILP)优化代码的特定部分,以允许 CPU 尽可能执行更多可以并行执行的指令。识别指令的数据依赖问题(data hazards)是主要步骤之一。

    不了解数据对齐 (#94)

    -

    记住Go中基本类型与其自身大小对齐,例如,按降序从大到小重新组织结构体的字段可以形成更紧凑的结构体(减少内存分配,更好的空间局部性),这有助于避免一些常见的错误。

    +

    记住 Go 中基本类型与其自身大小对齐,例如,按大小降序重新组织结构体的字段可以形成更紧凑的结构体(减少内存分配,更好的空间局部性),这有助于避免一些常见的错误。

    不了解 stack vs. heap (#95)

    -

    了解堆和栈之间的区别是开发人员的核心知识点,特别是要去优化一个Go程序时。栈分配的开销几乎为零,而堆分配则较慢,并且依赖GC来清理内存。

    -

    不知道如何减少内存分配次数 (API调整, compiler optimizations, and sync.Pool) (#96)

    -

    减少内存分配次数也是优化Go应用的一个重要方面。这可以通过不同的方式来实现,比如仔细设计API来避免不必要的拷贝,以及使用 sync.Pool 来对待分配对象进行池化。

    +

    了解堆和栈之间的区别是开发人员的核心知识点,特别是要去优化一个 Go 程序时。栈分配的开销几乎为零,而堆分配则较慢,并且依赖 GC 来清理内存。

    +

    不知道如何减少内存分配次数(API 调整,编译器优化和 sync.Pool) (#96)

    +

    减少内存分配次数也是优化 Go 应用的一个重要方面。这可以通过不同的方式来实现,比如仔细设计 API 来避免不必要的拷贝,以及使用 sync.Pool 来对分配对象进行池化。

    不注意使用内联 (#97)

    使用快速路径的内联技术来更加有效地减少调用函数的摊销时间。

    -

    不使用Go问题诊断工具 (#98)

    -

    了解Go profilng工具、执行时tracer来辅助判断一个应用程序是否正常,以及列出需要优化的部分。

    -

    不理解GC是如何工作的 (#99)

    -

    理解如何调优GC能够带来很多收益,例如有助于更高效地处理突增的负载。

    -

    不了解Docker或者K8S对运行的Go应用的性能影响 (#100)

    -

    为了避免CPU throttling(CPU限频)问题,当我们在Docker和Kubernetes部署应用时,要知道Go语言对CFS(完全公平调度器)无感知。

    +

    不使用 Go 问题诊断工具 (#98)

    +

    了解 Go profiling 工具、执行时 tracer 来辅助判断一个应用程序是否正常,以及列出需要优化的部分。

    +

    不理解 GC 是如何工作的 (#99)

    +

    理解如何调优 GC 能够带来很多收益,例如有助于更高效地处理突增的负载。

    +

    不了解 Docker 或者 K8S 对运行的 Go 应用的性能影响 (#100)

    +

    为了避免 CPU throttling(CPU 限频)问题,当我们在 Docker 和 Kubernetes 部署应用时,要知道 Go 语言对 CFS(完全公平调度器)无感知。