Repository: teivah/100-go-mistakes
Branch: master
Commit: ab425833b58d
Files: 208
Total size: 3.6 MB
Directory structure:
gitextract_0hghv22e/
├── .gitattributes
├── .github/
│ ├── FUNDING.yml
│ ├── ISSUE_TEMPLATE/
│ │ ├── community_mistake.md
│ │ └── erratum.md
│ └── workflows/
│ └── ci.yml
├── .gitignore
├── CONTRIBUTING.md
├── LICENSE.md
├── README.md
├── docs/
│ ├── 20-slice.md
│ ├── 28-maps-memory-leaks.md
│ ├── 5-interface-pollution.md
│ ├── 56-concurrency-faster.md
│ ├── 89-benchmarks.md
│ ├── 9-generics.md
│ ├── 92-false-sharing.md
│ ├── 98-profiling-execution-tracing.md
│ ├── CNAME
│ ├── book.md
│ ├── chapter-1.md
│ ├── external.md
│ ├── index.md
│ ├── ja.md
│ ├── pt-br.md
│ ├── stylesheets/
│ │ └── extra.css
│ └── zh.md
├── go.mod
├── go.sum
├── includes/
│ └── abbreviations.md
├── justfile
├── mkdocs.yml
├── overrides/
│ ├── main.html
│ └── partials/
│ └── comments.html
├── site/
│ ├── 20-slice/
│ │ └── index.html
│ ├── 28-maps-memory-leaks/
│ │ └── index.html
│ ├── 404.html
│ ├── 5-interface-pollution/
│ │ └── index.html
│ ├── 56-concurrency-faster/
│ │ └── index.html
│ ├── 89-benchmarks/
│ │ └── index.html
│ ├── 9-generics/
│ │ └── index.html
│ ├── 92-false-sharing/
│ │ └── index.html
│ ├── 98-profiling-execution-tracing/
│ │ └── index.html
│ ├── CNAME
│ ├── assets/
│ │ └── javascripts/
│ │ └── lunr/
│ │ ├── tinyseg.js
│ │ └── wordcut.js
│ ├── book/
│ │ └── index.html
│ ├── chapter-1/
│ │ └── index.html
│ ├── external/
│ │ └── index.html
│ ├── index.html
│ ├── ja/
│ │ └── index.html
│ ├── pt-br/
│ │ └── index.html
│ ├── search/
│ │ └── search_index.json
│ ├── sitemap.xml
│ ├── stylesheets/
│ │ └── extra.css
│ └── zh/
│ └── index.html
└── src/
├── 02-code-project-organization/
│ ├── 1-variable-shadowing/
│ │ └── main.go
│ ├── 10-type-embedding/
│ │ └── main.go
│ ├── 11-functional-options/
│ │ ├── builder/
│ │ │ └── main.go
│ │ ├── config-struct/
│ │ │ └── main.go
│ │ └── functional-options/
│ │ └── main.go
│ ├── 13-utility-packages/
│ │ └── stringset.go
│ ├── 2-nested-code/
│ │ └── main.go
│ ├── 3-init-functions/
│ │ ├── db/
│ │ │ └── main.go
│ │ ├── main/
│ │ │ └── main.go
│ │ └── redis/
│ │ └── redis.go
│ ├── 5-interface-pollution/
│ │ ├── copy/
│ │ │ ├── main.go
│ │ │ └── main_test.go
│ │ ├── decoupling/
│ │ │ ├── with.go
│ │ │ └── without.go
│ │ └── restricting-behavior/
│ │ └── main.go
│ ├── 6-interface-producer/
│ │ ├── client/
│ │ │ └── client.go
│ │ └── store/
│ │ └── store.go
│ ├── 8-any/
│ │ ├── main.go
│ │ └── store/
│ │ ├── after.go
│ │ └── before.go
│ └── 9-generics/
│ └── main.go
├── 03-data-types/
│ ├── 17-octal-literals/
│ │ └── main.go
│ ├── 18-integer-overflows/
│ │ └── main.go
│ ├── 19-floating-points/
│ │ └── main.go
│ ├── 20-slice-length-cap/
│ │ └── main.go
│ ├── 21-slice-init/
│ │ ├── main.go
│ │ └── main_test.go
│ ├── 22-nil-empty-slice/
│ │ ├── json/
│ │ │ └── main.go
│ │ └── slice-init/
│ │ └── main.go
│ ├── 23-checking-slice-empty/
│ │ └── main.go
│ ├── 24-slice-copy/
│ │ └── main.go
│ ├── 25-slice-append/
│ │ └── main.go
│ ├── 26-slice-memory-leak/
│ │ ├── capacity-leak/
│ │ │ └── main.go
│ │ └── slice-pointers/
│ │ └── main.go
│ ├── 27-map-init/
│ │ └── main_test.go
│ ├── 28-map-memory-leak/
│ │ └── main.go
│ └── 29-comparing-values/
│ └── main.go
├── 04-control-structures/
│ ├── 30-range-loop-element-copied/
│ │ ├── concepts/
│ │ │ └── main.go
│ │ └── value-copy/
│ │ └── main.go
│ ├── 31-range-loop-arg-evaluation/
│ │ ├── arrays/
│ │ │ └── main.go
│ │ ├── channels/
│ │ │ └── main.go
│ │ └── concepts/
│ │ └── main.go
│ ├── 32-range-loop-pointers/
│ │ ├── concepts/
│ │ │ └── main.go
│ │ └── customer-store/
│ │ └── main.go
│ ├── 33-map-iteration/
│ │ └── main.go
│ ├── 34-break/
│ │ └── main.go
│ └── 35-defer-loop/
│ └── main.go
├── 05-strings/
│ ├── 36-rune/
│ │ └── main.go
│ ├── 37-string-iteration/
│ │ └── main.go
│ ├── 38-trim/
│ │ └── main.go
│ ├── 39-string-concat/
│ │ ├── main.go
│ │ └── main_test.go
│ ├── 40-string-conversion/
│ │ └── main.go
│ └── 41-substring-memory-leak/
│ └── main.go
├── 06-functions-methods/
│ ├── 42-receiver/
│ │ ├── pointer/
│ │ │ └── main.go
│ │ ├── struct-with-pointer/
│ │ │ └── main.go
│ │ └── value/
│ │ └── main.go
│ ├── 43-named-result-parameters/
│ │ └── main.go
│ ├── 44-side-effects-named-result-parameters/
│ │ └── main.go
│ ├── 45-nil-receiver/
│ │ └── main.go
│ ├── 46-function-input/
│ │ ├── main.go
│ │ └── main_test.go
│ └── 47-defer-evaluation/
│ ├── args/
│ │ └── main.go
│ └── receiver/
│ ├── pointer/
│ │ └── main.go
│ └── value/
│ └── main.go
├── 07-error-management/
│ ├── 48-panic/
│ │ └── main.go
│ ├── 49-error-wrapping/
│ │ └── main.go
│ ├── 50-compare-error-type/
│ │ └── main.go
│ ├── 51-comparing-error-value/
│ │ └── main.go
│ ├── 52-handling-error-twice/
│ │ └── main.go
│ ├── 53-not-handling-error/
│ │ └── main.go
│ └── 54-defer-errors/
│ └── main.go
├── 08-concurrency-foundations/
│ ├── 56-faster/
│ │ ├── main.go
│ │ └── main_test.go
│ ├── 58-races/
│ │ ├── memory-model/
│ │ │ └── main.go
│ │ └── races/
│ │ └── main.go
│ ├── 59-workload-type/
│ │ └── main.go
│ └── 60-contexts/
│ ├── flight/
│ │ └── flight.go
│ └── main.go
├── 09-concurrency-practice/
│ ├── 61-inappropriate-context/
│ │ └── main.go
│ ├── 62-starting-goroutine/
│ │ ├── listing1/
│ │ │ └── main.go
│ │ ├── listing2/
│ │ │ └── main.go
│ │ └── listing3/
│ │ └── main.go
│ ├── 63-goroutines-loop-variables/
│ │ └── main.go
│ ├── 64-select-behavior/
│ │ └── main.go
│ ├── 66-nil-channels/
│ │ └── main.go
│ ├── 68-string-formatting/
│ │ └── main.go
│ ├── 69-data-race-append/
│ │ └── main.go
│ ├── 70-mutex-slices-maps/
│ │ └── main.go
│ ├── 71-wait-group/
│ │ └── main.go
│ ├── 72-cond/
│ │ └── main.go
│ ├── 73-errgroup/
│ │ └── main.go
│ └── 74-copying-sync/
│ └── main.go
├── 10-standard-lib/
│ ├── 75-wrong-time-duration/
│ │ └── main.go
│ ├── 76-time-after/
│ │ └── main.go
│ ├── 77-json-handling/
│ │ ├── map-any/
│ │ │ └── main.go
│ │ ├── monotonic-clock/
│ │ │ └── main.go
│ │ └── type-embedding/
│ │ └── main.go
│ ├── 78-sql/
│ │ ├── null-values/
│ │ │ └── main.go
│ │ ├── prepared-statements/
│ │ │ └── main.go
│ │ ├── rows-iterations-errors/
│ │ │ └── main.go
│ │ └── sql-open/
│ │ └── main.go
│ ├── 79-closing-resources/
│ │ ├── http/
│ │ │ └── main.go
│ │ ├── os-file/
│ │ │ └── main.go
│ │ └── sql-rows/
│ │ └── main.go
│ ├── 80-http-return/
│ │ └── main.go
│ └── 81-default-http-client-server/
│ ├── client/
│ │ └── main.go
│ └── server/
│ └── main.go
├── 11-testing/
│ ├── 82-categorizing-tests/
│ │ ├── build-tags/
│ │ │ └── db_test.go
│ │ └── short-mode/
│ │ └── main_test.go
│ ├── 85-table-driven-tests/
│ │ ├── main.go
│ │ └── main_test.go
│ ├── 86-sleeping/
│ │ ├── main.go
│ │ └── main_test.go
│ ├── 87-time-api/
│ │ ├── listing1/
│ │ │ ├── main.go
│ │ │ └── main_test.go
│ │ ├── listing2/
│ │ │ ├── main.go
│ │ │ └── main_test.go
│ │ ├── listing3/
│ │ │ ├── main.go
│ │ │ └── main_test.go
│ │ └── listing4/
│ │ ├── main.go
│ │ └── main_test.go
│ ├── 88-utility-package/
│ │ ├── httptest/
│ │ │ ├── main.go
│ │ │ └── main_test.go
│ │ └── iotest/
│ │ ├── main.go
│ │ └── main_test.go
│ ├── 89-benchmark/
│ │ ├── compiler-optimizations/
│ │ │ ├── main.go
│ │ │ └── main_test.go
│ │ ├── observer-effect/
│ │ │ ├── main.go
│ │ │ └── main_test.go
│ │ ├── timer/
│ │ │ └── main_test.go
│ │ └── wrong-assumptions/
│ │ └── main_test.go
│ └── 90-testing-features/
│ ├── different-package/
│ │ ├── main.go
│ │ └── main_test.go
│ ├── setup-teardown/
│ │ └── main_test.go
│ └── utility-function/
│ ├── main.go
│ └── main_test.go
└── 12-optimizations/
├── 91-cpu-caches/
│ ├── cache-line/
│ │ ├── main.go
│ │ └── main_test.go
│ ├── predictability/
│ │ ├── main.go
│ │ └── main_test.go
│ └── slice-structs/
│ ├── main.go
│ └── main_test.go
├── 92-false-sharing/
│ ├── main.go
│ └── main_test.go
├── 93-instruction-level-parallelism/
│ ├── main.go
│ └── main_test.go
├── 94-data-alignment/
│ ├── main.go
│ └── main_test.go
├── 95-stack-heap/
│ ├── main.go
│ └── main_test.go
└── 96-reduce-allocations/
├── compiler/
│ └── main.go
└── sync-pool/
└── main.go
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitattributes
================================================
*.html linguist-detectable=false
*.js linguist-detectable=false
*.css linguist-detectable=false
================================================
FILE: .github/FUNDING.yml
================================================
# These are supported funding model platforms
github: teivah
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
================================================
FILE: .github/ISSUE_TEMPLATE/community_mistake.md
================================================
---
name: Community mistake
about: Propose a new community mistake
title: ''
labels: community mistake
---
**Describe the mistake**
Describe the mistake and the context.
**Solution**
One or multiple solutions to avoid the mistake.
================================================
FILE: .github/ISSUE_TEMPLATE/erratum.md
================================================
---
name: Erratum
about: Suggest a book correction
title: ''
labels: erratum
---
**Describe the book error**
Describe a factual error in the book and suggest a correction.
================================================
FILE: .github/workflows/ci.yml
================================================
name: ci
on:
push:
branches:
- master
- main
permissions:
contents: write
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.x
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
- uses: actions/cache@v3
with:
key: mkdocs-material-${{ env.cache_id }}
path: .cache
restore-keys: |
mkdocs-material-
- run: pip install mkdocs-material
- run: pip install pillow cairosvg
- run: pip install mkdocs-glightbox
- run: mkdocs gh-deploy --force
================================================
FILE: .gitignore
================================================
.idea
*.out
100-go-mistakes.iml
================================================
FILE: CONTRIBUTING.md
================================================
# Contributing to 100 Go Mistakes
First of all, thank you for taking the time to contribute! 🎉
We aim to create a collaborative space that could become the canonical place to find common mistakes to avoid in Go.
If you want to participate, please look at the open issues. The 100 Go Mistakes repository is a documentation project, so all the content that can be enriched is in the
[/docs](https://github.com/teivah/100-go-mistakes/tree/master/docs) folder. The main file is [index.md](https://github.com/teivah/100-go-mistakes/blob/master/docs/index.md),
the one containing the main content of [100go.co](https://100go.co/).
If you submit a PR, please do not care about the HTML generation (the website uses MkDocs), I'll take care of it. Said differently, most PRs should
only contain modifications to .md files.
================================================
FILE: LICENSE.md
================================================
# Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International
Creative Commons Corporation (“Creative Commons”) is not a law firm and does not provide legal services or legal advice. Distribution of Creative Commons public licenses does not create a lawyer-client or other relationship. Creative Commons makes its licenses and related information available on an “as-is” basis. Creative Commons gives no warranties regarding its licenses, any material licensed under their terms and conditions, or any related information. Creative Commons disclaims all liability for damages resulting from their use to the fullest extent possible.
### Using Creative Commons Public Licenses
Creative Commons public licenses provide a standard set of terms and conditions that creators and other rights holders may use to share original works of authorship and other material subject to copyright and certain other rights specified in the public license below. The following considerations are for informational purposes only, are not exhaustive, and do not form part of our licenses.
* __Considerations for licensors:__ Our public licenses are intended for use by those authorized to give the public permission to use material in ways otherwise restricted by copyright and certain other rights. Our licenses are irrevocable. Licensors should read and understand the terms and conditions of the license they choose before applying it. Licensors should also secure all rights necessary before applying our licenses so that the public can reuse the material as expected. Licensors should clearly mark any material not subject to the license. This includes other CC-licensed material, or material used under an exception or limitation to copyright. [More considerations for licensors](http://wiki.creativecommons.org/Considerations_for_licensors_and_licensees#Considerations_for_licensors).
* __Considerations for the public:__ By using one of our public licenses, a licensor grants the public permission to use the licensed material under specified terms and conditions. If the licensor’s permission is not necessary for any reason–for example, because of any applicable exception or limitation to copyright–then that use is not regulated by the license. Our licenses grant only permissions under copyright and certain other rights that a licensor has authority to grant. Use of the licensed material may still be restricted for other reasons, including because others have copyright or other rights in the material. A licensor may make special requests, such as asking that all changes be marked or described. Although not required by our licenses, you are encouraged to respect those requests where reasonable. [More considerations for the public](http://wiki.creativecommons.org/Considerations_for_licensors_and_licensees#Considerations_for_licensees).
## Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License
By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions.
### Section 1 – Definitions.
a. __Adapted Material__ means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image.
b. __Copyright and Similar Rights__ means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights.
e. __Effective Technological Measures__ means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements.
f. __Exceptions and Limitations__ means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material.
h. __Licensed Material__ means the artistic or literary work, database, or other material to which the Licensor applied this Public License.
i. __Licensed Rights__ means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license.
h. __Licensor__ means the individual(s) or entity(ies) granting rights under this Public License.
i. __NonCommercial__ means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of this Public License, the exchange of the Licensed Material for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange.
j. __Share__ means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them.
k. __Sui Generis Database Rights__ means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world.
l. __You__ means the individual or entity exercising the Licensed Rights under this Public License. __Your__ has a corresponding meaning.
### Section 2 – Scope.
a. ___License grant.___
1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to:
A. reproduce and Share the Licensed Material, in whole or in part, for NonCommercial purposes only; and
B. produce and reproduce, but not Share, Adapted Material for NonCommercial purposes only.
2. __Exceptions and Limitations.__ For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions.
3. __Term.__ The term of this Public License is specified in Section 6(a).
4. __Media and formats; technical modifications allowed.__ The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material.
5. __Downstream recipients.__
A. __Offer from the Licensor – Licensed Material.__ Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License.
B. __No downstream restrictions.__ You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material.
6. __No endorsement.__ Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i).
b. ___Other rights.___
1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise.
2. Patent and trademark rights are not licensed under this Public License.
3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties, including when the Licensed Material is used other than for NonCommercial purposes.
### Section 3 – License Conditions.
Your exercise of the Licensed Rights is expressly made subject to the following conditions.
a. ___Attribution.___
1. If You Share the Licensed Material, You must:
A. retain the following if it is supplied by the Licensor with the Licensed Material:
i. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated);
ii. a copyright notice;
iii. a notice that refers to this Public License;
iv. a notice that refers to the disclaimer of warranties;
v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable;
B. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and
C. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License.
For the avoidance of doubt, You do not have permission under this Public License to Share Adapted Material.
2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information.
3. If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable.
### Section 4 – Sui Generis Database Rights.
Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material:
a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database for NonCommercial purposes only and provided You do not Share Adapted Material;
b. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material; and
c. You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database.
For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights.
### Section 5 – Disclaimer of Warranties and Limitation of Liability.
a. __Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You.__
b. __To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You.__
c. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability.
### Section 6 – Term and Termination.
a. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically.
b. Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates:
1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or
2. upon express reinstatement by the Licensor.
For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License.
c. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License.
d. Sections 1, 5, 6, 7, and 8 survive termination of this Public License.
### Section 7 – Other Terms and Conditions.
a. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed.
b. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License.
### Section 8 – Interpretation.
a. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License.
b. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions.
c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor.
d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority.
> Creative Commons is not a party to its public licenses. Notwithstanding, Creative Commons may elect to apply one of its public licenses to material it publishes and in those instances will be considered the “Licensor.” Except for the limited purpose of indicating that material is shared under a Creative Commons public license or as otherwise permitted by the Creative Commons policies published at [creativecommons.org/policies](http://creativecommons.org/policies), Creative Commons does not authorize the use of the trademark “Creative Commons” or any other trademark or logo of Creative Commons without its prior written consent including, without limitation, in connection with any unauthorized modifications to any of its public licenses or any other arrangements, understandings, or agreements concerning use of licensed material. For the avoidance of doubt, this paragraph does not form part of the public licenses.
>
> Creative Commons may be contacted at [creativecommons.org](http://creativecommons.org).
================================================
FILE: README.md
================================================
# 100 Go Mistakes and How to Avoid Them
Source code and community space of 📖 _100 Go Mistakes and How to Avoid Them_, published by Manning in August 2022.
Read online: [100go.co](https://100go.co).
## Powered by
[](https://jb.gg/OpenSource)
================================================
FILE: docs/20-slice.md
================================================
---
title: Not understanding slice length and capacity (#20)
comments: true
hide:
- toc
---
# Not understanding slice length and capacity

It’s 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’s 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’s 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’s go through a few examples to make things clearer. First, let’s initialize a slice with a given length and capacity:
```go
s := make([]int, 3, 6) // Three-length, six-capacity slice
```
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’s second element: s[1] = 1.
However, accessing an element outside the length range is forbidden, even though it’s 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
```
How can we use the remaining space of the slice? By using the `append` built-in function:
```go
s = append(s, 2)
```
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’t large enough?
```go
s = append(s, 3, 4, 5)
fmt.Println(s)
```
If we run this code, we see that the slice was able to cope with our request:
```
[0 1 0 2 3 4 5]
```
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’s no longer referenced, it’s eventually freed by the garbage collector (GC) if allocated on the heap. (We discuss heap memory in mistake #95, “[Not understanding stack vs. heap](https://100go.co#not-understanding-stack-vs-heap-95),” and we look at how the GC works in mistake #99, “[Not understanding how the GC works](https://100go.co#not-understanding-how-the-gc-works-99).”)
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:
```go
s1 := make([]int, 3, 6) // Three-length, six-capacity slice
s2 := s1[1:3] // Slicing from indices 1 to 3
```

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?
```go
s2 = append(s2, 2)
```
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`:
```go
s1=[0 1 0], s2=[1 0 2]
```
It’s important to understand this behavior so that we don’t 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’s add three more elements so that the backing array will not have enough capacity:
```go
s2 = append(s2, 3)
s2 = append(s2, 4) // At this stage, the backing is already full
s2 = append(s2, 5)
```
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’s 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.
================================================
FILE: docs/28-maps-memory-leaks.md
================================================
---
title: Maps and memory leaks (#28)
comments: true
hide:
- toc
---
# Maps and memory leaks

When working with maps in Go, we need to understand some important characteristics of how a map grows and shrinks. Let’s delve into this to prevent issues that can cause memory leaks.
First, to view a concrete example of this problem, let’s design a scenario where we will work with the following map:
```go
m := make(map[int][128]byte)
```
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:
```go
func main() {
n := 1_000_000
m := make(map[int][128]byte)
printAlloc()
for i := 0; i < n; i++ { // Adds 1 million elements
m[i] = [128]byte{}
}
printAlloc()
for i := 0; i < n; i++ { // Deletes 1 million elements
delete(m, i)
}
runtime.GC() // Triggers a manual GC
printAlloc()
runtime.KeepAlive(m) // Keeps a reference to m so that the map isn’t collected
}
func printAlloc() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("%d MB\n", m.Alloc/(1024*1024))
}
```
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`](https://pkg.go.dev/runtime#KeepAlive) so that the map isn’t collected as well. Let’s run this example:
```
0 MB <-- After m is allocated
461 MB <-- After we add 1 million elements
293 MB <-- After we remove 1 million elements
```
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’t 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’s 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](https://github.com/golang/go/blob/0262ea1ff9ac3b9fd268a48fcaaa6811c20cbea2/src/runtime/map.go#L117-L131) struct. This struct contains multiple fields, including a B field, giving the number of buckets in the map:
```go
type hmap struct {
B uint8 // log_2 of # of buckets
// (can hold up to loadFactor * 2^B items)
// ...
}
```
After adding 1 million elements, the value of B equals 18, which means 2¹⁸ = 262,144 buckets. When we remove 1 million elements, what’s 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’t 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’t impact the map itself. Even the number of extra buckets (the buckets created because of overflows) remains the same.
Let’s 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’t worry about the fact that a map cannot shrink.
However, let’s 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’t significantly decrease in such a scenario.
What are the solutions if we don’t 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’t 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’s 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’t 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.
================================================
FILE: docs/5-interface-pollution.md
================================================
---
title: Interface pollution (#5)
comments: true
hide:
- toc
status: new
---
# Interface pollution

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’s a common mistake made by developers coming from another language with different habits. Before delving into the topic, let’s refresh our minds about Go’s interfaces. Then, we will see when it’s appropriate to use interfaces and when it may be considered pollution.
## Concepts
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:
```go
type Reader interface {
Read(p []byte) (n int, err error)
}
```
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:
```go
type Writer interface {
Write(p []byte) (n int, err error)
}
```
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:
* `io.Reader` reads data from a source
* `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.File`. Or, we can choose to create a more generic function using `io.Reader` and `io.Writer` abstractions:
```go
func copySourceToDest(source io.Reader, dest io.Writer) error {
// ...
}
```
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:
```go
func TestCopySourceToDest(t *testing.T) {
const input = "foo"
source := strings.NewReader(input) // Creates an io.Reader
dest := bytes.NewBuffer(make([]byte, 0)) // Creates an io.Writer
err := copySourceToDest(source, dest) // Calls copySourceToDest from a *strings.Reader and a *bytes.Buffer
if err != nil {
t.FailNow()
}
got := dest.String()
if got != input {
t.Errorf("expected: %s, got: %s", input, got)
}
}
```
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](https://www.youtube.com/watch?v=PAAkCSZUG1c&t=318s) in Go relates to how big an interface should be:
!!! quote "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:
```go
type ReadWriter interface {
Reader
Writer
}
```
???+ note
As Einstein said, “_Everything should be made as simple as possible, but no simpler._” Applied to interfaces, this denotes that finding the perfect granularity for an interface isn’t necessarily a straightforward process.
Let’s now discuss common cases where interfaces are recommended.
## When to use interfaces
When should we create interfaces in Go? Let’s look at three concrete use cases where interfaces are usually considered to bring value. Note that the goal isn’t 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:
* Common behavior
* Decoupling
* Restricting behavior
### Common behavior
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:
* Retrieving the number of elements in the collection
* Reporting whether one element must be sorted before another
* Swapping two elements
Hence, the following interface was added to the `sort` package:
```go
type Interface interface {
Len() int // Number of elements
Less(i, j int) bool // Checks two elements
Swap(i, j int) // Swaps two elements
}
```
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’t 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:
```go
func IsSorted(data Interface) bool {
n := data.Len()
for i := n - 1; i > 0; i-- {
if data.Less(i, i-1) {
return false
}
}
return true
}
```
Because `sort.Interface` is the right level of abstraction, it makes it highly valuable.
Let’s now see another main use case when using interfaces.
### Decoupling
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’s [SOLID](https://en.wikipedia.org/wiki/SOLID) design principles).
One benefit of decoupling can be related to unit testing. Let’s 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’s say a `mysql.Store` struct):
```go
type CustomerService struct {
store mysql.Store // Depends on the concrete implementation
}
func (cs CustomerService) CreateNewCustomer(id string) error {
customer := Customer{id: id}
return cs.store.StoreCustomer(customer)
}
```
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`](https://github.com/DATA-DOG/go-sqlmock), but this isn’t the scope of this section). Although integration tests are helpful, that’s 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:
```go
type customerStorer interface { // Creates a storage abstraction
StoreCustomer(Customer) error
}
type CustomerService struct {
storer customerStorer // Decouples CustomerService from the actual implementation
}
func (cs CustomerService) CreateNewCustomer(id string) error {
customer := Customer{id: id}
return cs.storer.StoreCustomer(customer)
}
```
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:
* Use the concrete implementation via integration tests
* Use a mock (or any kind of test double) via unit tests
* Or both
Let’s now discuss another use case: to restrict a behavior.
### Restricting behavior
The last use case we will discuss can be pretty counterintuitive at first sight. It’s about restricting a type to a specific behavior. Let’s 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’s how that code would look:
```go
type IntConfig struct {
// ...
}
func (c *IntConfig) Get() int {
// Retrieve configuration
}
func (c *IntConfig) Set(value int) {
// Update configuration
}
```
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’t want to change our configuration package? By creating an abstraction that restricts the behavior to retrieving only a config value:
```go
type intConfigGetter interface {
Get() int
}
```
Then, in our code, we can rely on `intConfigGetter` instead of the concrete implementation:
```go
type Foo struct {
threshold intConfigGetter
}
func NewFoo(threshold intConfigGetter) Foo { // Injects the configuration getter
return Foo{threshold: threshold}
}
func (f Foo) Bar() {
threshold := f.threshold.Get() // Reads the configuration
// ...
}
```
In this example, the configuration getter is injected into the `NewFoo` factory method. It doesn’t 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’t exhaustive, but it should give us a general understanding of when interfaces are helpful in Go.
Now, let’s finish this section and discuss the problems with interface pollution.
## Interface pollution
It’s fairly common to see interfaces being overused in Go projects. Perhaps the developer’s background was C# or Java, and they found it natural to create interfaces before concrete types. However, this isn’t 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’t start creating abstractions in our code if there is no immediate reason to do so. We shouldn’t 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’s 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’t bring any value; it creates a worthless abstraction making the code more difficult to read, understand, and reason about. If we don’t have a strong reason for adding an interface and it’s unclear how an interface makes a code better, we should challenge this interface’s 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’s data structure to find the concrete type an interface points to. But this isn’t an issue in many contexts as the overhead is minimal.
In summary, we should be cautious when creating abstractions in our code—abstractions should be discovered, not created. It’s 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.
!!! quote "Rob Pike"
Don’t design with interfaces, discover them.
Let’s not try to solve a problem abstractly but solve what has to be solved now. Last, but not least, if it’s unclear how an interface makes the code better, we should probably consider removing it to make our code simpler.
================================================
FILE: docs/56-concurrency-faster.md
================================================
---
title: Thinking concurrency is always faster (#56)
comments: true
hide:
- toc
---
# Thinking concurrency is always faster

A misconception among many developers is believing that a concurrent solution is always faster than a sequential one. This couldn’t 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’t necessarily faster.
## Go Scheduling
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:
* _Concurrent_ — Two or more threads can start, run, and complete in overlapping time periods.
* _Parallel_ — The same task can be executed multiple times at once.
The OS is responsible for scheduling the thread’s processes optimally so that:
* All the threads can consume CPU cycles without being starved for too much time.
* The workload is distributed as evenly as possible among the different CPU cores.
???+ 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](https://en.wikipedia.org/wiki/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’s 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’t 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–32, 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’s now discuss how the Go scheduler works to overview how goroutines are handled. Internally, the Go scheduler uses the following terminology (see [proc.go](https://github.com/golang/go/blob/go1.17.6/src/runtime/proc.go#L22)):
* _G_ — Goroutine
* _M_ — OS thread (stands for machine)
* _P_ — CPU core (stands for processor)
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:
* _Executing_ — The goroutine is scheduled on an M and executing its instructions.
* _Runnable_ — The goroutine is waiting to be in an executing state.
* _Waiting_ — The goroutine is stopped and pending something completing, such as a system call or a synchronization operation (such as acquiring a mutex).
There’s 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’t 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’s no goroutine to be executed. This isn’t 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’s the scheduling implementation in pseudocode (see [proc.go](https://github.com/golang/go/blob/go1.17.6/src/runtime/proc.go#L3291)):
```
runtime.schedule() {
// Only 1/61 of the time, check the global runnable queue for a G.
// If not found, check the local queue.
// If not found,
// Try to steal from other Ps.
// If not, check the global runnable queue.
// If not found, poll network.
}
```
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’s 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’s look at a concrete example: implementing a merge sort in a parallel manner.
## Parallel Merge Sort
First, let’s briefly review how the merge sort algorithm works. Then we will implement a parallel version. Note that the objective isn’t to implement the most efficient version but to support a concrete example showing why concurrency isn’t 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’t include all of the code as it’s not the main point of this section:
```go
func sequentialMergesort(s []int) {
if len(s) <= 1 {
return
}
middle := len(s) / 2
sequentialMergesort(s[:middle]) // First half
sequentialMergesort(s[middle:]) // Second half
merge(s, middle) // Merges the two halves
}
func merge(s []int, middle int) {
// ...
}
```
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’t 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’s write a first parallel implementation:
```go
func parallelMergesortV1(s []int) {
if len(s) <= 1 {
return
}
middle := len(s) / 2
var wg sync.WaitGroup
wg.Add(2)
go func() { // Spins up the first half of the work in a goroutine
defer wg.Done()
parallelMergesortV1(s[:middle])
}()
go func() { // Spins up the second half of the work in a goroutine
defer wg.Done()
parallelMergesortV1(s[middle:])
}()
wg.Wait()
merge(s, middle) // Merges the halves
}
```
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’s run it on a four-core machine with 10,000 elements:
```
Benchmark_sequentialMergesort-4 2278993555 ns/op
Benchmark_parallelMergesortV1-4 17525998709 ns/op
```
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’s 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’re 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’s try another approach. Because merging a tiny number of elements within a new goroutine isn’t efficient, let’s 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’s a new version:
```go
const max = 2048 // Defines the threshold
func parallelMergesortV2(s []int) {
if len(s) <= 1 {
return
}
if len(s) <= max {
sequentialMergesort(s) // Calls our initial sequential version
} else { // If bigger than the threshold, keeps the parallel version
middle := len(s) / 2
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
parallelMergesortV2(s[:middle])
}()
go func() {
defer wg.Done()
parallelMergesortV2(s[middle:])
}()
wg.Wait()
merge(s, middle)
}
}
```
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
Benchmark_parallelMergesortV1-4 17525998709 ns/op
Benchmark_parallelMergesortV2-4 1313010260 ns/op
```
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’s also pretty interesting to note that running the same algorithm in a programming language that doesn’t 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.
## Conclusion
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’t 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’t always faster and shouldn’t 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’t use concurrency? Of course not. However, it’s essential to keep these conclusions in mind. If we’re 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, “[Not using Go diagnostics tooling](https://100go.co/98-profiling-execution-tracing/)”) and benchmarks (mistake #89, “[Writing inaccurate benchmarks](https://100go.co/89-benchmarks/)”), for example. It can be the only way to ensure that a concurrent implementation is worth it.
================================================
FILE: docs/89-benchmarks.md
================================================
---
title: Writing inaccurate benchmarks (#89)
comments: true
hide:
- toc
---
# Writing inaccurate benchmarks

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’s rarely a bad idea to test them. However, writing benchmarks isn’t 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:
* Not resetting or pausing the timer
* Making wrong assumptions about micro-benchmarks
* Not being careful about compiler optimizations
* Being fooled by the observer effect
## General concepts
Before discussing these traps, let’s briefly review how benchmarks work in Go. The skeleton of a benchmark is as follows:
```go
func BenchmarkFoo(b *testing.B) {
for i := 0; i < b.N; i++ {
foo()
}
}
```
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=.
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkFoo-4 73 16511228 ns/op
```
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
BenchmarkFoo-4 150 15832169 ns/op
```
`foo` was executed roughly twice more than during the previous benchmark.
Next, let’s look at some common traps.
## Not resetting or pausing the timer
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:
```go
func BenchmarkFoo(b *testing.B) {
expensiveSetup()
for i := 0; i < b.N; i++ {
functionUnderTest()
}
}
```
In this case, we can use the `ResetTimer` method before entering the loop:
```go
func BenchmarkFoo(b *testing.B) {
expensiveSetup()
b.ResetTimer() // Reset the benchmark timer
for i := 0; i < b.N; i++ {
functionUnderTest()
}
}
```
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?
```go
func BenchmarkFoo(b *testing.B) {
for i := 0; i < b.N; i++ {
expensiveSetup()
functionUnderTest()
}
}
```
We can’t 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`:
```go
func BenchmarkFoo(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer() // Pause the benchmark timer
expensiveSetup()
b.StartTimer() // Resume the benchmark timer
functionUnderTest()
}
}
```
Here, we pause the benchmark timer to perform the expensive setup and then resume the timer.
???+ note
There’s 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.
## Making wrong assumptions about micro-benchmarks
A micro-benchmark measures a tiny computation unit, and it can be extremely easy to make wrong assumptions about it. Let’s say, for example, that we aren’t 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:
```go
func BenchmarkAtomicStoreInt32(b *testing.B) {
var v int32
for i := 0; i < b.N; i++ {
atomic.StoreInt32(&v, 1)
}
}
func BenchmarkAtomicStoreInt64(b *testing.B) {
var v int64
for i := 0; i < b.N; i++ {
atomic.StoreInt64(&v, 1)
}
}
```
If we run this benchmark, here’s some example output:
```
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkAtomicStoreInt32
BenchmarkAtomicStoreInt32-4 197107742 5.682 ns/op
BenchmarkAtomicStoreInt64
BenchmarkAtomicStoreInt64-4 213917528 5.134 ns/op
```
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
BenchmarkAtomicStoreInt64-4 224900722 5.434 ns/op
BenchmarkAtomicStoreInt32
BenchmarkAtomicStoreInt32-4 230253900 5.159 ns/op
```
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’s 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
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkAtomicStoreInt32-4 234935682 5.124 ns/op
BenchmarkAtomicStoreInt32-4 235307204 5.112 ns/op
// ...
BenchmarkAtomicStoreInt64-4 235548591 5.107 ns/op
BenchmarkAtomicStoreInt64-4 235210292 5.090 ns/op
// ...
```
We can then run `benchstat` on this file:
```
$ benchstat stats.txt
name time/op
AtomicStoreInt32-4 5.10ns ± 1%
AtomicStoreInt64-4 5.10ns ± 1%
```
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: ± 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’s 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.
## Not being careful about compiler optimizations
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):
```go
const m1 = 0x5555555555555555
const m2 = 0x3333333333333333
const m4 = 0x0f0f0f0f0f0f0f0f
const h01 = 0x0101010101010101
func popcnt(x uint64) uint64 {
x -= (x >> 1) & m1
x = (x & m2) + ((x >> 2) & m2)
x = (x + (x >> 4)) & m4
return (x * h01) >> 56
}
```
This function takes and returns a `uint64`. To benchmark this function, we can write the following:
```go
func BenchmarkPopcnt1(b *testing.B) {
for i := 0; i < b.N; i++ {
popcnt(uint64(i))
}
}
```
However, if we execute this benchmark, we get a surprisingly low result:
```
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkPopcnt1-4 1000000000 0.2858 ns/op
```
A duration of 0.28 nanoseconds is roughly one clock cycle, so this number is unreasonably low. The problem is that the developer wasn’t 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:
```go
func BenchmarkPopcnt1(b *testing.B) {
for i := 0; i < b.N; i++ {
// Empty
}
}
```
The benchmark is now empty — 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:
```go
var global uint64 // Define a global variable
func BenchmarkPopcnt2(b *testing.B) {
var v uint64 // Define a local variable
for i := 0; i < b.N; i++ {
v = popcnt(uint64(i)) // Assign the result to the local variable
}
global = v // Assign the result to the global variable
}
```
`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: “[Not understanding stack vs. heap](https://100go.co#not-understanding-stack-vs-heap-95)”). 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
BenchmarkPopcnt1-4 1000000000 0.2858 ns/op
BenchmarkPopcnt2-4 606402058 1.993 ns/op
```
`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’s 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.
## Being fooled by the observer effect
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’s 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:
```go
func calculateSum512(s [][512]int64) int64 {
var sum int64
for i := 0; i < len(s); i++ { // Iterate over each row
for j := 0; j < 8; j++ { // Iterate over the first eight columns
sum += s[i][j] // Increment sum
}
}
return sum
}
func calculateSum513(s [][513]int64) int64 {
// Same implementation as calculateSum512
}
```
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:
```go
const rows = 1000
var res int64
func BenchmarkCalculateSum512(b *testing.B) {
var sum int64
s := createMatrix512(rows) // Create a matrix of 512 columns
b.ResetTimer()
for i := 0; i < b.N; i++ {
sum = calculateSum512(s) // Create a matrix of 512 columns
}
res = sum
}
func BenchmarkCalculateSum513(b *testing.B) {
var sum int64
s := createMatrix513(rows) // Create a matrix of 513 columns
b.ResetTimer()
for i := 0; i < b.N; i++ {
sum = calculateSum513(s) // Calculate the sum
}
res = sum
}
```
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’t the case (on my machine):
```
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkCalculateSum512-4 81854 15073 ns/op
BenchmarkCalculateSum513-4 161479 7358 ns/op
```
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’s 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’t in the scope of this post to explain why, but we look at this problem in 100 Go Mistakes, mistake #91: “[Not understanding CPU caches.](https://100go.co#not-understanding-cpu-caches-91)”
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’t measure the function’s 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:
```go
func BenchmarkCalculateSum512(b *testing.B) {
var sum int64
for i := 0; i < b.N; i++ {
b.StopTimer()
s := createMatrix512(rows) // Create a new matrix during each loop iteration
b.StartTimer()
sum = calculateSum512(s)
}
res = sum
}
```
A new matrix is now created during each loop iteration. If we run the benchmark again (and adjust `benchtime` — otherwise, it takes too long to execute), the results are closer to each other:
```
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkCalculateSum512-4 1116 33547 ns/op
BenchmarkCalculateSum513-4 998 35507 ns/op
```
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.
================================================
FILE: docs/9-generics.md
================================================
---
title: Being confused about when to use generics (#9)
comments: true
hide:
- toc
---
# Being confused about when to use generics
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.
## Concepts
Consider the following function that extracts all the keys from a `map[string]int` type:
```go
func getKeys(m map[string]int) []string {
var keys []string
for k := range m {
keys = append(keys, k)
}
return keys
}
```
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:
```go
func getKeys(m any) ([]any, error) {
switch t := m.(type) {
default:
return nil, fmt.Errorf("unknown type: %T", t)
case map[string]int:
var keys []any
for k := range t {
keys = append(keys, k)
}
return keys, nil
case map[int]string:
// Copy the extraction logic
}
}
```
We can start noticing a couple of issues:
* First, it increases boilerplate code. Indeed, whenever we want to add a case, it will require duplicating the `range` loop.
* Meanwhile, the function now accepts an empty interface, which means we are losing some of the benefits of Go being a typed language. Indeed, checking whether a type is supported is done at runtime instead of compile-time. Hence, we also need to return an error if the provided type is unknown.
* Last but not least, as the key type can be either `int` or `string`, we are obliged to return a slice of empty interfaces to factor out key types. This approach increases the effort on the caller-side as the client may also have to perform a type check of the keys or extra conversion.
Thanks to generics, we can now refactor this code using type parameters.
Type parameters are generic types we can use with functions and types. For example, the following function accepts a type parameter:
```go
func foo[T any](t T) {
// ...
}
```
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’s get back to the `getKeys` function and use type parameters to write a generic version that would accept any kind of map:
```go
func getKeys[K comparable, V any](m map[K]V) []K {
var keys []K
for k := range m {
keys = append(keys, k)
}
return keys
}
```
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’t be of any type. For example, we cannot use slices:
```go
var m map[[]byte]int
```
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:
* A set of behaviors (methods)
* But also arbitrary type
Let’s see a concrete example for the latter. Imagine we don’t 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:
```go
type customConstraint interface {
~int | ~string // Define a custom type that will restrict types to int and string
}
// Change the type parameter K to be custom
func getKeys[K customConstraint, V any](m map[K]V) []K {
// Same implementation
}
```
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:
```go
m = map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
keys := getKeys(m)
```
Note that Go can infer that `getKeys` is called with a `string` type argument. The previous call was similar to this:
```go
keys := getKeys[string](m)
```
???+ note
What’s 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’s imagine a constraint where we would like to restrict a type to any `int` type implementing the `String() string` method:
```go
type customConstraint interface {
~int
String() string
}
```
Using this constraint will restrict type arguments to custom types like this one:
```go
type customInt int
func (i customInt) String() string {
return strconv.Itoa(int(i))
}
```
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’t implement `String() string`.
Let’s also note the `constraints` package contains a set of common constraints such as `Signed` that includes all the signed integer types. Let’s ensure that a constraint doesn’t 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:
```go
type Node[T any] struct { // Use type parameter
Val T
next *Node[T]
}
func (n *Node[T]) Add(next *Node[T]) { // Instantiate type receiver
n.next = next
}
```
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’t be used on methods, only on functions. For example, the following method wouldn’t compile:
```go
type Foo struct {}
func (Foo) bar[T any](t T) {}
```
```
./main.go:29:15: methods cannot have type parameters
```
Now, let’s delve into concrete cases where we should and shouldn’t use generics.
## Common uses and misuses
So when are generics useful? Let’s discuss a couple of common uses where generics **are** recommended:
* Data structures. For example, we can use generics to factor out the element type if we implement a binary tree, a linked list, or a heap.
* Functions working with slices, maps, and channels of any type. For example, a function to merge two channels would work with any channel type. Hence, we could use type parameters to factor out the channel type:
```go
func merge[T any](ch1, ch2 <-chan T) <-chan T {
// ...
}
```
* Meanwhile, instead of factoring out a type, we can factor out behaviors. For example, the `sort` package contains functions to sort different slice types such as `sort.Ints` or `sort.Float64s`. Using type parameters, we can factor out the sorting behaviors that rely on three methods, `Len`, `Less`, and `Swap`:
```go
type sliceFn[T any] struct { // Use type parameter
s []T
compare func(T, T) bool // Compare two T elements
}
func (s sliceFn[T]) Len() int { return len(s.s) }
func (s sliceFn[T]) Less(i, j int) bool { return s.compare(s.s[i], s.s[j]) }
func (s sliceFn[T]) Swap(i, j int) { s.s[i], s.s[j] = s.s[j], s.s[i] }
```
Conversely, when is it recommended **not** to use generics?
* When just calling a method of the type argument. For example, consider a function that receives an `io.Writer` and call the `Write` method:
```go
func foo[T io.Writer](w T) {
b := getBytes()
_, _ = w.Write(b)
}
```
* When it makes our code more complex. Generics are never mandatory, and as Go developers, we have been able to live without them for more than a decade. If writing generic functions or structures we figure out that it doesn’t make our code clearer, we should probably reconsider our decision for this particular use case.
## Conclusion
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’s not pollute our code with needless abstractions, and let’s focus on solving concrete problems for now. It means that we shouldn’t use type parameters prematurely. Let’s wait until we are about to write boilerplate code to consider using generics.
================================================
FILE: docs/92-false-sharing.md
================================================
---
title: Writing concurrent code that leads to false sharing (#92)
comments: true
hide:
- toc
status: new
---
# Writing concurrent code that leads to false sharing

In previous sections, we have discussed the fundamental concepts of CPU caching. We have seen that some specific caches (typically, L1 and L2) aren’t 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’s 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`:
```go
type Input struct {
a int64
b int64
}
type Result struct {
sumA int64
sumB int64
}
```
The goal is to implement a `count` function that receives a slice of `Input` and computes the following:
* The sum of all the `Input.a` fields into `Result.sumA`
* The sum of all the `Input.b` fields into `Result.sumB`
For the sake of the example, we implement a concurrent solution with one goroutine that computes `sumA` and another that computes `sumB`:
```go
func count(inputs []Input) Result {
wg := sync.WaitGroup{}
wg.Add(2)
result := Result{} // Init the result struct
go func() {
for i := 0; i < len(inputs); i++ {
result.sumA += inputs[i].a // Computes sumA
}
wg.Done()
}()
go func() {
for i := 0; i < len(inputs); i++ {
result.sumB += inputs[i].b // Computes sumB
}
wg.Done()
}()
wg.Wait()
return result
}
```
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’t 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’s 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’s 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’t 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’t a conflict, but this isn’t the case. When we write a variable that’s in a cache, the granularity tracked by the CPU isn’t the variable: it’s 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](https://en.wikipedia.org/wiki/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’t real—it’s an illusion. This understanding comes from the fact that we don’t 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’ve shown but ensure that `sumA` and `sumB` aren’t 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 – 8 = 56 bytes of padding:
```go hl_lines="3"
type Result struct {
sumA int64
_ [56]byte // Padding
sumB int64
}
```
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.
================================================
FILE: docs/98-profiling-execution-tracing.md
================================================
---
title: Not using Go diagnostics tooling (#98)
comments: true
hide:
- toc
---
# Not using Go diagnostics tooling

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’s discuss profiling.
## Profiling
Profiling provides insights into the execution of an application. It allows us to resolve performance issues, detect contention, locate memory leaks, and more. These insights can be collected via several profiles:
* `CPU`— Determines where an application spends its time
* `Goroutine`— Reports the stack traces of the ongoing goroutines
* `Heap`— Reports heap memory allocation to monitor current memory usage and check for possible memory leaks
* `Mutex`— Reports lock contentions to see the behaviors of the mutexes used in our code and whether an application spends too much time in locking calls
* `Block`— Shows where goroutines block waiting on synchronization primitives
Profiling is achieved via instrumentation using a tool called a profiler, in Go: `pprof`. First, let’s understand how and when to enable `pprof`; then, we discuss the most critical profile types.
### Enabling pprof
There are several ways to enable `pprof`. For example, we can use the `net/http/pprof` package to serve the profiling data via HTTP:
```go
package main
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof" // Blank import to pprof
)
func main() {
// Exposes an HTTP endpoint
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "")
})
log.Fatal(http.ListenAndServe(":80", nil))
}
```
Importing `net/http/pprof` leads to a side effect that allows us to reach the pprof URL: [http://host/debug/pprof](http://host/debug/pprof). Note that enabling `pprof` is safe even in production ([https://go.dev/doc/diagnostics#profiling](https://go.dev/doc/diagnostics#profiling)). The profiles that impact performance, such as CPU profiling, aren’t 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’s discuss the most common profiles.
### CPU Profiling
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
```
From this file, we can navigate to the results using `go tool`:
```
$ go tool pprof -http=:8080
```
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:
* Too many calls to `runtime.mallogc` can mean an excessive number of small heap allocations that we can try to minimize.
* Too much time spent in channel operations or mutex locks can indicate excessive contention that is harming the application’s performance.
* Too much time spent on `syscall.Read` or `syscall.Write` means the application spends a significant amount of time in Kernel mode. Working on I/O buffering may be an avenue for improvement.
These are the kinds of insights we can get from the CPU profiler. It’s valuable to understand the hottest code path and identify bottlenecks. But it won’t 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`.
### Heap Profiling
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’t 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 — 1024 KB — were allocated in another method.

Figure 3: A heap graph.
This is how we can navigate the call chain to understand what part of an application is responsible for most of the heap allocations. We can also look at different sample types:
* `alloc_objects`— Total number of objects allocated
* `alloc_space`— Total amount of memory allocated
* `inuse_object`s — Number of objects allocated and not yet released
* `inuse_space`— Amount of memory allocated and not yet released
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’s 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
```
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 (–513 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’t part of this profiling, which only focuses on the heap.
### Goroutine Profiling
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.
### Block Profiling
The `block` profile reports where ongoing goroutines block waiting on synchronization primitives. Possibilities include
* Sending or receiving on an unbuffered channel
* Sending to a full channel
* Receiving from an empty channel
* Mutex contention
* Network or filesystem waits
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’t 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’t call the /debug/pprof/block endpoint. Let’s be cautious if we want to set a high rate so we don’t 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]:
github.com/Shopify/sarama.(*syncProducer).SendMessages(0xc00071a090,
[CA]{0xc0009bb800, 0xfb, 0xfb})
/app/vendor/github.com/Shopify/sarama/sync_producer.go:117 +0x149
```
### Mutex Profiling
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’s accessible via /debug/pprof/mutex.
This profile works in a manner similar to that for blocking. It’s 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 haven’t mentioned the `threadcreate` profile because it’s been broken since 2013 ([https://github.com/golang/go/issues/6104](https://github.com/golang/go/issues/6104)).
* Be sure to enable only one profiler at a time: for example, do not enable CPU and heap profiling simultaneously. Doing so can lead to erroneous observations.
* `pprof` is extensible, and we can create our own custom profiles using `pprof.Profile`.
We have seen the most important profiles that we can enable to help us understand how an application performs and possible avenues for optimization. In general, enabling `pprof` is recommended, even in production, because in most cases it offers an excellent balance between its footprint and the amount of insight we can get from it. Some profiles, such as the CPU profile, lead to performance penalties but only during the time they are enabled.
Let’s now look at the execution tracer.
## Execution Tracer
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:
* Understanding runtime events such as how the GC performs
* Understanding how goroutines execute
* Identifying poorly parallelized execution
Let’s try it with an example given the Concurrency isn’t 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’s 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
```
???+ 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
2021/11/26 21:36:03 Parsing trace...
2021/11/26 21:36:31 Splitting trace...
2021/11/26 21:37:00 Opening browser. Trace viewer is listening on
http://127.0.0.1:54518
```
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 “stop the
world” 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’t 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’t 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’s 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’s 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:
```go
var v int64
// Creates a fibonacci task
ctx, fibTask := trace.NewTask(context.Background(), "fibonacci")
trace.WithRegion(ctx, "main", func() {
v = fibonacci(10)
})
fibTask.End()
// Creates a store task
ctx, fibStore := trace.NewTask(ctx, "store")
trace.WithRegion(ctx, "main", func() {
atomic.StoreInt64(&result, v)
})
fibStore.End()
```
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?
* CPU profiling:
* Sample-based
* Per function
* Doesn’t go below the sampling rate (10 ms by default)
* User-level traces:
* Not sample-based
* Per-goroutine execution (unless we use the `runtime/trace` package)
* Time executions aren’t bound by any rate
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’s 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.
================================================
FILE: docs/CNAME
================================================
100go.co
================================================
FILE: docs/book.md
================================================
---
hide:
- toc
---
# 100 Go Mistakes and How to Avoid Them

## Description
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](index.md) of the 100 mistakes or the [first chapter](chapter-1.md).
## Quotes and Ratings
!!! quote "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.
!!! quote "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.
!!! quote "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.
!!! quote "Anupam Sengupta"
Not having this will be the **101st mistake** a Go programmer could make.
{width="300"}
{width="300"}

Manning, Goodreads, and Amazon reviews: 4.7/5 avg rating
## Where to Buy?
* _100 Go Mistakes and How to Avoid Them_ (🇬🇧 edition: paper, digital, or audiobook)
* [Manning](https://www.manning.com/books/100-go-mistakes-and-how-to-avoid-them) (please make sure to use my personal discount code for -35%: `au35har`)
* [O’Reilly](https://www.oreilly.com/library/view/100-go-mistakes/9781617299599/)
* Amazon: [.com](https://www.amazon.com/dp/1617299596), [.co.uk](https://www.amazon.co.uk/dp/B0BBSNJR6B), [.de](https://www.amazon.de/dp/B0BBHQD8BQ), [.fr](https://www.amazon.fr/100-Mistakes-How-Avoid-Them/dp/1617299596), [.in](https://www.amazon.in/dp/B0BBHQD8BQ), [.co.jp](https://www.amazon.co.jp/dp/B0BBHQD8BQ), [.es](https://www.amazon.es/dp/B0BBHQD8BQ), [.it](https://www.amazon.it/dp/B0BBHQD8BQ), [.com.br](https://www.amazon.com.br/dp/B0BBHQD8BQ)
* _Go言語100Tips 開発者にありがちな間違いへの対処法_ (🇯🇵 edition: paper or digital)
* Amazon: [.co.jp](https://www.amazon.co.jp/exec/obidos/ASIN/4295017531/)
* _100个Go语言典型错误_ (🇨🇳 edition: paper or digital)
* [Douban.com](https://read.douban.com/ebook/455919353/)
* _Go 100가지 실수 패턴과 솔루션_ (🇰🇷 edition: paper or digital)
* [Yes24.com](https://m.yes24.com/Goods/Detail/124158773)
{width="200"}
{width="200"}
{width="170"}
{width="200"}
Covers (English, Japanese, Chinese, and Korean)
## About the Author
[Teiva Harsanyi](https://teivah.dev) 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.
================================================
FILE: docs/chapter-1.md
================================================
---
title: Read the First Chapter
hide:
- toc
---
# Go: Simple to learn but hard to master
This chapter covers
* What makes Go an efficient, scalable, and productive language
* Exploring why Go is simple to learn but hard to master
* Presenting the common types of mistakes made by developers
Making mistakes is part of everyone’s life. As Albert Einstein once said,
!!! quote "Albert Einstein"
A person who never made a mistake never tried anything new.
What matters in the end isn’t 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’t 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’ll discuss why, despite Go being considered simple to learn, mastering its nuances can be challenging. Finally, we’ll introduce the concepts this book covers.
## Go outline
If you are reading this book, it’s likely that you’re 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—sometimes even hundreds, if not thousands. Nowadays, code must be readable, expressive, and maintainable to guarantee a system’s 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](https://go.dev/doc/faq) gives us some insight:
!!! quote "Go FAQ"
Why does Go not have feature X? Your favorite feature may be missing because it doesn’t 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’s 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:
* _Stability_—Even though Go receives frequent updates (including improvements and security patches), it remains a stable language. Some may even consider this one of the best features of the language.
* _Expressivity_—We can define expressivity in a programming language by how naturally and intuitively we can write and read code. A reduced number of keywords and limited ways to solve common problems make Go an expressive language for large codebases.
* _Compilation_—As developers, what can be more exasperating than having to wait for a build to test our application? Targeting fast compilation times has always been a conscious goal for the language designers. This, in turn, enables productivity.
* _Safety_—Go is a strong, statically typed language. Hence, it has strict compiletime rules, which ensure the code is type-safe in most cases.
Go was built from the ground up with solid features such as outstanding concurrency primitives with goroutines and channels. There’s 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’t necessarily wrong. For example, a newcomer can learn the language’s main features in less than a day. So why read a book centered on the concept of mistakes if Go is simple?
## Simple doesn’t mean easy
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’s take concurrency, for example. In 2019, a study focusing on concurrency bugs was published: [Understanding Real-World Concurrency Bugs in Go](https://songlh.github.io/paper/go-study.pdf). 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’s not a question of confronting message passing versus sharing memory and determining the winner. However, it’s 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’t an easy topic in practice.
This leitmotif—simple doesn’t mean easy—can 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.
## 100 Go mistakes
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’re facing mistakes. [^1] Haven’t 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’t 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.
!!! quote "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
* Bugs
* Needless complexity
* Weaker readability
* Suboptimal or unidiomatic organization
* Lack of API convenience
* Under-optimized code
* Lack of productivity
We introduce each mistake category next.
### Bugs
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’t 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’s essential to make sure we avoid common bugs.
### Needless complexity
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.
### Weaker readability
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’t that important. However, today’s 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).
### Suboptimal or unidiomatic organization
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’ll 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.
### Lack of API convenience
Making common mistakes that weaken how convenient an API is for our clients is another type of mistake. If an API isn’t 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.
### Under-optimized code
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.
### Lack of productivity
In most cases, what’s the best language we can choose when working on a new project? The one we’re 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’ll 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’s time to delve into those 100 common Go mistakes.
## Summary
* Go is a modern programming language that enables developer productivity, which is crucial for most companies today.
* Go is simple to learn but not easy to master. This is why we need to deepen our knowledge to make the most effective use of the language.
* Learning via mistakes and concrete examples is a powerful way to be proficient in a language. This book will accelerate our path to proficiency by exploring 100 common mistakes.
[^1]: J. S. Moser, H. S. Schroder, et al., “Mind Your Errors: Evidence for a Neural Mechanism Linking Growth Mindset to Adaptive Posterror Adjustments,” Psychological Science, vol. 22, no. 12, pp. 1484–1489, Dec. 2011.
[^2]: J. Metcalfe, “Learning from Errors,” Annual Review of Psychology, vol. 68, pp. 465–489, Jan. 2017.
[^3]: Synopsys, “The Cost of Poor Software Quality in the US: A 2020 Report.” 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](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).
================================================
FILE: docs/external.md
================================================
# External Resources
## English
### The Best Golang Book | Prime Reacts
### Book Review: 100 Go Mistakes (And How to Avoid Them)
[Post](https://boldlygo.tech/posts/2023-08-09-review-100-go-mistakes/)
### The Most Useful Book for a Go Programmer?
### How to make mistakes in Go - Go Time #190
* [Episode](https://changelog.com/gotime/190)
* [Spotify](https://open.spotify.com/episode/0K1DImrxHCy6E7zVY4AxMZ?si=akroInsPQ1mM5B5V2tHLUw&dl_branch=1)
### Go is AMAZING
### 8LU - 100% Test Coverage
### Some Tips I learned from 100 Mistakes in Go
[Post](https://raygervais.dev/articles/2023/04/100_mistakes_in_go/)
### What can be summarized from 100 Go Mistakes?
[Post](https://www.sobyte.net/post/2023-05/summarized-from-100-go-mistakes/)
### Book review: 100 Go Mistakes and How to Avoid Them
[Post](https://schfkt.dev/blog/book-10-go-mistakes/)
## Chinese
### 深度阅读之《100 Go Mistakes and How to Avoid Them
[Post](https://qcrao.com/post/100-go-mistakes-reading-notes/)
### 100 Go Mistakes 随记
[Post](https://zhuanlan.zhihu.com/p/592602656)
### 我为什么放弃Go语言?
[Post](https://juejin.cn/post/7241452578125824061)
## Japanese
### 最近読んだGo言語の本の紹介:100 Go Mistakes and How to Avoid Them
[Post](https://qiita.com/kentaro_suzuki/items/c9c31dc81217f237433c)
### 『100 Go Mistakes and How to Avoid Them』を読む
[Post](https://zenn.dev/yukibobier/books/066f07c8a59fa0)
## Portuguese
### Um ÓTIMO livro para programadores Go
================================================
FILE: docs/index.md
================================================
---
comments: true
description: Summary of the mistakes in the 100 Go Mistakes book.
status: new
---
# Common Go Mistakes
???+ tip "The Coder Cafe"
If you enjoyed my book, you may be interested in my latest project: [The Coder Cafe](https://thecoder.cafe?rd=100go.co), a newsletter for coders.
> Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve timeless concepts with your coffee. Written by a Google SWE and published author, we help you grow as an engineer, one coffee at a time.
This page is a summary of the mistakes in the [100 Go Mistakes and How to Avoid Them book](book.md). Meanwhile, it's also open to the community. If you believe that a common Go mistake should be added, please create an [issue](https://github.com/teivah/100-go-mistakes/issues/new?assignees=&labels=community+mistake&template=community_mistake.md&title=).

???+ warning "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](https://github.com/teivah/100-go-mistakes/issues/43)).
Progress:
## Code and Project Organization
### Unintended variable shadowing (#1)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/1-variable-shadowing/main.go)
### Unnecessary nested code (#2)
???+ info "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’s see some different applications of this rule to optimize our code for readability:
* When an `if` block returns, we should omit the `else` block in all cases. For example, we shouldn’t write:
```go
if foo() {
// ...
return true
} else {
// ...
}
```
Instead, we omit the `else` block like this:
```go
if foo() {
// ...
return true
}
// ...
```
* We can also follow this logic with a non-happy path:
```go
if s != "" {
// ...
} else {
return errors.New("empty string")
}
```
Here, an empty `s` represents the non-happy path. Hence, we should flip the
condition like so:
```go
if s == "" {
return errors.New("empty string")
}
// ...
```
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’s readability.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/2-nested-code/main.go)
### Misusing init functions (#3)
???+ info "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:
* They can limit error management.
* They can complicate how to implement tests (for example, an external dependency must be set up, which may not be necessary for the scope of unit tests).
* If the initialization requires us to set a state, that has to be done through global variables.
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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/3-init-functions/)
### Overusing getters and setters (#4)
???+ info "TL;DR"
Forcing the use of getters and setters isn’t 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’t overwhelm our code with getters and setters on structs if they don’t 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’s nothing wrong with using them.
### Interface pollution (#5)
???+ info "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](5-interface-pollution.md).
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/5-interface-pollution/)
### Interface on the producer side (#6)
???+ info "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’s not up to the producer to force a given abstraction for all the clients. Instead, it’s 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—not foresee—that 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/6-interface-producer/)
### Returning interfaces (#7)
???+ info "TL;DR"
To prevent being restricted in terms of flexibility, a function shouldn’t return interfaces but concrete implementations in most cases. Conversely, a function should accept interfaces whenever possible.
In most cases, we shouldn’t 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’t 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’s side.
### `any` says nothing (#8)
???+ info "TL;DR"
Only use `any` if you need to accept or return any possible type, such as `json.Marshal`. Otherwise, `any` doesn’t 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/8-any/main.go)
### Being confused about when to use generics (#9)
???+ info "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](9-generics.md).
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/9-generics/main.go)
### Not being aware of the possible problems with type embedding (#10)
???+ info "TL;DR"
Using type embedding can also help avoid boilerplate code; however, ensure that doing so doesn’t 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’t 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’s declared without a name. For example,
```go
type Foo struct {
Bar // Embedded field
}
type Bar struct {
Baz int
}
```
In the `Foo` struct, the `Bar` type is declared without an associated name; hence, it’s 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’s note that it’s 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:
* It shouldn’t be used solely as some syntactic sugar to simplify accessing a field (such as `Foo.Baz()` instead of `Foo.Bar.Baz()`). If this is the only rationale, let’s not embed the inner type and use a field instead.
* It shouldn’t promote data (fields) or a behavior (methods) we want to hide from the outside: for example, if it allows clients to access a locking behavior that should remain private to the struct.
Using type embedding consciously by keeping these constraints in mind can help avoid boilerplate code with additional forwarding methods. However, let’s make sure we don’t do it solely for cosmetics and not promote elements that should remain hidden.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/10-type-embedding/main.go)
### Not using the functional options pattern (#11)
???+ info "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:
* An unexported struct holds the configuration: options.
* Each option is a function that returns the same type: `type Option func(options *options) error`. For example, `WithPort` accepts an `int` argument that represents the port and returns an `Option` type that represents how to update the `options` struct.

```go
type options struct {
port *int
}
type Option func(options *options) error
func WithPort(port int) Option {
return func(options *options) error {
if port < 0 {
return errors.New("port should be positive")
}
options.port = &port
return nil
}
}
func NewServer(addr string, opts ...Option) ( *http.Server, error) {
var options options
for _, opt := range opts {
err := opt(&options)
if err != nil {
return nil, err
}
}
// At this stage, the options struct is built and contains the config
// Therefore, we can implement our logic related to port configuration
var port int
if options.port == nil {
port = defaultHTTPPort
} else {
if *options.port == 0 {
port = randomPort()
} else {
port = *options.port
}
}
// ...
}
```
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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/11-functional-options/)
### Project misorganization (project structure and package organization) (#12)
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’s 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’s 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’s 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’t straightforward, but following these rules should help make it easier to maintain. However, remember that consistency is also vital to ease maintainability. Therefore, let’s 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](https://go.dev/doc/modules/layout)
### Creating utility packages (#13)
???+ info "TL;DR"
Naming is a critical piece of application design. Creating packages such as `common`, `util`, and `shared` doesn’t 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/13-utility-packages/stringset.go)
### Ignoring package name collisions (#14)
???+ info "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’t 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.
### Missing code documentation (#15)
???+ info "TL;DR"
To help clients and maintainers understand your code’s 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’s 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’t necessarily be public.
To help clients and maintainers understand a package’s 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’s because it will appear in the package. Then, we can provide all the information we need in the following lines.
Documenting our code shouldn’t be a constraint. We should take the opportunity to make sure it helps clients and maintainers to understand the purpose of our code.
### Not using linters (#16)
???+ info "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’t 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’re not a regular user of linters, here is a list that you may want to use daily:
* [https://golang.org/cmd/vet](https://golang.org/cmd/vet)—A standard Go analyzer
* [https://github.com/kisielk/errcheck](https://github.com/kisielk/errcheck)—An error checker
* [https://github.com/fzipp/gocyclo](https://github.com/fzipp/gocyclo)—A cyclomatic complexity analyzer
* [https://github.com/jgautheron/goconst](https://github.com/jgautheron/goconst)—A repeated string constants analyzer
Besides linters, we should also use code formatters to fix code style. Here is a list of some code formatters for you to try:
* [https://golang.org/cmd/gofmt](https://golang.org/cmd/gofmt)—A standard Go code formatter
* [https://godoc.org/golang.org/x/tools/cmd/goimports](https://godoc.org/golang.org/x/tools/cmd/goimports)—A standard Go imports formatter
Meanwhile, we should also look at golangci-lint ([https://github.com/golangci/golangci-lint](https://github.com/golangci/golangci-lint)). It’s 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’s 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).
## Data Types
### Creating confusion with octal literals (#17)
???+ info "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:
* _Binary_—Uses a `0b` or `0B` prefix (for example, `0b100` is equal to 4 in base 10)
* _Hexadecimal_—Uses an `0x` or `0X` prefix (for example, `0xF` is equal to 15 in base 10)
* _Imaginary_—Uses an `i` suffix (for example, `3i`)
We can also use an underscore character (_) as a separator for readability. For example, we can write 1 billion this way: `1_000_000_000`. We can also use the underscore character with other representations (for example, `0b00_00_01`).
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/17-octal-literals/main.go)
### Neglecting integer overflows (#18)
???+ info "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,
```go
var counter int32 = math.MaxInt32 + 1
```
```shell
constant 2147483648 overflows int32
```
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).
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/18-integer-overflows)
### Not understanding floating-points (#19)
???+ info "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’ll look at a multiplication example:
```go
var n float32 = 1.0001
fmt.Println(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’s `float32` and `float64` types are approximations, we have to bear a few rules in mind:
* When comparing two floating-point numbers, check that their difference is within an acceptable range.
* When performing additions or subtractions, group operations with a similar order of magnitude for better accuracy.
* To favor accuracy, if a sequence of operations requires addition, subtraction, multiplication, or division, perform the multiplication and division operations first.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/19-floating-points/main.go)
### Not understanding slice length and capacity (#20)
???+ info "TL;DR"
Understanding the difference between slice length and capacity should be part of a Go developer’s 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](20-slice.md).
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/20-slice-length-cap/main.go)
### Inefficient slice initialization (#21)
???+ info "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’s 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/21-slice-init/main.go)
### Being confused about nil vs. empty slice (#22)
???+ info "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’t 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’t necessarily `nil`. Meanwhile, a nil slice doesn’t require any allocation. We have seen throughout this section how to initialize a slice depending on the context by using
* `var s []string` if we aren’t sure about the final length and the slice can be empty
* `[]string(nil)` as syntactic sugar to create a nil and empty slice
* `make([]string, length)` if the future length is known
The last option, `[]string{}`, should be avoided if we initialize the slice without elements. Finally, let’s check whether the libraries we use make the distinctions between nil and empty slices to prevent unexpected behaviors.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/22-nil-empty-slice/)
### Not properly checking if a slice is empty (#23)
???+ info "TL;DR"
To check if a slice doesn’t contain any element, check its length. This check works regardless of whether the slice is `nil` or empty. The same goes for maps. To design unambiguous APIs, you shouldn’t distinguish between nil and empty slices.
To determine whether a slice has elements, we can either do it by checking if the slice is nil or if its length is equal to 0. Checking the length is the best option to follow as it will cover both if the slice is empty or if the slice is nil.
Meanwhile, when designing interfaces, we should avoid distinguishing nil and empty slices, which leads to subtle programming errors. When returning slices, it should make neither a semantic nor a technical difference if we return a nil or empty slice. Both should mean the same thing for the callers. This principle is the same with maps. To check if a map is empty, check its length, not whether it’s nil.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/23-checking-slice-empty/main.go)
### Not making slice copies correctly (#24)
???+ info "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’s 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’ lengths. Also bear in mind that other alternatives exist to copy a slice, so we shouldn’t be surprised if we find them in a codebase.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/24-slice-copy/main.go)
### Unexpected side effects using slice append (#25)
???+ info "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’s capacity is equal to `max - low`.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/25-slice-append/main.go)
### Slices and memory leaks (#26)
???+ info "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.
#### Leaking capacity
Remember that slicing a large slice or array can lead to potential high memory consumption. The remaining space won’t 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/26-slice-memory-leak/capacity-leak)
#### Slice and pointers
When we use the slicing operation with pointers or structs with pointer fields, we need to know that the GC won’t 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`.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/26-slice-memory-leak/slice-pointers)
### Inefficient map initialization (#27)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/27-map-init/main_test.go)
### Maps and memory leaks (#28)
???+ info "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](28-maps-memory-leaks.md).
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/28-map-memory-leak/main.go)
### Comparing values incorrectly (#29)
???+ info "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’s essential to understand how to use `==` and `!=` to make comparisons effectively. We can use these operators on operands that are comparable:
* _Booleans_—Compare whether two Booleans are equal.
* _Numerics (int, float, and complex types)_—Compare whether two numerics are equal.
* _Strings_—Compare whether two strings are equal.
* _Channels_—Compare whether two channels were created by the same call to make or if both are nil.
* _Interfaces_—Compare whether two interfaces have identical dynamic types and equal dynamic values or if both are nil.
* _Pointers_—Compare whether two pointers point to the same value in memory or if both are nil.
* _Structs and arrays_—Compare whether they are composed of similar types.
???+ 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’t reinvent the wheel.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/29-comparing-values/main.go)
## Control Structures
### Ignoring that elements are copied in `range` loops (#30)
???+ info "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:
* String
* Array
* Pointer to an array
* Slice
* Map
* Receiving channel
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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/04-control-structures/30-range-loop-element-copied/)
### Ignoring how arguments are evaluated in `range` loops (channels and arrays) (#31)
???+ info "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:
```go
a := [3]int{0, 1, 2}
for i, v := range a {
a[2] = 10
if i == 2 {
fmt.Println(v)
}
}
```
This code updates the last index to 10. However, if we run this code, it does not print 10; it prints 2.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/04-control-structures/31-range-loop-arg-evaluation/)
### :warning: Ignoring the impacts of using pointer elements in `range` loops (#32)
???+ warning
This mistake isn't relevant anymore from Go 1.22 ([details](https://go.dev/blog/loopvar-preview)).
### Making wrong assumptions during map iterations (ordering and map insert during iteration) (#33)
???+ info "TL;DR"
To ensure predictable outputs when using maps, remember that a map data structure:
* Doesn’t order the data by keys
* Doesn’t preserve the insertion order
* Doesn’t have a deterministic iteration order
* Doesn’t guarantee that an element added during an iteration will be produced during this iteration
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/04-control-structures/33-map-iteration/main.go)
### Ignoring how the `break` statement works (#34)
???+ info "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:
```go
for i := 0; i < 5; i++ {
fmt.Printf("%d ", i)
switch i {
default:
case 2:
break
}
}
```
The break statement doesn’t 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:
```go hl_lines="1 8"
loop:
for i := 0; i < 5; i++ {
fmt.Printf("%d ", i)
switch i {
default:
case 2:
break loop
}
}
```
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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/04-control-structures/34-break/main.go)
### Using `defer` inside a loop (#35)
???+ info "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’s execution until the surrounding function returns. It’s 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:
```go
func readFiles(ch <-chan string) error {
for path := range ch {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Do something with file
}
return nil
}
```
The `defer` calls are executed not during each loop iteration but when the `readFiles` function returns. If `readFiles` doesn’t 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:
```go
func readFiles(ch <-chan string) error {
for path := range ch {
if err := readFile(path); err != nil {
return err
}
}
return nil
}
func readFile(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Do something with file
return nil
}
```
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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/04-control-structures/35-defer-loop/main.go)
## Strings
### Not understanding the concept of rune (#36)
???+ info "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’s core knowledge to work accurately with strings.
As runes are everywhere in Go, it's important to understand the following:
* A charset is a set of characters, whereas an encoding describes how to translate a charset into binary.
* In Go, a string references an immutable slice of arbitrary bytes.
* Go source code is encoded using UTF-8. Hence, all string literals are UTF-8 strings. But because a string can contain arbitrary bytes, if it’s obtained from somewhere else (not the source code), it isn’t guaranteed to be based on the UTF-8 encoding.
* A `rune` corresponds to the concept of a Unicode code point, meaning an item represented by a single value.
* Using UTF-8, a Unicode code point can be encoded into 1 to 4 bytes.
* Using `len()` on a string in Go returns the number of bytes, not the number of runes.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/36-rune/main.go)
### Inaccurate string iteration (#37)
???+ info "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’s 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’s easy to get confused about how iteration works.
For example, consider the following example:
```go
s := "hêllo"
for i := range s {
fmt.Printf("position %d: %c\n", i, s[i])
}
fmt.Printf("len=%d\n", len(s))
```
```
position 0: h
position 1: Ã
position 3: l
position 4: l
position 5: o
len=6
```
Let's highlight three points that might be confusing:
* The second rune is à in the output instead of ê.
* We jumped from position 1 to position 3: what is at position 2?
* len returns a count of 6, whereas s contains only 5 runes.
Let’s 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 "ê" isn’t 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’t print the ith rune; it prints the UTF-8 representation of the byte at index `i`. Hence, we printed "hÃllo" instead of "hêllo".
If we want to print all the different runes, we can either use the value element of the `range` operator:
```go
s := "hêllo"
for i, r := range s {
fmt.Printf("position %d: %c\n", i, r)
}
```
Or, we can convert the string into a slice of runes and iterate over it:
```go hl_lines="2"
s := "hêllo"
runes := []rune(s)
for i, r := range runes {
fmt.Printf("position %d: %c\n", i, r)
}
```
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’t have access to the rune index; rather, we know the starting index of a rune in the byte sequence.
```go
s := "hêllo"
r := []rune(s)[4]
fmt.Printf("%c\n", r) // o
```
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/37-string-iteration/main.go)
### Misusing trim functions (#38)
???+ info "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:
```go
fmt.Println(strings.TrimRight("123oxo", "xo"))
```
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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/38-trim/main.go)
### Under-optimized strings concatenation (#39)
???+ info "TL;DR"
Concatenating a list of strings should be done with `strings.Builder` to prevent allocating a new string during each iteration.
Let’s consider a `concat` function that concatenates all the string elements of a slice using the `+=` operator:
```go
func concat(values []string) string {
s := ""
for _, value := range values {
s += value
}
return s
}
```
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’t 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`:
```go hl_lines="2 4"
func concat(values []string) string {
sb := strings.Builder{}
for _, value := range values {
_, _ = sb.WriteString(value)
}
return sb.String()
}
```
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’s 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’t 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"](#inefficient-slice-initialization-21): 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:
```go
func concat(values []string) string {
total := 0
for i := 0; i < len(values); i++ {
total += len(values[i])
}
sb := strings.Builder{}
sb.Grow(total) (2)
for _, value := range values {
_, _ = sb.WriteString(value)
}
return sb.String()
}
```
Let’s 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
BenchmarkConcatV2-4 1188 878962 ns/op
BenchmarkConcatV3-4 5922 190340 ns/op
```
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`.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/39-string-concat/)
### Useless string conversions (#40)
???+ info "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’re wondering whether we should work with strings or `[]byte`, let’s recall that working with `[]byte` isn’t 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’re 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/40-string-conversion/main.go)
### Substring and memory leaks (#41)
???+ info "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, “Slices and memory leaks,”](#slice-and-memory-leaks--26-) 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/41-substring-memory-leak/main.go)
## Functions and Methods
### Not knowing which type of receiver to use (#42)
???+ info "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’t be copied, and how large the object is. When in doubt, use a pointer receiver.
Choosing between value and pointer receivers isn’t always straightforward. Let’s discuss some of the conditions to help us choose.
A receiver _must_ be a pointer
* If the method needs to mutate the receiver. This rule is also valid if the receiver is a slice and a method needs to append elements:
```go
type slice []int
func (s *slice) add(element int) {
*s = append(*s, element)
}
```
* If the method receiver contains a field that cannot be copied: for example, a type part of the sync package (see [#74, “Copying a sync type”](#copying-a-sync-type--74-)).
A receiver _should_ be a pointer
* If the receiver is a large object. Using a pointer can make the call more efficient, as doing so prevents making an extensive copy. When in doubt about how large is large, benchmarking can be the solution; it’s pretty much impossible to state a specific size, because it depends on many factors.
A receiver _must_ be a value
* If we have to enforce a receiver’s immutability.
* If the receiver is a map, function, or channel. Otherwise, a compilation error
occurs.
A receiver _should_ be a value
* If the receiver is a slice that doesn’t have to be mutated.
* If the receiver is a small array or struct that is naturally a value type without mutable fields, such as `time.Time`.
* If the receiver is a basic type such as `int`, `float64`, or `string`.
Of course, it’s impossible to be exhaustive, as there will always be edge cases, but this section’s goal was to provide guidance to cover most cases. By default, we can choose to go with a value receiver unless there’s a good reason not to do so. In doubt, we should use a pointer receiver.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/42-receiver/)
### Never using named result parameters (#43)
???+ info "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’s 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’s an example that uses a named result parameter `b`:
```go
func f(a int) (b int) {
b = a
return
}
```
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’s a clear benefit.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/43-named-result-parameters/main.go)
### Unintended side effects with named result parameters (#44)
???+ info "TL;DR"
See [#43](#never-using-named-result-parameters-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’re not careful enough. For example, can you spot what’s wrong with this code?
```go
func (l loc) getCoordinates(ctx context.Context, address string) (
lat, lng float32, err error) {
isValid := l.validateAddress(address) (1)
if !isValid {
return 0, 0, errors.New("invalid address")
}
if ctx.Err() != nil { (2)
return 0, 0, err
}
// Get and return coordinates
}
```
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’t assigned any value to the `err` variable. It’s 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’t always straightforward to spot while reading code. Therefore, let’s remain cautious when using named result parameters, to avoid potential side effects.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/44-side-effects-named-result-parameters/main.go)
### Returning a nil receiver (#45)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/45-nil-receiver/main.go)
### Using a filename as a function input (#46)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/46-function-input/)
### Ignoring how `defer` arguments and receivers are evaluated (argument evaluation, pointer, and value receivers) (#47)
???+ info "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.
```go
const (
StatusSuccess = "success"
StatusErrorFoo = "error_foo"
StatusErrorBar = "error_bar"
)
func f() error {
var status string
defer notify(status)
defer incrementCounter(status)
if err := foo(); err != nil {
status = StatusErrorFoo
return err
}
if err := bar(); err != nil {
status = StatusErrorBar
return err
}
status = StatusSuccess
return nil
}
```
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:
```go hl_lines="3 4"
func f() error {
var status string
defer notify(&status)
defer incrementCounter(&status)
// The rest of the function unchanged
}
```
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’s another solution: calling a closure (an anonymous function value that references variables from outside its body) as a `defer` statement:
```go hl_lines="3 4 5 6"
func f() error {
var status string
defer func() {
notify(status)
incrementCounter(status)
}()
// The rest of the function unchanged
}
```
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’t require `notify` and `incrementCounter` to change their signature.
Let's also note this behavior applies with method receiver: the receiver is evaluated immediately.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/47-defer-evaluation/)
## Error Management
### Panicking (#48)
???+ info "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:
```go
func main() {
fmt.Println("a")
panic("foo")
fmt.Println("b")
}
```
This code prints a and then stops before printing b:
```
a
panic: foo
goroutine 1 [running]:
main.main()
main.go:7 +0xb3
```
Panicking in Go should be used sparingly. There are two prominent cases, one to signal a programmer error (e.g., [`sql.Register`](https://cs.opensource.google/go/go/+/refs/tags/go1.20.7:src/database/sql/sql.go;l=44) 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/48-panic/main.go)
### Ignoring when to wrap an error (#49)
???+ info "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’t 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:
* Adding additional context to an error
* Marking an error as a specific error
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’t 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’t use error wrapping but error transformation, for example, using fmt.Errorf with the %v directive.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/49-error-wrapping/main.go)
### Comparing an error type inaccurately (#50)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/50-compare-error-type/main.go)
### Comparing an error value inaccurately (#51)
???+ info "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:
```go
import "errors"
var ErrFoo = errors.New("foo")
```
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:
* Expected errors should be designed as error values (sentinel errors): `var ErrFoo = errors.New("foo")`.
* Unexpected errors should be designed as error types: `type BarError struct { ... }`, with `BarError` implementing the `error` interface.
If we use error wrapping in our application with the `%w` directive and `fmt.Errorf`, checking an error against a specific value should be done using `errors.Is` instead of `==`. Thus, even if the sentinel error is wrapped, `errors.Is` can recursively unwrap it and compare each error in the chain against the provided value.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/51-comparing-error-value/main.go)
### Handling an error twice (#52)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/52-handling-error-twice/main.go)
### Not handling an error (#53)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/53-not-handling-error/main.go)
### Not handling `defer` errors (#54)
???+ info "TL;DR"
In many cases, you shouldn’t 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:
```go
func f() {
// ...
notify() // Error handling is omitted
}
func notify() error {
// ...
}
```
From a maintainability perspective, the code can lead to some issues. Let’s consider a new reader looking at it. This reader notices that notify returns an error but that the error isn’t 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 (`_`):
```go
_ = notify
```
In terms of compilation and run time, this approach doesn’t change anything compared to the first piece of code. But this new version makes explicit that we aren’t interested in the error. Also, we can add a comment that indicates the rationale for why an error is ignored:
```go
// At-most once delivery.
// Hence, it's accepted to miss some of them in case of errors.
_ = notify()
```
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/54-defer-errors/main.go)
## Concurrency: Foundations
### Mixing up concurrency and parallelism (#55)
???+ info "TL;DR"
Understanding the fundamental differences between concurrency and parallelism is a cornerstone of the Go developer’s knowledge. Concurrency is about structure, whereas parallelism is about execution.
Concurrency and parallelism are not the same:
* Concurrency is about structure. We can change a sequential implementation into a concurrent one by introducing different steps that separate concurrent goroutines can tackle.
* Meanwhile, parallelism is about execution. We can use parallism at the steps level by adding more parallel goroutines.
In summary, concurrency provides a structure to solve a problem with parts that may be parallelized. Therefore, _concurrency enables parallelism_.
### Thinking concurrency is always faster (#56)
???+ info "TL;DR"
To be a proficient developer, you must acknowledge that concurrency isn’t 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](56-concurrency-faster.md).
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/08-concurrency-foundations/56-faster/)
### Being puzzled about when to use channels or mutexes (#57)
???+ info "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:
* G1 and G2 are parallel goroutines. They may be two goroutines executing the same function that keeps receiving messages from a channel, or perhaps two goroutines executing the same HTTP handler at the same time.
* On the other hand, G1 and G3 are concurrent goroutines, as are G2 and G3. All the goroutines are part of an overall concurrent structure, but G1 and G2 perform the first step, whereas G3 does the next step.
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—therefore, channels.
Regarding concurrent goroutines, there’s 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’s important to know whether goroutines are parallel or concurrent because, in general, we need mutexes for parallel goroutines and channels for concurrent ones.
### Not understanding race problems (data races vs. race conditions and the Go memory model) (#58)
???+ info "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’t necessarily mean deterministic execution. When a behavior depends on the sequence or the timing of events that can’t 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.
#### Data Race
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:
* Using the `sync/atomic` package
* In synchronizing the two goroutines with an ad hoc data structure like a mutex
* Using channels to make the two goroutines communicating to ensure that a variable is updated by only one goroutine at a time
#### Race Condition
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’t be controlled. Here, the timing of events is the goroutines’ execution order.
In summary, when we work in concurrent applications, it’s 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’t 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/08-concurrency-foundations/58-races/)
### Not understanding the concurrency impacts of a workload type (#59)
???+ info "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:
* The speed of the CPU—For example, running a merge sort algorithm. The workload is called CPU-bound.
* The speed of I/O—For example, making a REST call or a database query. The workload is called I/O-bound.
* The amount of available memory—The workload is called memory-bound.
???+ 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/08-concurrency-foundations/59-workload-type/main.go)
### Misunderstanding Go contexts (#60)
???+ info "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.
!!! quote "https://pkg.go.dev/context"
A Context carries a deadline, a cancellation signal, and other values across API boundaries.
#### Deadline
A deadline refers to a specific point in time determined with one of the following:
* A `time.Duration` from now (for example, in 250 ms)
* A `time.Time` (for example, 2023-02-07 00:00:00 UTC)
The semantics of a deadline convey that an ongoing activity should be stopped if this deadline is met. An activity is, for example, an I/O request or a goroutine waiting to receive a message from a channel.
#### Cancellation signals
Another use case for Go contexts is to carry a cancellation signal. Let’s 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.
#### Context values
The last use case for Go contexts is to carry a key-value list. What’s 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.
#### Catching a context cancellation
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,
* The Done channel related to a context created with `context.WithCancel` is closed when the cancel function is called.
* The Done channel related to a context created with `context.WithDeadline` is closed when the deadline has expired.
One thing to note is that the internal channel should be closed when a context is canceled or has met a deadline, instead of when it receives a specific value, because the closure of a channel is the only channel action that all the consumer goroutines will receive. This way, all the consumers will be notified once a context is canceled or a deadline is reached.
In summary, to be a proficient Go developer, we have to understand what a context is and how to use it. In general, a function that users wait for should take a context, as doing so allows upstream callers to decide when calling this function should be aborted.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/08-concurrency-foundations/60-contexts/main.go)
## Concurrency: Practice
### Propagating an inappropriate context (#61)
???+ info "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’s 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’t 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:
```go
func handler(w http.ResponseWriter, r *http.Request) {
response, err := doSomeTask(r.Context(), r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go func() {
err := publish(r.Context(), response)
// Do something with err
}()
writeResponse(response)
}
```
What’s wrong with this piece of code? We have to know that the context attached to an HTTP request can cancel in different conditions:
* When the client’s connection closes
* In the case of an HTTP/2 request, when the request is canceled
* When the response has been written back to the client
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’s probably OK to call publish with a context already canceled so the message isn’t 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:
* If the response is written after the Kafka publication, we both return a response and publish a message successfully
* However, if the response is written before or during the Kafka publication, the message shouldn’t be published.
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`](https://pkg.go.dev/context#WithoutCancel) returns a copy of parent that is not canceled when parent is canceled.
In summary, propagating a context should be done cautiously.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/61-inappropriate-context/main.go)
### Starting a goroutine without knowing when to stop it (#62)
???+ info "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—so 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’s discuss a concrete example. We will design an application that needs to watch some external configuration (for example, using a database connection). Here’s a first implementation:
```go
func main() {
newWatcher()
// Run the application
}
type watcher struct { /* Some resources */ }
func newWatcher() {
w := watcher{}
go w.watch() // Creates a goroutine that watches some external configuration
}
```
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’t 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:
```go
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
newWatcher(ctx)
// Run the application
}
func newWatcher(ctx context.Context) {
w := watcher{}
go w.watch(ctx)
}
```
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—and that’s a design flaw.
The problem is that we used signaling to convey that a goroutine had to be stopped. We didn’t block the parent goroutine until the resources had been closed. Let’s make sure we do:
```go
func main() {
w := newWatcher()
defer w.close()
// Run the application
}
func newWatcher() watcher {
w := watcher{}
go w.watch()
return w
}
func (w watcher) close() {
// Close the resources
}
```
Instead of signaling `watcher` that it’s 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’s 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’s probably safer to wait for this goroutine to complete before exiting the application. This way, we can ensure that the resources can be freed.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/62-starting-goroutine/)
### :warning: Not being careful with goroutines and loop variables (#63)
???+ warning
This mistake isn't relevant anymore from Go 1.22 ([details](https://go.dev/blog/loopvar-preview)).
### Expecting a deterministic behavior using select and channels (#64)
???+ info "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
go func() {
for i := 0; i < 10; i++ {
messageCh <- i
}
disconnectCh <- struct{}{}
}()
for {
select {
case v := <-messageCh:
fmt.Println(v)
case <-disconnectCh:
fmt.Println("disconnection, return")
return
}
}
```
If we run this example multiple times, the result will be random:
```
0
1
2
disconnection, return
0
disconnection, return
```
Instead of consuming the 10 messages, we only received a few of them. What’s 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’s 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’s 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/64-select-behavior/main.go)
### Not using notification channels (#65)
???+ info "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’s 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`:
```go
disconnectCh := make(chan bool)
```
Now, let’s say we interact with an API that provides us with such a channel. Because it’s a channel of Booleans, we can receive either `true` or `false` messages. It’s probably clear what `true` conveys. But what does `false` mean? Does it mean we haven’t 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’s the case, meaning we don’t 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{}`.
### Not using nil channels (#66)
???+ info "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?
```go
var ch chan int
<-ch
```
`ch` is a `chan int` type. The zero value of a channel being nil, `ch` is `nil`. The goroutine won’t panic; however, it will block forever.
The principle is the same if we send a message to a nil channel. This goroutine blocks forever:
```go
var ch chan int
ch <- 0
```
Then what’s 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:
```go hl_lines="5 9 15"
func merge(ch1, ch2 <-chan int) <-chan int {
ch := make(chan int, 1)
go func() {
for ch1 != nil || ch2 != nil { // Continue if at least one channel isn’t nil
select {
case v, open := <-ch1:
if !open {
ch1 = nil // Assign ch1 to a nil channel once closed
break
}
ch <- v
case v, open := <-ch2:
if !open {
ch2 = nil // Assigns ch2 to a nil channel once closed
break
}
ch <- v
}
}
close(ch)
}()
return ch
}
```
This elegant solution relies on nil channels to somehow _remove_ one case from the `select` statement.
Let’s keep this idea in mind: nil channels are useful in some conditions and should be part of the Go developer’s toolset when dealing with concurrent code.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/66-nil-channels/main.go)
### Being puzzled about channel size (#67)
???+ info "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:
```go
ch1 := make(chan int)
ch2 := make(chan int, 0)
```
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:
```go
ch3 := make(chan int, 1)
```
With a buffered channel, a sender can send messages while the channel isn’t full. Once the channel is full, it will block until a receiver goroutine receives a message:
```go
ch3 := make(chan int, 1)
ch3 <-1 // Non-blocking
ch3 <-2 // Blocking
```
The first send isn’t blocking, whereas the second one is, as the channel is full at this stage.
What's the main difference between unbuffered and buffered channels:
* An unbuffered channel enables synchronization. We have the guarantee that two goroutines will be in a known state: one receiving and another sending a message.
* A buffered channel doesn’t provide any strong synchronization. Indeed, a producer goroutine can send a message and then continue its execution if the channel isn’t full. The only guarantee is that a goroutine won’t receive a message before it is sent. But this is only a guarantee because of causality (you don’t drink your coffee before you prepare it).
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’s a list of possible cases where we should use another size:
* While using a worker pooling-like pattern, meaning spinning a fixed number of goroutines that need to send data to a shared channel. In that case, we can tie the channel size to the number of goroutines created.
* When using channels for rate-limiting problems. For example, if we need to enforce resource utilization by bounding the number of requests, we should set up the channel size according to the limit.
If we are outside of these cases, using a different channel size should be done cautiously. Let’s bear in mind that deciding about an accurate queue size isn’t an easy problem:
!!! quote "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.
### Forgetting about possible side effects with string formatting (#68)
???+ info "TL;DR"
Being aware that string formatting may lead to calling existing functions means watching out for possible deadlocks and other data races.
It’s pretty easy to forget the potential side effects of string formatting while working in a concurrent application.
#### [etcd](https://github.com/etcd-io/etcd) data race
[github.com/etcd-io/etcd/pull/7816](https://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.
#### Deadlock
Can you see what the problem is in this code with a `Customer` struct exposing an `UpdateAge` method and implementing the `fmt.Stringer` interface?
```go
type Customer struct {
mutex sync.RWMutex // Uses a sync.RWMutex to protect concurrent accesses
id string
age int
}
func (c *Customer) UpdateAge(age int) error {
c.mutex.Lock() // Locks and defers unlock as we update Customer
defer c.mutex.Unlock()
if age < 0 { // Returns an error if age is negative
return fmt.Errorf("age should be positive for customer %v", c)
}
c.age = age
return nil
}
func (c *Customer) String() string {
c.mutex.RLock() // Locks and defers unlock as we read Customer
defer c.mutex.RUnlock()
return fmt.Sprintf("id %s, age %d", c.id, c.age)
}
```
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’t 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:
```go hl_lines="2 3 4"
func (c *Customer) UpdateAge(age int) error {
if age < 0 {
return fmt.Errorf("age should be positive for customer %v", c)
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.age = age
return nil
}
```
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:
```go hl_lines="6"
func (c *Customer) UpdateAge(age int) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if age < 0 {
return fmt.Errorf("age should be positive for customer id %s", c.id)
}
c.age = age
return nil
}
```
In concurrent applications, we should remain cautious about the possible side effects of string formatting.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/68-string-formatting/main.go)
### Creating data races with append (#69)
???+ info "TL;DR"
Calling `append` isn’t always data-race-free; hence, it shouldn’t 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?
```go
s := make([]int, 1)
go func() { // In a new goroutine, appends a new element on s
s1 := append(s, 1)
fmt.Println(s1)
}()
go func() { // Same
s2 := append(s, 1)
fmt.Println(s2)
}()
```
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’t mutate the existing array; hence, it doesn’t lead to a data race.
Now, let’s 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?
```go hl_lines="1"
s := make([]int, 0, 1)
go func() {
s1 := append(s, 1)
fmt.Println(s1)
}()
go func() {
s2 := append(s, 1)
fmt.Println(s2)
}()
```
The answer is yes. We create a slice with `make([]int, 0, 1)`. Therefore, the array isn’t 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/69-data-race-append/main.go)
### Using mutexes inaccurately with slices and maps (#70)
???+ info "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:
```go
type Cache struct {
mu sync.RWMutex
balances map[string]float64
}
```
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):
```go
func (c *Cache) AddBalance(id string, balance float64) {
c.mu.Lock()
c.balances[id] = balance
c.mu.Unlock()
}
```
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:
```go
func (c *Cache) AverageBalance() float64 {
c.mu.RLock()
balances := c.balances // Creates a copy of the balances map
c.mu.RUnlock()
sum := 0.
for _, balance := range balances { // Iterates over the copy, outside of the critical section
sum += balance
}
return sum / float64(len(balances))
}
```
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’s 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’t 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:
```go hl_lines="2 3"
func (c *Cache) AverageBalance() float64 {
c.mu.RLock()
defer c.mu.RUnlock() // Unlocks when the function returns
sum := 0.
for _, balance := range c.balances {
sum += balance
}
return sum / float64(len(c.balances))
}
```
Another option, if the iteration operation isn’t lightweight, is to work on an actual copy of the data and protect only the copy:
```go hl_lines="2 3 4 5 6 7"
func (c *Cache) AverageBalance() float64 {
c.mu.RLock()
m := maps.Clone(c.balances)
c.mu.RUnlock()
sum := 0.
for _, balance := range m {
sum += balance
}
return sum / float64(len(m))
}
```
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’t 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’s be cautious when designing critical sections and make sure the boundaries are accurately defined.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/70-mutex-slices-maps/main.go)
### Misusing `sync.WaitGroup` (#71)
???+ info "TL;DR"
To accurately use `sync.WaitGroup`, call the `Add` method before spinning up goroutines.
In the following example, we will initialize a wait group, start three goroutines that will update a counter atomically, and then wait for them to complete. We want to wait for these three goroutines to print the value of the counter (which should be 3):
```go
wg := sync.WaitGroup{}
var v uint64
for i := 0; i < 3; i++ {
go func() {
wg.Add(1)
atomic.AddUint64(&v, 1)
wg.Done()
}()
}
wg.Wait()
fmt.Println(v)
```
If we run this example, we get a non-deterministic value: the code can print any value from 0 to 3. Also, if we enable the `-race` flag, Go will even catch a data race.
The problem is that `wg.Add(1)` is called within the newly created goroutine, not in the parent goroutine. Hence, there is no guarantee that we have indicated to the wait group that we want to wait for three goroutines before calling `wg.Wait()`.
To fix this issue, we can call `wg.Add` before the loop:
```go
wg := sync.WaitGroup{}
var v uint64
wg.Add(3)
for i := 0; i < 3; i++ {
go func() {
atomic.AddUint64(&v, 1)
wg.Done()
}()
}
wg.Wait()
fmt.Println(v)
```
Or inside the loop but not in the newly created goroutine:
```go
wg := sync.WaitGroup{}
var v uint64
for i := 0; i < 3; i++ {
wg.Add(1)
go func() {
atomic.AddUint64(&v, 1)
wg.Done()
}()
}
wg.Wait()
fmt.Println(v)
```
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/71-wait-group/main.go)
### Forgetting about `sync.Cond` (#72)
???+ info "TL;DR"
You can send repeated notifications to multiple goroutines with `sync.Cond`.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/72-cond/main.go)
### Not using `errgroup` (#73)
???+ info "TL;DR"
You can synchronize a group of goroutines and handle errors and contexts with the `errgroup` package.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/73-errgroup/main.go)
### Copying a `sync` type (#74)
???+ info "TL;DR"
`sync` types shouldn’t be copied.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/74-copying-sync/main.go)
## Standard Library
### Providing a wrong time duration (#75)
???+ info "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`:
```go
ticker := time.NewTicker(1000)
for {
select {
case <-ticker.C:
// Do something
}
}
```
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:
```go
ticker = time.NewTicker(time.Microsecond)
// Or
ticker = time.NewTicker(1000 * time.Nanosecond)
```
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/75-wrong-time-duration/main.go)
### `time.After` and memory leaks (#76)
???+ warning
This mistake isn't relevant anymore from Go 1.23 ([details](https://go.dev/wiki/Go123Timer)).
### JSON handling common mistakes (#77)
* Unexpected behavior because of type embedding
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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/77-json-handling/type-embedding/main.go)
* JSON and the monotonic clock
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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/77-json-handling/monotonic-clock/main.go)
* Map of `any`
To avoid wrong assumptions when you provide a map while unmarshaling JSON data, remember that numerics are converted to `float64` by default.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/77-json-handling/map-any/main.go)
### Common SQL mistakes (#78)
* Forgetting that `sql.Open` doesn't necessarily establish connections to a database
Call the `Ping` or `PingContext` method if you need to test your configuration and make sure a database is reachable.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/78-sql/sql-open)
* Forgetting about connections pooling
Configure the database connection parameters for production-grade applications.
* Not using prepared statements
Using SQL prepared statements makes queries more efficient and more secure.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/78-sql/prepared-statements)
* Mishandling null values
Deal with nullable columns in tables using pointers or `sql.NullXXX` types.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/78-sql/null-values/main.go)
* Not handling rows iteration errors
Call the `Err` method of `sql.Rows` after row iterations to ensure that you haven’t missed an error while preparing the next row.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/78-sql/rows-iterations-errors)
### Not closing transient resources (HTTP body, `sql.Rows`, and `os.File`) (#79)
???+ info "TL;DR"
Eventually close all structs implementing `io.Closer` to avoid possible leaks.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/79-closing-resources/)
### Forgetting the return statement after replying to an HTTP request (#80)
???+ info "TL;DR"
To avoid unexpected behaviors in HTTP handler implementations, make sure you don’t 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`:
```go
func handler(w http.ResponseWriter, req *http.Request) {
err := foo(req)
if err != nil {
http.Error(w, "foo", http.StatusInternalServerError)
}
_, _ = w.Write([]byte("all good"))
w.WriteHeader(http.StatusCreated)
}
```
If we run this code and `err != nil`, the HTTP response would be:
```
foo
all good
```
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)
```
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:
```go
func handler(w http.ResponseWriter, req *http.Request) {
err := foo(req)
if err != nil {
http.Error(w, "foo", http.StatusInternalServerError)
return // Adds the return statement
}
_, _ = w.Write([]byte("all good"))
w.WriteHeader(http.StatusCreated)
}
```
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/80-http-return/main.go)
### Using the default HTTP client and server (#81)
???+ info "TL;DR"
For production-grade applications, don’t use the default HTTP client and server implementations. These implementations are missing timeouts and behaviors that should be mandatory in production.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/81-default-http-client-server/)
## Testing
### Not categorizing tests (build tags, environment variables, and short mode) (#82)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/82-categorizing-tests/)
### Not enabling the race flag (#83)
???+ info "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’t a static analysis tool used during compilation; instead, it’s 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:
```bash
go test -race ./...
```
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:
```go
package main
import (
"fmt"
)
func main() {
i := 0
go func() { i++ }()
fmt.Println(i)
}
```
Running this code with the `-race` logs the following warning:
```bash hl_lines="3 7 11"
==================
WARNING: DATA RACE
Write at 0x00c000026078 by goroutine 7: # (1)
main.main.func1()
/tmp/app/main.go:9 +0x4e
Previous read at 0x00c000026078 by main goroutine: # (2)
main.main()
/tmp/app/main.go:10 +0x88
Goroutine 7 (running) created at: # (3)
main.main()
/tmp/app/main.go:9 +0x7a
==================
```
1. Indicates that goroutine 7 was writing
2. Indicates that the main goroutine was reading
3. Indicates when the goroutine 7 was created
Let’s make sure we are comfortable reading these messages. Go always logs the following:
* The concurrent goroutines that are incriminated: here, the main goroutine and goroutine 7.
* Where accesses occur in the code: in this case, lines 9 and 10.
* When these goroutines were created: goroutine 7 was created in main().
In addition, if a specific file contains tests that lead to data races, we can exclude it :material-information-outline:{ title="temporarily! 😉" } from race detection using the `!race` build tag:
```go
//go:build !race
package main
import (
"testing"
)
func TestFoo(t *testing.T) {
// ...
}
```
### Not using test execution modes (parallel and shuffle) (#84)
???+ info "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’t rely on wrong assumptions that could hide bugs.
### Not using table-driven tests (#85)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/85-table-driven-tests/main_test.go)
### Sleeping in unit tests (#86)
???+ info "TL;DR"
Avoid sleeps using synchronization to make a test less flaky and more robust. If synchronization isn’t possible, consider a retry approach.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/86-sleeping/main_test.go)
### Not dealing with the time API efficiently (#87)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/87-time-api/)
### Not using testing utility packages (`httptest` and `iotest`) (#88)
* The `httptest` package is helpful for dealing with HTTP applications. It provides a set of utilities to test both clients and servers.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/88-utility-package/httptest/main_test.go)
* The `iotest` package helps write io.Reader and test that an application is tolerant to errors.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/88-utility-package/iotest/main_test.go)
### Writing inaccurate benchmarks (#89)
???+ info "TL;DR"
Regarding benchmarks:
* Use time methods to preserve the accuracy of a benchmark.
* Increasing benchtime or using tools such as benchstat can be helpful when dealing with micro-benchmarks.
* Be careful with the results of a micro-benchmark if the system that ends up running the application is different from the one running the micro-benchmark.
* Make sure the function under test leads to a side effect, to prevent compiler optimizations from fooling you about the benchmark results.
* To prevent the observer effect, force a benchmark to re-create the data used by a CPU-bound function.
Read the full section [here](89-benchmarks.md).
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/89-benchmark/)
### Not exploring all the Go testing features (#90)
* Code coverage
Use code coverage with the `-coverprofile` flag to quickly see which part of the code needs more attention.
* Testing from a different package
Place unit tests in a different package to enforce writing tests that focus on an exposed behavior, not internals.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/90-testing-features/different-package/main_test.go)
* Utility functions
Handling errors using the `*testing.T` variable instead of the classic `if err != nil` makes code shorter and easier to read.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/90-testing-features/utility-function/main_test.go)
* Setup and teardown
You can use setup and teardown functions to configure a complex environment, such as in the case of integration tests.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/90-testing-features/setup-teardown/main_test.go)
### Not using fuzzing (community mistake)
???+ info "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](https://github.com/jeromedoucet)
## Optimizations
### Not understanding CPU caches (#91)
* CPU architecture
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.
* Cache line
Being conscious of the cache line concept is critical to understanding how to organize data in data-intensive applications. A CPU doesn’t 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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/91-cpu-caches/cache-line/)
* Slice of structs vs. struct of slices
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/91-cpu-caches/slice-structs/)
* Predictability
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’t predictable.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/91-cpu-caches/predictability/)
* Cache placement policy
To avoid a critical stride, hence utilizing only a tiny portion of the cache, be aware that caches are partitioned.
### Writing concurrent code that leads to false sharing (#92)
???+ info "TL;DR"
Knowing that lower levels of CPU caches aren’t 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](92-false-sharing.md).
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/92-false-sharing/)
### Not taking into account instruction-level parallelism (#93)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/93-instruction-level-parallelism/)
### Not being aware of data alignment (#94)
???+ info "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).
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/94-data-alignment/)
### Not understanding stack vs. heap (#95)
???+ info "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.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/95-stack-heap/)
### Not knowing how to reduce allocations (API change, compiler optimizations, and `sync.Pool`) (#96)
???+ info "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`.
[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/96-reduce-allocations/)
### Not relying on inlining (#97)
???+ info "TL;DR"
Use the fast-path inlining technique to efficiently reduce the amortized time to call a function.
### Not using Go diagnostics tooling (#98)
???+ info "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](98-profiling-execution-tracing.md).
### Not understanding how the GC works (#99)
???+ info "TL;DR"
Understanding how to tune the GC can lead to multiple benefits such as handling sudden load increases more efficiently.
### :warning: Not understanding the impacts of running Go in Docker and Kubernetes (#100)
???+ warning
This mistake isn't relevant anymore from Go 1.25 ([details](https://go.dev/blog/container-aware-gomaxprocs)).
## Community
Thanks to all the contributors:
## Powered by
[](https://jb.gg/OpenSource)
================================================
FILE: docs/ja.md
================================================
---
title: Japanese Version
comments: true
---
# Go言語でありがちな間違い
???+ tip "The Coder Cafe"
もし私の本を楽しんでいただけたなら、私の最新プロジェクトにもご興味があるかもしれません。[The Coder Cafe](https://thecoder.cafe?rd=100go.co/ja)は、コーダー向けの日刊ニュースレターです。
> Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve one essential concept for coders. Written by a senior software engineer at Google, it's perfectly brewed for your morning coffee, helping you grow your skills deeply.
Esta página é um resumo dos erros do [100 Go Mistakes and How to Avoid Them book](book.md). Enquanto isso, também está aberto à comunidade. Se você acredita que um erro comum do Go deve ser adicionado, crie uma [issue](https://github.com/teivah/100-go-mistakes/issues/new?assignees=&labels=community+mistake&template=community_mistake.md&title=).

???+ warning "Beta"
Você está visualizando uma versão beta enriquecida com muito mais conteúdo. No entanto, esta versão ainda não está completa e estou procurando voluntários para me ajudar a resumir os erros restantes ([GitHub issue #43](https://github.com/teivah/100-go-mistakes/issues/43)).
Progresso:
## Código e Organização do Projeto
### Sombreamento não intencional de variável (#1)
???+ info "TL;DR"
Evitar variáveis sombreadas pode ajudar a evitar erros, como fazer referência à variável errada ou confundir os desenvolvedores.
O sombreamento de variável ocorre quando um nome de variável é redeclarado em um bloco interno, mas essa prática está sujeita a erros. A imposição de uma regra para proibir variáveis obscuras depende do gosto pessoal. Por exemplo, às vezes pode ser conveniente reutilizar um nome de variável existente, como `err` no caso de erros. Porém, em geral, devemos ser cautelosos porque agora sabemos que podemos enfrentar um cenário onde o código compila, mas a variável que recebe o valor não é a esperada.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/1-variable-shadowing/main.go)
### Código aninhado desnecessário (#2)
???+ info "TL;DR"
Evitar níveis aninhados e manter o caminho feliz alinhado à esquerda facilita a construção de um modelo de código mental.
Em geral, quanto mais níveis aninhados uma função exigir, mais complexa será sua leitura e compreensão. Vamos ver algumas aplicações diferentes desta regra para otimizar a legibilidade do nosso código:
* Quando um bloco `if` retorna, devemos omitir o `else` em todos os casos. Por exemplo, não deveríamos escrever:
```go
if foo() {
// ...
return true
} else {
// ...
}
```
Em vez disso, omitimos o bloco `else` assim:
```go
if foo() {
// ...
return true
}
// ...
```
* Também podemos seguir esta lógica com um caminho não feliz:
```go
if s != "" {
// ...
} else {
return errors.New("empty string")
}
```
Aqui, um `s` vazio representa o caminho não feliz. Portanto, devemos inverter a condição assim:
```go
if s == "" {
return errors.New("empty string")
}
// ...
```
Escrever código legível é um desafio importante para todo desenvolvedor. Esforçar-se para reduzir o número de blocos aninhados, alinhar o caminho feliz à esquerda e retornar o mais cedo possível são meios concretos para melhorar a legibilidade do nosso código.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/2-nested-code/main.go)
### Uso indevido de funções init (#3)
???+ info "TL;DR"
Ao inicializar variáveis, lembre-se de que as funções init têm tratamento de erros limitado e tornam o tratamento de estado e os testes mais complexos. Na maioria dos casos, as inicializações devem ser tratadas como funções específicas.
Uma função init é uma função usada para inicializar o estado de um aplicativo. Não aceita argumentos e não retorna nenhum resultado (uma função `func()`). Quando um pacote é inicializado, todas as declarações de constantes e variáveis do pacote são avaliadas. Então, as funções init são executadas.
As funções de inicialização podem levar a alguns problemas:
* Elas podem limitar o gerenciamento de erros.
* Elas podem complicar a implementação de testes (por exemplo, uma dependência externa deve ser configurada, o que pode não ser necessário para o escopo dos testes unitários).
* Se a inicialização exigir que definamos um estado, isso deverá ser feito por meio de variáveis globais.
Devemos ser cautelosos com as funções init. No entanto, elas podem ser úteis em algumas situações, como na definição de configuração estática. Caso contrário, e na maioria dos casos, devemos tratar as inicializações através de funções ad hoc.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/3-init-functions/)
### Uso excessivo de getters e setters (#4)
???+ info "TL;DR"
Forcing the use of getters and setters isn’t 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ão meios de habilitar o encapsulamento, fornecendo métodos exportados sobre campos de objetos não exportados.
No Go, não há suporte automático para getters e setters como vemos em algumas linguagens. Também não é considerado obrigatório nem idiomático o uso de getters e setters para acessar campos struct. Não devemos sobrecarregar nosso código com getters e setters em structs se eles não trouxerem nenhum valor. Deveríamos ser pragmáticos e nos esforçar para encontrar o equilíbrio certo entre eficiência e seguir expressões que às vezes são consideradas indiscutíveis em outros paradigmas de programação.
Lembre-se de que Go é uma linguagem única projetada para muitas características, 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ão há nada de errado em usá-los.
### Interface poluidas (#5)
???+ info "TL;DR"
Abstrações devem ser descobertas, não criadas. Para evitar complexidade desnecessária, crie uma interface quando precisar dela e não quando você prevêr que será necessária, ou se puder pelo menos provar que a abstração é válida.
Leia a seção completa [aqui](5-interface-pollution.md).
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/5-interface-pollution/)
### Interface do lado do producer (#6)
???+ info "TL;DR"
Manter interfaces no lado do cliente evita abstrações desnecessárias.
As interfaces são satisfeitas implicitamente em Go, o que tende a ser um divisor de águas em comparação com linguagens com implementação explícita. Na maioria dos casos, a abordagem a seguir é semelhante à que descrevemos na seção anterior: _as abstrações devem ser descobertas, não criadas_. Isso significa que não cabe ao producer forçar uma determinada abstração para todos os clientes. Em vez disso, cabe ao cliente decidir se precisa de alguma forma de abstração e então determinar o melhor nível de abstração para suas necessidades.
Uma interface deve residir no lado do consumidor na maioria dos casos. Contudo, em contextos específicos (por exemplo, quando sabemos – e não prevemos – que uma abstração será útil para os consumidores), podemos querer tê-la do lado do procuder. Se o fizermos, devemos nos esforçar para mantê-lo o mínimo possível, aumentando o seu potencial de reutilização e tornando-o mais facilmente combinável.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/6-interface-producer/)
### Interfaces de retorno (#7)
???+ info "TL;DR"
Para evitar restrições em termos de flexibilidade, uma função não deve retornar interfaces, mas implementações concretas na maioria dos casos. Por outro lado, uma função deve aceitar interfaces sempre que possível.
Na maioria dos casos, não devemos retornar interfaces, mas implementações concretas. Caso contrário, isso pode tornar nosso design mais complexo devido às dependências do pacote e pode restringir a flexibilidade porque todos os clientes teriam que contar com a mesma abstração. Novamente, a conclusão é semelhante às seções anteriores: se sabemos (não prevemos) que uma abstração será útil para os clientes, podemos considerar o retorno de uma interface. Caso contrário, não deveríamos forçar abstrações; eles devem ser descobertas pelos clientes. Se um cliente precisar abstrair uma implementação por qualquer motivo, ele ainda poderá fazer isso do lado do cliente.
### `any` não diz nada (#8)
???+ info "TL;DR"
Use apenas `any` se precisar aceitar ou retornar qualquer tipo possível, como `json.Marshal`. Caso contrário, `any` não fornece informações significativas e pode levar a problemas de tempo de compilação, permitindo que um chamador chame métodos com qualquer tipo de dados.
O tipo `any` pode ser útil se houver uma necessidade genuína de aceitar ou retornar qualquer tipo possível (por exemplo, quando se trata de empacotamento ou formatação). Em geral, devemos evitar a todo custo generalizar demais o código que escrevemos. Talvez um pouco de código duplicado possa ocasionalmente ser melhor se melhorar outros aspectos, como a expressividade do código.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/8-any/main.go)
### Ficar confuso sobre quando usar genéricos (#9)
???+ info "TL;DR"
Depender de parâmetros genéricos e de tipo pode impedir a gravação de código clichê (boilerplate) para fatorar elementos ou comportamentos. No entanto, não use parâmetros de tipo prematuramente, mas somente quando você perceber uma necessidade concreta deles. Caso contrário, introduzem abstrações e complexidade desnecessárias.
Leia a seção completa [aqui](9-generics.md).
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/9-generics/main.go)
### Não estar ciente dos possíveis problemas com a incorporação de tipos (#10)
???+ info "TL;DR"
Usar a incorporação de tipo (type embedding) também pode ajudar a evitar código clichê (boilerplate); no entanto, certifique-se de que isso não leve a problemas de visibilidade onde alguns campos deveriam ter permanecido ocultos.
Ao criar uma struct, Go oferece a opção de incorporar tipos. Mas isso às vezes pode levar a comportamentos inesperados se não compreendermos todas as implicações da incorporação de tipos. Ao longo desta seção, veremos como incorporar tipos, o que eles trazem e os possíveis problemas.
No Go, um campo struct é chamado de incorporado se for declarado sem nome. Por exemplo,
```go
type Foo struct {
Bar // Embedded field
}
type Bar struct {
Baz int
}
```
Na estrutura `Foo`, o tipo `Bar` é declarado sem nome associado; portanto, é um campo incorporado.
Usamos incorporação para promover os campos e métodos de um tipo incorporado. Como `Bar` contém um campo `Baz`, esse campo é promovido para `Foo`. Portanto, `Baz` fica disponível a partir de `Foo`.
O que podemos dizer sobre a incorporação de tipos? Primeiro, observemos que raramente é uma necessidade e significa que, qualquer que seja o caso de uso, provavelmente também poderemos resolvê-lo sem incorporação de tipo. A incorporação de tipos é usada principalmente por conveniência: na maioria dos casos, para promover comportamentos.
Se decidirmos usar incorporação de tipo, precisamos ter em mente duas restrições principais:
* Não deve ser usado apenas como um açúcar sintático para simplificar o acesso a um campo (como `Foo.Baz()` em vez de `Foo.Bar.Baz()`). Se esta for a única justificativa, não vamos incorporar o tipo interno e usar um campo.
* Não deve promover dados (campos) ou um comportamento (métodos) que queremos ocultar do exterior: por exemplo, se permitir que os clientes acessem um comportamento de bloqueio que deve permanecer privado da struct.
Usar a incorporação de tipo de forma consciente, mantendo essas restrições em mente, pode ajudar a evitar código clichê (boilerplate) com métodos de encaminhamento adicionais. No entanto, vamos garantir que não o fazemos apenas por motivos cosméticos e não promovemos elementos que deveriam permanecer ocultos.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/10-type-embedding/main.go)
### Não usar o padrão de opções funcionais (functional options pattern) (#11)
???+ info "TL;DR"
Para lidar com opções de maneira conveniente e amigável à API, use o padrão de opções funcionais.
Embora existam diferentes implementações com pequenas variações, a ideia principal é a seguinte:
* Uma estrutura não exportada contém a configuração: opções.
* Cada opção é uma função que retorna o mesmo tipo: `type Option func(options *options) error`. Por exemplo, `WithPort` aceita um argumento `int` que representa a porta e retorna um tipo `Option` que representa como atualizar a struct `options`.

```go
type options struct {
port *int
}
type Option func(options *options) error
func WithPort(port int) Option {
return func(options *options) error {
if port < 0 {
return errors.New("port should be positive")
}
options.port = &port
return nil
}
}
func NewServer(addr string, opts ...Option) ( *http.Server, error) {
var options options
for _, opt := range opts {
err := opt(&options)
if err != nil {
return nil, err
}
}
// At this stage, the options struct is built and contains the config
// Therefore, we can implement our logic related to port configuration
var port int
if options.port == nil {
port = defaultHTTPPort
} else {
if *options.port == 0 {
port = randomPort()
} else {
port = *options.port
}
}
// ...
}
```
O padrão de opções funcionais fornece uma maneira prática e amigável à API de lidar com opções. Embora o padrão do construtor possa ser uma opção válida, ele tem algumas desvantagens menores (ter que passar uma estrutura de configuração que pode estar vazia ou uma maneira menos prática de lidar com o gerenciamento de erros) que tendem a tornar o padrão de opções funcionais a maneira idiomática de lidar com esse tipo de problema no Go.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/11-functional-options/)
### Desorganização do projeto (estrutura do projeto e organização do pacote) (#12)
No que diz respeito à organização geral, existem diferentes escolas de pensamento. Por exemplo, devemos organizar a nossa aplicação por contexto ou por camada? Depende de nossas preferências. Podemos preferir agrupar o código por contexto (como o contexto do cliente, o contexto do contrato, etc.), ou podemos preferir seguir os princípios da arquitetura hexagonal e agrupar por camada técnica. Se a decisão que tomarmos se adequar ao nosso caso de uso, não pode ser uma decisão errada, desde que permaneçamos consistentes com ela.
Em relação aos pacotes, existem várias práticas recomendadas que devemos seguir. Primeiro, devemos evitar pacotes prematuros porque podem complicar demais um projeto. Às vezes, é melhor usar uma organização simples e fazer nosso projeto evoluir quando entendemos o que ele contém, em vez de nos forçarmos a fazer a estrutura perfeita desde o início. A granularidade é outra coisa essencial a considerar. Devemos evitar dezenas de pacotes nano contendo apenas um ou dois arquivos. Se o fizermos, é porque provavelmente perdemos algumas conexões lógicas entre esses pacotes, tornando nosso projeto mais difícil de ser compreendido pelos leitores. Por outro lado, também devemos evitar pacotes grandes que diluem o significado do nome de um pacote.
A nomenclatura dos pacotes também deve ser considerada com cuidado. Como todos sabemos (como desenvolvedores), nomear é difícil. Para ajudar os clientes a entender um projeto Go, devemos nomear nossos pacotes de acordo com o que eles fornecem, não com o que contêm. Além disso, a nomenclatura deve ser significativa. Portanto, o nome de um pacote deve ser curto, conciso, expressivo e, por convenção, uma única palavra minúscula.
Quanto ao que exportar, a regra é bastante simples. Devemos minimizar o que deve ser exportado tanto quanto possível para reduzir o acoplamento entre pacotes e manter ocultos os elementos exportados desnecessários. Se não tivermos certeza se devemos ou não exportar um elemento, devemos optar por não exportá-lo. Mais tarde, se descobrirmos que precisamos exportá-lo, poderemos ajustar nosso código. Vamos também ter em mente algumas exceções, como fazer com que os campos sejam exportados para que uma estrutura possa ser desempacotada com encoding/json.
Organizar um projeto não é simples, mas seguir essas regras deve ajudar a facilitar sua manutenção. No entanto, lembre-se de que a consistência também é vital para facilitar a manutenção. Portanto, vamos nos certificar de manter as coisas o mais consistentes possível dentro de uma base de código.
???+ note
Em 2023, a equipe Go publicou uma diretriz oficial para organizar/estruturar um projeto Go: [go.dev/doc/modules/layout](https://go.dev/doc/modules/layout)
### Criando pacotes de utilitários (#13)
???+ info "TL;DR"
A nomenclatura é uma parte crítica do design do aplicativo. Criar pacotes como `common`, `util` e `shared` não traz muito valor para o leitor. Refatore esses pacotes em nomes de pacotes significativos e específicos.
Além disso, tenha em mente que nomear um pacote com base no que ele fornece e não no que ele contém pode ser uma forma eficiente de aumentar sua expressividade.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/02-code-project-organization/13-utility-packages/stringset.go)
### Ignorando colisões de nomes de pacotes (#14)
???+ info "TL;DR"
Para evitar colisões de nomes entre variáveis e pacotes, levando a confusão ou talvez até bugs, use nomes exclusivos para cada um. Se isso não for viável, use um alias de importação para alterar o qualificador para diferenciar o nome do pacote do nome da variável ou pense em um nome melhor.
As colisões de pacotes ocorrem quando um nome de variável colide com um nome de pacote existente, impedindo que o pacote seja reutilizado. Devemos evitar colisões de nomes de variáveis para evitar ambiguidade. Se enfrentarmos uma colisão, devemos encontrar outro nome significativo ou usar um alias de importação.
### Documentação de código ausente (#15)
???+ info "TL;DR"
Para ajudar clientes e mantenedores a entender a finalidade do seu código, documente os elementos exportados.
A documentação é um aspecto importante da programação. Simplifica como os clientes podem consumir uma API, mas também pode ajudar na manutenção de um projeto. No Go, devemos seguir algumas regras para tornar nosso código idiomático:
Primeiro, cada elemento exportado deve ser documentado. Seja uma estrutura, uma interface, uma função ou qualquer outra coisa, se for exportado deve ser documentado. A convenção é adicionar comentários, começando com o nome do elemento exportado.
Por convenção, cada comentário deve ser uma frase completa que termina com pontuação. Tenha também em mente que quando documentamos uma função (ou um método), devemos destacar o que a função pretende fazer, não como o faz; isso pertence ao núcleo de uma função e comentários, não à documentação. Além disso, o ideal é que a documentação forneça informações suficientes para que o consumidor não precise olhar nosso código para entender como usar um elemento exportado.
Quando se trata de documentar uma variável ou constante, podemos estar interessados em transmitir dois aspectos: sua finalidade e seu conteúdo. O primeiro deve funcionar como documentação de código para ser útil para clientes externos. Este último, porém, não deveria ser necessariamente público.
Para ajudar clientes e mantenedores a entender o escopo de um pacote, devemos também documentar cada pacote. A convenção é iniciar o comentário com `// Package` seguido do nome do pacote. A primeira linha de um comentário de pacote deve ser concisa. Isso porque ele aparecerá no pacote. Então, podemos fornecer todas as informações que precisamos nas linhas seguintes.
Documentar nosso código não deve ser uma restrição. Devemos aproveitar a oportunidade para garantir que isso ajude os clientes e mantenedores a entender o propósito do nosso código.
### Não usando linters (#16)
???+ info "TL;DR"
Para melhorar a qualidade e consistência do código, use linters e formatadores.
Um linter é uma ferramenta automática para analisar código e detectar erros. O escopo desta seção não é fornecer uma lista exaustiva dos linters existentes; caso contrário, ele ficará obsoleto rapidamente. Mas devemos entender e lembrar por que os linters são essenciais para a maioria dos projetos Go.
No entanto, se você não é um usuário regular de linters, aqui está uma lista que você pode usar diariamente:
* [https://golang.org/cmd/vet](https://golang.org/cmd/vet)—A standard Go analyzer
* [https://github.com/kisielk/errcheck](https://github.com/kisielk/errcheck)—An error checker
* [https://github.com/fzipp/gocyclo](https://github.com/fzipp/gocyclo)—A cyclomatic complexity analyzer
* [https://github.com/jgautheron/goconst](https://github.com/jgautheron/goconst)—A repeated string constants analyzer
Além dos linters, também devemos usar formatadores de código para corrigir o estilo do código. Aqui está uma lista de alguns formatadores de código para você experimentar:
* [https://golang.org/cmd/gofmt](https://golang.org/cmd/gofmt)—A standard Go code formatter
* [https://godoc.org/golang.org/x/tools/cmd/goimports](https://godoc.org/golang.org/x/tools/cmd/goimports)—A standard Go imports formatter
Enquanto isso, devemos também dar uma olhada em golangci-lint ([https://github.com/golangci/golangci-lint](https://github.com/golangci/golangci-lint)).
É uma ferramenta de linting que fornece uma fachada sobre muitos linters e formatadores úteis. Além disso, permite executar os linters em paralelo para melhorar a velocidade de análise, o que é bastante útil.
Linters e formatadores são uma forma poderosa de melhorar a qualidade e consistência de nossa base de código. Vamos dedicar um tempo para entender qual deles devemos usar e garantir que automatizamos sua execução (como um precommit hook de CI ou Git).
## Tipos de dados
### Criando confusão com literais octais (#17)
???+ info "TL;DR"
Ao ler o código existente, lembre-se de que literais inteiros começando com `0` são números octais. Além disso, para melhorar a legibilidade, torne os inteiros octais explícitos prefixando-os com `0o`.
Os números octais começam com 0 (por exemplo, `010` é igual a 8 na base 10). Para melhorar a legibilidade e evitar possíveis erros para futuros leitores de código, devemos tornar os números octais explícitos usando o prefixo `0o` (por exemplo, `0o10`).
Devemos também observar as outras representações literais inteiras:
* _Binário_—Usa um prefixo `0b` ou `0B`(por exemplo, `0b100` é igual a 4 na base 10)
* _Hexadecimal_—Usa um prefixo `0x` ou `0X` (por exemplo, `0xF` é igual a 15 na base 10)
* _Imaginário_—Usa um `i` sufixo (por exemplo, `3i`)
Também podemos usar um caractere de sublinhado (_) como separador para facilitar a leitura. Por exemplo, podemos escrever 1 bilhão desta forma: `1_000_000_000`. Também podemos usar o caractere sublinhado com outras representações (por exemplo, `0b00_00_01`).
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/17-octal-literals/main.go)
### Negligenciando estouros de número inteiro (#18)
???+ info "TL;DR"
Como os overflows e underflows de números inteiros são tratados silenciosamente no Go, você pode implementar suas próprias funções para capturá-los.
No Go, um estouro de número inteiro que pode ser detectado em tempo de compilação gera um erro de compilação. Por exemplo,
```go
var counter int32 = math.MaxInt32 + 1
```
```shell
constant 2147483648 overflows int32
```
No entanto, em tempo de execução, um overflow ou underflow de inteiro é silencioso; isso não leva ao pânico do aplicativo. É essencial ter esse comportamento em mente, pois ele pode levar a bugs sorrateiros (por exemplo, um incremento de número inteiro ou adição de números inteiros positivos que leva a um resultado negativo).
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/18-integer-overflows)
### Não entendendo os pontos flutuantes (#19)
???+ info "TL;DR"
Fazer comparações de ponto flutuante dentro de um determinado delta pode garantir que seu código seja portátil. Ao realizar adição ou subtração, agrupe as operações com ordem de grandeza semelhante para favorecer a precisão. Além disso, execute multiplicação e divisão antes da adição e subtração.
Em Go, existem dois tipos de ponto flutuante (se omitirmos os números imaginários): float32 e float64. O conceito de ponto flutuante foi inventado para resolver o principal problema dos números inteiros: sua incapacidade de representar valores fracionários. Para evitar surpresas desagradáveis, precisamos saber que a aritmética de ponto flutuante é uma aproximação da aritmética real.
Para isso, veremos um exemplo de multiplicação:
```go
var n float32 = 1.0001
fmt.Println(n * n)
```
Podemos esperar que este código imprima o resultado de 1.0001 * 1.0001 = 1,00020001, certo? No entanto, executá-lo na maioria dos processadores x86 imprime 1.0002.
Como os tipos `float32` e `float64` em Go são aproximações, temos que ter algumas regras em mente:
* Ao comparar dois números de ponto flutuante, verifique se a diferença está dentro de um intervalo aceitável.
* Ao realizar adições ou subtrações, agrupe operações com ordem de magnitude semelhante para melhor precisão.
* Para favorecer a precisão, se uma sequência de operações exigir adição, subtração, multiplicação ou divisão, execute primeiro as operações de multiplicação e divisão.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/19-floating-points/main.go)
### Não entendendo o comprimento e a capacidade de slice (#20)
???+ info "TL;DR"
Compreender a diferença entre comprimento e capacidade da slice deve fazer parte do conhecimento básico de um desenvolvedor Go. O comprimento de slice é o número de elementos disponíveis na slice, enquanto a capacidade de slice é o número de elementos na matriz de apoio.
Leia a seção completa [aqui](20-slice.md).
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/20-slice-length-cap/main.go)
### Inicialização de slice ineficiente (#21)
???+ info "TL;DR"
Ao criar uma fatia, inicialize-a com um determinado comprimento ou capacidade se o seu comprimento já for conhecido. Isso reduz o número de alocações 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âmetros quando faz sentido é um erro generalizado. Na verdade, isso pode levar a múltiplas cópias e esforço adicional para o GC limpar as matrizes de apoio temporárias. Em termos de desempenho, não há uma boa razão para não ajudar o tempo de execução do Go.
Nossas opções são alocar uma fatia com determinada capacidade ou comprimento. Destas duas soluções, vimos que a segunda tende a ser um pouco mais rápida. Mas usar uma determinada capacidade e anexar pode ser mais fácil de implementar e ler em alguns contextos.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/21-slice-init/main.go)
### Estar confuso sobre slice nula vs. slice vazia (#22)
???+ info "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’t require allocation.
No Go, há uma distinção entre slices nulas e vazias. Uma slice nula é igual a `nil`, enquanto uma slice vazia tem comprimento zero. Uma slice nula está vazia, mas uma slice vazia não é necessariamente `nil`. Enquanto isso, uma slice nula não requer nenhuma alocação. Vimos ao longo desta seção como inicializar uma slice dependendo do contexto usando
* `var s []string` se não tivermos certeza sobre o comprimento final e a fatia pode estar vazia
* `[]string(nil)` como açúcar sintático para criar uma fatia nula e vazia
* `make([]string, length)` se o comprimento futuro for conhecido
A última opção, `[]string{}` deve ser evitada se inicializarmos a fatia sem elementos. Finalmente, vamos verificar se as bibliotecas que usamos fazem distinções entre fatias nulas e vazias para evitar comportamentos inesperados.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/22-nil-empty-slice/)
### Não verificar corretamente se um slice está vazio (#23)
???+ info "TL;DR"
Para verificar se uma fatia não contém nenhum elemento, verifique seu comprimento. Esta verificação funciona independentemente de o slice estar `nil` ou vazio. O mesmo vale para maps. Para projetar APIs inequívocas, você não deve distinguir entre slice nulos e vazios.
Para determinar se um slice possui elementos, podemos fazê-lo verificando se o slice é nulo ou se seu comprimento é igual a 0. Verificar o comprimento é a melhor opção a seguir, pois cobrirá ambos se o slice estiver vazio ou se o slice é nulo.
Enquanto isso, ao projetar interfaces, devemos evitar distinguir slices nulos e vazios, o que leva a erros sutis de programação. Ao retornar slices, não deve haver diferença semântica nem técnica se retornarmos um slice nulo ou vazio. Ambos devem significar a mesma coisa para quem liga. Este princípio é o mesmo com maps. Para verificar se um map está vazio, verifique seu comprimento, não se é nulo.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/23-checking-slice-empty/main.go)
### Não fazer cópias de slcies corretamente (#24)
???+ info "TL;DR"
Para copiar um slice para outro usando a função `copy`, lembre-se que o número de elementos copiados corresponde ao mínimo entre os comprimentos dos dois slices.
Copiar elementos de um slice para outro é uma operação razoavelmente frequente. Ao utilizar a cópia, devemos lembrar que o número de elementos copiados para o destino corresponde ao mínimo entre os comprimentos dos dois slices. Tenha também em mente que existem outras alternativas para copiar um slice, por isso não devemos nos surpreender se as encontrarmos em uma base de código.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/24-slice-copy/main.go)
### Efeitos colaterais inesperados usando o slice append (#25)
???+ info "TL;DR"
Usar `copy` ou a expressão de slice completa é uma forma de evitar que `append` crie conflitos se duas funções diferentes usarem slices apoiados pela mesmo array. No entanto, apenas uma cópia de slice evita vazamentos de memória se você quiser reduzir um slice grande.
Ao usar o slicing, devemos lembrar que podemos enfrentar uma situação que leva a efeitos colaterais não intencionais. Se o slice resultante tiver um comprimento menor que sua capacidade, o acréscimo poderá alterar o slice original. Se quisermos restringir a gama de possíveis efeitos colaterais, podemos usar uma cópia de slice ou a expressão de slice completa, o que nos impede de fazer uma cópia.
???+ note
`s[low:high:max]`(expressão de slice completo): Esta instrução cria um slice semelhante àquele criado com `s[low:high]`, exceto que a capacidade de slice resultante é igual a `max - low`.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/25-slice-append/main.go)
### Slices e vazamentos de memória (#26)
???+ info "TL;DR"
Trabalhando com um slice de ponteiros ou estruturas com campos de ponteiro, você pode evitar vazamentos de memória marcando como nulos os elementos excluídos por uma operação de fatiamento.
#### Vazamento de capacidade
Lembre-se de que fatiar um slice ou array grande pode levar a um potencial alto consumo de memória. O espaço restante não será recuperado pelo GC e podemos manter um grande array de apoio, apesar de usarmos apenas alguns elementos. Usar uma cópia em slice é a solução para evitar tal caso.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/26-slice-memory-leak/capacity-leak)
#### Slice e ponteiros
Quando usamos a operação de fatiamento com ponteiros ou estruturas com campos de ponteiro, precisamos saber que o GC não recuperará esses elementos. Nesse caso, as duas opções são realizar uma cópia ou marcar explicitamente os elementos restantes ou seus campos como `nil`.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/26-slice-memory-leak/slice-pointers)
### Inicialização ineficiente do mapa (#27)
???+ info "TL;DR"
Ao criar um mapa, inicialize-o com um determinado comprimento se o seu comprimento já for conhecido. Isso reduz o número de alocações e melhora o desempenho.
Um mapa fornece uma coleção não ordenada de pares chave-valor em que todas as chaves são distintas. No Go, um mapa é baseado na estrutura de dados da tabela hash. Internamente, uma tabela hash é uma matriz de intervalos e cada intervalo é um ponteiro para uma matriz de pares de valores-chave.
Se soubermos de antemão o número de elementos que um mapa conterá, devemos criá-lo fornecendo um tamanho inicial. Fazer isso evita o crescimento potencial do mapa, o que é bastante pesado em termos de computação porque requer a realocação de espaço suficiente e o reequilíbrio de todos os elementos.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/27-map-init/main_test.go)
### Mapas e vazamentos de memória (#28)
???+ info "TL;DR"
Um mapa sempre pode crescer na memória, mas nunca diminui. Portanto, se isso causar alguns problemas de memória, você pode tentar diferentes opções, como forçar Go a recriar o mapa ou usar ponteiros.
Leia a seção completa [aqui](28-maps-memory-leaks.md).
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/28-map-memory-leak/main.go)
### Comparando valores incorretamente (#29)
???+ info "TL;DR"
Para comparar tipos em Go, você pode usar os operadores == e != se dois tipos forem comparáveis: booleanos, numerais, strings, ponteiros, canais e estruturas são compostos inteiramente de tipos comparáveis. Caso contrário, você pode usar `reflect.DeepEquale` pagar o preço da reflexão ou usar implementações e bibliotecas personalizadas.
É essencial entender como usar `==` e `!=` para fazer comparações de forma eficaz. Podemos usar esses operadores em operandos comparáveis:
* _Booleans_—Compara se dois booleanos são iguais.
* _Numerics (int, float, and complex types)_—Compare se dois números são iguais.
* _Strings_—Compare se duas strings são iguais.
* _Channels_—Compare se dois canais foram criados pela mesma chamada a ser feita ou se ambos são nulos.
* _Interfaces_—Compare se duas interfaces têm tipos dinâmicos idênticos e valores dinâmicos iguais ou se ambas são nulas.
* _Pointers_—Compare se dois ponteiros apontam para o mesmo valor na memória ou se ambos são nulos.
* _Structs and arrays_—Compare se são compostas de tipos semelhantes.
???+ note
Também podemos usar os operadores `?`, `>=`, `<` e `>` com tipos numéricos para comparar valores e com strings para comparar sua ordem lexical.
Se os operandos não forem comparáveis (por exemplo, slices e mapas), teremos que usar outras opções, como reflexão. A reflexão é uma forma de metaprogramação e se refere à capacidade de um aplicativo de introspectar e modificar sua estrutura e comportamento. Por exemplo, em Go, podemos usar `reflect.DeepEqual`. Esta função informa se dois elementos são profundamente iguais percorrendo recursivamente dois valores. Os elementos que ele aceita são tipos básicos mais arrays, estruturas, slices, mapas, ponteiros, interfaces e funções. No entanto, o principal problema é a penalidade de desempenho.
Se o desempenho for crucial em tempo de execução, implementar nosso método customizado pode ser a melhor solução. Uma observação adicional: devemos lembrar que a biblioteca padrão possui alguns métodos de comparação existentes. Por exemplo, podemos usar a função `bytes.Compare` otimizada para comparar duas slices de bytes. Antes de implementar um método customizado, precisamos ter certeza de não reinventar a roda.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/03-data-types/29-comparing-values/main.go)
## Estruturas de Controle
### Ignorando que os elementos são copiados em loops de `range` (#30)
???+ info "TL;DR"
O elemento de valor em um loop de `range` é uma cópia. Portanto, para modificar uma struct, por exemplo, acesse-a através de seu índice ou através de um loop `for` clássico (a menos que o elemento ou campo que você deseja modificar seja um ponteiro).
Um range loop permite iterar em diferentes estruturas de dados:
* String
* Array
* Pointer to an array
* Slice
* Map
* Receiving channel
Comparado a um for `loop` clássico, um loop `range` é uma maneira conveniente de iterar todos os elementos de uma dessas estruturas de dados, graças à sua sintaxe concisa.
Ainda assim, devemos lembrar que o elemento de valor em um range loop é uma cópia. Portanto, se o valor for uma estrutura que precisamos sofrer mutação, atualizaremos apenas a cópia, não o elemento em si, a menos que o valor ou campo que modificamos seja um ponteiro. As opções preferidas são acessar o elemento através do índice usando um range loop ou um loop for clássico.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/04-control-structures/30-range-loop-element-copied/)
### Ignorando como os argumentos são avaliados em range loops (canais e arrays) (#31)
???+ info "TL;DR"
Entender que a expressão passada ao operador `range` é avaliada apenas uma vez antes do início do loop pode ajudar a evitar erros comuns, como atribuição ineficiente em canal ou iteração de slice.
O range loop avalia a expressão fornecida apenas uma vez, antes do início do loop, fazendo uma cópia (independentemente do tipo). Devemos lembrar deste comportamento para evitar erros comuns que podem, por exemplo, nos levar a acessar o elemento errado. Por exemplo:
```go
a := [3]int{0, 1, 2}
for i, v := range a {
a[2] = 10
if i == 2 {
fmt.Println(v)
}
}
```
Este código atualiza o último índice para 10. No entanto, se executarmos este código, ele não imprimirá 10; imprime 2.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/04-control-structures/31-range-loop-arg-evaluation/)
### :warning: Ignorando os impactos do uso de elementos ponteiros em `range` loops (#32)
???+ warning
Este erro não é mais relevante no Go 1.22 ([detalhes](https://go.dev/blog/loopvar-preview)).
### Fazendo suposições erradas durante as iterações de maps (ordenação e inserção do mapa durante a iteração) (#33)
???+ info "TL;DR"
Para garantir resultados previsíveis ao usar maps, lembre-se de que uma estrutura de dados de mapa:
* Não ordena os dados por chaves
* Não preserva o pedido de inserção
* Não tem uma ordem de iteração determinística
* Não garante que um elemento adicionado durante uma iteração será produzido durante esta iteração
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/04-control-structures/33-map-iteration/main.go)
### Ignorando como a declaração `break` funciona (#34)
???+ info "TL;DR"
Usar `break` ou `continue` com um rótulo impõe a quebra de uma instrução específica. Isso pode ser útil com instruções `switch` ou `select` dentro de loops.
Uma instrução break é comumente usada para encerrar a execução de um loop. Quando loops são usados em conjunto com switch ou select, os desenvolvedores frequentemente cometem o erro de quebrar a instrução errada. Por exemplo:
```go
for i := 0; i < 5; i++ {
fmt.Printf("%d ", i)
switch i {
default:
case 2:
break
}
}
```
A instrução break não encerra o loop `for`: em vez disso, ela encerra a instrução `switch`. Portanto, em vez de iterar de 0 a 2, este código itera de 0 a 4: `0 1 2 3 4`.
Uma regra essencial a ter em mente é que uma instrução `break` encerra a execução da instrução `for`, `switch`, ou mais interna `select`. No exemplo anterior, ele encerra a instrução `switch`.
Para quebrar o loop em vez da instrução `switch`, a maneira mais idiomática é usar um rótulo:
```go hl_lines="1 8"
loop:
for i := 0; i < 5; i++ {
fmt.Printf("%d ", i)
switch i {
default:
case 2:
break loop
}
}
```
Aqui, associamos o `loop`rótulo ao `for` loop. Então, como fornecemos o `loop` rótulo para a instrução `break`, ela interrompe o loop, não a opção. Portanto, esta nova versão será impressa `0 1 2`, como esperávamos.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/04-control-structures/34-break/main.go)
### Usando `defer` dentro de um loop (#35)
???+ info "TL;DR"
Extrair a lógica do loop dentro de uma função leva à execução de uma instrução `defer` no final de cada iteração.
A instrução `defer` atrasa a execução de uma chamada até que a função circundante retorne. É usado principalmente para reduzir o código padrão. Por exemplo, se um recurso precisar ser fechado eventualmente, podemos usar `defer` para evitar a repetição das chamadas de fechamento antes de cada `return`.
Um erro comum com `defer` é esquecer que ele agenda uma chamada de função quando a função _circundante_ retorna. Por exemplo:
```go
func readFiles(ch <-chan string) error {
for path := range ch {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Do something with file
}
return nil
}
```
As chamadas `defer` não são executadas durante cada iteração do loop, mas quando a função `readFiles` retorna. Se `readFiles` não retornar, os descritores de arquivos ficarão abertos para sempre, causando vazamentos.
Uma opção comum para corrigir esse problema é criar uma função circundante após `defer`, chamada durante cada iteração:
```go
func readFiles(ch <-chan string) error {
for path := range ch {
if err := readFile(path); err != nil {
return err
}
}
return nil
}
func readFile(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Do something with file
return nil
}
```
Outra solução é tornar a função `readFile` um encerramento, mas intrinsecamente, esta permanece a mesma solução: adicionar outra função circundante para executar as chamadas `defer` durante cada iteração.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/04-control-structures/35-defer-loop/main.go)
## Strings
### Não entendendo o conceito de rune (#36)
???+ info "TL;DR"
Entender que uma runa corresponde ao conceito de um ponto de código Unicode e que pode ser composta de múltiplos bytes deve fazer parte do conhecimento básico do desenvolvedor Go para trabalhar com precisão com strings.
Como as runas estão por toda parte no Go, é importante entender o seguinte:
* Um conjunto de caracteres é um conjunto de caracteres, enquanto uma codificação descreve como traduzir um conjunto de caracteres em binário.
* No Go, uma string faz referência a uma fatia imutável de bytes arbitrários.
* O código-fonte Go é codificado usando UTF-8. Portanto, todos os literais de string são strings UTF-8. Mas como uma string pode conter bytes arbitrários, se for obtida de outro lugar (não do código-fonte), não há garantia de que seja baseada na codificação UTF-8.
* A `rune` corresponde ao conceito de ponto de código Unicode, significando um item representado por um único valor.
* Usando UTF-8, um ponto de código Unicode pode ser codificado em 1 a 4 bytes.
* Usar `len()` na string em Go retorna o número de bytes, não o número de runas.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/36-rune/main.go)
### Iteração de string imprecisa (#37)
???+ info "TL;DR"
Iterar em uma string com o operador `range` itera nas runas com o índice correspondente ao índice inicial da sequência de bytes da runa. Para acessar um índice de runa específico (como a terceira runa), converta a string em um arquivo `[]rune`.
Iterar em uma string é uma operação comum para desenvolvedores. Talvez queiramos realizar uma operação para cada runa na string ou implementar uma função personalizada para procurar uma substring específica. Em ambos os casos, temos que iterar nas diferentes runas de uma string. Mas é fácil ficar confuso sobre como funciona a iteração.
For example, consider the following example:
```go
s := "hêllo"
for i := range s {
fmt.Printf("position %d: %c\n", i, s[i])
}
fmt.Printf("len=%d\n", len(s))
```
```
position 0: h
position 1: Ã
position 3: l
position 4: l
position 5: o
len=6
```
Vamos destacar três pontos que podem ser confusos:
* A segunda runa é Ã na saída em vez de ê.
* Saltamos da posição 1 para a posição 3: o que há na posição 2?
* len retorna uma contagem de 6, enquanto s contém apenas 5 runas.
Vamos começar com a última observação. Já mencionamos que len retorna o número de bytes em uma string, não o número de runas. Como atribuímos uma string literal a `s`, `s` é uma string UTF-8. Enquanto isso, o caractere especial “ê” não é codificado em um único byte; requer 2 bytes. Portanto, chamar `len(s)` retorna 6.
Enquanto isso, no exemplo anterior, temos que entender que não repetimos cada runa; em vez disso, iteramos sobre cada índice inicial de uma runa:

Imprimir `s[i]` não imprime a i-ésima runa; imprime a representação UTF-8 do byte em index `i`. Portanto, imprimimos "hÃllo" em vez de "hêllo".
Se quisermos imprimir todas as diferentes runas, podemos usar o elemento value do operador `range`:
```go
s := "hêllo"
for i, r := range s {
fmt.Printf("position %d: %c\n", i, r)
}
```
Ou podemos converter a string em uma fatia de runas e iterar sobre ela:
```go hl_lines="2"
s := "hêllo"
runes := []rune(s)
for i, r := range runes {
fmt.Printf("position %d: %c\n", i, r)
}
```
Observe que esta solução introduz uma sobrecarga de tempo de execução em comparação com a anterior. Na verdade, converter uma string em uma fatia de runas requer a alocação de uma fatia adicional e a conversão dos bytes em runas: uma complexidade de tempo O(n) com n o número de bytes na string. Portanto, se quisermos iterar todas as runas, devemos usar a primeira solução.
Porém, se quisermos acessar a i-ésima runa de uma string com a primeira opção, não teremos acesso ao índice da runa; em vez disso, conhecemos o índice inicial de uma runa na sequência de bytes.
```go
s := "hêllo"
r := []rune(s)[4]
fmt.Printf("%c\n", r) // o
```
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/37-string-iteration/main.go)
### Uso indevido de funções de trim (#38)
???+ info "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:
```go
fmt.Println(strings.TrimRight("123oxo", "xo"))
```
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.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/38-trim/main.go)
### Concatenação de strings subotimizada (#39)
???+ info "TL;DR"
A concatenação de uma lista de strings deve ser feita com `strings.Builder` para evitar a alocação de uma nova string durante cada iteração.
Vamos considerar uma função `concat` que concatena todos os elementos string de uma fatia usando o operador `+=`:
```go
func concat(values []string) string {
s := ""
for _, value := range values {
s += value
}
return s
}
```
Durante cada iteração, o operador `+=` concatena com `s` a sequência de valores. À primeira vista, esta função pode não parecer errada. Mas com esta implementação, esquecemos uma das principais características de uma string: a sua imutabilidade. Portanto, cada iteração não é atualizada `s`; ele realoca uma nova string na memória, o que impacta significativamente o desempenho desta função.
Felizmente, existe uma solução para lidar com esse problema, usando `strings.Builder`:
```go hl_lines="2 4"
func concat(values []string) string {
sb := strings.Builder{}
for _, value := range values {
_, _ = sb.WriteString(value)
}
return sb.String()
}
```
Durante cada iteração, construímos a string resultante chamando o método `WriteString` que anexa o conteúdo do valor ao seu buffer interno, minimizando assim a cópia da memória.
???+ note
`WriteString` retorna um erro como segunda saída, mas nós o ignoramos propositalmente. Na verdade, este método nunca retornará um erro diferente de zero. Então, qual é o propósito deste método retornar um erro como parte de sua assinatura? `strings.Builder` implementa a `io.StringWriter` interface, que contém um único método: `WriteString(s string) (n int, err error)`. Portanto, para estar em conformidade com esta interface, `WriteString` deve retornar um erro.
Internamente, `strings.Builder` contém uma fatia de bytes. Cada chamada para `WriteString` resulta em uma chamada para anexar nesta fatia. Existem dois impactos. Primeiro, esta estrutura não deve ser usada simultaneamente, pois as chamadas `append` levariam a condições de corrida. O segundo impacto é algo que vimos no [mistake #21, "Inicialização de slice ineficiente"](#inefficient-slice-initialization-21): se o comprimento futuro de uma slice já for conhecido, devemos pré-alocá-la. Para isso, `strings.Builder` expõe um método `Grow(n int)` para garantir espaço para outros `n` bytes:
```go
func concat(values []string) string {
total := 0
for i := 0; i < len(values); i++ {
total += len(values[i])
}
sb := strings.Builder{}
sb.Grow(total) (2)
for _, value := range values {
_, _ = sb.WriteString(value)
}
return sb.String()
}
```
Vamos executar um benchmark para comparar as três versões (v1 usando `+=`; v2 usando `strings.Builder{}` sem pré-alocação; e v3 usando `strings.Builder{}` com pré-alocação). A slice de entrada contém 1.000 strings e cada string contém 1.000 bytes:
```
BenchmarkConcatV1-4 16 72291485 ns/op
BenchmarkConcatV2-4 1188 878962 ns/op
BenchmarkConcatV3-4 5922 190340 ns/op
```
Como podemos ver, a versão mais recente é de longe a mais eficiente: 99% mais rápida que a v1 e 78% mais rápida que a v2.
`strings.Builder` é a solução recomendada para concatenar uma lista de strings. Normalmente, esta solução 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ão é recomendado, pois isso tornará o código um pouco menos legível do que usar o operador `+=` or `fmt.Sprintf`.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/39-string-concat/)
### Conversões de string inúteis (#40)
???+ info "TL;DR"
Lembrar que o pacote `bytes` oferece as mesmas operações que o pacote `strings` pode ajudar a evitar conversões extras de bytes/string.
Ao optar por trabalhar com uma string ou um `[]byte`, a maioria dos programadores tende a preferir strings por conveniência. Mas a maior parte da E/S é realmente feita com `[]byte`. Por exemplo, `io.Reader`, `io.Writer` e `io.ReadAll` trabalham com `[]byte`, não com strings.
Quando nos perguntamos se devemos trabalhar com strings ou `[]byte`, lembremos que trabalhar com `[]byte`não é necessariamente menos conveniente. Na verdade, todas as funções exportadas do pacote strings também possuem alternativas no pacote `bytes`: `Split`, `Count`, `Contains`, `Index` e assim por diante. Portanto, estejamos fazendo I/O ou não, devemos primeiro verificar se poderíamos implementar um fluxo de trabalho completo usando bytes em vez de strings e evitar o preço de conversões adicionais.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/40-string-conversion/main.go)
### Vazamentos de substring e memória (#41)
???+ info "TL;DR"
Usar cópias em vez de substrings pode evitar vazamentos de memória, pois a string retornada por uma operação de substring será apoiada pela mesma matriz de bytes.
In mistake [#26, “Slices and memory leaks,”](#slice-and-memory-leaks--26-) 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.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/05-strings/41-substring-memory-leak/main.go)
## Functions and Methods
### Não saber que tipo de receptor usar (#42)
???+ info "TL;DR"
A decisão de usar um valor ou um receptor de ponteiro deve ser tomada com base em fatores como o tipo, se deve sofrer mutação, se contém um campo que não pode ser copiado e o tamanho do objeto. Em caso de dúvida, use um receptor de ponteiro.
Choosing between value and pointer receivers isn’t always straightforward. Let’s discuss some of the conditions to help us choose.
A receiver _must_ be a pointer
* If the method needs to mutate the receiver. This rule is also valid if the receiver is a slice and a method needs to append elements:
```go
type slice []int
func (s *slice) add(element int) {
*s = append(*s, element)
}
```
* If the method receiver contains a field that cannot be copied: for example, a type part of the sync package (see [#74, “Copying a sync type”](#copying-a-sync-type--74-)).
A receiver _should_ be a pointer
* If the receiver is a large object. Using a pointer can make the call more efficient, as doing so prevents making an extensive copy. When in doubt about how large is large, benchmarking can be the solution; it’s pretty much impossible to state a specific size, because it depends on many factors.
A receiver _must_ be a value
* If we have to enforce a receiver’s immutability.
* If the receiver is a map, function, or channel. Otherwise, a compilation error
occurs.
A receiver _should_ be a value
* If the receiver is a slice that doesn’t have to be mutated.
* If the receiver is a small array or struct that is naturally a value type without mutable fields, such as `time.Time`.
* If the receiver is a basic type such as `int`, `float64`, or `string`.
Of course, it’s impossible to be exhaustive, as there will always be edge cases, but this section’s goal was to provide guidance to cover most cases. By default, we can choose to go with a value receiver unless there’s a good reason not to do so. In doubt, we should use a pointer receiver.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/42-receiver/)
### Nunca usando parâmetros de resultado nomeados (#43)
???+ info "TL;DR"
Usar parâmetros de resultado nomeados pode ser uma maneira eficiente de melhorar a legibilidade de uma função/método, especialmente se vários parâmetros de resultado tiverem o mesmo tipo. Em alguns casos, esta abordagem também pode ser conveniente porque os parâmetros de resultado nomeados são inicializados com seu valor zero. Mas tenha cuidado com os possíveis 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’s 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’s an example that uses a named result parameter `b`:
```go
func f(a int) (b int) {
b = a
return
}
```
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’s a clear benefit.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/43-named-result-parameters/main.go)
### Efeitos colaterais não intencionais com parâmetros de resultado nomeados (#44)
???+ info "TL;DR"
Consulte [#43](#never-using-named-result-parameters-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’re not careful enough. For example, can you spot what’s wrong with this code?
```go
func (l loc) getCoordinates(ctx context.Context, address string) (
lat, lng float32, err error) {
isValid := l.validateAddress(address) (1)
if !isValid {
return 0, 0, errors.New("invalid address")
}
if ctx.Err() != nil { (2)
return 0, 0, err
}
// Get and return coordinates
}
```
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’t assigned any value to the `err` variable. It’s 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’t always straightforward to spot while reading code. Therefore, let’s remain cautious when using named result parameters, to avoid potential side effects.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/44-side-effects-named-result-parameters/main.go)
### Retornando um receptor nulo (#45)
???+ info "TL;DR"
Ao retornar uma interface, tenha cuidado para não retornar um ponteiro nulo, mas um valor nulo explícito. Caso contrário, poderão ocorrer consequências não intencionais e o chamador receberá um valor diferente de zero.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/45-nil-receiver/main.go)
### Usando um nome de arquivo como entrada de função (#46)
???+ info "TL;DR"
Projetar funções para receber tipos `io.Reader` em vez de nomes de arquivos melhora a capacidade de reutilização de uma função 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.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/46-function-input/)
### Ignorando como argumentos `defer` e receptores são avaliados (avaliação de argumentos, ponteiros e receptores de valor) (#47)
???+ info "TL;DR"
Passar um ponteiro para uma função `defer` e agrupar uma chamada dentro de um closure são duas soluções possíveis para superar a avaliação 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.
```go
const (
StatusSuccess = "success"
StatusErrorFoo = "error_foo"
StatusErrorBar = "error_bar"
)
func f() error {
var status string
defer notify(status)
defer incrementCounter(status)
if err := foo(); err != nil {
status = StatusErrorFoo
return err
}
if err := bar(); err != nil {
status = StatusErrorBar
return err
}
status = StatusSuccess <5>
return nil
}
```
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:
```go hl_lines="3 4"
func f() error {
var status string
defer notify(&status)
defer incrementCounter(&status)
// The rest of the function unchanged
}
```
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’s another solution: calling a closure (an anonymous function value that references variables from outside its body) as a `defer` statement:
```go hl_lines="3 4 5 6"
func f() error {
var status string
defer func() {
notify(status)
incrementCounter(status)
}()
// The rest of the function unchanged
}
```
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’t require `notify` and `incrementCounter` to change their signature.
Let's also note this behavior applies with method receiver: the receiver is evaluated immediately.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/06-functions-methods/47-defer-evaluation/)
## Error Management
### Pânico (#48)
???+ info "TL;DR"
Usar `panic` é uma opção para lidar com erros no Go. No entanto, só deve ser usado com moderação em condições irrecuperáveis: por exemplo, para sinalizar um erro do programador ou quando você não consegue carregar uma dependência obrigatória.
In Go, panic is a built-in function that stops the ordinary flow:
```go
func main() {
fmt.Println("a")
panic("foo")
fmt.Println("b")
}
```
This code prints a and then stops before printing b:
```
a
panic: foo
goroutine 1 [running]:
main.main()
main.go:7 +0xb3
```
Panicking in Go should be used sparingly. There are two prominent cases, one to signal a programmer error (e.g., [`sql.Register`](https://cs.opensource.google/go/go/+/refs/tags/go1.20.7:src/database/sql/sql.go;l=44) 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.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/48-panic/main.go)
### Ignorando quando embrulhar um erro (#49)
???+ info "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ê quiser evitar isso, não use a agrupamento automático 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:
* Adding additional context to an error
* Marking an error as a specific error
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’t 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’t use error wrapping but error transformation, for example, using fmt.Errorf with the %v directive.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/49-error-wrapping/main.go)
### Comparando um tipo de erro de forma imprecisa (#50)
???+ info "TL;DR"
Se você usar o agrupamento de erros do Go 1.13 com a diretiva `%w` e `fmt.Errorf`, a comparação de um erro com um tipo deverá ser feita usando `errors.As`. Caso contrário, se o erro retornado que você deseja verificar for embrulhado, as verificações falharão.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/50-compare-error-type/main.go)
### Comparando um valor de erro incorretamente (#51)
???+ info "TL;DR"
Se você usar o agrupamento de erros do Go 1.13 com a diretiva `%w` e `fmt.Errorf`, a comparação de um erro ou de um valor deverá ser feita usando `errors.As`. Caso contrário, se o erro retornado que você deseja verificar for embrulhado, as verificações falharão.
A sentinel error is an error defined as a global variable:
```go
import "errors"
var ErrFoo = errors.New("foo")
```
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:
* Expected errors should be designed as error values (sentinel errors): `var ErrFoo = errors.New("foo")`.
* Unexpected errors should be designed as error types: `type BarError struct { ... }`, with `BarError` implementing the `error` interface.
If we use error wrapping in our application with the `%w` directive and `fmt.Errorf`, checking an error against a specific value should be done using `errors.Is` instead of `==`. Thus, even if the sentinel error is wrapped, `errors.Is` can recursively unwrap it and compare each error in the chain against the provided value.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/51-comparing-error-value/main.go)
### Lidando com um erro duas vezes (#52)
???+ info "TL;DR"
Na maioria das situações, um erro deve ser tratado apenas uma vez. Registrar um erro é tratar um erro. Portanto, você deve escolher entre registrar ou retornar um erro. Em muitos casos, o embrulho automático de erros é a solução, 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.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/52-handling-error-twice/main.go)
### Não tratando de um erro (#53)
???+ info "TL;DR"
Ignorar um erro, seja durante uma chamada de função ou em uma função `defer`, deve ser feito explicitamente usando o identificador em branco. Caso contrário, os futuros leitores poderão ficar confusos sobre se foi intencional ou um erro.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/53-not-handling-error/main.go)
### Não tratando erros de `defer` (#54)
???+ info "TL;DR"
Em muitos casos, você não deve ignorar um erro retornado por uma função `defer`. Manipule-o diretamente ou propague-o para o chamador, dependendo do contexto. Se você quiser ignorá-lo, use o identificador em branco.
Consider the following code:
```go
func f() {
// ...
notify() // Error handling is omitted
}
func notify() error {
// ...
}
```
From a maintainability perspective, the code can lead to some issues. Let’s consider a new reader looking at it. This reader notices that notify returns an error but that the error isn’t 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 (`_`):
```go
_ = notify
```
In terms of compilation and run time, this approach doesn’t change anything compared to the first piece of code. But this new version makes explicit that we aren’t interested in the error. Also, we can add a comment that indicates the rationale for why an error is ignored:
```go
// At-most once delivery.
// Hence, it's accepted to miss some of them in case of errors.
_ = notify()
```
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/07-error-management/54-defer-errors/main.go)
## Concurrency: Foundations
### Misturando simultaneidade e paralelismo (#55)
???+ info "TL;DR"
Compreender as diferenças fundamentais entre simultaneidade e paralelismo é a base do conhecimento do desenvolvedor Go. A simultaneidade tem a ver com estrutura, enquanto o paralelismo tem a ver com execução.
Concurrency and parallelism are not the same:
* Concurrency is about structure. We can change a sequential implementation into a concurrent one by introducing different steps that separate concurrent goroutines can tackle.
* Meanwhile, parallelism is about execution. We can use parallism at the steps level by adding more parallel goroutines.
In summary, concurrency provides a structure to solve a problem with parts that may be parallelized. Therefore, _concurrency enables parallelism_.
### Pensar que a simultaneidade é sempre mais rápida (#56)
???+ info "TL;DR"
Para ser um desenvolvedor proficiente, você deve reconhecer que a simultaneidade nem sempre é mais rápida. As soluções que envolvem a paralelização de cargas de trabalho mínimas podem não ser necessariamente mais rápidas do que uma implementação sequencial. A avaliação comparativa de soluções sequenciais versus soluções simultâneas deve ser a forma de validar suposições.
Read the full section [here](56-concurrency-faster.md).
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/08-concurrency-foundations/56-faster/)
### Ficar confuso sobre quando usar canais ou mutexes (#57)
???+ info "TL;DR"
Estar ciente das interações goroutine também pode ser útil ao decidir entre canais e mutexes. Em geral, goroutines paralelas requerem sincronização e, portanto, mutexes. Por outro lado, goroutines simultâneas geralmente requerem coordenação e orquestração 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:
* G1 and G2 are parallel goroutines. They may be two goroutines executing the same function that keeps receiving messages from a channel, or perhaps two goroutines executing the same HTTP handler at the same time.
* On the other hand, G1 and G3 are concurrent goroutines, as are G2 and G3. All the goroutines are part of an overall concurrent structure, but G1 and G2 perform the first step, whereas G3 does the next step.
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—therefore, channels.
Regarding concurrent goroutines, there’s 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’s important to know whether goroutines are parallel or concurrent because, in general, we need mutexes for parallel goroutines and channels for concurrent ones.
### Não entender os problemas de corrida (corridas de dados vs. condições de corrida e o modelo de memória Go) (#58)
???+ info "TL;DR"
Ser proficiente em simultaneidade também significa compreender que corridas de dados e condições de corrida são conceitos diferentes. As corridas de dados ocorrem quando várias goroutines acessam simultaneamente o mesmo local de memória e pelo menos uma delas está gravando. Enquanto isso, estar livre de disputa de dados não significa necessariamente execução determinística. Quando um comportamento depende da sequência ou do tempo de eventos que não podem ser controlados, esta é uma condição 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.
#### Data Race
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:
* Using the `sync/atomic` package
* In synchronizing the two goroutines with an ad hoc data structure like a mutex
* Using channels to make the two goroutines communicating to ensure that a variable is updated by only one goroutine at a time
#### Race Condition
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’t be controlled. Here, the timing of events is the goroutines’ execution order.
In summary, when we work in concurrent applications, it’s 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’t 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.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/08-concurrency-foundations/58-races/)
### Não compreender os impactos de simultaneidade de um tipo de carga de trabalho (#59)
???+ info "TL;DR"
Ao criar um determinado número de goroutines, considere o tipo de carga de trabalho. Criar goroutines vinculadas à CPU significa limitar esse número próximo à variável GOMAXPROCS (baseado por padrão no número de núcleos de CPU no host). A criação 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:
* The speed of the CPU—For example, running a merge sort algorithm. The workload is called CPU-bound.
* The speed of I/O—For example, making a REST call or a database query. The workload is called I/O-bound.
* The amount of available memory—The workload is called memory-bound.
???+ 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.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/08-concurrency-foundations/59-workload-type/main.go)
### Incompreensão dos contextos Go (#60)
???+ info "TL;DR"
Os contextos Go também são um dos pilares da simultaneidade em Go. Um contexto permite que você carregue um prazo, um sinal de cancelamento e/ou uma lista de valores-chave.
!!! quote "https://pkg.go.dev/context"
A Context carries a deadline, a cancellation signal, and other values across API boundaries.
#### Deadline
A deadline refers to a specific point in time determined with one of the following:
* A `time.Duration` from now (for example, in 250 ms)
* A `time.Time` (for example, 2023-02-07 00:00:00 UTC)
The semantics of a deadline convey that an ongoing activity should be stopped if this deadline is met. An activity is, for example, an I/O request or a goroutine waiting to receive a message from a channel.
#### Cancellation signals
Another use case for Go contexts is to carry a cancellation signal. Let’s 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.
#### Context values
The last use case for Go contexts is to carry a key-value list. What’s 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.
#### Catching a context cancellation
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,
* The Done channel related to a context created with `context.WithCancel` is closed when the cancel function is called.
* The Done channel related to a context created with `context.WithDeadline` is closed when the deadline has expired.
One thing to note is that the internal channel should be closed when a context is canceled or has met a deadline, instead of when it receives a specific value, because the closure of a channel is the only channel action that all the consumer goroutines will receive. This way, all the consumers will be notified once a context is canceled or a deadline is reached.
In summary, to be a proficient Go developer, we have to understand what a context is and how to use it. In general, a function that users wait for should take a context, as doing so allows upstream callers to decide when calling this function should be aborted.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/08-concurrency-foundations/60-contexts/main.go)
## Concurrency: Practice
### Propagando um contexto impróprio (#61)
???+ info "TL;DR"
Compreender as condições em que um contexto pode ser cancelado deve ser importante ao propagá-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’s 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’t 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:
```go
func handler(w http.ResponseWriter, r *http.Request) {
response, err := doSomeTask(r.Context(), r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go func() {
err := publish(r.Context(), response)
// Do something with err
}()
writeResponse(response)
}
```
What’s wrong with this piece of code? We have to know that the context attached to an HTTP request can cancel in different conditions:
* When the client’s connection closes
* In the case of an HTTP/2 request, when the request is canceled
* When the response has been written back to the client
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’s probably OK to call publish with a context already canceled so the message isn’t 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:
* If the response is written after the Kafka publication, we both return a response and publish a message successfully
* However, if the response is written before or during the Kafka publication, the message shouldn’t be published.
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`](https://pkg.go.dev/context#WithoutCancel) returns a copy of parent that is not canceled when parent is canceled.
In summary, propagating a context should be done cautiously.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/61-inappropriate-context/main.go)
### Iniciando uma goroutine sem saber quando interrompê-la (#62)
???+ info "TL;DR"
Evitar vazamentos significa estar ciente de que sempre que uma goroutine for iniciada, você deve ter um plano para interrompê-la eventualmente.
Goroutines are easy and cheap to start—so 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’s discuss a concrete example. We will design an application that needs to watch some external configuration (for example, using a database connection). Here’s a first implementation:
```go
func main() {
newWatcher()
// Run the application
}
type watcher struct { /* Some resources */ }
func newWatcher() {
w := watcher{}
go w.watch() // Creates a goroutine that watches some external configuration
}
```
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’t 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:
```go
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
newWatcher(ctx)
// Run the application
}
func newWatcher(ctx context.Context) {
w := watcher{}
go w.watch(ctx)
}
```
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—and that’s a design flaw.
The problem is that we used signaling to convey that a goroutine had to be stopped. We didn’t block the parent goroutine until the resources had been closed. Let’s make sure we do:
```go
func main() {
w := newWatcher()
defer w.close()
// Run the application
}
func newWatcher() watcher {
w := watcher{}
go w.watch()
return w
}
func (w watcher) close() {
// Close the resources
}
```
Instead of signaling `watcher` that it’s 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’s 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’s probably safer to wait for this goroutine to complete before exiting the application. This way, we can ensure that the resources can be freed.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/62-starting-goroutine/)
### :warning: Não ter cuidado com goroutines e variáveis de loop (#63)
???+ warning
Este erro não é mais relevante no Go 1.22 ([detalhes](https://go.dev/blog/loopvar-preview)).
### Esperando um comportamento determinístico usando seleção e canais (#64)
???+ info "TL;DR"
Compreender que com `select` vários canais escolhe o caso aleatoriamente se múltiplas opções forem possíveis evita fazer suposições 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
go func() {
for i := 0; i < 10; i++ {
messageCh <- i
}
disconnectCh <- struct{}{}
}()
for {
select {
case v := <-messageCh:
fmt.Println(v)
case <-disconnectCh:
fmt.Println("disconnection, return")
return
}
}
```
If we run this example multiple times, the result will be random:
```
0
1
2
disconnection, return
0
disconnection, return
```
Instead of consuming the 10 messages, we only received a few of them. What’s 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’s 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’s 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.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/64-select-behavior/main.go)
### Não usar canais de notificação (#65)
???+ info "TL;DR"
Envie notificações 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’s 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`:
```go
disconnectCh := make(chan bool)
```
Now, let’s say we interact with an API that provides us with such a channel. Because it’s a channel of Booleans, we can receive either `true` or `false` messages. It’s probably clear what `true` conveys. But what does `false` mean? Does it mean we haven’t 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’s the case, meaning we don’t 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{}`.
### Não usar canais nulos (#66)
???+ info "TL;DR"
O uso de canais nulos deve fazer parte do seu conjunto de ferramentas de simultaneidade porque permite remover casos de instruções `select`, por exemplo.
What should this code do?
```go
var ch chan int
<-ch
```
`ch` is a `chan int` type. The zero value of a channel being nil, `ch` is `nil`. The goroutine won’t panic; however, it will block forever.
The principle is the same if we send a message to a nil channel. This goroutine blocks forever:
```go
var ch chan int
ch <- 0
```
Then what’s 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:
```go hl_lines="5 9 15"
func merge(ch1, ch2 <-chan int) <-chan int {
ch := make(chan int, 1)
go func() {
for ch1 != nil || ch2 != nil { // Continue if at least one channel isn’t nil
select {
case v, open := <-ch1:
if !open {
ch1 = nil // Assign ch1 to a nil channel once closed
break
}
ch <- v
case v, open := <-ch2:
if !open {
ch2 = nil // Assigns ch2 to a nil channel once closed
break
}
ch <- v
}
}
close(ch)
}()
return ch
}
```
This elegant solution relies on nil channels to somehow _remove_ one case from the `select` statement.
Let’s keep this idea in mind: nil channels are useful in some conditions and should be part of the Go developer’s toolset when dealing with concurrent code.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/66-nil-channels/main.go)
### Ficar intrigado com o tamanho do canal (#67)
???+ info "TL;DR"
Decida cuidadosamente o tipo de canal correto a ser usado, considerando o problema. Somente canais sem buffer oferecem fortes garantias de sincronização. Para canais em buffer, você 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:
```go
ch1 := make(chan int)
ch2 := make(chan int, 0)
```
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:
```go
ch3 := make(chan int, 1)
```
With a buffered channel, a sender can send messages while the channel isn’t full. Once the channel is full, it will block until a receiver goroutine receives a message:
```go
ch3 := make(chan int, 1)
ch3 <-1 // Non-blocking
ch3 <-2 // Blocking
```
The first send isn’t blocking, whereas the second one is, as the channel is full at this stage.
What's the main difference between unbuffered and buffered channels:
* An unbuffered channel enables synchronization. We have the guarantee that two goroutines will be in a known state: one receiving and another sending a message.
* A buffered channel doesn’t provide any strong synchronization. Indeed, a producer goroutine can send a message and then continue its execution if the channel isn’t full. The only guarantee is that a goroutine won’t receive a message before it is sent. But this is only a guarantee because of causality (you don’t drink your coffee before you prepare it).
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’s a list of possible cases where we should use another size:
* While using a worker pooling-like pattern, meaning spinning a fixed number of goroutines that need to send data to a shared channel. In that case, we can tie the channel size to the number of goroutines created.
* When using channels for rate-limiting problems. For example, if we need to enforce resource utilization by bounding the number of requests, we should set up the channel size according to the limit.
If we are outside of these cases, using a different channel size should be done cautiously. Let’s bear in mind that deciding about an accurate queue size isn’t an easy problem:
!!! quote "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.
### Esquecendo os possíveis efeitos colaterais da formatação de strings (#68)
???+ info "TL;DR"
Estar ciente de que a formatação de strings pode levar à chamada de funções existentes significa estar atento a possíveis impasses e outras disputas de dados.
It’s pretty easy to forget the potential side effects of string formatting while working in a concurrent application.
#### [etcd](https://github.com/etcd-io/etcd) data race
[github.com/etcd-io/etcd/pull/7816](https://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.
#### Deadlock
Can you see what the problem is in this code with a `Customer` struct exposing an `UpdateAge` method and implementing the `fmt.Stringer` interface?
```go
type Customer struct {
mutex sync.RWMutex // Uses a sync.RWMutex to protect concurrent accesses
id string
age int
}
func (c *Customer) UpdateAge(age int) error {
c.mutex.Lock() // Locks and defers unlock as we update Customer
defer c.mutex.Unlock()
if age < 0 { // Returns an error if age is negative
return fmt.Errorf("age should be positive for customer %v", c)
}
c.age = age
return nil
}
func (c *Customer) String() string {
c.mutex.RLock() // Locks and defers unlock as we read Customer
defer c.mutex.RUnlock()
return fmt.Sprintf("id %s, age %d", c.id, c.age)
}
```
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’t 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:
```go hl_lines="2 3 4"
func (c *Customer) UpdateAge(age int) error {
if age < 0 {
return fmt.Errorf("age should be positive for customer %v", c)
}
c.mutex.Lock() <1>
defer c.mutex.Unlock()
c.age = age
return nil
}
```
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:
```go hl_lines="6"
func (c *Customer) UpdateAge(age int) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if age < 0 {
return fmt.Errorf("age should be positive for customer id %s", c.id)
}
c.age = age
return nil
}
```
In concurrent applications, we should remain cautious about the possible side effects of string formatting.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/68-string-formatting/main.go)
### Criando corridas de dados com acréscimo (#69)
???+ info "TL;DR"
As chamadas `append` nem sempre são isentas de disputa de dados; portanto, não 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?
```go
s := make([]int, 1)
go func() { // In a new goroutine, appends a new element on s
s1 := append(s, 1)
fmt.Println(s1)
}()
go func() { // Same
s2 := append(s, 1)
fmt.Println(s2)
}()
```
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’t mutate the existing array; hence, it doesn’t lead to a data race.
Now, let’s 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?
```go hl_lines="1"
s := make([]int, 0, 1)
go func() {
s1 := append(s, 1)
fmt.Println(s1)
}()
go func() {
s2 := append(s, 1)
fmt.Println(s2)
}()
```
The answer is yes. We create a slice with `make([]int, 0, 1)`. Therefore, the array isn’t 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.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/69-data-race-append/main.go)
### Usando mutexes imprecisamente com slices e maps (#70)
???+ info "TL;DR"
Lembrar que slices e maps são 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:
```go
type Cache struct {
mu sync.RWMutex
balances map[string]float64
}
```
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):
```go
func (c *Cache) AddBalance(id string, balance float64) {
c.mu.Lock()
c.balances[id] = balance
c.mu.Unlock()
}
```
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:
```go
func (c *Cache) AverageBalance() float64 {
c.mu.RLock()
balances := c.balances // Creates a copy of the balances map
c.mu.RUnlock()
sum := 0.
for _, balance := range balances { // Iterates over the copy, outside of the critical section
sum += balance
}
return sum / float64(len(balances))
}
```
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’s 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’t 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:
```go hl_lines="2 3"
func (c *Cache) AverageBalance() float64 {
c.mu.RLock()
defer c.mu.RUnlock() // Unlocks when the function returns
sum := 0.
for _, balance := range c.balances {
sum += balance
}
return sum / float64(len(c.balances))
}
```
Another option, if the iteration operation isn’t lightweight, is to work on an actual copy of the data and protect only the copy:
```go hl_lines="2 3 4 5 6 7"
func (c *Cache) AverageBalance() float64 {
c.mu.RLock()
m := make(map[string]float64, len(c.balances)) // Copies the map
for k, v := range c.balances {
m[k] = v
}
c.mu.RUnlock()
sum := 0.
for _, balance := range m {
sum += balance
}
return sum / float64(len(m))
}
```
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’t 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’s be cautious when designing critical sections and make sure the boundaries are accurately defined.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/70-mutex-slices-maps/main.go)
### Uso indevido `sync.WaitGroup` (#71)
???+ info "TL;DR"
Para usar com precisão `sync.WaitGroup`, chame o método `Add` antes de ativar goroutines.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/71-wait-group/main.go)
### Esquecendo `sync.Cond` (#72)
???+ info "TL;DR"
Você pode enviar notificações repetidas para vários goroutines com `sync.Cond`.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/72-cond/main.go)
### Não usando `errgroup` (#73)
???+ info "TL;DR"
Você pode sincronizar um grupo de goroutines e lidar com erros e contextos com o pacote `errgroup`.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/73-errgroup/main.go)
### Copiando um tipo `sync` (#74)
???+ info "TL;DR"
Tipos `sync` não devem ser copiados.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/74-copying-sync/main.go)
## Standard Library
### Fornecendo uma duração de tempo errada (#75)
???+ info "TL;DR"
Seja cauteloso com funções que aceitam um arquivo `time.Duration`. Mesmo que a passagem de um número inteiro seja permitida, tente usar a API time para evitar qualquer possível confusão.
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`:
```go
ticker := time.NewTicker(1000)
for {
select {
case <-ticker.C:
// Do something
}
}
```
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:
```go
ticker = time.NewTicker(time.Microsecond)
// Or
ticker = time.NewTicker(1000 * time.Nanosecond)
```
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/75-wrong-time-duration/main.go)
### `time.After` e vazamentos de memória (#76)
???+ info "TL;DR"
Evitar chamadas para funções `time.After` repetidas (como loops ou manipuladores HTTP) pode evitar pico de consumo de memória. Os recursos criados por `time.After` são liberados somente quando o cronômetro 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:
```go
func consumer(ch <-chan Event) {
for {
select {
case event := <-ch:
handle(event)
case <-time.After(time.Hour):
log.Println("warning: no messages received")
}
}
}
```
The source code of the function time.After is as follows:
```go
func After(d Duration) <-chan Time {
return NewTimer(d).C
}
```
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:
```go
func consumer(ch <-chan Event) {
for {
ctx, cancel := context.WithTimeout(context.Background(), time.Hour)
select {
case event := <-ch:
cancel()
handle(event)
case <-ctx.Done():
log.Println("warning: no messages received")
}
}
}
```
We can also use `time.NewTimer` like so:
```go
func consumer(ch <-chan Event) {
timerDuration := 1 * time.Hour
timer := time.NewTimer(timerDuration)
for {
timer.Reset(timerDuration)
select {
case event := <-ch:
handle(event)
case <-timer.C:
log.Println("warning: no messages received")
}
}
}
```
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/76-time-after/main.go)
### Lidando com erros comuns JSON (#77)
* Comportamento inesperado devido à incorporação de tipo
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ão.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/77-json-handling/type-embedding/main.go)
* JSON e o relógio monotônico
Ao comparar duas estruturas `time.Time`, lembre-se de que `time.Time` contém um relógio de parede e um relógio monotônico, e a comparação usando o operador == é feita em ambos os relógios.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/77-json-handling/monotonic-clock/main.go)
* Map de `any`
Para evitar suposições erradas ao fornecer um map ao desempacotar (unmarshaling) dados JSON, lembre-se de que os valores numéricos são convertidos para `float64` por padrão.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/77-json-handling/map-any/main.go)
### Erros comuns de SQL (#78)
* Esquecer `sql.Open` não necessariamente estabelece conexões com um banco de dados
Esquecer `sql.Open` não necessariamente estabelece conexões com um banco de dados
Chame o método `Ping` ou `PingContext` se precisar testar sua configuração e garantir que um banco de dados esteja acessível.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/78-sql/sql-open)
* Esquecendo o pool de conexões
Configure os parâmetros de conexão do banco de dados para aplicativos de nível de produção.
* Não usar declarações preparadas
O uso de instruções preparadas em SQL torna as consultas mais eficientes e seguras.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/78-sql/prepared-statements)
* Tratamento incorreto de valores nulos
Lide com colunas anuláveis em tabelas usando ponteiros ou tipos `sql.NullXXX`.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/78-sql/null-values/main.go)
* Não tratando de erros de iteração de linhas
Chame o método `Err` de `sql.Rows` iterações posteriores à linha para garantir que você não perdeu nenhum erro ao preparar a próxima linha.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/78-sql/rows-iterations-errors)
### Não fechando recursos transitórios (body HTTP, `sql.Rows` e `os.File`) (#79)
???+ info "TL;DR"
Eventualmente feche todas as estruturas implementadas `io.Closer` para evitar possíveis vazamentos.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/79-closing-resources/)
### Esquecendo a instrução return após responder a uma solicitação HTTP (#80)
???+ info "TL;DR"
Para evitar comportamentos inesperados nas implementações do manipulador HTTP, certifique-se de não perder a instrução `return` se quiser que um manipulador pare após `http.Error`.
Consider the following HTTP handler that handles an error from `foo` using `http.Error`:
```go
func handler(w http.ResponseWriter, req *http.Request) {
err := foo(req)
if err != nil {
http.Error(w, "foo", http.StatusInternalServerError)
}
_, _ = w.Write([]byte("all good"))
w.WriteHeader(http.StatusCreated)
}
```
If we run this code and `err != nil`, the HTTP response would be:
```
foo
all good
```
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)
```
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:
```go
func handler(w http.ResponseWriter, req *http.Request) {
err := foo(req)
if err != nil {
http.Error(w, "foo", http.StatusInternalServerError)
return // Adds the return statement
}
_, _ = w.Write([]byte("all good"))
w.WriteHeader(http.StatusCreated)
}
```
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/80-http-return/main.go)
### Usando o cliente e servidor HTTP padrão (#81)
???+ info "TL;DR"
Para aplicativos de nível de produção, não use as implementações de cliente e servidor HTTP padrão. Essas implementações não possuem tempos limite e comportamentos que deveriam ser obrigatórios na produção.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/81-default-http-client-server/)
## Teste
### Não categorizar testes (tags de construção, variáveis de ambiente e modo abreviado) (#82)
???+ info "TL;DR"
Categorizar testes usando sinalizadores de construção, variáveis de ambiente ou modo curto torna o processo de teste mais eficiente. Você pode criar categorias de teste usando sinalizadores de construção ou variáveis de ambiente (por exemplo, testes de unidade versus testes de integração) e diferenciar testes curtos de testes de longa duração para decidir quais tipos de testes executar.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/82-categorizing-tests/)
### Não habilitando a bandeira de corrida (#83)
???+ info "TL;DR"
A ativação do sinalizador `-race` é altamente recomendada ao escrever aplicativos simultâneos. Isso permite que você detecte possíveis corridas de dados que podem levar a bugs de software.
In Go, the race detector isn’t a static analysis tool used during compilation; instead, it’s 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:
```bash
go test -race ./...
```
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:
```go
package main
import (
"fmt"
)
func main() {
i := 0
go func() { i++ }()
fmt.Println(i)
}
```
Runnig this code with the `-race` logs the following warning:
```bash hl_lines="3 7 11"
==================
WARNING: DATA RACE
Write at 0x00c000026078 by goroutine 7: # (1)
main.main.func1()
/tmp/app/main.go:9 +0x4e
Previous read at 0x00c000026078 by main goroutine: # (2)
main.main()
/tmp/app/main.go:10 +0x88
Goroutine 7 (running) created at: # (3)
main.main()
/tmp/app/main.go:9 +0x7a
==================
```
1. Indicates that goroutine 7 was writing
2. Indicates that the main goroutine was reading
3. Indicates when the goroutine 7 was created
Let’s make sure we are comfortable reading these messages. Go always logs the following:
* The concurrent goroutines that are incriminated: here, the main goroutine and goroutine 7.
* Where accesses occur in the code: in this case, lines 9 and 10.
* When these goroutines were created: goroutine 7 was created in main().
In addition, if a specific file contains tests that lead to data races, we can exclude it :material-information-outline:{ title="temporarily! 😉" } from race detection using the `!race` build tag:
```go
//go:build !race
package main
import (
"testing"
)
func TestFoo(t *testing.T) {
// ...
}
```
### Não usar modos de execução de teste (paralelo e aleatório) (#84)
???+ info "TL;DR"
Usar o sinalizador `-parallel` é uma forma eficiente de acelerar testes, especialmente os de longa duração. Use o sinalizador `-shuffle` para ajudar a garantir que um conjunto de testes não se baseie em suposições erradas que possam ocultar bugs.
### Não usar testes baseados em tabela (#85)
???+ info "TL;DR"
Os testes baseados em tabelas são uma maneira eficiente de agrupar um conjunto de testes semelhantes para evitar a duplicação de código e facilitar o manuseio de atualizações futuras.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/85-table-driven-tests/main_test.go)
### Dormindo em testes unitários (#86)
???+ info "TL;DR"
Evite interrupções usando a sincronização para tornar o teste menos instável e mais robusto. Se a sincronização não for possível, considere uma abordagem de nova tentativa.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/86-sleeping/main_test.go)
### Não lidar com a API de tempo de forma eficiente (#87)
???+ info "TL;DR"
Entender como lidar com funções usando a API time é outra maneira de tornar um teste menos complicado. Você pode usar técnicas padrão, como lidar com o tempo como parte de uma dependência oculta ou solicitar que os clientes o forneçam.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/87-time-api/)
### Não usar pacotes de utilitários de teste ( `httptest` e `iotest`) (#88)
* O pacote `httptest` é útil para lidar com aplicativos HTTP. Ele fornece um conjunto de utilitários para testar clientes e servidores.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/88-utility-package/httptest/main_test.go)
* O pacote `iotest` ajuda a escrever io.Reader e testar se um aplicativo é tolerante a erros.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/88-utility-package/iotest/main_test.go)
### Escrevendo benchmarks imprecisos (#89)
???+ info "TL;DR"
Regarding benchmarks:
* Use métodos de tempo para preservar a precisão de um benchmark.
* Aumentar o tempo de teste ou usar ferramentas como o benchstat pode ser útil ao lidar com micro-benchmarks.
* Tenha cuidado com os resultados de um micro-benchmark se o sistema que executa o aplicativo for diferente daquele que executa o micro-benchmark.
* Certifique-se de que a função em teste cause um efeito colateral, para evitar que as otimizações do compilador enganem você sobre os resultados do benchmark.
* Para evitar o efeito observador, force um benchmark a recriar os dados usados por uma função vinculada à CPU.
Leia a seção completa [aqui](89-benchmarks.md).
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/89-benchmark/)
### Não explorando todos os recursos de teste do Go (#90)
* Cobertura de código
Use a cobertura de código com o sinalizador `-coverprofile` para ver rapidamente qual parte do código precisa de mais atenção.
* Testando de um pacote diferente
Coloque os testes unitários em um pacote diferente para impor testes de escrita que se concentrem em um comportamento exposto, não em internos.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/90-testing-features/different-package/main_test.go)
* Funções utilitárias
O tratamento de erros usando a variável `*testing.T` em vez do clássico `if err != nil` torna o código mais curto e fácil de ler.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/90-testing-features/utility-function/main_test.go)
* Configuração e desmontagem
Você pode usar funções de setup e teardown para configurar um ambiente complexo, como no caso de testes de integração.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/11-testing/90-testing-features/setup-teardown/main_test.go)
### Não usar fuzzing (erro da comunidade)
???+ info "TL;DR"
Fuzzing é uma estratégia eficiente para detectar entradas aleatórias, inesperadas ou malformadas em funções e métodos complexos, a fim de descobrir vulnerabilidades, bugs ou até mesmo travamentos potenciais.
Credits: [@jeromedoucet](https://github.com/jeromedoucet)
## Otimizações
### Não entendendo os caches da CPU (#91)
* Arquitetura da CPU
Compreender como usar caches de CPU é importante para otimizar aplicativos vinculados à CPU porque o cache L1 é cerca de 50 a 100 vezes mais rápido que a memória principal.
* Linha de cache
Estar consciente do conceito de linha de cache é fundamental para entender como organizar dados em aplicativos com uso intensivo de dados. Uma CPU não busca memória palavra por palavra; em vez disso, geralmente copia um bloco de memória para uma linha de cache de 64 bytes. Para aproveitar ao máximo cada linha de cache individual, imponha a localidade espacial.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/91-cpu-caches/cache-line/)
* Slice de estruturas vs. estrutura de slices
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/91-cpu-caches/slice-structs/)
* Previsibilidade
Tornar o código previsível para a CPU também pode ser uma forma eficiente de otimizar certas funções. Por exemplo, uma passada unitária ou constante é previsível para a CPU, mas uma passada não unitária (por exemplo, uma lista vinculada) não é previsível.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/91-cpu-caches/predictability/)
* Política de posicionamento de cache
Para evitar um avanço crítico e, portanto, utilizar apenas uma pequena parte do cache, esteja ciente de que os caches são particionados.
### Escrevendo código simultâneo que leva a compartilhamento falso (#92)
???+ info "TL;DR"
Saber que níveis mais baixos de caches de CPU não são compartilhados entre todos os núcleos ajuda a evitar padrões que degradam o desempenho, como compartilhamento falso ao escrever código de simultaneidade. Compartilhar memória é uma ilusão.
Leia a seção completa [aqui](92-false-sharing.md).
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/92-false-sharing/)
### Não levando em consideração o paralelismo no nível de instrução (#93)
???+ info "TL;DR"
Use o ILP para otimizar partes específicas do seu código para permitir que uma CPU execute tantas instruções paralelas quanto possível. Identificar perigos nos dados é uma das etapas principais.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/93-instruction-level-parallelism/)
### Não estar ciente do alinhamento dos dados (#94)
???+ info "TL;DR"
Você pode evitar erros comuns lembrando que no Go os tipos básicos são alinhados com seu próprio 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ção de memória e potencialmente uma melhor localidade espacial).
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/94-data-alignment/)
### Não entendendo stack vs. heap (#95)
???+ info "TL;DR"
Compreender as diferenças fundamentais entre heap e pilha também deve fazer parte do seu conhecimento básico ao otimizar um aplicativo Go. As alocações de pilha são quase gratuitas, enquanto as alocações de heap são mais lentas e dependem do GC para limpar a memória.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/95-stack-heap/)
### Não saber como reduzir alocações (mudança de API, otimizações de compilador e `sync.Pool`) (#96)
???+ info "TL;DR"
A redução das alocações também é um aspecto essencial da otimização de um aplicativo Go. Isso pode ser feito de diferentes maneiras, como projetar a API cuidadosamente para evitar compartilhamento, compreender as otimizações comuns do compilador Go e usar `sync.Pool`.
[:simple-github: Código fonte](https://github.com/teivah/100-go-mistakes/tree/master/src/12-optimizations/96-reduce-allocations/)
### Não dependendo do inlining (#97)
???+ info "TL;DR"
Use a técnica de inlining de caminho rápido para reduzir com eficiência o tempo amortizado para chamar uma função.
### Não usar ferramentas de diagnóstico Go (#98)
???+ info "TL;DR"
Confie na criação de perfil e no rastreador de execução para entender o desempenho de um aplicativo e as partes a serem otimizadas.
Leia a seção completa [aqui](98-profiling-execution-tracing.md).
### Não entendendo como funciona o GC (#99)
???+ info "TL;DR"
Compreender como ajustar o GC pode levar a vários benefícios, como lidar com aumentos repentinos de carga com mais eficiência.
### Não entendendo os impactos da execução do Go no Docker e Kubernetes (#100)
???+ info "TL;DR"
Para ajudar a evitar a limitação da CPU quando implantado no Docker e no Kubernetes, lembre-se de que Go não 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](https://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](https://kubernetes.io/docs/concepts/configuration/manage-resources-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](https://github.com/uber-go/automaxprocs) that automatically set `GOMAXPROCS` to match the Linux container CPU quota.
## Community
Thanks to all the contributors:
================================================
FILE: docs/stylesheets/extra.css
================================================
.md-typeset figure img {
display: inline;
}
.md-typeset .admonition,
.md-typeset details {
font-size: 15px
}
================================================
FILE: docs/zh.md
================================================
---
title: Chinese (Simplified) Version
comments: true
---
# 100 个 Go 常见错误及如何避免
???+ tip "The Coder Cafe"
如果您喜欢我的书,您可能会对我的最新项目感兴趣:[The Coder Cafe](https://thecoder.cafe?rd=100go.co/zh),这是一个为程序员提供的每日简报。
> Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve one essential concept for coders. Written by a senior software engineer at Google, it's perfectly brewed for your morning coffee, helping you grow your skills deeply.
{% endif %}
================================================
FILE: site/20-slice/index.html
================================================
Not understanding slice length and capacity (#20) - 100 Go Mistakes and How to Avoid Them
It’s 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’s 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’s 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’s go through a few examples to make things clearer. First, let’s initialize a slice with a given length and capacity:
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’s second element: s[1] = 1.
However, accessing an element outside the length range is forbidden, even though it’s 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
How can we use the remaining space of the slice? By using the append built-in function:
s=append(s,2)
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’t large enough?
s=append(s,3,4,5)fmt.Println(s)
If we run this code, we see that the slice was able to cope with our request:
[0 1 0 2 3 4 5]
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’s no longer referenced, it’s eventually freed by the garbage collector (GC) if allocated on the heap. (We discuss heap memory in mistake #95, “Not understanding stack vs. heap,” and we look at how the GC works in mistake #99, “Not understanding how the GC works.”)
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 slices2:=s1[1:3]// Slicing from indices 1 to 3
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)
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=[010],s2=[102]
It’s important to understand this behavior so that we don’t 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’s add three more elements so that the backing array will not have enough capacity:
s2=append(s2,3)s2=append(s2,4)// At this stage, the backing is already fulls2=append(s2,5)
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’s 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.
Comments
================================================
FILE: site/28-maps-memory-leaks/index.html
================================================
Maps and memory leaks (#28) - 100 Go Mistakes and How to Avoid Them
When working with maps in Go, we need to understand some important characteristics of how a map grows and shrinks. Let’s delve into this to prevent issues that can cause memory leaks.
First, to view a concrete example of this problem, let’s design a scenario where we will work with the following map:
m:=make(map[int][128]byte)
Each value of m is an array of 128 bytes. We will do the following:
Allocate an empty map.
Add 1 million elements.
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:
funcmain(){n:=1_000_000m:=make(map[int][128]byte)printAlloc()fori:=0;i<n;i++{// Adds 1 million elementsm[i]=[128]byte{}}printAlloc()fori:=0;i<n;i++{// Deletes 1 million elementsdelete(m,i)}runtime.GC()// Triggers a manual GCprintAlloc()runtime.KeepAlive(m)// Keeps a reference to m so that the map isn’t collected}funcprintAlloc(){varmruntime.MemStatsruntime.ReadMemStats(&m)fmt.Printf("%d MB\n",m.Alloc/(1024*1024))}
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’t collected as well. Let’s run this example:
0 MB <-- After m is allocated
461 MB <-- After we add 1 million elements
293 MB <-- After we remove 1 million elements
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’t 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’s 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:
typehmapstruct{Buint8// log_2 of # of buckets// (can hold up to loadFactor * 2^B items)// ...}
After adding 1 million elements, the value of B equals 18, which means 2¹⁸ = 262,144 buckets. When we remove 1 million elements, what’s 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’t 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’t impact the map itself. Even the number of extra buckets (the buckets created because of overflows) remains the same.
Let’s 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’t worry about the fact that a map cannot shrink.
However, let’s 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’t significantly decrease in such a scenario.
What are the solutions if we don’t 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’t 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’s 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’t 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.
Comments
================================================
FILE: site/404.html
================================================
100 Go Mistakes and How to Avoid Them
================================================
FILE: site/5-interface-pollution/index.html
================================================
Interface pollution (#5) - 100 Go Mistakes and How to Avoid Them
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’s a common mistake made by developers coming from another language with different habits. Before delving into the topic, let’s refresh our minds about Go’s interfaces. Then, we will see when it’s appropriate to use interfaces and when it may be considered pollution.
Concepts
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:
typeReaderinterface{Read(p[]byte)(nint,errerror)}
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:
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:
io.Reader reads data from a source
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.File. Or, we can choose to create a more generic function using io.Reader and io.Writer abstractions:
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:
funcTestCopySourceToDest(t*testing.T){constinput="foo"source:=strings.NewReader(input)// Creates an io.Readerdest:=bytes.NewBuffer(make([]byte,0))// Creates an io.Writererr:=copySourceToDest(source,dest)// Calls copySourceToDest from a *strings.Reader and a *bytes.Bufferiferr!=nil{t.FailNow()}got:=dest.String()ifgot!=input{t.Errorf("expected: %s, got: %s",input,got)}}
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:
typeReadWriterinterface{ReaderWriter}
Note
As Einstein said, “Everything should be made as simple as possible, but no simpler.” Applied to interfaces, this denotes that finding the perfect granularity for an interface isn’t necessarily a straightforward process.
Let’s now discuss common cases where interfaces are recommended.
When to use interfaces
When should we create interfaces in Go? Let’s look at three concrete use cases where interfaces are usually considered to bring value. Note that the goal isn’t 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:
Common behavior
Decoupling
Restricting behavior
Common behavior
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:
Retrieving the number of elements in the collection
Reporting whether one element must be sorted before another
Swapping two elements
Hence, the following interface was added to the sort package:
typeInterfaceinterface{Len()int// Number of elementsLess(i,jint)bool// Checks two elementsSwap(i,jint)// Swaps two elements}
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’t 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:
Because sort.Interface is the right level of abstraction, it makes it highly valuable.
Let’s now see another main use case when using interfaces.
Decoupling
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’s SOLID design principles).
One benefit of decoupling can be related to unit testing. Let’s 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’s say a mysql.Store struct):
typeCustomerServicestruct{storemysql.Store// Depends on the concrete implementation}func(csCustomerService)CreateNewCustomer(idstring)error{customer:=Customer{id:id}returncs.store.StoreCustomer(customer)}
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’t the scope of this section). Although integration tests are helpful, that’s 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:
typecustomerStorerinterface{// Creates a storage abstractionStoreCustomer(Customer)error}typeCustomerServicestruct{storercustomerStorer// Decouples CustomerService from the actual implementation}func(csCustomerService)CreateNewCustomer(idstring)error{customer:=Customer{id:id}returncs.storer.StoreCustomer(customer)}
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:
Use the concrete implementation via integration tests
Use a mock (or any kind of test double) via unit tests
Or both
Let’s now discuss another use case: to restrict a behavior.
Restricting behavior
The last use case we will discuss can be pretty counterintuitive at first sight. It’s about restricting a type to a specific behavior. Let’s 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’s how that code would look:
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’t want to change our configuration package? By creating an abstraction that restricts the behavior to retrieving only a config value:
typeintConfigGetterinterface{Get()int}
Then, in our code, we can rely on intConfigGetter instead of the concrete implementation:
typeFoostruct{thresholdintConfigGetter}funcNewFoo(thresholdintConfigGetter)Foo{// Injects the configuration getterreturnFoo{threshold:threshold}}func(fFoo)Bar(){threshold:=f.threshold.Get()// Reads the configuration// ...}
In this example, the configuration getter is injected into the NewFoo factory method. It doesn’t 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’t exhaustive, but it should give us a general understanding of when interfaces are helpful in Go.
Now, let’s finish this section and discuss the problems with interface pollution.
Interface pollution
It’s fairly common to see interfaces being overused in Go projects. Perhaps the developer’s background was C# or Java, and they found it natural to create interfaces before concrete types. However, this isn’t 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’t start creating abstractions in our code if there is no immediate reason to do so. We shouldn’t 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’s 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’t bring any value; it creates a worthless abstraction making the code more difficult to read, understand, and reason about. If we don’t have a strong reason for adding an interface and it’s unclear how an interface makes a code better, we should challenge this interface’s 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’s data structure to find the concrete type an interface points to. But this isn’t an issue in many contexts as the overhead is minimal.
In summary, we should be cautious when creating abstractions in our code—abstractions should be discovered, not created. It’s 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’t design with interfaces, discover them.
Let’s not try to solve a problem abstractly but solve what has to be solved now. Last, but not least, if it’s unclear how an interface makes the code better, we should probably consider removing it to make our code simpler.
Comments
================================================
FILE: site/56-concurrency-faster/index.html
================================================
Thinking concurrency is always faster (#56) - 100 Go Mistakes and How to Avoid Them
A misconception among many developers is believing that a concurrent solution is always faster than a sequential one. This couldn’t 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’t necessarily faster.
Go Scheduling
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:
Concurrent — Two or more threads can start, run, and complete in overlapping time periods.
Parallel — The same task can be executed multiple times at once.
The OS is responsible for scheduling the thread’s processes optimally so that:
All the threads can consume CPU cycles without being starved for too much time.
The workload is distributed as evenly as possible among the different CPU cores.
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’s 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’t 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–32, 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’s now discuss how the Go scheduler works to overview how goroutines are handled. Internally, the Go scheduler uses the following terminology (see proc.go):
G — Goroutine
M — OS thread (stands for machine)
P — CPU core (stands for processor)
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:
Executing — The goroutine is scheduled on an M and executing its instructions.
Runnable — The goroutine is waiting to be in an executing state.
Waiting — The goroutine is stopped and pending something completing, such as a system call or a synchronization operation (such as acquiring a mutex).
There’s 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’t 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’s no goroutine to be executed. This isn’t 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’s the scheduling implementation in pseudocode (see proc.go):
runtime.schedule() {
// Only 1/61 of the time, check the global runnable queue for a G.
// If not found, check the local queue.
// If not found,
// Try to steal from other Ps.
// If not, check the global runnable queue.
// If not found, poll network.
}
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’s 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’s look at a concrete example: implementing a merge sort in a parallel manner.
Parallel Merge Sort
First, let’s briefly review how the merge sort algorithm works. Then we will implement a parallel version. Note that the objective isn’t to implement the most efficient version but to support a concrete example showing why concurrency isn’t 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’t include all of the code as it’s not the main point of this section:
funcsequentialMergesort(s[]int){iflen(s)<=1{return}middle:=len(s)/2sequentialMergesort(s[:middle])// First halfsequentialMergesort(s[middle:])// Second halfmerge(s,middle)// Merges the two halves}funcmerge(s[]int,middleint){// ...}
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’t 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’s write a first parallel implementation:
funcparallelMergesortV1(s[]int){iflen(s)<=1{return}middle:=len(s)/2varwgsync.WaitGroupwg.Add(2)gofunc(){// Spins up the first half of the work in a goroutinedeferwg.Done()parallelMergesortV1(s[:middle])}()gofunc(){// Spins up the second half of the work in a goroutinedeferwg.Done()parallelMergesortV1(s[middle:])}()wg.Wait()merge(s,middle)// Merges the halves}
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’s run it on a four-core machine with 10,000 elements:
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’s 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’re 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’s try another approach. Because merging a tiny number of elements within a new goroutine isn’t efficient, let’s 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’s a new version:
constmax=2048// Defines the thresholdfuncparallelMergesortV2(s[]int){iflen(s)<=1{return}iflen(s)<=max{sequentialMergesort(s)// Calls our initial sequential version}else{// If bigger than the threshold, keeps the parallel versionmiddle:=len(s)/2varwgsync.WaitGroupwg.Add(2)gofunc(){deferwg.Done()parallelMergesortV2(s[:middle])}()gofunc(){deferwg.Done()parallelMergesortV2(s[middle:])}()wg.Wait()merge(s,middle)}}
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:
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’s also pretty interesting to note that running the same algorithm in a programming language that doesn’t 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.
Conclusion
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’t 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’t always faster and shouldn’t 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’t use concurrency? Of course not. However, it’s essential to keep these conclusions in mind. If we’re 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, “Not using Go diagnostics tooling”) and benchmarks (mistake #89, “Writing inaccurate benchmarks”), for example. It can be the only way to ensure that a concurrent implementation is worth it.
Comments
================================================
FILE: site/89-benchmarks/index.html
================================================
Writing inaccurate benchmarks (#89) - 100 Go Mistakes and How to Avoid Them
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’s rarely a bad idea to test them. However, writing benchmarks isn’t 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:
Not resetting or pausing the timer
Making wrong assumptions about micro-benchmarks
Not being careful about compiler optimizations
Being fooled by the observer effect
General concepts
Before discussing these traps, let’s briefly review how benchmarks work in Go. The skeleton of a benchmark is as follows:
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=.
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkFoo-4 73 16511228 ns/op
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
BenchmarkFoo-4 150 15832169 ns/op
foo was executed roughly twice more than during the previous benchmark.
Next, let’s look at some common traps.
Not resetting or pausing the timer
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:
In this case, we can use the ResetTimer method before entering the loop:
funcBenchmarkFoo(b*testing.B){expensiveSetup()b.ResetTimer()// Reset the benchmark timerfori:=0;i<b.N;i++{functionUnderTest()}}
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?
We can’t 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:
funcBenchmarkFoo(b*testing.B){fori:=0;i<b.N;i++{b.StopTimer()// Pause the benchmark timerexpensiveSetup()b.StartTimer()// Resume the benchmark timerfunctionUnderTest()}}
Here, we pause the benchmark timer to perform the expensive setup and then resume the timer.
Note
There’s 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.
Making wrong assumptions about micro-benchmarks
A micro-benchmark measures a tiny computation unit, and it can be extremely easy to make wrong assumptions about it. Let’s say, for example, that we aren’t 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:
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:
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’s 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
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkAtomicStoreInt32-4 234935682 5.124 ns/op
BenchmarkAtomicStoreInt32-4 235307204 5.112 ns/op
// ...
BenchmarkAtomicStoreInt64-4 235548591 5.107 ns/op
BenchmarkAtomicStoreInt64-4 235210292 5.090 ns/op
// ...
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: ± 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’s 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.
Not being careful about compiler optimizations
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):
A duration of 0.28 nanoseconds is roughly one clock cycle, so this number is unreasonably low. The problem is that the developer wasn’t 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:
The benchmark is now empty — 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:
During each loop iteration, assign the result to a local variable (local in the context of the benchmark function).
Assign the latest result to a global variable.
In our case, we write the following benchmark:
varglobaluint64// Define a global variablefuncBenchmarkPopcnt2(b*testing.B){varvuint64// Define a local variablefori:=0;i<b.N;i++{v=popcnt(uint64(i))// Assign the result to the local variable}global=v// Assign the result to the global variable}
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: “Not understanding stack vs. heap”). 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:
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’s 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.
Being fooled by the observer effect
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’s 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:
funccalculateSum512(s[][512]int64)int64{varsumint64fori:=0;i<len(s);i++{// Iterate over each rowforj:=0;j<8;j++{// Iterate over the first eight columnssum+=s[i][j]// Increment sum}}returnsum}funccalculateSum513(s[][513]int64)int64{// Same implementation as calculateSum512}
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:
constrows=1000varresint64funcBenchmarkCalculateSum512(b*testing.B){varsumint64s:=createMatrix512(rows)// Create a matrix of 512 columnsb.ResetTimer()fori:=0;i<b.N;i++{sum=calculateSum512(s)// Create a matrix of 512 columns}res=sum}funcBenchmarkCalculateSum513(b*testing.B){varsumint64s:=createMatrix513(rows)// Create a matrix of 513 columnsb.ResetTimer()fori:=0;i<b.N;i++{sum=calculateSum513(s)// Calculate the sum}res=sum}
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’t the case (on my machine):
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’s 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’t in the scope of this post to explain why, but we look at this problem in 100 Go Mistakes, mistake #91: “Not understanding CPU caches.”
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’t measure the function’s 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:
funcBenchmarkCalculateSum512(b*testing.B){varsumint64fori:=0;i<b.N;i++{b.StopTimer()s:=createMatrix512(rows)// Create a new matrix during each loop iterationb.StartTimer()sum=calculateSum512(s)}res=sum}
A new matrix is now created during each loop iteration. If we run the benchmark again (and adjust benchtime — otherwise, it takes too long to execute), the results are closer to each other:
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.
Comments
================================================
FILE: site/9-generics/index.html
================================================
Being confused about when to use generics (#9) - 100 Go Mistakes and How to Avoid Them
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.
Concepts
Consider the following function that extracts all the keys from a map[string]int type:
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:
funcgetKeys(many)([]any,error){switcht:=m.(type){default:returnnil,fmt.Errorf("unknown type: %T",t)casemap[string]int:varkeys[]anyfork:=ranget{keys=append(keys,k)}returnkeys,nilcasemap[int]string:// Copy the extraction logic}}
We can start noticing a couple of issues:
First, it increases boilerplate code. Indeed, whenever we want to add a case, it will require duplicating the range loop.
Meanwhile, the function now accepts an empty interface, which means we are losing some of the benefits of Go being a typed language. Indeed, checking whether a type is supported is done at runtime instead of compile-time. Hence, we also need to return an error if the provided type is unknown.
Last but not least, as the key type can be either int or string, we are obliged to return a slice of empty interfaces to factor out key types. This approach increases the effort on the caller-side as the client may also have to perform a type check of the keys or extra conversion.
Thanks to generics, we can now refactor this code using type parameters.
Type parameters are generic types we can use with functions and types. For example, the following function accepts a type parameter:
funcfoo[Tany](tT){// ...}
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’s get back to the getKeys function and use type parameters to write a generic version that would accept any kind of map:
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’t be of any type. For example, we cannot use slices:
varmmap[[]byte]int
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:
A set of behaviors (methods)
But also arbitrary type
Let’s see a concrete example for the latter. Imagine we don’t 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:
typecustomConstraintinterface{~int|~string// Define a custom type that will restrict types to int and string}// Change the type parameter K to be customfuncgetKeys[KcustomConstraint,Vany](mmap[K]V)[]K{// Same implementation}
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:
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)
Note
What’s 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’s imagine a constraint where we would like to restrict a type to any int type implementing the String() string method:
typecustomConstraintinterface{~intString()string}
Using this constraint will restrict type arguments to custom types like this one:
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’t implement String() string.
Let’s also note the constraints package contains a set of common constraints such as Signed that includes all the signed integer types. Let’s ensure that a constraint doesn’t 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:
typeNode[Tany]struct{// Use type parameterValTnext*Node[T]}func(n*Node[T])Add(next*Node[T]){// Instantiate type receivern.next=next}
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’t be used on methods, only on functions. For example, the following method wouldn’t compile:
typeFoostruct{}func(Foo)bar[Tany](tT){}
./main.go:29:15: methods cannot have type parameters
Now, let’s delve into concrete cases where we should and shouldn’t use generics.
Common uses and misuses
So when are generics useful? Let’s discuss a couple of common uses where generics are recommended:
Data structures. For example, we can use generics to factor out the element type if we implement a binary tree, a linked list, or a heap.
Functions working with slices, maps, and channels of any type. For example, a function to merge two channels would work with any channel type. Hence, we could use type parameters to factor out the channel type:
funcmerge[Tany](ch1,ch2<-chanT)<-chanT{// ...}
Meanwhile, instead of factoring out a type, we can factor out behaviors. For example, the sort package contains functions to sort different slice types such as sort.Ints or sort.Float64s. Using type parameters, we can factor out the sorting behaviors that rely on three methods, Len, Less, and Swap:
typesliceFn[Tany]struct{// Use type parameters[]Tcomparefunc(T,T)bool// Compare two T elements}func(ssliceFn[T])Len()int{returnlen(s.s)}func(ssliceFn[T])Less(i,jint)bool{returns.compare(s.s[i],s.s[j])}func(ssliceFn[T])Swap(i,jint){s.s[i],s.s[j]=s.s[j],s.s[i]}
Conversely, when is it recommended not to use generics?
When just calling a method of the type argument. For example, consider a function that receives an io.Writer and call the Write method:
When it makes our code more complex. Generics are never mandatory, and as Go developers, we have been able to live without them for more than a decade. If writing generic functions or structures we figure out that it doesn’t make our code clearer, we should probably reconsider our decision for this particular use case.
Conclusion
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’s not pollute our code with needless abstractions, and let’s focus on solving concrete problems for now. It means that we shouldn’t use type parameters prematurely. Let’s wait until we are about to write boilerplate code to consider using generics.
Comments
================================================
FILE: site/92-false-sharing/index.html
================================================
Writing concurrent code that leads to false sharing (#92) - 100 Go Mistakes and How to Avoid Them
Writing concurrent code that leads to false sharing
In previous sections, we have discussed the fundamental concepts of CPU caching. We have seen that some specific caches (typically, L1 and L2) aren’t 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’s 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:
The goal is to implement a count function that receives a slice of Input and computes the following:
The sum of all the Input.a fields into Result.sumA
The sum of all the Input.b fields into Result.sumB
For the sake of the example, we implement a concurrent solution with one goroutine that computes sumA and another that computes sumB:
funccount(inputs[]Input)Result{wg:=sync.WaitGroup{}wg.Add(2)result:=Result{}// Init the result structgofunc(){fori:=0;i<len(inputs);i++{result.sumA+=inputs[i].a// Computes sumA}wg.Done()}()gofunc(){fori:=0;i<len(inputs);i++{result.sumB+=inputs[i].b// Computes sumB}wg.Done()}()wg.Wait()returnresult}
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’t 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’s 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’s 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’t 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’t a conflict, but this isn’t the case. When we write a variable that’s in a cache, the granularity tracked by the CPU isn’t the variable: it’s 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’t real—it’s an illusion. This understanding comes from the fact that we don’t 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’ve shown but ensure that sumA and sumB aren’t 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 – 8 = 56 bytes of padding:
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.
Comments
================================================
FILE: site/98-profiling-execution-tracing/index.html
================================================
Not using Go diagnostics tooling (#98) - 100 Go Mistakes and How to Avoid Them
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’s discuss profiling.
Profiling
Profiling provides insights into the execution of an application. It allows us to resolve performance issues, detect contention, locate memory leaks, and more. These insights can be collected via several profiles:
CPU— Determines where an application spends its time
Goroutine— Reports the stack traces of the ongoing goroutines
Heap— Reports heap memory allocation to monitor current memory usage and check for possible memory leaks
Mutex— Reports lock contentions to see the behaviors of the mutexes used in our code and whether an application spends too much time in locking calls
Block— Shows where goroutines block waiting on synchronization primitives
Profiling is achieved via instrumentation using a tool called a profiler, in Go: pprof. First, let’s understand how and when to enable pprof; then, we discuss the most critical profile types.
Enabling pprof
There are several ways to enable pprof. For example, we can use the net/http/pprof package to serve the profiling data via HTTP:
packagemainimport("fmt""log""net/http"_"net/http/pprof"// Blank import to pprof)funcmain(){// Exposes an HTTP endpointhttp.HandleFunc("/",func(whttp.ResponseWriter,r*http.Request){fmt.Fprintf(w,"")})log.Fatal(http.ListenAndServe(":80",nil))}
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’t 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’s discuss the most common profiles.
CPU Profiling
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
From this file, we can navigate to the results using go tool:
$ go tool pprof -http=:8080 <file>
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:
Too many calls to runtime.mallogc can mean an excessive number of small heap allocations that we can try to minimize.
Too much time spent in channel operations or mutex locks can indicate excessive contention that is harming the application’s performance.
Too much time spent on syscall.Read or syscall.Write means the application spends a significant amount of time in Kernel mode. Working on I/O buffering may be an avenue for improvement.
These are the kinds of insights we can get from the CPU profiler. It’s valuable to understand the hottest code path and identify bottlenecks. But it won’t 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.
Heap Profiling
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’t 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 — 1024 KB — were allocated in another method.
Figure 3: A heap graph.
This is how we can navigate the call chain to understand what part of an application is responsible for most of the heap allocations. We can also look at different sample types:
alloc_objects— Total number of objects allocated
alloc_space— Total amount of memory allocated
inuse_objects — Number of objects allocated and not yet released
inuse_space— Amount of memory allocated and not yet released
Another very helpful capability with heap profiling is tracking memory leaks. With a GC-based language, the usual procedure is the following:
Trigger a GC.
Download heap data.
Wait for a few seconds/minutes.
Trigger another GC.
Download another heap data.
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’s a leak or objects that the next GC will collect.
Using pprof, we can download a heap profile and force a GC in the meantime. The procedure in Go is the following:
Go to /debug/pprof/heap?gc=1 (trigger the GC and download the heap profile).
Wait for a few seconds/minutes.
Go to /debug/pprof/heap?gc=1 again.
Use go tool to compare both heap profiles:
$ go tool pprof -http=:8080 -diff_base <file2> <file1>
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 (–513 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’t part of this profiling, which only focuses on the heap.
Goroutine Profiling
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.
Block Profiling
The block profile reports where ongoing goroutines block waiting on synchronization primitives. Possibilities include
Sending or receiving on an unbuffered channel
Sending to a full channel
Receiving from an empty channel
Mutex contention
Network or filesystem waits
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’t 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’t call the /debug/pprof/block endpoint. Let’s be cautious if we want to set a high rate so we don’t 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:
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’s accessible via /debug/pprof/mutex.
This profile works in a manner similar to that for blocking. It’s 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:
Be sure to enable only one profiler at a time: for example, do not enable CPU and heap profiling simultaneously. Doing so can lead to erroneous observations.
pprof is extensible, and we can create our own custom profiles using pprof.Profile.
We have seen the most important profiles that we can enable to help us understand how an application performs and possible avenues for optimization. In general, enabling pprof is recommended, even in production, because in most cases it offers an excellent balance between its footprint and the amount of insight we can get from it. Some profiles, such as the CPU profile, lead to performance penalties but only during the time they are enabled.
Let’s now look at the execution tracer.
Execution Tracer
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:
Understanding runtime events such as how the GC performs
Understanding how goroutines execute
Identifying poorly parallelized execution
Let’s try it with an example given the Concurrency isn’t 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’s 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
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
2021/11/26 21:36:03 Parsing trace...
2021/11/26 21:36:31 Splitting trace...
2021/11/26 21:37:00 Opening browser. Trace viewer is listening on
http://127.0.0.1:54518
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 “stop the
world” 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’t 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’t 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’s 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’s 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:
varvint64// Creates a fibonacci taskctx,fibTask:=trace.NewTask(context.Background(),"fibonacci")trace.WithRegion(ctx,"main",func(){v=fibonacci(10)})fibTask.End()// Creates a store taskctx,fibStore:=trace.NewTask(ctx,"store")trace.WithRegion(ctx,"main",func(){atomic.StoreInt64(&result,v)})fibStore.End()
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?
CPU profiling:
Sample-based
Per function
Doesn’t go below the sampling rate (10 ms by default)
User-level traces:
Not sample-based
Per-goroutine execution (unless we use the runtime/trace package)
Time executions aren’t bound by any rate
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’s 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.
Comments
================================================
FILE: site/CNAME
================================================
100go.co
================================================
FILE: site/assets/javascripts/lunr/tinyseg.js
================================================
/**
* export the module via AMD, CommonJS or as a browser global
* Export code from https://github.com/umdjs/umd/blob/master/returnExports.js
*/
;(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory)
} else if (typeof exports === 'object') {
/**
* Node. Does not work with strict CommonJS, but
* only CommonJS-like environments that support module.exports,
* like Node.
*/
module.exports = factory()
} else {
// Browser globals (root is window)
factory()(root.lunr);
}
}(this, function () {
/**
* Just return a value to define the module export.
* This example returns an object, but the module
* can return a function as the exported value.
*/
return function(lunr) {
// TinySegmenter 0.1 -- Super compact Japanese tokenizer in Javascript
// (c) 2008 Taku Kudo
// TinySegmenter is freely distributable under the terms of a new BSD licence.
// For details, see http://chasen.org/~taku/software/TinySegmenter/LICENCE.txt
function TinySegmenter() {
var patterns = {
"[一二三四五六七八九十百千万億兆]":"M",
"[一-龠々〆ヵヶ]":"H",
"[ぁ-ん]":"I",
"[ァ-ヴーア-ン゙ー]":"K",
"[a-zA-Za-zA-Z]":"A",
"[0-90-9]":"N"
}
this.chartype_ = [];
for (var i in patterns) {
var regexp = new RegExp(i);
this.chartype_.push([regexp, patterns[i]]);
}
this.BIAS__ = -332
this.BC1__ = {"HH":6,"II":2461,"KH":406,"OH":-1378};
this.BC2__ = {"AA":-3267,"AI":2744,"AN":-878,"HH":-4070,"HM":-1711,"HN":4012,"HO":3761,"IA":1327,"IH":-1184,"II":-1332,"IK":1721,"IO":5492,"KI":3831,"KK":-8741,"MH":-3132,"MK":3334,"OO":-2920};
this.BC3__ = {"HH":996,"HI":626,"HK":-721,"HN":-1307,"HO":-836,"IH":-301,"KK":2762,"MK":1079,"MM":4034,"OA":-1652,"OH":266};
this.BP1__ = {"BB":295,"OB":304,"OO":-125,"UB":352};
this.BP2__ = {"BO":60,"OO":-1762};
this.BQ1__ = {"BHH":1150,"BHM":1521,"BII":-1158,"BIM":886,"BMH":1208,"BNH":449,"BOH":-91,"BOO":-2597,"OHI":451,"OIH":-296,"OKA":1851,"OKH":-1020,"OKK":904,"OOO":2965};
this.BQ2__ = {"BHH":118,"BHI":-1159,"BHM":466,"BIH":-919,"BKK":-1720,"BKO":864,"OHH":-1139,"OHM":-181,"OIH":153,"UHI":-1146};
this.BQ3__ = {"BHH":-792,"BHI":2664,"BII":-299,"BKI":419,"BMH":937,"BMM":8335,"BNN":998,"BOH":775,"OHH":2174,"OHM":439,"OII":280,"OKH":1798,"OKI":-793,"OKO":-2242,"OMH":-2402,"OOO":11699};
this.BQ4__ = {"BHH":-3895,"BIH":3761,"BII":-4654,"BIK":1348,"BKK":-1806,"BMI":-3385,"BOO":-12396,"OAH":926,"OHH":266,"OHK":-2036,"ONN":-973};
this.BW1__ = {",と":660,",同":727,"B1あ":1404,"B1同":542,"、と":660,"、同":727,"」と":1682,"あっ":1505,"いう":1743,"いっ":-2055,"いる":672,"うし":-4817,"うん":665,"から":3472,"がら":600,"こう":-790,"こと":2083,"こん":-1262,"さら":-4143,"さん":4573,"した":2641,"して":1104,"すで":-3399,"そこ":1977,"それ":-871,"たち":1122,"ため":601,"った":3463,"つい":-802,"てい":805,"てき":1249,"でき":1127,"です":3445,"では":844,"とい":-4915,"とみ":1922,"どこ":3887,"ない":5713,"なっ":3015,"など":7379,"なん":-1113,"にし":2468,"には":1498,"にも":1671,"に対":-912,"の一":-501,"の中":741,"ませ":2448,"まで":1711,"まま":2600,"まる":-2155,"やむ":-1947,"よっ":-2565,"れた":2369,"れで":-913,"をし":1860,"を見":731,"亡く":-1886,"京都":2558,"取り":-2784,"大き":-2604,"大阪":1497,"平方":-2314,"引き":-1336,"日本":-195,"本当":-2423,"毎日":-2113,"目指":-724,"B1あ":1404,"B1同":542,"」と":1682};
this.BW2__ = {"..":-11822,"11":-669,"――":-5730,"−−":-13175,"いう":-1609,"うか":2490,"かし":-1350,"かも":-602,"から":-7194,"かれ":4612,"がい":853,"がら":-3198,"きた":1941,"くな":-1597,"こと":-8392,"この":-4193,"させ":4533,"され":13168,"さん":-3977,"しい":-1819,"しか":-545,"した":5078,"して":972,"しな":939,"その":-3744,"たい":-1253,"たた":-662,"ただ":-3857,"たち":-786,"たと":1224,"たは":-939,"った":4589,"って":1647,"っと":-2094,"てい":6144,"てき":3640,"てく":2551,"ては":-3110,"ても":-3065,"でい":2666,"でき":-1528,"でし":-3828,"です":-4761,"でも":-4203,"とい":1890,"とこ":-1746,"とと":-2279,"との":720,"とみ":5168,"とも":-3941,"ない":-2488,"なが":-1313,"など":-6509,"なの":2614,"なん":3099,"にお":-1615,"にし":2748,"にな":2454,"によ":-7236,"に対":-14943,"に従":-4688,"に関":-11388,"のか":2093,"ので":-7059,"のに":-6041,"のの":-6125,"はい":1073,"はが":-1033,"はず":-2532,"ばれ":1813,"まし":-1316,"まで":-6621,"まれ":5409,"めて":-3153,"もい":2230,"もの":-10713,"らか":-944,"らし":-1611,"らに":-1897,"りし":651,"りま":1620,"れた":4270,"れて":849,"れば":4114,"ろう":6067,"われ":7901,"を通":-11877,"んだ":728,"んな":-4115,"一人":602,"一方":-1375,"一日":970,"一部":-1051,"上が":-4479,"会社":-1116,"出て":2163,"分の":-7758,"同党":970,"同日":-913,"大阪":-2471,"委員":-1250,"少な":-1050,"年度":-8669,"年間":-1626,"府県":-2363,"手権":-1982,"新聞":-4066,"日新":-722,"日本":-7068,"日米":3372,"曜日":-601,"朝鮮":-2355,"本人":-2697,"東京":-1543,"然と":-1384,"社会":-1276,"立て":-990,"第に":-1612,"米国":-4268,"11":-669};
this.BW3__ = {"あた":-2194,"あり":719,"ある":3846,"い.":-1185,"い。":-1185,"いい":5308,"いえ":2079,"いく":3029,"いた":2056,"いっ":1883,"いる":5600,"いわ":1527,"うち":1117,"うと":4798,"えと":1454,"か.":2857,"か。":2857,"かけ":-743,"かっ":-4098,"かに":-669,"から":6520,"かり":-2670,"が,":1816,"が、":1816,"がき":-4855,"がけ":-1127,"がっ":-913,"がら":-4977,"がり":-2064,"きた":1645,"けど":1374,"こと":7397,"この":1542,"ころ":-2757,"さい":-714,"さを":976,"し,":1557,"し、":1557,"しい":-3714,"した":3562,"して":1449,"しな":2608,"しま":1200,"す.":-1310,"す。":-1310,"する":6521,"ず,":3426,"ず、":3426,"ずに":841,"そう":428,"た.":8875,"た。":8875,"たい":-594,"たの":812,"たり":-1183,"たる":-853,"だ.":4098,"だ。":4098,"だっ":1004,"った":-4748,"って":300,"てい":6240,"てお":855,"ても":302,"です":1437,"でに":-1482,"では":2295,"とう":-1387,"とし":2266,"との":541,"とも":-3543,"どう":4664,"ない":1796,"なく":-903,"など":2135,"に,":-1021,"に、":-1021,"にし":1771,"にな":1906,"には":2644,"の,":-724,"の、":-724,"の子":-1000,"は,":1337,"は、":1337,"べき":2181,"まし":1113,"ます":6943,"まっ":-1549,"まで":6154,"まれ":-793,"らし":1479,"られ":6820,"るる":3818,"れ,":854,"れ、":854,"れた":1850,"れて":1375,"れば":-3246,"れる":1091,"われ":-605,"んだ":606,"んで":798,"カ月":990,"会議":860,"入り":1232,"大会":2217,"始め":1681,"市":965,"新聞":-5055,"日,":974,"日、":974,"社会":2024,"カ月":990};
this.TC1__ = {"AAA":1093,"HHH":1029,"HHM":580,"HII":998,"HOH":-390,"HOM":-331,"IHI":1169,"IOH":-142,"IOI":-1015,"IOM":467,"MMH":187,"OOI":-1832};
this.TC2__ = {"HHO":2088,"HII":-1023,"HMM":-1154,"IHI":-1965,"KKH":703,"OII":-2649};
this.TC3__ = {"AAA":-294,"HHH":346,"HHI":-341,"HII":-1088,"HIK":731,"HOH":-1486,"IHH":128,"IHI":-3041,"IHO":-1935,"IIH":-825,"IIM":-1035,"IOI":-542,"KHH":-1216,"KKA":491,"KKH":-1217,"KOK":-1009,"MHH":-2694,"MHM":-457,"MHO":123,"MMH":-471,"NNH":-1689,"NNO":662,"OHO":-3393};
this.TC4__ = {"HHH":-203,"HHI":1344,"HHK":365,"HHM":-122,"HHN":182,"HHO":669,"HIH":804,"HII":679,"HOH":446,"IHH":695,"IHO":-2324,"IIH":321,"III":1497,"IIO":656,"IOO":54,"KAK":4845,"KKA":3386,"KKK":3065,"MHH":-405,"MHI":201,"MMH":-241,"MMM":661,"MOM":841};
this.TQ1__ = {"BHHH":-227,"BHHI":316,"BHIH":-132,"BIHH":60,"BIII":1595,"BNHH":-744,"BOHH":225,"BOOO":-908,"OAKK":482,"OHHH":281,"OHIH":249,"OIHI":200,"OIIH":-68};
this.TQ2__ = {"BIHH":-1401,"BIII":-1033,"BKAK":-543,"BOOO":-5591};
this.TQ3__ = {"BHHH":478,"BHHM":-1073,"BHIH":222,"BHII":-504,"BIIH":-116,"BIII":-105,"BMHI":-863,"BMHM":-464,"BOMH":620,"OHHH":346,"OHHI":1729,"OHII":997,"OHMH":481,"OIHH":623,"OIIH":1344,"OKAK":2792,"OKHH":587,"OKKA":679,"OOHH":110,"OOII":-685};
this.TQ4__ = {"BHHH":-721,"BHHM":-3604,"BHII":-966,"BIIH":-607,"BIII":-2181,"OAAA":-2763,"OAKK":180,"OHHH":-294,"OHHI":2446,"OHHO":480,"OHIH":-1573,"OIHH":1935,"OIHI":-493,"OIIH":626,"OIII":-4007,"OKAK":-8156};
this.TW1__ = {"につい":-4681,"東京都":2026};
this.TW2__ = {"ある程":-2049,"いった":-1256,"ころが":-2434,"しょう":3873,"その後":-4430,"だって":-1049,"ていた":1833,"として":-4657,"ともに":-4517,"もので":1882,"一気に":-792,"初めて":-1512,"同時に":-8097,"大きな":-1255,"対して":-2721,"社会党":-3216};
this.TW3__ = {"いただ":-1734,"してい":1314,"として":-4314,"につい":-5483,"にとっ":-5989,"に当た":-6247,"ので,":-727,"ので、":-727,"のもの":-600,"れから":-3752,"十二月":-2287};
this.TW4__ = {"いう.":8576,"いう。":8576,"からな":-2348,"してい":2958,"たが,":1516,"たが、":1516,"ている":1538,"という":1349,"ました":5543,"ません":1097,"ようと":-4258,"よると":5865};
this.UC1__ = {"A":484,"K":93,"M":645,"O":-505};
this.UC2__ = {"A":819,"H":1059,"I":409,"M":3987,"N":5775,"O":646};
this.UC3__ = {"A":-1370,"I":2311};
this.UC4__ = {"A":-2643,"H":1809,"I":-1032,"K":-3450,"M":3565,"N":3876,"O":6646};
this.UC5__ = {"H":313,"I":-1238,"K":-799,"M":539,"O":-831};
this.UC6__ = {"H":-506,"I":-253,"K":87,"M":247,"O":-387};
this.UP1__ = {"O":-214};
this.UP2__ = {"B":69,"O":935};
this.UP3__ = {"B":189};
this.UQ1__ = {"BH":21,"BI":-12,"BK":-99,"BN":142,"BO":-56,"OH":-95,"OI":477,"OK":410,"OO":-2422};
this.UQ2__ = {"BH":216,"BI":113,"OK":1759};
this.UQ3__ = {"BA":-479,"BH":42,"BI":1913,"BK":-7198,"BM":3160,"BN":6427,"BO":14761,"OI":-827,"ON":-3212};
this.UW1__ = {",":156,"、":156,"「":-463,"あ":-941,"う":-127,"が":-553,"き":121,"こ":505,"で":-201,"と":-547,"ど":-123,"に":-789,"の":-185,"は":-847,"も":-466,"や":-470,"よ":182,"ら":-292,"り":208,"れ":169,"を":-446,"ん":-137,"・":-135,"主":-402,"京":-268,"区":-912,"午":871,"国":-460,"大":561,"委":729,"市":-411,"日":-141,"理":361,"生":-408,"県":-386,"都":-718,"「":-463,"・":-135};
this.UW2__ = {",":-829,"、":-829,"〇":892,"「":-645,"」":3145,"あ":-538,"い":505,"う":134,"お":-502,"か":1454,"が":-856,"く":-412,"こ":1141,"さ":878,"ざ":540,"し":1529,"す":-675,"せ":300,"そ":-1011,"た":188,"だ":1837,"つ":-949,"て":-291,"で":-268,"と":-981,"ど":1273,"な":1063,"に":-1764,"の":130,"は":-409,"ひ":-1273,"べ":1261,"ま":600,"も":-1263,"や":-402,"よ":1639,"り":-579,"る":-694,"れ":571,"を":-2516,"ん":2095,"ア":-587,"カ":306,"キ":568,"ッ":831,"三":-758,"不":-2150,"世":-302,"中":-968,"主":-861,"事":492,"人":-123,"会":978,"保":362,"入":548,"初":-3025,"副":-1566,"北":-3414,"区":-422,"大":-1769,"天":-865,"太":-483,"子":-1519,"学":760,"実":1023,"小":-2009,"市":-813,"年":-1060,"強":1067,"手":-1519,"揺":-1033,"政":1522,"文":-1355,"新":-1682,"日":-1815,"明":-1462,"最":-630,"朝":-1843,"本":-1650,"東":-931,"果":-665,"次":-2378,"民":-180,"気":-1740,"理":752,"発":529,"目":-1584,"相":-242,"県":-1165,"立":-763,"第":810,"米":509,"自":-1353,"行":838,"西":-744,"見":-3874,"調":1010,"議":1198,"込":3041,"開":1758,"間":-1257,"「":-645,"」":3145,"ッ":831,"ア":-587,"カ":306,"キ":568};
this.UW3__ = {",":4889,"1":-800,"−":-1723,"、":4889,"々":-2311,"〇":5827,"」":2670,"〓":-3573,"あ":-2696,"い":1006,"う":2342,"え":1983,"お":-4864,"か":-1163,"が":3271,"く":1004,"け":388,"げ":401,"こ":-3552,"ご":-3116,"さ":-1058,"し":-395,"す":584,"せ":3685,"そ":-5228,"た":842,"ち":-521,"っ":-1444,"つ":-1081,"て":6167,"で":2318,"と":1691,"ど":-899,"な":-2788,"に":2745,"の":4056,"は":4555,"ひ":-2171,"ふ":-1798,"へ":1199,"ほ":-5516,"ま":-4384,"み":-120,"め":1205,"も":2323,"や":-788,"よ":-202,"ら":727,"り":649,"る":5905,"れ":2773,"わ":-1207,"を":6620,"ん":-518,"ア":551,"グ":1319,"ス":874,"ッ":-1350,"ト":521,"ム":1109,"ル":1591,"ロ":2201,"ン":278,"・":-3794,"一":-1619,"下":-1759,"世":-2087,"両":3815,"中":653,"主":-758,"予":-1193,"二":974,"人":2742,"今":792,"他":1889,"以":-1368,"低":811,"何":4265,"作":-361,"保":-2439,"元":4858,"党":3593,"全":1574,"公":-3030,"六":755,"共":-1880,"円":5807,"再":3095,"分":457,"初":2475,"別":1129,"前":2286,"副":4437,"力":365,"動":-949,"務":-1872,"化":1327,"北":-1038,"区":4646,"千":-2309,"午":-783,"協":-1006,"口":483,"右":1233,"各":3588,"合":-241,"同":3906,"和":-837,"員":4513,"国":642,"型":1389,"場":1219,"外":-241,"妻":2016,"学":-1356,"安":-423,"実":-1008,"家":1078,"小":-513,"少":-3102,"州":1155,"市":3197,"平":-1804,"年":2416,"広":-1030,"府":1605,"度":1452,"建":-2352,"当":-3885,"得":1905,"思":-1291,"性":1822,"戸":-488,"指":-3973,"政":-2013,"教":-1479,"数":3222,"文":-1489,"新":1764,"日":2099,"旧":5792,"昨":-661,"時":-1248,"曜":-951,"最":-937,"月":4125,"期":360,"李":3094,"村":364,"東":-805,"核":5156,"森":2438,"業":484,"氏":2613,"民":-1694,"決":-1073,"法":1868,"海":-495,"無":979,"物":461,"特":-3850,"生":-273,"用":914,"町":1215,"的":7313,"直":-1835,"省":792,"県":6293,"知":-1528,"私":4231,"税":401,"立":-960,"第":1201,"米":7767,"系":3066,"約":3663,"級":1384,"統":-4229,"総":1163,"線":1255,"者":6457,"能":725,"自":-2869,"英":785,"見":1044,"調":-562,"財":-733,"費":1777,"車":1835,"軍":1375,"込":-1504,"通":-1136,"選":-681,"郎":1026,"郡":4404,"部":1200,"金":2163,"長":421,"開":-1432,"間":1302,"関":-1282,"雨":2009,"電":-1045,"非":2066,"駅":1620,"1":-800,"」":2670,"・":-3794,"ッ":-1350,"ア":551,"グ":1319,"ス":874,"ト":521,"ム":1109,"ル":1591,"ロ":2201,"ン":278};
this.UW4__ = {",":3930,".":3508,"―":-4841,"、":3930,"。":3508,"〇":4999,"「":1895,"」":3798,"〓":-5156,"あ":4752,"い":-3435,"う":-640,"え":-2514,"お":2405,"か":530,"が":6006,"き":-4482,"ぎ":-3821,"く":-3788,"け":-4376,"げ":-4734,"こ":2255,"ご":1979,"さ":2864,"し":-843,"じ":-2506,"す":-731,"ず":1251,"せ":181,"そ":4091,"た":5034,"だ":5408,"ち":-3654,"っ":-5882,"つ":-1659,"て":3994,"で":7410,"と":4547,"な":5433,"に":6499,"ぬ":1853,"ね":1413,"の":7396,"は":8578,"ば":1940,"ひ":4249,"び":-4134,"ふ":1345,"へ":6665,"べ":-744,"ほ":1464,"ま":1051,"み":-2082,"む":-882,"め":-5046,"も":4169,"ゃ":-2666,"や":2795,"ょ":-1544,"よ":3351,"ら":-2922,"り":-9726,"る":-14896,"れ":-2613,"ろ":-4570,"わ":-1783,"を":13150,"ん":-2352,"カ":2145,"コ":1789,"セ":1287,"ッ":-724,"ト":-403,"メ":-1635,"ラ":-881,"リ":-541,"ル":-856,"ン":-3637,"・":-4371,"ー":-11870,"一":-2069,"中":2210,"予":782,"事":-190,"井":-1768,"人":1036,"以":544,"会":950,"体":-1286,"作":530,"側":4292,"先":601,"党":-2006,"共":-1212,"内":584,"円":788,"初":1347,"前":1623,"副":3879,"力":-302,"動":-740,"務":-2715,"化":776,"区":4517,"協":1013,"参":1555,"合":-1834,"和":-681,"員":-910,"器":-851,"回":1500,"国":-619,"園":-1200,"地":866,"場":-1410,"塁":-2094,"士":-1413,"多":1067,"大":571,"子":-4802,"学":-1397,"定":-1057,"寺":-809,"小":1910,"屋":-1328,"山":-1500,"島":-2056,"川":-2667,"市":2771,"年":374,"庁":-4556,"後":456,"性":553,"感":916,"所":-1566,"支":856,"改":787,"政":2182,"教":704,"文":522,"方":-856,"日":1798,"時":1829,"最":845,"月":-9066,"木":-485,"来":-442,"校":-360,"業":-1043,"氏":5388,"民":-2716,"気":-910,"沢":-939,"済":-543,"物":-735,"率":672,"球":-1267,"生":-1286,"産":-1101,"田":-2900,"町":1826,"的":2586,"目":922,"省":-3485,"県":2997,"空":-867,"立":-2112,"第":788,"米":2937,"系":786,"約":2171,"経":1146,"統":-1169,"総":940,"線":-994,"署":749,"者":2145,"能":-730,"般":-852,"行":-792,"規":792,"警":-1184,"議":-244,"谷":-1000,"賞":730,"車":-1481,"軍":1158,"輪":-1433,"込":-3370,"近":929,"道":-1291,"選":2596,"郎":-4866,"都":1192,"野":-1100,"銀":-2213,"長":357,"間":-2344,"院":-2297,"際":-2604,"電":-878,"領":-1659,"題":-792,"館":-1984,"首":1749,"高":2120,"「":1895,"」":3798,"・":-4371,"ッ":-724,"ー":-11870,"カ":2145,"コ":1789,"セ":1287,"ト":-403,"メ":-1635,"ラ":-881,"リ":-541,"ル":-856,"ン":-3637};
this.UW5__ = {",":465,".":-299,"1":-514,"E2":-32768,"]":-2762,"、":465,"。":-299,"「":363,"あ":1655,"い":331,"う":-503,"え":1199,"お":527,"か":647,"が":-421,"き":1624,"ぎ":1971,"く":312,"げ":-983,"さ":-1537,"し":-1371,"す":-852,"だ":-1186,"ち":1093,"っ":52,"つ":921,"て":-18,"で":-850,"と":-127,"ど":1682,"な":-787,"に":-1224,"の":-635,"は":-578,"べ":1001,"み":502,"め":865,"ゃ":3350,"ょ":854,"り":-208,"る":429,"れ":504,"わ":419,"を":-1264,"ん":327,"イ":241,"ル":451,"ン":-343,"中":-871,"京":722,"会":-1153,"党":-654,"務":3519,"区":-901,"告":848,"員":2104,"大":-1296,"学":-548,"定":1785,"嵐":-1304,"市":-2991,"席":921,"年":1763,"思":872,"所":-814,"挙":1618,"新":-1682,"日":218,"月":-4353,"査":932,"格":1356,"機":-1508,"氏":-1347,"田":240,"町":-3912,"的":-3149,"相":1319,"省":-1052,"県":-4003,"研":-997,"社":-278,"空":-813,"統":1955,"者":-2233,"表":663,"語":-1073,"議":1219,"選":-1018,"郎":-368,"長":786,"間":1191,"題":2368,"館":-689,"1":-514,"E2":-32768,"「":363,"イ":241,"ル":451,"ン":-343};
this.UW6__ = {",":227,".":808,"1":-270,"E1":306,"、":227,"。":808,"あ":-307,"う":189,"か":241,"が":-73,"く":-121,"こ":-200,"じ":1782,"す":383,"た":-428,"っ":573,"て":-1014,"で":101,"と":-105,"な":-253,"に":-149,"の":-417,"は":-236,"も":-206,"り":187,"る":-135,"を":195,"ル":-673,"ン":-496,"一":-277,"中":201,"件":-800,"会":624,"前":302,"区":1792,"員":-1212,"委":798,"学":-960,"市":887,"広":-695,"後":535,"業":-697,"相":753,"社":-507,"福":974,"空":-822,"者":1811,"連":463,"郎":1082,"1":-270,"E1":306,"ル":-673,"ン":-496};
return this;
}
TinySegmenter.prototype.ctype_ = function(str) {
for (var i in this.chartype_) {
if (str.match(this.chartype_[i][0])) {
return this.chartype_[i][1];
}
}
return "O";
}
TinySegmenter.prototype.ts_ = function(v) {
if (v) { return v; }
return 0;
}
TinySegmenter.prototype.segment = function(input) {
if (input == null || input == undefined || input == "") {
return [];
}
var result = [];
var seg = ["B3","B2","B1"];
var ctype = ["O","O","O"];
var o = input.split("");
for (i = 0; i < o.length; ++i) {
seg.push(o[i]);
ctype.push(this.ctype_(o[i]))
}
seg.push("E1");
seg.push("E2");
seg.push("E3");
ctype.push("O");
ctype.push("O");
ctype.push("O");
var word = seg[3];
var p1 = "U";
var p2 = "U";
var p3 = "U";
for (var i = 4; i < seg.length - 3; ++i) {
var score = this.BIAS__;
var w1 = seg[i-3];
var w2 = seg[i-2];
var w3 = seg[i-1];
var w4 = seg[i];
var w5 = seg[i+1];
var w6 = seg[i+2];
var c1 = ctype[i-3];
var c2 = ctype[i-2];
var c3 = ctype[i-1];
var c4 = ctype[i];
var c5 = ctype[i+1];
var c6 = ctype[i+2];
score += this.ts_(this.UP1__[p1]);
score += this.ts_(this.UP2__[p2]);
score += this.ts_(this.UP3__[p3]);
score += this.ts_(this.BP1__[p1 + p2]);
score += this.ts_(this.BP2__[p2 + p3]);
score += this.ts_(this.UW1__[w1]);
score += this.ts_(this.UW2__[w2]);
score += this.ts_(this.UW3__[w3]);
score += this.ts_(this.UW4__[w4]);
score += this.ts_(this.UW5__[w5]);
score += this.ts_(this.UW6__[w6]);
score += this.ts_(this.BW1__[w2 + w3]);
score += this.ts_(this.BW2__[w3 + w4]);
score += this.ts_(this.BW3__[w4 + w5]);
score += this.ts_(this.TW1__[w1 + w2 + w3]);
score += this.ts_(this.TW2__[w2 + w3 + w4]);
score += this.ts_(this.TW3__[w3 + w4 + w5]);
score += this.ts_(this.TW4__[w4 + w5 + w6]);
score += this.ts_(this.UC1__[c1]);
score += this.ts_(this.UC2__[c2]);
score += this.ts_(this.UC3__[c3]);
score += this.ts_(this.UC4__[c4]);
score += this.ts_(this.UC5__[c5]);
score += this.ts_(this.UC6__[c6]);
score += this.ts_(this.BC1__[c2 + c3]);
score += this.ts_(this.BC2__[c3 + c4]);
score += this.ts_(this.BC3__[c4 + c5]);
score += this.ts_(this.TC1__[c1 + c2 + c3]);
score += this.ts_(this.TC2__[c2 + c3 + c4]);
score += this.ts_(this.TC3__[c3 + c4 + c5]);
score += this.ts_(this.TC4__[c4 + c5 + c6]);
// score += this.ts_(this.TC5__[c4 + c5 + c6]);
score += this.ts_(this.UQ1__[p1 + c1]);
score += this.ts_(this.UQ2__[p2 + c2]);
score += this.ts_(this.UQ3__[p3 + c3]);
score += this.ts_(this.BQ1__[p2 + c2 + c3]);
score += this.ts_(this.BQ2__[p2 + c3 + c4]);
score += this.ts_(this.BQ3__[p3 + c2 + c3]);
score += this.ts_(this.BQ4__[p3 + c3 + c4]);
score += this.ts_(this.TQ1__[p2 + c1 + c2 + c3]);
score += this.ts_(this.TQ2__[p2 + c2 + c3 + c4]);
score += this.ts_(this.TQ3__[p3 + c1 + c2 + c3]);
score += this.ts_(this.TQ4__[p3 + c2 + c3 + c4]);
var p = "O";
if (score > 0) {
result.push(word);
word = "";
p = "B";
}
p1 = p2;
p2 = p3;
p3 = p;
word += seg[i];
}
result.push(word);
return result;
}
lunr.TinySegmenter = TinySegmenter;
};
}));
================================================
FILE: site/assets/javascripts/lunr/wordcut.js
================================================
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}(g.lunr || (g.lunr = {})).wordcut = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 1;
})
this.addWords(words, false)
}
if(finalize){
this.finalizeDict();
}
},
dictSeek: function (l, r, ch, strOffset, pos) {
var ans = null;
while (l <= r) {
var m = Math.floor((l + r) / 2),
dict_item = this.dict[m],
len = dict_item.length;
if (len <= strOffset) {
l = m + 1;
} else {
var ch_ = dict_item[strOffset];
if (ch_ < ch) {
l = m + 1;
} else if (ch_ > ch) {
r = m - 1;
} else {
ans = m;
if (pos == LEFT) {
r = m - 1;
} else {
l = m + 1;
}
}
}
}
return ans;
},
isFinal: function (acceptor) {
return this.dict[acceptor.l].length == acceptor.strOffset;
},
createAcceptor: function () {
return {
l: 0,
r: this.dict.length - 1,
strOffset: 0,
isFinal: false,
dict: this,
transit: function (ch) {
return this.dict.transit(this, ch);
},
isError: false,
tag: "DICT",
w: 1,
type: "DICT"
};
},
transit: function (acceptor, ch) {
var l = this.dictSeek(acceptor.l,
acceptor.r,
ch,
acceptor.strOffset,
LEFT);
if (l !== null) {
var r = this.dictSeek(l,
acceptor.r,
ch,
acceptor.strOffset,
RIGHT);
acceptor.l = l;
acceptor.r = r;
acceptor.strOffset++;
acceptor.isFinal = this.isFinal(acceptor);
} else {
acceptor.isError = true;
}
return acceptor;
},
sortuniq: function(a){
return a.sort().filter(function(item, pos, arr){
return !pos || item != arr[pos - 1];
})
},
flatten: function(a){
//[[1,2],[3]] -> [1,2,3]
return [].concat.apply([], a);
}
};
module.exports = WordcutDict;
}).call(this,"/dist/tmp")
},{"glob":16,"path":22}],3:[function(require,module,exports){
var WordRule = {
createAcceptor: function(tag) {
if (tag["WORD_RULE"])
return null;
return {strOffset: 0,
isFinal: false,
transit: function(ch) {
var lch = ch.toLowerCase();
if (lch >= "a" && lch <= "z") {
this.isFinal = true;
this.strOffset++;
} else {
this.isError = true;
}
return this;
},
isError: false,
tag: "WORD_RULE",
type: "WORD_RULE",
w: 1};
}
};
var NumberRule = {
createAcceptor: function(tag) {
if (tag["NUMBER_RULE"])
return null;
return {strOffset: 0,
isFinal: false,
transit: function(ch) {
if (ch >= "0" && ch <= "9") {
this.isFinal = true;
this.strOffset++;
} else {
this.isError = true;
}
return this;
},
isError: false,
tag: "NUMBER_RULE",
type: "NUMBER_RULE",
w: 1};
}
};
var SpaceRule = {
tag: "SPACE_RULE",
createAcceptor: function(tag) {
if (tag["SPACE_RULE"])
return null;
return {strOffset: 0,
isFinal: false,
transit: function(ch) {
if (ch == " " || ch == "\t" || ch == "\r" || ch == "\n" ||
ch == "\u00A0" || ch=="\u2003"//nbsp and emsp
) {
this.isFinal = true;
this.strOffset++;
} else {
this.isError = true;
}
return this;
},
isError: false,
tag: SpaceRule.tag,
w: 1,
type: "SPACE_RULE"};
}
}
var SingleSymbolRule = {
tag: "SINSYM",
createAcceptor: function(tag) {
return {strOffset: 0,
isFinal: false,
transit: function(ch) {
if (this.strOffset == 0 && ch.match(/^[\@\(\)\/\,\-\."`]$/)) {
this.isFinal = true;
this.strOffset++;
} else {
this.isError = true;
}
return this;
},
isError: false,
tag: "SINSYM",
w: 1,
type: "SINSYM"};
}
}
var LatinRules = [WordRule, SpaceRule, SingleSymbolRule, NumberRule];
module.exports = LatinRules;
},{}],4:[function(require,module,exports){
var _ = require("underscore")
, WordcutCore = require("./wordcut_core");
var PathInfoBuilder = {
/*
buildByPartAcceptors: function(path, acceptors, i) {
var
var genInfos = partAcceptors.reduce(function(genInfos, acceptor) {
}, []);
return genInfos;
}
*/
buildByAcceptors: function(path, finalAcceptors, i) {
var self = this;
var infos = finalAcceptors.map(function(acceptor) {
var p = i - acceptor.strOffset + 1
, _info = path[p];
var info = {p: p,
mw: _info.mw + (acceptor.mw === undefined ? 0 : acceptor.mw),
w: acceptor.w + _info.w,
unk: (acceptor.unk ? acceptor.unk : 0) + _info.unk,
type: acceptor.type};
if (acceptor.type == "PART") {
for(var j = p + 1; j <= i; j++) {
path[j].merge = p;
}
info.merge = p;
}
return info;
});
return infos.filter(function(info) { return info; });
},
fallback: function(path, leftBoundary, text, i) {
var _info = path[leftBoundary];
if (text[i].match(/[\u0E48-\u0E4E]/)) {
if (leftBoundary != 0)
leftBoundary = path[leftBoundary].p;
return {p: leftBoundary,
mw: 0,
w: 1 + _info.w,
unk: 1 + _info.unk,
type: "UNK"};
/* } else if(leftBoundary > 0 && path[leftBoundary].type !== "UNK") {
leftBoundary = path[leftBoundary].p;
return {p: leftBoundary,
w: 1 + _info.w,
unk: 1 + _info.unk,
type: "UNK"}; */
} else {
return {p: leftBoundary,
mw: _info.mw,
w: 1 + _info.w,
unk: 1 + _info.unk,
type: "UNK"};
}
},
build: function(path, finalAcceptors, i, leftBoundary, text) {
var basicPathInfos = this.buildByAcceptors(path, finalAcceptors, i);
if (basicPathInfos.length > 0) {
return basicPathInfos;
} else {
return [this.fallback(path, leftBoundary, text, i)];
}
}
};
module.exports = function() {
return _.clone(PathInfoBuilder);
}
},{"./wordcut_core":8,"underscore":25}],5:[function(require,module,exports){
var _ = require("underscore");
var PathSelector = {
selectPath: function(paths) {
var path = paths.reduce(function(selectedPath, path) {
if (selectedPath == null) {
return path;
} else {
if (path.unk < selectedPath.unk)
return path;
if (path.unk == selectedPath.unk) {
if (path.mw < selectedPath.mw)
return path
if (path.mw == selectedPath.mw) {
if (path.w < selectedPath.w)
return path;
}
}
return selectedPath;
}
}, null);
return path;
},
createPath: function() {
return [{p:null, w:0, unk:0, type: "INIT", mw:0}];
}
};
module.exports = function() {
return _.clone(PathSelector);
};
},{"underscore":25}],6:[function(require,module,exports){
function isMatch(pat, offset, ch) {
if (pat.length <= offset)
return false;
var _ch = pat[offset];
return _ch == ch ||
(_ch.match(/[กข]/) && ch.match(/[ก-ฮ]/)) ||
(_ch.match(/[มบ]/) && ch.match(/[ก-ฮ]/)) ||
(_ch.match(/\u0E49/) && ch.match(/[\u0E48-\u0E4B]/));
}
var Rule0 = {
pat: "เหก็ม",
createAcceptor: function(tag) {
return {strOffset: 0,
isFinal: false,
transit: function(ch) {
if (isMatch(Rule0.pat, this.strOffset,ch)) {
this.isFinal = (this.strOffset + 1 == Rule0.pat.length);
this.strOffset++;
} else {
this.isError = true;
}
return this;
},
isError: false,
tag: "THAI_RULE",
type: "THAI_RULE",
w: 1};
}
};
var PartRule = {
createAcceptor: function(tag) {
return {strOffset: 0,
patterns: [
"แก", "เก", "ก้", "กก์", "กา", "กี", "กิ", "กืก"
],
isFinal: false,
transit: function(ch) {
var offset = this.strOffset;
this.patterns = this.patterns.filter(function(pat) {
return isMatch(pat, offset, ch);
});
if (this.patterns.length > 0) {
var len = 1 + offset;
this.isFinal = this.patterns.some(function(pat) {
return pat.length == len;
});
this.strOffset++;
} else {
this.isError = true;
}
return this;
},
isError: false,
tag: "PART",
type: "PART",
unk: 1,
w: 1};
}
};
var ThaiRules = [Rule0, PartRule];
module.exports = ThaiRules;
},{}],7:[function(require,module,exports){
var sys = require("sys")
, WordcutDict = require("./dict")
, WordcutCore = require("./wordcut_core")
, PathInfoBuilder = require("./path_info_builder")
, PathSelector = require("./path_selector")
, Acceptors = require("./acceptors")
, latinRules = require("./latin_rules")
, thaiRules = require("./thai_rules")
, _ = require("underscore");
var Wordcut = Object.create(WordcutCore);
Wordcut.defaultPathInfoBuilder = PathInfoBuilder;
Wordcut.defaultPathSelector = PathSelector;
Wordcut.defaultAcceptors = Acceptors;
Wordcut.defaultLatinRules = latinRules;
Wordcut.defaultThaiRules = thaiRules;
Wordcut.defaultDict = WordcutDict;
Wordcut.initNoDict = function(dict_path) {
var self = this;
self.pathInfoBuilder = new self.defaultPathInfoBuilder;
self.pathSelector = new self.defaultPathSelector;
self.acceptors = new self.defaultAcceptors;
self.defaultLatinRules.forEach(function(rule) {
self.acceptors.creators.push(rule);
});
self.defaultThaiRules.forEach(function(rule) {
self.acceptors.creators.push(rule);
});
};
Wordcut.init = function(dict_path, withDefault, additionalWords) {
withDefault = withDefault || false;
this.initNoDict();
var dict = _.clone(this.defaultDict);
dict.init(dict_path, withDefault, additionalWords);
this.acceptors.creators.push(dict);
};
module.exports = Wordcut;
},{"./acceptors":1,"./dict":2,"./latin_rules":3,"./path_info_builder":4,"./path_selector":5,"./thai_rules":6,"./wordcut_core":8,"sys":28,"underscore":25}],8:[function(require,module,exports){
var WordcutCore = {
buildPath: function(text) {
var self = this
, path = self.pathSelector.createPath()
, leftBoundary = 0;
self.acceptors.reset();
for (var i = 0; i < text.length; i++) {
var ch = text[i];
self.acceptors.transit(ch);
var possiblePathInfos = self
.pathInfoBuilder
.build(path,
self.acceptors.getFinalAcceptors(),
i,
leftBoundary,
text);
var selectedPath = self.pathSelector.selectPath(possiblePathInfos)
path.push(selectedPath);
if (selectedPath.type !== "UNK") {
leftBoundary = i;
}
}
return path;
},
pathToRanges: function(path) {
var e = path.length - 1
, ranges = [];
while (e > 0) {
var info = path[e]
, s = info.p;
if (info.merge !== undefined && ranges.length > 0) {
var r = ranges[ranges.length - 1];
r.s = info.merge;
s = r.s;
} else {
ranges.push({s:s, e:e});
}
e = s;
}
return ranges.reverse();
},
rangesToText: function(text, ranges, delimiter) {
return ranges.map(function(r) {
return text.substring(r.s, r.e);
}).join(delimiter);
},
cut: function(text, delimiter) {
var path = this.buildPath(text)
, ranges = this.pathToRanges(path);
return this
.rangesToText(text, ranges,
(delimiter === undefined ? "|" : delimiter));
},
cutIntoRanges: function(text, noText) {
var path = this.buildPath(text)
, ranges = this.pathToRanges(path);
if (!noText) {
ranges.forEach(function(r) {
r.text = text.substring(r.s, r.e);
});
}
return ranges;
},
cutIntoArray: function(text) {
var path = this.buildPath(text)
, ranges = this.pathToRanges(path);
return ranges.map(function(r) {
return text.substring(r.s, r.e)
});
}
};
module.exports = WordcutCore;
},{}],9:[function(require,module,exports){
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
//
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
//
// Originally from narwhal.js (http://narwhaljs.org)
// Copyright (c) 2009 Thomas Robinson <280north.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// when used in node, this will actually load the util module we depend on
// versus loading the builtin util module as happens otherwise
// this is a bug in node module loading as far as I am concerned
var util = require('util/');
var pSlice = Array.prototype.slice;
var hasOwn = Object.prototype.hasOwnProperty;
// 1. The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
// assert module must conform to the following interface.
var assert = module.exports = ok;
// 2. The AssertionError is defined in assert.
// new assert.AssertionError({ message: message,
// actual: actual,
// expected: expected })
assert.AssertionError = function AssertionError(options) {
this.name = 'AssertionError';
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;
if (options.message) {
this.message = options.message;
this.generatedMessage = false;
} else {
this.message = getMessage(this);
this.generatedMessage = true;
}
var stackStartFunction = options.stackStartFunction || fail;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, stackStartFunction);
}
else {
// non v8 browsers so we can have a stacktrace
var err = new Error();
if (err.stack) {
var out = err.stack;
// try to strip useless frames
var fn_name = stackStartFunction.name;
var idx = out.indexOf('\n' + fn_name);
if (idx >= 0) {
// once we have located the function frame
// we need to strip out everything before it (and its line)
var next_line = out.indexOf('\n', idx + 1);
out = out.substring(next_line + 1);
}
this.stack = out;
}
}
};
// assert.AssertionError instanceof Error
util.inherits(assert.AssertionError, Error);
function replacer(key, value) {
if (util.isUndefined(value)) {
return '' + value;
}
if (util.isNumber(value) && !isFinite(value)) {
return value.toString();
}
if (util.isFunction(value) || util.isRegExp(value)) {
return value.toString();
}
return value;
}
function truncate(s, n) {
if (util.isString(s)) {
return s.length < n ? s : s.slice(0, n);
} else {
return s;
}
}
function getMessage(self) {
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
self.operator + ' ' +
truncate(JSON.stringify(self.expected, replacer), 128);
}
// At present only the three keys mentioned above are used and
// understood by the spec. Implementations or sub modules can pass
// other keys to the AssertionError's constructor - they will be
// ignored.
// 3. All of the following functions must throw an AssertionError
// when a corresponding condition is not met, with a message that
// may be undefined if not provided. All assertion methods provide
// both the actual and expected values to the assertion error for
// display purposes.
function fail(actual, expected, message, operator, stackStartFunction) {
throw new assert.AssertionError({
message: message,
actual: actual,
expected: expected,
operator: operator,
stackStartFunction: stackStartFunction
});
}
// EXTENSION! allows for well behaved errors defined elsewhere.
assert.fail = fail;
// 4. Pure assertion tests whether a value is truthy, as determined
// by !!guard.
// assert.ok(guard, message_opt);
// This statement is equivalent to assert.equal(true, !!guard,
// message_opt);. To test strictly for the value true, use
// assert.strictEqual(true, guard, message_opt);.
function ok(value, message) {
if (!value) fail(value, true, message, '==', assert.ok);
}
assert.ok = ok;
// 5. The equality assertion tests shallow, coercive equality with
// ==.
// assert.equal(actual, expected, message_opt);
assert.equal = function equal(actual, expected, message) {
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
};
// 6. The non-equality assertion tests for whether two objects are not equal
// with != assert.notEqual(actual, expected, message_opt);
assert.notEqual = function notEqual(actual, expected, message) {
if (actual == expected) {
fail(actual, expected, message, '!=', assert.notEqual);
}
};
// 7. The equivalence assertion tests a deep equality relation.
// assert.deepEqual(actual, expected, message_opt);
assert.deepEqual = function deepEqual(actual, expected, message) {
if (!_deepEqual(actual, expected)) {
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
}
};
function _deepEqual(actual, expected) {
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
} else if (util.isBuffer(actual) && util.isBuffer(expected)) {
if (actual.length != expected.length) return false;
for (var i = 0; i < actual.length; i++) {
if (actual[i] !== expected[i]) return false;
}
return true;
// 7.2. If the expected value is a Date object, the actual value is
// equivalent if it is also a Date object that refers to the same time.
} else if (util.isDate(actual) && util.isDate(expected)) {
return actual.getTime() === expected.getTime();
// 7.3 If the expected value is a RegExp object, the actual value is
// equivalent if it is also a RegExp object with the same source and
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
} else if (util.isRegExp(actual) && util.isRegExp(expected)) {
return actual.source === expected.source &&
actual.global === expected.global &&
actual.multiline === expected.multiline &&
actual.lastIndex === expected.lastIndex &&
actual.ignoreCase === expected.ignoreCase;
// 7.4. Other pairs that do not both pass typeof value == 'object',
// equivalence is determined by ==.
} else if (!util.isObject(actual) && !util.isObject(expected)) {
return actual == expected;
// 7.5 For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical 'prototype' property. Note: this
// accounts for both named and indexed properties on Arrays.
} else {
return objEquiv(actual, expected);
}
}
function isArguments(object) {
return Object.prototype.toString.call(object) == '[object Arguments]';
}
function objEquiv(a, b) {
if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b))
return false;
// an identical 'prototype' property.
if (a.prototype !== b.prototype) return false;
// if one is a primitive, the other must be same
if (util.isPrimitive(a) || util.isPrimitive(b)) {
return a === b;
}
var aIsArgs = isArguments(a),
bIsArgs = isArguments(b);
if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
return false;
if (aIsArgs) {
a = pSlice.call(a);
b = pSlice.call(b);
return _deepEqual(a, b);
}
var ka = objectKeys(a),
kb = objectKeys(b),
key, i;
// having the same number of owned properties (keys incorporates
// hasOwnProperty)
if (ka.length != kb.length)
return false;
//the same set of keys (although not necessarily the same order),
ka.sort();
kb.sort();
//~~~cheap key test
for (i = ka.length - 1; i >= 0; i--) {
if (ka[i] != kb[i])
return false;
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = ka.length - 1; i >= 0; i--) {
key = ka[i];
if (!_deepEqual(a[key], b[key])) return false;
}
return true;
}
// 8. The non-equivalence assertion tests for any deep inequality.
// assert.notDeepEqual(actual, expected, message_opt);
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (_deepEqual(actual, expected)) {
fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
}
};
// 9. The strict equality assertion tests strict equality, as determined by ===.
// assert.strictEqual(actual, expected, message_opt);
assert.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected) {
fail(actual, expected, message, '===', assert.strictEqual);
}
};
// 10. The strict non-equality assertion tests for strict inequality, as
// determined by !==. assert.notStrictEqual(actual, expected, message_opt);
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected) {
fail(actual, expected, message, '!==', assert.notStrictEqual);
}
};
function expectedException(actual, expected) {
if (!actual || !expected) {
return false;
}
if (Object.prototype.toString.call(expected) == '[object RegExp]') {
return expected.test(actual);
} else if (actual instanceof expected) {
return true;
} else if (expected.call({}, actual) === true) {
return true;
}
return false;
}
function _throws(shouldThrow, block, expected, message) {
var actual;
if (util.isString(expected)) {
message = expected;
expected = null;
}
try {
block();
} catch (e) {
actual = e;
}
message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
(message ? ' ' + message : '.');
if (shouldThrow && !actual) {
fail(actual, expected, 'Missing expected exception' + message);
}
if (!shouldThrow && expectedException(actual, expected)) {
fail(actual, expected, 'Got unwanted exception' + message);
}
if ((shouldThrow && actual && expected &&
!expectedException(actual, expected)) || (!shouldThrow && actual)) {
throw actual;
}
}
// 11. Expected to throw an error:
// assert.throws(block, Error_opt, message_opt);
assert.throws = function(block, /*optional*/error, /*optional*/message) {
_throws.apply(this, [true].concat(pSlice.call(arguments)));
};
// EXTENSION! This is annoying to write outside this module.
assert.doesNotThrow = function(block, /*optional*/message) {
_throws.apply(this, [false].concat(pSlice.call(arguments)));
};
assert.ifError = function(err) { if (err) {throw err;}};
var objectKeys = Object.keys || function (obj) {
var keys = [];
for (var key in obj) {
if (hasOwn.call(obj, key)) keys.push(key);
}
return keys;
};
},{"util/":28}],10:[function(require,module,exports){
'use strict';
module.exports = balanced;
function balanced(a, b, str) {
if (a instanceof RegExp) a = maybeMatch(a, str);
if (b instanceof RegExp) b = maybeMatch(b, str);
var r = range(a, b, str);
return r && {
start: r[0],
end: r[1],
pre: str.slice(0, r[0]),
body: str.slice(r[0] + a.length, r[1]),
post: str.slice(r[1] + b.length)
};
}
function maybeMatch(reg, str) {
var m = str.match(reg);
return m ? m[0] : null;
}
balanced.range = range;
function range(a, b, str) {
var begs, beg, left, right, result;
var ai = str.indexOf(a);
var bi = str.indexOf(b, ai + 1);
var i = ai;
if (ai >= 0 && bi > 0) {
begs = [];
left = str.length;
while (i >= 0 && !result) {
if (i == ai) {
begs.push(i);
ai = str.indexOf(a, i + 1);
} else if (begs.length == 1) {
result = [ begs.pop(), bi ];
} else {
beg = begs.pop();
if (beg < left) {
left = beg;
right = bi;
}
bi = str.indexOf(b, i + 1);
}
i = ai < bi && ai >= 0 ? ai : bi;
}
if (begs.length) {
result = [ left, right ];
}
}
return result;
}
},{}],11:[function(require,module,exports){
var concatMap = require('concat-map');
var balanced = require('balanced-match');
module.exports = expandTop;
var escSlash = '\0SLASH'+Math.random()+'\0';
var escOpen = '\0OPEN'+Math.random()+'\0';
var escClose = '\0CLOSE'+Math.random()+'\0';
var escComma = '\0COMMA'+Math.random()+'\0';
var escPeriod = '\0PERIOD'+Math.random()+'\0';
function numeric(str) {
return parseInt(str, 10) == str
? parseInt(str, 10)
: str.charCodeAt(0);
}
function escapeBraces(str) {
return str.split('\\\\').join(escSlash)
.split('\\{').join(escOpen)
.split('\\}').join(escClose)
.split('\\,').join(escComma)
.split('\\.').join(escPeriod);
}
function unescapeBraces(str) {
return str.split(escSlash).join('\\')
.split(escOpen).join('{')
.split(escClose).join('}')
.split(escComma).join(',')
.split(escPeriod).join('.');
}
// Basically just str.split(","), but handling cases
// where we have nested braced sections, which should be
// treated as individual members, like {a,{b,c},d}
function parseCommaParts(str) {
if (!str)
return [''];
var parts = [];
var m = balanced('{', '}', str);
if (!m)
return str.split(',');
var pre = m.pre;
var body = m.body;
var post = m.post;
var p = pre.split(',');
p[p.length-1] += '{' + body + '}';
var postParts = parseCommaParts(post);
if (post.length) {
p[p.length-1] += postParts.shift();
p.push.apply(p, postParts);
}
parts.push.apply(parts, p);
return parts;
}
function expandTop(str) {
if (!str)
return [];
// I don't know why Bash 4.3 does this, but it does.
// Anything starting with {} will have the first two bytes preserved
// but *only* at the top level, so {},a}b will not expand to anything,
// but a{},b}c will be expanded to [a}c,abc].
// One could argue that this is a bug in Bash, but since the goal of
// this module is to match Bash's rules, we escape a leading {}
if (str.substr(0, 2) === '{}') {
str = '\\{\\}' + str.substr(2);
}
return expand(escapeBraces(str), true).map(unescapeBraces);
}
function identity(e) {
return e;
}
function embrace(str) {
return '{' + str + '}';
}
function isPadded(el) {
return /^-?0\d/.test(el);
}
function lte(i, y) {
return i <= y;
}
function gte(i, y) {
return i >= y;
}
function expand(str, isTop) {
var expansions = [];
var m = balanced('{', '}', str);
if (!m || /\$$/.test(m.pre)) return [str];
var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
var isSequence = isNumericSequence || isAlphaSequence;
var isOptions = m.body.indexOf(',') >= 0;
if (!isSequence && !isOptions) {
// {a},b}
if (m.post.match(/,.*\}/)) {
str = m.pre + '{' + m.body + escClose + m.post;
return expand(str);
}
return [str];
}
var n;
if (isSequence) {
n = m.body.split(/\.\./);
} else {
n = parseCommaParts(m.body);
if (n.length === 1) {
// x{{a,b}}y ==> x{a}y x{b}y
n = expand(n[0], false).map(embrace);
if (n.length === 1) {
var post = m.post.length
? expand(m.post, false)
: [''];
return post.map(function(p) {
return m.pre + n[0] + p;
});
}
}
}
// at this point, n is the parts, and we know it's not a comma set
// with a single entry.
// no need to expand pre, since it is guaranteed to be free of brace-sets
var pre = m.pre;
var post = m.post.length
? expand(m.post, false)
: [''];
var N;
if (isSequence) {
var x = numeric(n[0]);
var y = numeric(n[1]);
var width = Math.max(n[0].length, n[1].length)
var incr = n.length == 3
? Math.abs(numeric(n[2]))
: 1;
var test = lte;
var reverse = y < x;
if (reverse) {
incr *= -1;
test = gte;
}
var pad = n.some(isPadded);
N = [];
for (var i = x; test(i, y); i += incr) {
var c;
if (isAlphaSequence) {
c = String.fromCharCode(i);
if (c === '\\')
c = '';
} else {
c = String(i);
if (pad) {
var need = width - c.length;
if (need > 0) {
var z = new Array(need + 1).join('0');
if (i < 0)
c = '-' + z + c.slice(1);
else
c = z + c;
}
}
}
N.push(c);
}
} else {
N = concatMap(n, function(el) { return expand(el, false) });
}
for (var j = 0; j < N.length; j++) {
for (var k = 0; k < post.length; k++) {
var expansion = pre + N[j] + post[k];
if (!isTop || isSequence || expansion)
expansions.push(expansion);
}
}
return expansions;
}
},{"balanced-match":10,"concat-map":13}],12:[function(require,module,exports){
},{}],13:[function(require,module,exports){
module.exports = function (xs, fn) {
var res = [];
for (var i = 0; i < xs.length; i++) {
var x = fn(xs[i], i);
if (isArray(x)) res.push.apply(res, x);
else res.push(x);
}
return res;
};
var isArray = Array.isArray || function (xs) {
return Object.prototype.toString.call(xs) === '[object Array]';
};
},{}],14:[function(require,module,exports){
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
function EventEmitter() {
this._events = this._events || {};
this._maxListeners = this._maxListeners || undefined;
}
module.exports = EventEmitter;
// Backwards-compat with node 0.10.x
EventEmitter.EventEmitter = EventEmitter;
EventEmitter.prototype._events = undefined;
EventEmitter.prototype._maxListeners = undefined;
// By default EventEmitters will print a warning if more than 10 listeners are
// added to it. This is a useful default which helps finding memory leaks.
EventEmitter.defaultMaxListeners = 10;
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
EventEmitter.prototype.setMaxListeners = function(n) {
if (!isNumber(n) || n < 0 || isNaN(n))
throw TypeError('n must be a positive number');
this._maxListeners = n;
return this;
};
EventEmitter.prototype.emit = function(type) {
var er, handler, len, args, i, listeners;
if (!this._events)
this._events = {};
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events.error ||
(isObject(this._events.error) && !this._events.error.length)) {
er = arguments[1];
if (er instanceof Error) {
throw er; // Unhandled 'error' event
}
throw TypeError('Uncaught, unspecified "error" event.');
}
}
handler = this._events[type];
if (isUndefined(handler))
return false;
if (isFunction(handler)) {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
len = arguments.length;
args = new Array(len - 1);
for (i = 1; i < len; i++)
args[i - 1] = arguments[i];
handler.apply(this, args);
}
} else if (isObject(handler)) {
len = arguments.length;
args = new Array(len - 1);
for (i = 1; i < len; i++)
args[i - 1] = arguments[i];
listeners = handler.slice();
len = listeners.length;
for (i = 0; i < len; i++)
listeners[i].apply(this, args);
}
return true;
};
EventEmitter.prototype.addListener = function(type, listener) {
var m;
if (!isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events)
this._events = {};
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
if (this._events.newListener)
this.emit('newListener', type,
isFunction(listener.listener) ?
listener.listener : listener);
if (!this._events[type])
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
else if (isObject(this._events[type]))
// If we've already got an array, just append.
this._events[type].push(listener);
else
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
// Check for listener leak
if (isObject(this._events[type]) && !this._events[type].warned) {
var m;
if (!isUndefined(this._maxListeners)) {
m = this._maxListeners;
} else {
m = EventEmitter.defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
if (typeof console.trace === 'function') {
// not supported in IE 10
console.trace();
}
}
}
return this;
};
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function(type, listener) {
if (!isFunction(listener))
throw TypeError('listener must be a function');
var fired = false;
function g() {
this.removeListener(type, g);
if (!fired) {
fired = true;
listener.apply(this, arguments);
}
}
g.listener = listener;
this.on(type, g);
return this;
};
// emits a 'removeListener' event iff the listener was removed
EventEmitter.prototype.removeListener = function(type, listener) {
var list, position, length, i;
if (!isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events || !this._events[type])
return this;
list = this._events[type];
length = list.length;
position = -1;
if (list === listener ||
(isFunction(list.listener) && list.listener === listener)) {
delete this._events[type];
if (this._events.removeListener)
this.emit('removeListener', type, listener);
} else if (isObject(list)) {
for (i = length; i-- > 0;) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener)) {
position = i;
break;
}
}
if (position < 0)
return this;
if (list.length === 1) {
list.length = 0;
delete this._events[type];
} else {
list.splice(position, 1);
}
if (this._events.removeListener)
this.emit('removeListener', type, listener);
}
return this;
};
EventEmitter.prototype.removeAllListeners = function(type) {
var key, listeners;
if (!this._events)
return this;
// not listening for removeListener, no need to emit
if (!this._events.removeListener) {
if (arguments.length === 0)
this._events = {};
else if (this._events[type])
delete this._events[type];
return this;
}
// emit removeListener for all listeners on all events
if (arguments.length === 0) {
for (key in this._events) {
if (key === 'removeListener') continue;
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = {};
return this;
}
listeners = this._events[type];
if (isFunction(listeners)) {
this.removeListener(type, listeners);
} else {
// LIFO order
while (listeners.length)
this.removeListener(type, listeners[listeners.length - 1]);
}
delete this._events[type];
return this;
};
EventEmitter.prototype.listeners = function(type) {
var ret;
if (!this._events || !this._events[type])
ret = [];
else if (isFunction(this._events[type]))
ret = [this._events[type]];
else
ret = this._events[type].slice();
return ret;
};
EventEmitter.listenerCount = function(emitter, type) {
var ret;
if (!emitter._events || !emitter._events[type])
ret = 0;
else if (isFunction(emitter._events[type]))
ret = 1;
else
ret = emitter._events[type].length;
return ret;
};
function isFunction(arg) {
return typeof arg === 'function';
}
function isNumber(arg) {
return typeof arg === 'number';
}
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
}
function isUndefined(arg) {
return arg === void 0;
}
},{}],15:[function(require,module,exports){
(function (process){
exports.alphasort = alphasort
exports.alphasorti = alphasorti
exports.setopts = setopts
exports.ownProp = ownProp
exports.makeAbs = makeAbs
exports.finish = finish
exports.mark = mark
exports.isIgnored = isIgnored
exports.childrenIgnored = childrenIgnored
function ownProp (obj, field) {
return Object.prototype.hasOwnProperty.call(obj, field)
}
var path = require("path")
var minimatch = require("minimatch")
var isAbsolute = require("path-is-absolute")
var Minimatch = minimatch.Minimatch
function alphasorti (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase())
}
function alphasort (a, b) {
return a.localeCompare(b)
}
function setupIgnores (self, options) {
self.ignore = options.ignore || []
if (!Array.isArray(self.ignore))
self.ignore = [self.ignore]
if (self.ignore.length) {
self.ignore = self.ignore.map(ignoreMap)
}
}
function ignoreMap (pattern) {
var gmatcher = null
if (pattern.slice(-3) === '/**') {
var gpattern = pattern.replace(/(\/\*\*)+$/, '')
gmatcher = new Minimatch(gpattern)
}
return {
matcher: new Minimatch(pattern),
gmatcher: gmatcher
}
}
function setopts (self, pattern, options) {
if (!options)
options = {}
// base-matching: just use globstar for that.
if (options.matchBase && -1 === pattern.indexOf("/")) {
if (options.noglobstar) {
throw new Error("base matching requires globstar")
}
pattern = "**/" + pattern
}
self.silent = !!options.silent
self.pattern = pattern
self.strict = options.strict !== false
self.realpath = !!options.realpath
self.realpathCache = options.realpathCache || Object.create(null)
self.follow = !!options.follow
self.dot = !!options.dot
self.mark = !!options.mark
self.nodir = !!options.nodir
if (self.nodir)
self.mark = true
self.sync = !!options.sync
self.nounique = !!options.nounique
self.nonull = !!options.nonull
self.nosort = !!options.nosort
self.nocase = !!options.nocase
self.stat = !!options.stat
self.noprocess = !!options.noprocess
self.maxLength = options.maxLength || Infinity
self.cache = options.cache || Object.create(null)
self.statCache = options.statCache || Object.create(null)
self.symlinks = options.symlinks || Object.create(null)
setupIgnores(self, options)
self.changedCwd = false
var cwd = process.cwd()
if (!ownProp(options, "cwd"))
self.cwd = cwd
else {
self.cwd = options.cwd
self.changedCwd = path.resolve(options.cwd) !== cwd
}
self.root = options.root || path.resolve(self.cwd, "/")
self.root = path.resolve(self.root)
if (process.platform === "win32")
self.root = self.root.replace(/\\/g, "/")
self.nomount = !!options.nomount
// disable comments and negation unless the user explicitly
// passes in false as the option.
options.nonegate = options.nonegate === false ? false : true
options.nocomment = options.nocomment === false ? false : true
deprecationWarning(options)
self.minimatch = new Minimatch(pattern, options)
self.options = self.minimatch.options
}
// TODO(isaacs): remove entirely in v6
// exported to reset in tests
exports.deprecationWarned
function deprecationWarning(options) {
if (!options.nonegate || !options.nocomment) {
if (process.noDeprecation !== true && !exports.deprecationWarned) {
var msg = 'glob WARNING: comments and negation will be disabled in v6'
if (process.throwDeprecation)
throw new Error(msg)
else if (process.traceDeprecation)
console.trace(msg)
else
console.error(msg)
exports.deprecationWarned = true
}
}
}
function finish (self) {
var nou = self.nounique
var all = nou ? [] : Object.create(null)
for (var i = 0, l = self.matches.length; i < l; i ++) {
var matches = self.matches[i]
if (!matches || Object.keys(matches).length === 0) {
if (self.nonull) {
// do like the shell, and spit out the literal glob
var literal = self.minimatch.globSet[i]
if (nou)
all.push(literal)
else
all[literal] = true
}
} else {
// had matches
var m = Object.keys(matches)
if (nou)
all.push.apply(all, m)
else
m.forEach(function (m) {
all[m] = true
})
}
}
if (!nou)
all = Object.keys(all)
if (!self.nosort)
all = all.sort(self.nocase ? alphasorti : alphasort)
// at *some* point we statted all of these
if (self.mark) {
for (var i = 0; i < all.length; i++) {
all[i] = self._mark(all[i])
}
if (self.nodir) {
all = all.filter(function (e) {
return !(/\/$/.test(e))
})
}
}
if (self.ignore.length)
all = all.filter(function(m) {
return !isIgnored(self, m)
})
self.found = all
}
function mark (self, p) {
var abs = makeAbs(self, p)
var c = self.cache[abs]
var m = p
if (c) {
var isDir = c === 'DIR' || Array.isArray(c)
var slash = p.slice(-1) === '/'
if (isDir && !slash)
m += '/'
else if (!isDir && slash)
m = m.slice(0, -1)
if (m !== p) {
var mabs = makeAbs(self, m)
self.statCache[mabs] = self.statCache[abs]
self.cache[mabs] = self.cache[abs]
}
}
return m
}
// lotta situps...
function makeAbs (self, f) {
var abs = f
if (f.charAt(0) === '/') {
abs = path.join(self.root, f)
} else if (isAbsolute(f) || f === '') {
abs = f
} else if (self.changedCwd) {
abs = path.resolve(self.cwd, f)
} else {
abs = path.resolve(f)
}
return abs
}
// Return true, if pattern ends with globstar '**', for the accompanying parent directory.
// Ex:- If node_modules/** is the pattern, add 'node_modules' to ignore list along with it's contents
function isIgnored (self, path) {
if (!self.ignore.length)
return false
return self.ignore.some(function(item) {
return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path))
})
}
function childrenIgnored (self, path) {
if (!self.ignore.length)
return false
return self.ignore.some(function(item) {
return !!(item.gmatcher && item.gmatcher.match(path))
})
}
}).call(this,require('_process'))
},{"_process":24,"minimatch":20,"path":22,"path-is-absolute":23}],16:[function(require,module,exports){
(function (process){
// Approach:
//
// 1. Get the minimatch set
// 2. For each pattern in the set, PROCESS(pattern, false)
// 3. Store matches per-set, then uniq them
//
// PROCESS(pattern, inGlobStar)
// Get the first [n] items from pattern that are all strings
// Join these together. This is PREFIX.
// If there is no more remaining, then stat(PREFIX) and
// add to matches if it succeeds. END.
//
// If inGlobStar and PREFIX is symlink and points to dir
// set ENTRIES = []
// else readdir(PREFIX) as ENTRIES
// If fail, END
//
// with ENTRIES
// If pattern[n] is GLOBSTAR
// // handle the case where the globstar match is empty
// // by pruning it out, and testing the resulting pattern
// PROCESS(pattern[0..n] + pattern[n+1 .. $], false)
// // handle other cases.
// for ENTRY in ENTRIES (not dotfiles)
// // attach globstar + tail onto the entry
// // Mark that this entry is a globstar match
// PROCESS(pattern[0..n] + ENTRY + pattern[n .. $], true)
//
// else // not globstar
// for ENTRY in ENTRIES (not dotfiles, unless pattern[n] is dot)
// Test ENTRY against pattern[n]
// If fails, continue
// If passes, PROCESS(pattern[0..n] + item + pattern[n+1 .. $])
//
// Caveat:
// Cache all stats and readdirs results to minimize syscall. Since all
// we ever care about is existence and directory-ness, we can just keep
// `true` for files, and [children,...] for directories, or `false` for
// things that don't exist.
module.exports = glob
var fs = require('fs')
var minimatch = require('minimatch')
var Minimatch = minimatch.Minimatch
var inherits = require('inherits')
var EE = require('events').EventEmitter
var path = require('path')
var assert = require('assert')
var isAbsolute = require('path-is-absolute')
var globSync = require('./sync.js')
var common = require('./common.js')
var alphasort = common.alphasort
var alphasorti = common.alphasorti
var setopts = common.setopts
var ownProp = common.ownProp
var inflight = require('inflight')
var util = require('util')
var childrenIgnored = common.childrenIgnored
var isIgnored = common.isIgnored
var once = require('once')
function glob (pattern, options, cb) {
if (typeof options === 'function') cb = options, options = {}
if (!options) options = {}
if (options.sync) {
if (cb)
throw new TypeError('callback provided to sync glob')
return globSync(pattern, options)
}
return new Glob(pattern, options, cb)
}
glob.sync = globSync
var GlobSync = glob.GlobSync = globSync.GlobSync
// old api surface
glob.glob = glob
glob.hasMagic = function (pattern, options_) {
var options = util._extend({}, options_)
options.noprocess = true
var g = new Glob(pattern, options)
var set = g.minimatch.set
if (set.length > 1)
return true
for (var j = 0; j < set[0].length; j++) {
if (typeof set[0][j] !== 'string')
return true
}
return false
}
glob.Glob = Glob
inherits(Glob, EE)
function Glob (pattern, options, cb) {
if (typeof options === 'function') {
cb = options
options = null
}
if (options && options.sync) {
if (cb)
throw new TypeError('callback provided to sync glob')
return new GlobSync(pattern, options)
}
if (!(this instanceof Glob))
return new Glob(pattern, options, cb)
setopts(this, pattern, options)
this._didRealPath = false
// process each pattern in the minimatch set
var n = this.minimatch.set.length
// The matches are stored as {: true,...} so that
// duplicates are automagically pruned.
// Later, we do an Object.keys() on these.
// Keep them as a list so we can fill in when nonull is set.
this.matches = new Array(n)
if (typeof cb === 'function') {
cb = once(cb)
this.on('error', cb)
this.on('end', function (matches) {
cb(null, matches)
})
}
var self = this
var n = this.minimatch.set.length
this._processing = 0
this.matches = new Array(n)
this._emitQueue = []
this._processQueue = []
this.paused = false
if (this.noprocess)
return this
if (n === 0)
return done()
for (var i = 0; i < n; i ++) {
this._process(this.minimatch.set[i], i, false, done)
}
function done () {
--self._processing
if (self._processing <= 0)
self._finish()
}
}
Glob.prototype._finish = function () {
assert(this instanceof Glob)
if (this.aborted)
return
if (this.realpath && !this._didRealpath)
return this._realpath()
common.finish(this)
this.emit('end', this.found)
}
Glob.prototype._realpath = function () {
if (this._didRealpath)
return
this._didRealpath = true
var n = this.matches.length
if (n === 0)
return this._finish()
var self = this
for (var i = 0; i < this.matches.length; i++)
this._realpathSet(i, next)
function next () {
if (--n === 0)
self._finish()
}
}
Glob.prototype._realpathSet = function (index, cb) {
var matchset = this.matches[index]
if (!matchset)
return cb()
var found = Object.keys(matchset)
var self = this
var n = found.length
if (n === 0)
return cb()
var set = this.matches[index] = Object.create(null)
found.forEach(function (p, i) {
// If there's a problem with the stat, then it means that
// one or more of the links in the realpath couldn't be
// resolved. just return the abs value in that case.
p = self._makeAbs(p)
fs.realpath(p, self.realpathCache, function (er, real) {
if (!er)
set[real] = true
else if (er.syscall === 'stat')
set[p] = true
else
self.emit('error', er) // srsly wtf right here
if (--n === 0) {
self.matches[index] = set
cb()
}
})
})
}
Glob.prototype._mark = function (p) {
return common.mark(this, p)
}
Glob.prototype._makeAbs = function (f) {
return common.makeAbs(this, f)
}
Glob.prototype.abort = function () {
this.aborted = true
this.emit('abort')
}
Glob.prototype.pause = function () {
if (!this.paused) {
this.paused = true
this.emit('pause')
}
}
Glob.prototype.resume = function () {
if (this.paused) {
this.emit('resume')
this.paused = false
if (this._emitQueue.length) {
var eq = this._emitQueue.slice(0)
this._emitQueue.length = 0
for (var i = 0; i < eq.length; i ++) {
var e = eq[i]
this._emitMatch(e[0], e[1])
}
}
if (this._processQueue.length) {
var pq = this._processQueue.slice(0)
this._processQueue.length = 0
for (var i = 0; i < pq.length; i ++) {
var p = pq[i]
this._processing--
this._process(p[0], p[1], p[2], p[3])
}
}
}
}
Glob.prototype._process = function (pattern, index, inGlobStar, cb) {
assert(this instanceof Glob)
assert(typeof cb === 'function')
if (this.aborted)
return
this._processing++
if (this.paused) {
this._processQueue.push([pattern, index, inGlobStar, cb])
return
}
//console.error('PROCESS %d', this._processing, pattern)
// Get the first [n] parts of pattern that are all strings.
var n = 0
while (typeof pattern[n] === 'string') {
n ++
}
// now n is the index of the first one that is *not* a string.
// see if there's anything else
var prefix
switch (n) {
// if not, then this is rather simple
case pattern.length:
this._processSimple(pattern.join('/'), index, cb)
return
case 0:
// pattern *starts* with some non-trivial item.
// going to readdir(cwd), but not include the prefix in matches.
prefix = null
break
default:
// pattern has some string bits in the front.
// whatever it starts with, whether that's 'absolute' like /foo/bar,
// or 'relative' like '../baz'
prefix = pattern.slice(0, n).join('/')
break
}
var remain = pattern.slice(n)
// get the list of entries.
var read
if (prefix === null)
read = '.'
else if (isAbsolute(prefix) || isAbsolute(pattern.join('/'))) {
if (!prefix || !isAbsolute(prefix))
prefix = '/' + prefix
read = prefix
} else
read = prefix
var abs = this._makeAbs(read)
//if ignored, skip _processing
if (childrenIgnored(this, read))
return cb()
var isGlobStar = remain[0] === minimatch.GLOBSTAR
if (isGlobStar)
this._processGlobStar(prefix, read, abs, remain, index, inGlobStar, cb)
else
this._processReaddir(prefix, read, abs, remain, index, inGlobStar, cb)
}
Glob.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar, cb) {
var self = this
this._readdir(abs, inGlobStar, function (er, entries) {
return self._processReaddir2(prefix, read, abs, remain, index, inGlobStar, entries, cb)
})
}
Glob.prototype._processReaddir2 = function (prefix, read, abs, remain, index, inGlobStar, entries, cb) {
// if the abs isn't a dir, then nothing can match!
if (!entries)
return cb()
// It will only match dot entries if it starts with a dot, or if
// dot is set. Stuff like @(.foo|.bar) isn't allowed.
var pn = remain[0]
var negate = !!this.minimatch.negate
var rawGlob = pn._glob
var dotOk = this.dot || rawGlob.charAt(0) === '.'
var matchedEntries = []
for (var i = 0; i < entries.length; i++) {
var e = entries[i]
if (e.charAt(0) !== '.' || dotOk) {
var m
if (negate && !prefix) {
m = !e.match(pn)
} else {
m = e.match(pn)
}
if (m)
matchedEntries.push(e)
}
}
//console.error('prd2', prefix, entries, remain[0]._glob, matchedEntries)
var len = matchedEntries.length
// If there are no matched entries, then nothing matches.
if (len === 0)
return cb()
// if this is the last remaining pattern bit, then no need for
// an additional stat *unless* the user has specified mark or
// stat explicitly. We know they exist, since readdir returned
// them.
if (remain.length === 1 && !this.mark && !this.stat) {
if (!this.matches[index])
this.matches[index] = Object.create(null)
for (var i = 0; i < len; i ++) {
var e = matchedEntries[i]
if (prefix) {
if (prefix !== '/')
e = prefix + '/' + e
else
e = prefix + e
}
if (e.charAt(0) === '/' && !this.nomount) {
e = path.join(this.root, e)
}
this._emitMatch(index, e)
}
// This was the last one, and no stats were needed
return cb()
}
// now test all matched entries as stand-ins for that part
// of the pattern.
remain.shift()
for (var i = 0; i < len; i ++) {
var e = matchedEntries[i]
var newPattern
if (prefix) {
if (prefix !== '/')
e = prefix + '/' + e
else
e = prefix + e
}
this._process([e].concat(remain), index, inGlobStar, cb)
}
cb()
}
Glob.prototype._emitMatch = function (index, e) {
if (this.aborted)
return
if (this.matches[index][e])
return
if (isIgnored(this, e))
return
if (this.paused) {
this._emitQueue.push([index, e])
return
}
var abs = this._makeAbs(e)
if (this.nodir) {
var c = this.cache[abs]
if (c === 'DIR' || Array.isArray(c))
return
}
if (this.mark)
e = this._mark(e)
this.matches[index][e] = true
var st = this.statCache[abs]
if (st)
this.emit('stat', e, st)
this.emit('match', e)
}
Glob.prototype._readdirInGlobStar = function (abs, cb) {
if (this.aborted)
return
// follow all symlinked directories forever
// just proceed as if this is a non-globstar situation
if (this.follow)
return this._readdir(abs, false, cb)
var lstatkey = 'lstat\0' + abs
var self = this
var lstatcb = inflight(lstatkey, lstatcb_)
if (lstatcb)
fs.lstat(abs, lstatcb)
function lstatcb_ (er, lstat) {
if (er)
return cb()
var isSym = lstat.isSymbolicLink()
self.symlinks[abs] = isSym
// If it's not a symlink or a dir, then it's definitely a regular file.
// don't bother doing a readdir in that case.
if (!isSym && !lstat.isDirectory()) {
self.cache[abs] = 'FILE'
cb()
} else
self._readdir(abs, false, cb)
}
}
Glob.prototype._readdir = function (abs, inGlobStar, cb) {
if (this.aborted)
return
cb = inflight('readdir\0'+abs+'\0'+inGlobStar, cb)
if (!cb)
return
//console.error('RD %j %j', +inGlobStar, abs)
if (inGlobStar && !ownProp(this.symlinks, abs))
return this._readdirInGlobStar(abs, cb)
if (ownProp(this.cache, abs)) {
var c = this.cache[abs]
if (!c || c === 'FILE')
return cb()
if (Array.isArray(c))
return cb(null, c)
}
var self = this
fs.readdir(abs, readdirCb(this, abs, cb))
}
function readdirCb (self, abs, cb) {
return function (er, entries) {
if (er)
self._readdirError(abs, er, cb)
else
self._readdirEntries(abs, entries, cb)
}
}
Glob.prototype._readdirEntries = function (abs, entries, cb) {
if (this.aborted)
return
// if we haven't asked to stat everything, then just
// assume that everything in there exists, so we can avoid
// having to stat it a second time.
if (!this.mark && !this.stat) {
for (var i = 0; i < entries.length; i ++) {
var e = entries[i]
if (abs === '/')
e = abs + e
else
e = abs + '/' + e
this.cache[e] = true
}
}
this.cache[abs] = entries
return cb(null, entries)
}
Glob.prototype._readdirError = function (f, er, cb) {
if (this.aborted)
return
// handle errors, and cache the information
switch (er.code) {
case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
case 'ENOTDIR': // totally normal. means it *does* exist.
this.cache[this._makeAbs(f)] = 'FILE'
break
case 'ENOENT': // not terribly unusual
case 'ELOOP':
case 'ENAMETOOLONG':
case 'UNKNOWN':
this.cache[this._makeAbs(f)] = false
break
default: // some unusual error. Treat as failure.
this.cache[this._makeAbs(f)] = false
if (this.strict) {
this.emit('error', er)
// If the error is handled, then we abort
// if not, we threw out of here
this.abort()
}
if (!this.silent)
console.error('glob error', er)
break
}
return cb()
}
Glob.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar, cb) {
var self = this
this._readdir(abs, inGlobStar, function (er, entries) {
self._processGlobStar2(prefix, read, abs, remain, index, inGlobStar, entries, cb)
})
}
Glob.prototype._processGlobStar2 = function (prefix, read, abs, remain, index, inGlobStar, entries, cb) {
//console.error('pgs2', prefix, remain[0], entries)
// no entries means not a dir, so it can never have matches
// foo.txt/** doesn't match foo.txt
if (!entries)
return cb()
// test without the globstar, and with every child both below
// and replacing the globstar.
var remainWithoutGlobStar = remain.slice(1)
var gspref = prefix ? [ prefix ] : []
var noGlobStar = gspref.concat(remainWithoutGlobStar)
// the noGlobStar pattern exits the inGlobStar state
this._process(noGlobStar, index, false, cb)
var isSym = this.symlinks[abs]
var len = entries.length
// If it's a symlink, and we're in a globstar, then stop
if (isSym && inGlobStar)
return cb()
for (var i = 0; i < len; i++) {
var e = entries[i]
if (e.charAt(0) === '.' && !this.dot)
continue
// these two cases enter the inGlobStar state
var instead = gspref.concat(entries[i], remainWithoutGlobStar)
this._process(instead, index, true, cb)
var below = gspref.concat(entries[i], remain)
this._process(below, index, true, cb)
}
cb()
}
Glob.prototype._processSimple = function (prefix, index, cb) {
// XXX review this. Shouldn't it be doing the mounting etc
// before doing stat? kinda weird?
var self = this
this._stat(prefix, function (er, exists) {
self._processSimple2(prefix, index, er, exists, cb)
})
}
Glob.prototype._processSimple2 = function (prefix, index, er, exists, cb) {
//console.error('ps2', prefix, exists)
if (!this.matches[index])
this.matches[index] = Object.create(null)
// If it doesn't exist, then just mark the lack of results
if (!exists)
return cb()
if (prefix && isAbsolute(prefix) && !this.nomount) {
var trail = /[\/\\]$/.test(prefix)
if (prefix.charAt(0) === '/') {
prefix = path.join(this.root, prefix)
} else {
prefix = path.resolve(this.root, prefix)
if (trail)
prefix += '/'
}
}
if (process.platform === 'win32')
prefix = prefix.replace(/\\/g, '/')
// Mark this as a match
this._emitMatch(index, prefix)
cb()
}
// Returns either 'DIR', 'FILE', or false
Glob.prototype._stat = function (f, cb) {
var abs = this._makeAbs(f)
var needDir = f.slice(-1) === '/'
if (f.length > this.maxLength)
return cb()
if (!this.stat && ownProp(this.cache, abs)) {
var c = this.cache[abs]
if (Array.isArray(c))
c = 'DIR'
// It exists, but maybe not how we need it
if (!needDir || c === 'DIR')
return cb(null, c)
if (needDir && c === 'FILE')
return cb()
// otherwise we have to stat, because maybe c=true
// if we know it exists, but not what it is.
}
var exists
var stat = this.statCache[abs]
if (stat !== undefined) {
if (stat === false)
return cb(null, stat)
else {
var type = stat.isDirectory() ? 'DIR' : 'FILE'
if (needDir && type === 'FILE')
return cb()
else
return cb(null, type, stat)
}
}
var self = this
var statcb = inflight('stat\0' + abs, lstatcb_)
if (statcb)
fs.lstat(abs, statcb)
function lstatcb_ (er, lstat) {
if (lstat && lstat.isSymbolicLink()) {
// If it's a symlink, then treat it as the target, unless
// the target does not exist, then treat it as a file.
return fs.stat(abs, function (er, stat) {
if (er)
self._stat2(f, abs, null, lstat, cb)
else
self._stat2(f, abs, er, stat, cb)
})
} else {
self._stat2(f, abs, er, lstat, cb)
}
}
}
Glob.prototype._stat2 = function (f, abs, er, stat, cb) {
if (er) {
this.statCache[abs] = false
return cb()
}
var needDir = f.slice(-1) === '/'
this.statCache[abs] = stat
if (abs.slice(-1) === '/' && !stat.isDirectory())
return cb(null, false, stat)
var c = stat.isDirectory() ? 'DIR' : 'FILE'
this.cache[abs] = this.cache[abs] || c
if (needDir && c !== 'DIR')
return cb()
return cb(null, c, stat)
}
}).call(this,require('_process'))
},{"./common.js":15,"./sync.js":17,"_process":24,"assert":9,"events":14,"fs":12,"inflight":18,"inherits":19,"minimatch":20,"once":21,"path":22,"path-is-absolute":23,"util":28}],17:[function(require,module,exports){
(function (process){
module.exports = globSync
globSync.GlobSync = GlobSync
var fs = require('fs')
var minimatch = require('minimatch')
var Minimatch = minimatch.Minimatch
var Glob = require('./glob.js').Glob
var util = require('util')
var path = require('path')
var assert = require('assert')
var isAbsolute = require('path-is-absolute')
var common = require('./common.js')
var alphasort = common.alphasort
var alphasorti = common.alphasorti
var setopts = common.setopts
var ownProp = common.ownProp
var childrenIgnored = common.childrenIgnored
function globSync (pattern, options) {
if (typeof options === 'function' || arguments.length === 3)
throw new TypeError('callback provided to sync glob\n'+
'See: https://github.com/isaacs/node-glob/issues/167')
return new GlobSync(pattern, options).found
}
function GlobSync (pattern, options) {
if (!pattern)
throw new Error('must provide pattern')
if (typeof options === 'function' || arguments.length === 3)
throw new TypeError('callback provided to sync glob\n'+
'See: https://github.com/isaacs/node-glob/issues/167')
if (!(this instanceof GlobSync))
return new GlobSync(pattern, options)
setopts(this, pattern, options)
if (this.noprocess)
return this
var n = this.minimatch.set.length
this.matches = new Array(n)
for (var i = 0; i < n; i ++) {
this._process(this.minimatch.set[i], i, false)
}
this._finish()
}
GlobSync.prototype._finish = function () {
assert(this instanceof GlobSync)
if (this.realpath) {
var self = this
this.matches.forEach(function (matchset, index) {
var set = self.matches[index] = Object.create(null)
for (var p in matchset) {
try {
p = self._makeAbs(p)
var real = fs.realpathSync(p, self.realpathCache)
set[real] = true
} catch (er) {
if (er.syscall === 'stat')
set[self._makeAbs(p)] = true
else
throw er
}
}
})
}
common.finish(this)
}
GlobSync.prototype._process = function (pattern, index, inGlobStar) {
assert(this instanceof GlobSync)
// Get the first [n] parts of pattern that are all strings.
var n = 0
while (typeof pattern[n] === 'string') {
n ++
}
// now n is the index of the first one that is *not* a string.
// See if there's anything else
var prefix
switch (n) {
// if not, then this is rather simple
case pattern.length:
this._processSimple(pattern.join('/'), index)
return
case 0:
// pattern *starts* with some non-trivial item.
// going to readdir(cwd), but not include the prefix in matches.
prefix = null
break
default:
// pattern has some string bits in the front.
// whatever it starts with, whether that's 'absolute' like /foo/bar,
// or 'relative' like '../baz'
prefix = pattern.slice(0, n).join('/')
break
}
var remain = pattern.slice(n)
// get the list of entries.
var read
if (prefix === null)
read = '.'
else if (isAbsolute(prefix) || isAbsolute(pattern.join('/'))) {
if (!prefix || !isAbsolute(prefix))
prefix = '/' + prefix
read = prefix
} else
read = prefix
var abs = this._makeAbs(read)
//if ignored, skip processing
if (childrenIgnored(this, read))
return
var isGlobStar = remain[0] === minimatch.GLOBSTAR
if (isGlobStar)
this._processGlobStar(prefix, read, abs, remain, index, inGlobStar)
else
this._processReaddir(prefix, read, abs, remain, index, inGlobStar)
}
GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar) {
var entries = this._readdir(abs, inGlobStar)
// if the abs isn't a dir, then nothing can match!
if (!entries)
return
// It will only match dot entries if it starts with a dot, or if
// dot is set. Stuff like @(.foo|.bar) isn't allowed.
var pn = remain[0]
var negate = !!this.minimatch.negate
var rawGlob = pn._glob
var dotOk = this.dot || rawGlob.charAt(0) === '.'
var matchedEntries = []
for (var i = 0; i < entries.length; i++) {
var e = entries[i]
if (e.charAt(0) !== '.' || dotOk) {
var m
if (negate && !prefix) {
m = !e.match(pn)
} else {
m = e.match(pn)
}
if (m)
matchedEntries.push(e)
}
}
var len = matchedEntries.length
// If there are no matched entries, then nothing matches.
if (len === 0)
return
// if this is the last remaining pattern bit, then no need for
// an additional stat *unless* the user has specified mark or
// stat explicitly. We know they exist, since readdir returned
// them.
if (remain.length === 1 && !this.mark && !this.stat) {
if (!this.matches[index])
this.matches[index] = Object.create(null)
for (var i = 0; i < len; i ++) {
var e = matchedEntries[i]
if (prefix) {
if (prefix.slice(-1) !== '/')
e = prefix + '/' + e
else
e = prefix + e
}
if (e.charAt(0) === '/' && !this.nomount) {
e = path.join(this.root, e)
}
this.matches[index][e] = true
}
// This was the last one, and no stats were needed
return
}
// now test all matched entries as stand-ins for that part
// of the pattern.
remain.shift()
for (var i = 0; i < len; i ++) {
var e = matchedEntries[i]
var newPattern
if (prefix)
newPattern = [prefix, e]
else
newPattern = [e]
this._process(newPattern.concat(remain), index, inGlobStar)
}
}
GlobSync.prototype._emitMatch = function (index, e) {
var abs = this._makeAbs(e)
if (this.mark)
e = this._mark(e)
if (this.matches[index][e])
return
if (this.nodir) {
var c = this.cache[this._makeAbs(e)]
if (c === 'DIR' || Array.isArray(c))
return
}
this.matches[index][e] = true
if (this.stat)
this._stat(e)
}
GlobSync.prototype._readdirInGlobStar = function (abs) {
// follow all symlinked directories forever
// just proceed as if this is a non-globstar situation
if (this.follow)
return this._readdir(abs, false)
var entries
var lstat
var stat
try {
lstat = fs.lstatSync(abs)
} catch (er) {
// lstat failed, doesn't exist
return null
}
var isSym = lstat.isSymbolicLink()
this.symlinks[abs] = isSym
// If it's not a symlink or a dir, then it's definitely a regular file.
// don't bother doing a readdir in that case.
if (!isSym && !lstat.isDirectory())
this.cache[abs] = 'FILE'
else
entries = this._readdir(abs, false)
return entries
}
GlobSync.prototype._readdir = function (abs, inGlobStar) {
var entries
if (inGlobStar && !ownProp(this.symlinks, abs))
return this._readdirInGlobStar(abs)
if (ownProp(this.cache, abs)) {
var c = this.cache[abs]
if (!c || c === 'FILE')
return null
if (Array.isArray(c))
return c
}
try {
return this._readdirEntries(abs, fs.readdirSync(abs))
} catch (er) {
this._readdirError(abs, er)
return null
}
}
GlobSync.prototype._readdirEntries = function (abs, entries) {
// if we haven't asked to stat everything, then just
// assume that everything in there exists, so we can avoid
// having to stat it a second time.
if (!this.mark && !this.stat) {
for (var i = 0; i < entries.length; i ++) {
var e = entries[i]
if (abs === '/')
e = abs + e
else
e = abs + '/' + e
this.cache[e] = true
}
}
this.cache[abs] = entries
// mark and cache dir-ness
return entries
}
GlobSync.prototype._readdirError = function (f, er) {
// handle errors, and cache the information
switch (er.code) {
case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
case 'ENOTDIR': // totally normal. means it *does* exist.
this.cache[this._makeAbs(f)] = 'FILE'
break
case 'ENOENT': // not terribly unusual
case 'ELOOP':
case 'ENAMETOOLONG':
case 'UNKNOWN':
this.cache[this._makeAbs(f)] = false
break
default: // some unusual error. Treat as failure.
this.cache[this._makeAbs(f)] = false
if (this.strict)
throw er
if (!this.silent)
console.error('glob error', er)
break
}
}
GlobSync.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar) {
var entries = this._readdir(abs, inGlobStar)
// no entries means not a dir, so it can never have matches
// foo.txt/** doesn't match foo.txt
if (!entries)
return
// test without the globstar, and with every child both below
// and replacing the globstar.
var remainWithoutGlobStar = remain.slice(1)
var gspref = prefix ? [ prefix ] : []
var noGlobStar = gspref.concat(remainWithoutGlobStar)
// the noGlobStar pattern exits the inGlobStar state
this._process(noGlobStar, index, false)
var len = entries.length
var isSym = this.symlinks[abs]
// If it's a symlink, and we're in a globstar, then stop
if (isSym && inGlobStar)
return
for (var i = 0; i < len; i++) {
var e = entries[i]
if (e.charAt(0) === '.' && !this.dot)
continue
// these two cases enter the inGlobStar state
var instead = gspref.concat(entries[i], remainWithoutGlobStar)
this._process(instead, index, true)
var below = gspref.concat(entries[i], remain)
this._process(below, index, true)
}
}
GlobSync.prototype._processSimple = function (prefix, index) {
// XXX review this. Shouldn't it be doing the mounting etc
// before doing stat? kinda weird?
var exists = this._stat(prefix)
if (!this.matches[index])
this.matches[index] = Object.create(null)
// If it doesn't exist, then just mark the lack of results
if (!exists)
return
if (prefix && isAbsolute(prefix) && !this.nomount) {
var trail = /[\/\\]$/.test(prefix)
if (prefix.charAt(0) === '/') {
prefix = path.join(this.root, prefix)
} else {
prefix = path.resolve(this.root, prefix)
if (trail)
prefix += '/'
}
}
if (process.platform === 'win32')
prefix = prefix.replace(/\\/g, '/')
// Mark this as a match
this.matches[index][prefix] = true
}
// Returns either 'DIR', 'FILE', or false
GlobSync.prototype._stat = function (f) {
var abs = this._makeAbs(f)
var needDir = f.slice(-1) === '/'
if (f.length > this.maxLength)
return false
if (!this.stat && ownProp(this.cache, abs)) {
var c = this.cache[abs]
if (Array.isArray(c))
c = 'DIR'
// It exists, but maybe not how we need it
if (!needDir || c === 'DIR')
return c
if (needDir && c === 'FILE')
return false
// otherwise we have to stat, because maybe c=true
// if we know it exists, but not what it is.
}
var exists
var stat = this.statCache[abs]
if (!stat) {
var lstat
try {
lstat = fs.lstatSync(abs)
} catch (er) {
return false
}
if (lstat.isSymbolicLink()) {
try {
stat = fs.statSync(abs)
} catch (er) {
stat = lstat
}
} else {
stat = lstat
}
}
this.statCache[abs] = stat
var c = stat.isDirectory() ? 'DIR' : 'FILE'
this.cache[abs] = this.cache[abs] || c
if (needDir && c !== 'DIR')
return false
return c
}
GlobSync.prototype._mark = function (p) {
return common.mark(this, p)
}
GlobSync.prototype._makeAbs = function (f) {
return common.makeAbs(this, f)
}
}).call(this,require('_process'))
},{"./common.js":15,"./glob.js":16,"_process":24,"assert":9,"fs":12,"minimatch":20,"path":22,"path-is-absolute":23,"util":28}],18:[function(require,module,exports){
(function (process){
var wrappy = require('wrappy')
var reqs = Object.create(null)
var once = require('once')
module.exports = wrappy(inflight)
function inflight (key, cb) {
if (reqs[key]) {
reqs[key].push(cb)
return null
} else {
reqs[key] = [cb]
return makeres(key)
}
}
function makeres (key) {
return once(function RES () {
var cbs = reqs[key]
var len = cbs.length
var args = slice(arguments)
// XXX It's somewhat ambiguous whether a new callback added in this
// pass should be queued for later execution if something in the
// list of callbacks throws, or if it should just be discarded.
// However, it's such an edge case that it hardly matters, and either
// choice is likely as surprising as the other.
// As it happens, we do go ahead and schedule it for later execution.
try {
for (var i = 0; i < len; i++) {
cbs[i].apply(null, args)
}
} finally {
if (cbs.length > len) {
// added more in the interim.
// de-zalgo, just in case, but don't call again.
cbs.splice(0, len)
process.nextTick(function () {
RES.apply(null, args)
})
} else {
delete reqs[key]
}
}
})
}
function slice (args) {
var length = args.length
var array = []
for (var i = 0; i < length; i++) array[i] = args[i]
return array
}
}).call(this,require('_process'))
},{"_process":24,"once":21,"wrappy":29}],19:[function(require,module,exports){
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
} else {
// old school shim for old browsers
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
var TempCtor = function () {}
TempCtor.prototype = superCtor.prototype
ctor.prototype = new TempCtor()
ctor.prototype.constructor = ctor
}
}
},{}],20:[function(require,module,exports){
module.exports = minimatch
minimatch.Minimatch = Minimatch
var path = { sep: '/' }
try {
path = require('path')
} catch (er) {}
var GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
var expand = require('brace-expansion')
var plTypes = {
'!': { open: '(?:(?!(?:', close: '))[^/]*?)'},
'?': { open: '(?:', close: ')?' },
'+': { open: '(?:', close: ')+' },
'*': { open: '(?:', close: ')*' },
'@': { open: '(?:', close: ')' }
}
// any single thing other than /
// don't need to escape / when using new RegExp()
var qmark = '[^/]'
// * => any number of characters
var star = qmark + '*?'
// ** when dots are allowed. Anything goes, except .. and .
// not (^ or / followed by one or two dots followed by $ or /),
// followed by anything, any number of times.
var twoStarDot = '(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?'
// not a ^ or / followed by a dot,
// followed by anything, any number of times.
var twoStarNoDot = '(?:(?!(?:\\\/|^)\\.).)*?'
// characters that need to be escaped in RegExp.
var reSpecials = charSet('().*{}+?[]^$\\!')
// "abc" -> { a:true, b:true, c:true }
function charSet (s) {
return s.split('').reduce(function (set, c) {
set[c] = true
return set
}, {})
}
// normalizes slashes.
var slashSplit = /\/+/
minimatch.filter = filter
function filter (pattern, options) {
options = options || {}
return function (p, i, list) {
return minimatch(p, pattern, options)
}
}
function ext (a, b) {
a = a || {}
b = b || {}
var t = {}
Object.keys(b).forEach(function (k) {
t[k] = b[k]
})
Object.keys(a).forEach(function (k) {
t[k] = a[k]
})
return t
}
minimatch.defaults = function (def) {
if (!def || !Object.keys(def).length) return minimatch
var orig = minimatch
var m = function minimatch (p, pattern, options) {
return orig.minimatch(p, pattern, ext(def, options))
}
m.Minimatch = function Minimatch (pattern, options) {
return new orig.Minimatch(pattern, ext(def, options))
}
return m
}
Minimatch.defaults = function (def) {
if (!def || !Object.keys(def).length) return Minimatch
return minimatch.defaults(def).Minimatch
}
function minimatch (p, pattern, options) {
if (typeof pattern !== 'string') {
throw new TypeError('glob pattern string required')
}
if (!options) options = {}
// shortcut: comments match nothing.
if (!options.nocomment && pattern.charAt(0) === '#') {
return false
}
// "" only matches ""
if (pattern.trim() === '') return p === ''
return new Minimatch(pattern, options).match(p)
}
function Minimatch (pattern, options) {
if (!(this instanceof Minimatch)) {
return new Minimatch(pattern, options)
}
if (typeof pattern !== 'string') {
throw new TypeError('glob pattern string required')
}
if (!options) options = {}
pattern = pattern.trim()
// windows support: need to use /, not \
if (path.sep !== '/') {
pattern = pattern.split(path.sep).join('/')
}
this.options = options
this.set = []
this.pattern = pattern
this.regexp = null
this.negate = false
this.comment = false
this.empty = false
// make the set of regexps etc.
this.make()
}
Minimatch.prototype.debug = function () {}
Minimatch.prototype.make = make
function make () {
// don't do it more than once.
if (this._made) return
var pattern = this.pattern
var options = this.options
// empty patterns and comments match nothing.
if (!options.nocomment && pattern.charAt(0) === '#') {
this.comment = true
return
}
if (!pattern) {
this.empty = true
return
}
// step 1: figure out negation, etc.
this.parseNegate()
// step 2: expand braces
var set = this.globSet = this.braceExpand()
if (options.debug) this.debug = console.error
this.debug(this.pattern, set)
// step 3: now we have a set, so turn each one into a series of path-portion
// matching patterns.
// These will be regexps, except in the case of "**", which is
// set to the GLOBSTAR object for globstar behavior,
// and will not contain any / characters
set = this.globParts = set.map(function (s) {
return s.split(slashSplit)
})
this.debug(this.pattern, set)
// glob --> regexps
set = set.map(function (s, si, set) {
return s.map(this.parse, this)
}, this)
this.debug(this.pattern, set)
// filter out everything that didn't compile properly.
set = set.filter(function (s) {
return s.indexOf(false) === -1
})
this.debug(this.pattern, set)
this.set = set
}
Minimatch.prototype.parseNegate = parseNegate
function parseNegate () {
var pattern = this.pattern
var negate = false
var options = this.options
var negateOffset = 0
if (options.nonegate) return
for (var i = 0, l = pattern.length
; i < l && pattern.charAt(i) === '!'
; i++) {
negate = !negate
negateOffset++
}
if (negateOffset) this.pattern = pattern.substr(negateOffset)
this.negate = negate
}
// Brace expansion:
// a{b,c}d -> abd acd
// a{b,}c -> abc ac
// a{0..3}d -> a0d a1d a2d a3d
// a{b,c{d,e}f}g -> abg acdfg acefg
// a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
//
// Invalid sets are not expanded.
// a{2..}b -> a{2..}b
// a{b}c -> a{b}c
minimatch.braceExpand = function (pattern, options) {
return braceExpand(pattern, options)
}
Minimatch.prototype.braceExpand = braceExpand
function braceExpand (pattern, options) {
if (!options) {
if (this instanceof Minimatch) {
options = this.options
} else {
options = {}
}
}
pattern = typeof pattern === 'undefined'
? this.pattern : pattern
if (typeof pattern === 'undefined') {
throw new TypeError('undefined pattern')
}
if (options.nobrace ||
!pattern.match(/\{.*\}/)) {
// shortcut. no need to expand.
return [pattern]
}
return expand(pattern)
}
// parse a component of the expanded set.
// At this point, no pattern may contain "/" in it
// so we're going to return a 2d array, where each entry is the full
// pattern, split on '/', and then turned into a regular expression.
// A regexp is made at the end which joins each array with an
// escaped /, and another full one which joins each regexp with |.
//
// Following the lead of Bash 4.1, note that "**" only has special meaning
// when it is the *only* thing in a path portion. Otherwise, any series
// of * is equivalent to a single *. Globstar behavior is enabled by
// default, and can be disabled by setting options.noglobstar.
Minimatch.prototype.parse = parse
var SUBPARSE = {}
function parse (pattern, isSub) {
if (pattern.length > 1024 * 64) {
throw new TypeError('pattern is too long')
}
var options = this.options
// shortcuts
if (!options.noglobstar && pattern === '**') return GLOBSTAR
if (pattern === '') return ''
var re = ''
var hasMagic = !!options.nocase
var escaping = false
// ? => one single character
var patternListStack = []
var negativeLists = []
var stateChar
var inClass = false
var reClassStart = -1
var classStart = -1
// . and .. never match anything that doesn't start with .,
// even when options.dot is set.
var patternStart = pattern.charAt(0) === '.' ? '' // anything
// not (start or / followed by . or .. followed by / or end)
: options.dot ? '(?!(?:^|\\\/)\\.{1,2}(?:$|\\\/))'
: '(?!\\.)'
var self = this
function clearStateChar () {
if (stateChar) {
// we had some state-tracking character
// that wasn't consumed by this pass.
switch (stateChar) {
case '*':
re += star
hasMagic = true
break
case '?':
re += qmark
hasMagic = true
break
default:
re += '\\' + stateChar
break
}
self.debug('clearStateChar %j %j', stateChar, re)
stateChar = false
}
}
for (var i = 0, len = pattern.length, c
; (i < len) && (c = pattern.charAt(i))
; i++) {
this.debug('%s\t%s %s %j', pattern, i, re, c)
// skip over any that are escaped.
if (escaping && reSpecials[c]) {
re += '\\' + c
escaping = false
continue
}
switch (c) {
case '/':
// completely not allowed, even escaped.
// Should already be path-split by now.
return false
case '\\':
clearStateChar()
escaping = true
continue
// the various stateChar values
// for the "extglob" stuff.
case '?':
case '*':
case '+':
case '@':
case '!':
this.debug('%s\t%s %s %j <-- stateChar', pattern, i, re, c)
// all of those are literals inside a class, except that
// the glob [!a] means [^a] in regexp
if (inClass) {
this.debug(' in class')
if (c === '!' && i === classStart + 1) c = '^'
re += c
continue
}
// if we already have a stateChar, then it means
// that there was something like ** or +? in there.
// Handle the stateChar, then proceed with this one.
self.debug('call clearStateChar %j', stateChar)
clearStateChar()
stateChar = c
// if extglob is disabled, then +(asdf|foo) isn't a thing.
// just clear the statechar *now*, rather than even diving into
// the patternList stuff.
if (options.noext) clearStateChar()
continue
case '(':
if (inClass) {
re += '('
continue
}
if (!stateChar) {
re += '\\('
continue
}
patternListStack.push({
type: stateChar,
start: i - 1,
reStart: re.length,
open: plTypes[stateChar].open,
close: plTypes[stateChar].close
})
// negation is (?:(?!js)[^/]*)
re += stateChar === '!' ? '(?:(?!(?:' : '(?:'
this.debug('plType %j %j', stateChar, re)
stateChar = false
continue
case ')':
if (inClass || !patternListStack.length) {
re += '\\)'
continue
}
clearStateChar()
hasMagic = true
var pl = patternListStack.pop()
// negation is (?:(?!js)[^/]*)
// The others are (?:)
re += pl.close
if (pl.type === '!') {
negativeLists.push(pl)
}
pl.reEnd = re.length
continue
case '|':
if (inClass || !patternListStack.length || escaping) {
re += '\\|'
escaping = false
continue
}
clearStateChar()
re += '|'
continue
// these are mostly the same in regexp and glob
case '[':
// swallow any state-tracking char before the [
clearStateChar()
if (inClass) {
re += '\\' + c
continue
}
inClass = true
classStart = i
reClassStart = re.length
re += c
continue
case ']':
// a right bracket shall lose its special
// meaning and represent itself in
// a bracket expression if it occurs
// first in the list. -- POSIX.2 2.8.3.2
if (i === classStart + 1 || !inClass) {
re += '\\' + c
escaping = false
continue
}
// handle the case where we left a class open.
// "[z-a]" is valid, equivalent to "\[z-a\]"
if (inClass) {
// split where the last [ was, make sure we don't have
// an invalid re. if so, re-walk the contents of the
// would-be class to re-translate any characters that
// were passed through as-is
// TODO: It would probably be faster to determine this
// without a try/catch and a new RegExp, but it's tricky
// to do safely. For now, this is safe and works.
var cs = pattern.substring(classStart + 1, i)
try {
RegExp('[' + cs + ']')
} catch (er) {
// not a valid class!
var sp = this.parse(cs, SUBPARSE)
re = re.substr(0, reClassStart) + '\\[' + sp[0] + '\\]'
hasMagic = hasMagic || sp[1]
inClass = false
continue
}
}
// finish up the class.
hasMagic = true
inClass = false
re += c
continue
default:
// swallow any state char that wasn't consumed
clearStateChar()
if (escaping) {
// no need
escaping = false
} else if (reSpecials[c]
&& !(c === '^' && inClass)) {
re += '\\'
}
re += c
} // switch
} // for
// handle the case where we left a class open.
// "[abc" is valid, equivalent to "\[abc"
if (inClass) {
// split where the last [ was, and escape it
// this is a huge pita. We now have to re-walk
// the contents of the would-be class to re-translate
// any characters that were passed through as-is
cs = pattern.substr(classStart + 1)
sp = this.parse(cs, SUBPARSE)
re = re.substr(0, reClassStart) + '\\[' + sp[0]
hasMagic = hasMagic || sp[1]
}
// handle the case where we had a +( thing at the *end*
// of the pattern.
// each pattern list stack adds 3 chars, and we need to go through
// and escape any | chars that were passed through as-is for the regexp.
// Go through and escape them, taking care not to double-escape any
// | chars that were already escaped.
for (pl = patternListStack.pop(); pl; pl = patternListStack.pop()) {
var tail = re.slice(pl.reStart + pl.open.length)
this.debug('setting tail', re, pl)
// maybe some even number of \, then maybe 1 \, followed by a |
tail = tail.replace(/((?:\\{2}){0,64})(\\?)\|/g, function (_, $1, $2) {
if (!$2) {
// the | isn't already escaped, so escape it.
$2 = '\\'
}
// need to escape all those slashes *again*, without escaping the
// one that we need for escaping the | character. As it works out,
// escaping an even number of slashes can be done by simply repeating
// it exactly after itself. That's why this trick works.
//
// I am sorry that you have to see this.
return $1 + $1 + $2 + '|'
})
this.debug('tail=%j\n %s', tail, tail, pl, re)
var t = pl.type === '*' ? star
: pl.type === '?' ? qmark
: '\\' + pl.type
hasMagic = true
re = re.slice(0, pl.reStart) + t + '\\(' + tail
}
// handle trailing things that only matter at the very end.
clearStateChar()
if (escaping) {
// trailing \\
re += '\\\\'
}
// only need to apply the nodot start if the re starts with
// something that could conceivably capture a dot
var addPatternStart = false
switch (re.charAt(0)) {
case '.':
case '[':
case '(': addPatternStart = true
}
// Hack to work around lack of negative lookbehind in JS
// A pattern like: *.!(x).!(y|z) needs to ensure that a name
// like 'a.xyz.yz' doesn't match. So, the first negative
// lookahead, has to look ALL the way ahead, to the end of
// the pattern.
for (var n = negativeLists.length - 1; n > -1; n--) {
var nl = negativeLists[n]
var nlBefore = re.slice(0, nl.reStart)
var nlFirst = re.slice(nl.reStart, nl.reEnd - 8)
var nlLast = re.slice(nl.reEnd - 8, nl.reEnd)
var nlAfter = re.slice(nl.reEnd)
nlLast += nlAfter
// Handle nested stuff like *(*.js|!(*.json)), where open parens
// mean that we should *not* include the ) in the bit that is considered
// "after" the negated section.
var openParensBefore = nlBefore.split('(').length - 1
var cleanAfter = nlAfter
for (i = 0; i < openParensBefore; i++) {
cleanAfter = cleanAfter.replace(/\)[+*?]?/, '')
}
nlAfter = cleanAfter
var dollar = ''
if (nlAfter === '' && isSub !== SUBPARSE) {
dollar = '$'
}
var newRe = nlBefore + nlFirst + nlAfter + dollar + nlLast
re = newRe
}
// if the re is not "" at this point, then we need to make sure
// it doesn't match against an empty path part.
// Otherwise a/* will match a/, which it should not.
if (re !== '' && hasMagic) {
re = '(?=.)' + re
}
if (addPatternStart) {
re = patternStart + re
}
// parsing just a piece of a larger pattern.
if (isSub === SUBPARSE) {
return [re, hasMagic]
}
// skip the regexp for non-magical patterns
// unescape anything in it, though, so that it'll be
// an exact match against a file etc.
if (!hasMagic) {
return globUnescape(pattern)
}
var flags = options.nocase ? 'i' : ''
try {
var regExp = new RegExp('^' + re + '$', flags)
} catch (er) {
// If it was an invalid regular expression, then it can't match
// anything. This trick looks for a character after the end of
// the string, which is of course impossible, except in multi-line
// mode, but it's not a /m regex.
return new RegExp('$.')
}
regExp._glob = pattern
regExp._src = re
return regExp
}
minimatch.makeRe = function (pattern, options) {
return new Minimatch(pattern, options || {}).makeRe()
}
Minimatch.prototype.makeRe = makeRe
function makeRe () {
if (this.regexp || this.regexp === false) return this.regexp
// at this point, this.set is a 2d array of partial
// pattern strings, or "**".
//
// It's better to use .match(). This function shouldn't
// be used, really, but it's pretty convenient sometimes,
// when you just want to work with a regex.
var set = this.set
if (!set.length) {
this.regexp = false
return this.regexp
}
var options = this.options
var twoStar = options.noglobstar ? star
: options.dot ? twoStarDot
: twoStarNoDot
var flags = options.nocase ? 'i' : ''
var re = set.map(function (pattern) {
return pattern.map(function (p) {
return (p === GLOBSTAR) ? twoStar
: (typeof p === 'string') ? regExpEscape(p)
: p._src
}).join('\\\/')
}).join('|')
// must match entire pattern
// ending in a * or ** will make it less strict.
re = '^(?:' + re + ')$'
// can match anything, as long as it's not this.
if (this.negate) re = '^(?!' + re + ').*$'
try {
this.regexp = new RegExp(re, flags)
} catch (ex) {
this.regexp = false
}
return this.regexp
}
minimatch.match = function (list, pattern, options) {
options = options || {}
var mm = new Minimatch(pattern, options)
list = list.filter(function (f) {
return mm.match(f)
})
if (mm.options.nonull && !list.length) {
list.push(pattern)
}
return list
}
Minimatch.prototype.match = match
function match (f, partial) {
this.debug('match', f, this.pattern)
// short-circuit in the case of busted things.
// comments, etc.
if (this.comment) return false
if (this.empty) return f === ''
if (f === '/' && partial) return true
var options = this.options
// windows: need to use /, not \
if (path.sep !== '/') {
f = f.split(path.sep).join('/')
}
// treat the test path as a set of pathparts.
f = f.split(slashSplit)
this.debug(this.pattern, 'split', f)
// just ONE of the pattern sets in this.set needs to match
// in order for it to be valid. If negating, then just one
// match means that we have failed.
// Either way, return on the first hit.
var set = this.set
this.debug(this.pattern, 'set', set)
// Find the basename of the path by looking for the last non-empty segment
var filename
var i
for (i = f.length - 1; i >= 0; i--) {
filename = f[i]
if (filename) break
}
for (i = 0; i < set.length; i++) {
var pattern = set[i]
var file = f
if (options.matchBase && pattern.length === 1) {
file = [filename]
}
var hit = this.matchOne(file, pattern, partial)
if (hit) {
if (options.flipNegate) return true
return !this.negate
}
}
// didn't get any hits. this is success if it's a negative
// pattern, failure otherwise.
if (options.flipNegate) return false
return this.negate
}
// set partial to true to test if, for example,
// "/a/b" matches the start of "/*/b/*/d"
// Partial means, if you run out of file before you run
// out of pattern, then that's fine, as long as all
// the parts match.
Minimatch.prototype.matchOne = function (file, pattern, partial) {
var options = this.options
this.debug('matchOne',
{ 'this': this, file: file, pattern: pattern })
this.debug('matchOne', file.length, pattern.length)
for (var fi = 0,
pi = 0,
fl = file.length,
pl = pattern.length
; (fi < fl) && (pi < pl)
; fi++, pi++) {
this.debug('matchOne loop')
var p = pattern[pi]
var f = file[fi]
this.debug(pattern, p, f)
// should be impossible.
// some invalid regexp stuff in the set.
if (p === false) return false
if (p === GLOBSTAR) {
this.debug('GLOBSTAR', [pattern, p, f])
// "**"
// a/**/b/**/c would match the following:
// a/b/x/y/z/c
// a/x/y/z/b/c
// a/b/x/b/x/c
// a/b/c
// To do this, take the rest of the pattern after
// the **, and see if it would match the file remainder.
// If so, return success.
// If not, the ** "swallows" a segment, and try again.
// This is recursively awful.
//
// a/**/b/**/c matching a/b/x/y/z/c
// - a matches a
// - doublestar
// - matchOne(b/x/y/z/c, b/**/c)
// - b matches b
// - doublestar
// - matchOne(x/y/z/c, c) -> no
// - matchOne(y/z/c, c) -> no
// - matchOne(z/c, c) -> no
// - matchOne(c, c) yes, hit
var fr = fi
var pr = pi + 1
if (pr === pl) {
this.debug('** at the end')
// a ** at the end will just swallow the rest.
// We have found a match.
// however, it will not swallow /.x, unless
// options.dot is set.
// . and .. are *never* matched by **, for explosively
// exponential reasons.
for (; fi < fl; fi++) {
if (file[fi] === '.' || file[fi] === '..' ||
(!options.dot && file[fi].charAt(0) === '.')) return false
}
return true
}
// ok, let's see if we can swallow whatever we can.
while (fr < fl) {
var swallowee = file[fr]
this.debug('\nglobstar while', file, fr, pattern, pr, swallowee)
// XXX remove this slice. Just pass the start index.
if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
this.debug('globstar found match!', fr, fl, swallowee)
// found a match.
return true
} else {
// can't swallow "." or ".." ever.
// can only swallow ".foo" when explicitly asked.
if (swallowee === '.' || swallowee === '..' ||
(!options.dot && swallowee.charAt(0) === '.')) {
this.debug('dot detected!', file, fr, pattern, pr)
break
}
// ** swallows a segment, and continue.
this.debug('globstar swallow a segment, and continue')
fr++
}
}
// no match was found.
// However, in partial mode, we can't say this is necessarily over.
// If there's more *pattern* left, then
if (partial) {
// ran out of file
this.debug('\n>>> no match, partial?', file, fr, pattern, pr)
if (fr === fl) return true
}
return false
}
// something other than **
// non-magic patterns just have to match exactly
// patterns with magic have been turned into regexps.
var hit
if (typeof p === 'string') {
if (options.nocase) {
hit = f.toLowerCase() === p.toLowerCase()
} else {
hit = f === p
}
this.debug('string match', p, f, hit)
} else {
hit = f.match(p)
this.debug('pattern match', p, f, hit)
}
if (!hit) return false
}
// Note: ending in / means that we'll get a final ""
// at the end of the pattern. This can only match a
// corresponding "" at the end of the file.
// If the file ends in /, then it can only match a
// a pattern that ends in /, unless the pattern just
// doesn't have any more for it. But, a/b/ should *not*
// match "a/b/*", even though "" matches against the
// [^/]*? pattern, except in partial mode, where it might
// simply not be reached yet.
// However, a/b/ should still satisfy a/*
// now either we fell off the end of the pattern, or we're done.
if (fi === fl && pi === pl) {
// ran out of pattern and filename at the same time.
// an exact hit!
return true
} else if (fi === fl) {
// ran out of file, but still had pattern left.
// this is ok if we're doing the match as part of
// a glob fs traversal.
return partial
} else if (pi === pl) {
// ran out of pattern, still have file left.
// this is only acceptable if we're on the very last
// empty segment of a file with a trailing slash.
// a/* should match a/b/
var emptyFileEnd = (fi === fl - 1) && (file[fi] === '')
return emptyFileEnd
}
// should be unreachable.
throw new Error('wtf?')
}
// replace stuff like \* with *
function globUnescape (s) {
return s.replace(/\\(.)/g, '$1')
}
function regExpEscape (s) {
return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
}
},{"brace-expansion":11,"path":22}],21:[function(require,module,exports){
var wrappy = require('wrappy')
module.exports = wrappy(once)
module.exports.strict = wrappy(onceStrict)
once.proto = once(function () {
Object.defineProperty(Function.prototype, 'once', {
value: function () {
return once(this)
},
configurable: true
})
Object.defineProperty(Function.prototype, 'onceStrict', {
value: function () {
return onceStrict(this)
},
configurable: true
})
})
function once (fn) {
var f = function () {
if (f.called) return f.value
f.called = true
return f.value = fn.apply(this, arguments)
}
f.called = false
return f
}
function onceStrict (fn) {
var f = function () {
if (f.called)
throw new Error(f.onceError)
f.called = true
return f.value = fn.apply(this, arguments)
}
var name = fn.name || 'Function wrapped with `once`'
f.onceError = name + " shouldn't be called more than once"
f.called = false
return f
}
},{"wrappy":29}],22:[function(require,module,exports){
(function (process){
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
// resolves . and .. elements in a path array with directory names there
// must be no slashes, empty elements, or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts, allowAboveRoot) {
// if the path tries to go above the root, `up` ends up > 0
var up = 0;
for (var i = parts.length - 1; i >= 0; i--) {
var last = parts[i];
if (last === '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
// if the path is allowed to go above the root, restore leading ..s
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
var splitPathRe =
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
var splitPath = function(filename) {
return splitPathRe.exec(filename).slice(1);
};
// path.resolve([from ...], to)
// posix version
exports.resolve = function() {
var resolvedPath = '',
resolvedAbsolute = false;
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
var path = (i >= 0) ? arguments[i] : process.cwd();
// Skip empty and invalid entries
if (typeof path !== 'string') {
throw new TypeError('Arguments to path.resolve must be strings');
} else if (!path) {
continue;
}
resolvedPath = path + '/' + resolvedPath;
resolvedAbsolute = path.charAt(0) === '/';
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {
return !!p;
}), !resolvedAbsolute).join('/');
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
};
// path.normalize(path)
// posix version
exports.normalize = function(path) {
var isAbsolute = exports.isAbsolute(path),
trailingSlash = substr(path, -1) === '/';
// Normalize the path
path = normalizeArray(filter(path.split('/'), function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
}
if (path && trailingSlash) {
path += '/';
}
return (isAbsolute ? '/' : '') + path;
};
// posix version
exports.isAbsolute = function(path) {
return path.charAt(0) === '/';
};
// posix version
exports.join = function() {
var paths = Array.prototype.slice.call(arguments, 0);
return exports.normalize(filter(paths, function(p, index) {
if (typeof p !== 'string') {
throw new TypeError('Arguments to path.join must be strings');
}
return p;
}).join('/'));
};
// path.relative(from, to)
// posix version
exports.relative = function(from, to) {
from = exports.resolve(from).substr(1);
to = exports.resolve(to).substr(1);
function trim(arr) {
var start = 0;
for (; start < arr.length; start++) {
if (arr[start] !== '') break;
}
var end = arr.length - 1;
for (; end >= 0; end--) {
if (arr[end] !== '') break;
}
if (start > end) return [];
return arr.slice(start, end - start + 1);
}
var fromParts = trim(from.split('/'));
var toParts = trim(to.split('/'));
var length = Math.min(fromParts.length, toParts.length);
var samePartsLength = length;
for (var i = 0; i < length; i++) {
if (fromParts[i] !== toParts[i]) {
samePartsLength = i;
break;
}
}
var outputParts = [];
for (var i = samePartsLength; i < fromParts.length; i++) {
outputParts.push('..');
}
outputParts = outputParts.concat(toParts.slice(samePartsLength));
return outputParts.join('/');
};
exports.sep = '/';
exports.delimiter = ':';
exports.dirname = function(path) {
var result = splitPath(path),
root = result[0],
dir = result[1];
if (!root && !dir) {
// No dirname whatsoever
return '.';
}
if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
}
return root + dir;
};
exports.basename = function(path, ext) {
var f = splitPath(path)[2];
// TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
return f;
};
exports.extname = function(path) {
return splitPath(path)[3];
};
function filter (xs, f) {
if (xs.filter) return xs.filter(f);
var res = [];
for (var i = 0; i < xs.length; i++) {
if (f(xs[i], i, xs)) res.push(xs[i]);
}
return res;
}
// String.prototype.substr - negative index don't work in IE8
var substr = 'ab'.substr(-1) === 'b'
? function (str, start, len) { return str.substr(start, len) }
: function (str, start, len) {
if (start < 0) start = str.length + start;
return str.substr(start, len);
}
;
}).call(this,require('_process'))
},{"_process":24}],23:[function(require,module,exports){
(function (process){
'use strict';
function posix(path) {
return path.charAt(0) === '/';
}
function win32(path) {
// https://github.com/nodejs/node/blob/b3fcc245fb25539909ef1d5eaa01dbf92e168633/lib/path.js#L56
var splitDeviceRe = /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/;
var result = splitDeviceRe.exec(path);
var device = result[1] || '';
var isUnc = Boolean(device && device.charAt(1) !== ':');
// UNC paths are always absolute
return Boolean(result[2] || isUnc);
}
module.exports = process.platform === 'win32' ? win32 : posix;
module.exports.posix = posix;
module.exports.win32 = win32;
}).call(this,require('_process'))
},{"_process":24}],24:[function(require,module,exports){
// shim for using process in browser
var process = module.exports = {};
// cached from whatever global is present so that test runners that stub it
// don't break things. But we need to wrap it in a try catch in case it is
// wrapped in strict mode code which doesn't define any globals. It's inside a
// function because try/catches deoptimize in certain engines.
var cachedSetTimeout;
var cachedClearTimeout;
function defaultSetTimout() {
throw new Error('setTimeout has not been defined');
}
function defaultClearTimeout () {
throw new Error('clearTimeout has not been defined');
}
(function () {
try {
if (typeof setTimeout === 'function') {
cachedSetTimeout = setTimeout;
} else {
cachedSetTimeout = defaultSetTimout;
}
} catch (e) {
cachedSetTimeout = defaultSetTimout;
}
try {
if (typeof clearTimeout === 'function') {
cachedClearTimeout = clearTimeout;
} else {
cachedClearTimeout = defaultClearTimeout;
}
} catch (e) {
cachedClearTimeout = defaultClearTimeout;
}
} ())
function runTimeout(fun) {
if (cachedSetTimeout === setTimeout) {
//normal enviroments in sane situations
return setTimeout(fun, 0);
}
// if setTimeout wasn't available but was latter defined
if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
cachedSetTimeout = setTimeout;
return setTimeout(fun, 0);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedSetTimeout(fun, 0);
} catch(e){
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedSetTimeout.call(null, fun, 0);
} catch(e){
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
return cachedSetTimeout.call(this, fun, 0);
}
}
}
function runClearTimeout(marker) {
if (cachedClearTimeout === clearTimeout) {
//normal enviroments in sane situations
return clearTimeout(marker);
}
// if clearTimeout wasn't available but was latter defined
if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
cachedClearTimeout = clearTimeout;
return clearTimeout(marker);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedClearTimeout(marker);
} catch (e){
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedClearTimeout.call(null, marker);
} catch (e){
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
// Some versions of I.E. have different rules for clearTimeout vs setTimeout
return cachedClearTimeout.call(this, marker);
}
}
}
var queue = [];
var draining = false;
var currentQueue;
var queueIndex = -1;
function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
}
draining = false;
if (currentQueue.length) {
queue = currentQueue.concat(queue);
} else {
queueIndex = -1;
}
if (queue.length) {
drainQueue();
}
}
function drainQueue() {
if (draining) {
return;
}
var timeout = runTimeout(cleanUpNextTick);
draining = true;
var len = queue.length;
while(len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
if (currentQueue) {
currentQueue[queueIndex].run();
}
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
runClearTimeout(timeout);
}
process.nextTick = function (fun) {
var args = new Array(arguments.length - 1);
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
runTimeout(drainQueue);
}
};
// v8 likes predictible objects
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
process.version = ''; // empty string to avoid regexp issues
process.versions = {};
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.prependListener = noop;
process.prependOnceListener = noop;
process.listeners = function (name) { return [] }
process.binding = function (name) {
throw new Error('process.binding is not supported');
};
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
process.umask = function() { return 0; };
},{}],25:[function(require,module,exports){
// Underscore.js 1.8.3
// http://underscorejs.org
// (c) 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
(function() {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `exports` on the server.
var root = this;
// Save the previous value of the `_` variable.
var previousUnderscore = root._;
// Save bytes in the minified (but not gzipped) version:
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
// Create quick reference variables for speed access to core prototypes.
var
push = ArrayProto.push,
slice = ArrayProto.slice,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty;
// All **ECMAScript 5** native function implementations that we hope to use
// are declared here.
var
nativeIsArray = Array.isArray,
nativeKeys = Object.keys,
nativeBind = FuncProto.bind,
nativeCreate = Object.create;
// Naked function reference for surrogate-prototype-swapping.
var Ctor = function(){};
// Create a safe reference to the Underscore object for use below.
var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
// Export the Underscore object for **Node.js**, with
// backwards-compatibility for the old `require()` API. If we're in
// the browser, add `_` as a global object.
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}
// Current version.
_.VERSION = '1.8.3';
// Internal function that returns an efficient (for current engines) version
// of the passed-in callback, to be repeatedly applied in other Underscore
// functions.
var optimizeCb = function(func, context, argCount) {
if (context === void 0) return func;
switch (argCount == null ? 3 : argCount) {
case 1: return function(value) {
return func.call(context, value);
};
case 2: return function(value, other) {
return func.call(context, value, other);
};
case 3: return function(value, index, collection) {
return func.call(context, value, index, collection);
};
case 4: return function(accumulator, value, index, collection) {
return func.call(context, accumulator, value, index, collection);
};
}
return function() {
return func.apply(context, arguments);
};
};
// A mostly-internal function to generate callbacks that can be applied
// to each element in a collection, returning the desired result — either
// identity, an arbitrary callback, a property matcher, or a property accessor.
var cb = function(value, context, argCount) {
if (value == null) return _.identity;
if (_.isFunction(value)) return optimizeCb(value, context, argCount);
if (_.isObject(value)) return _.matcher(value);
return _.property(value);
};
_.iteratee = function(value, context) {
return cb(value, context, Infinity);
};
// An internal function for creating assigner functions.
var createAssigner = function(keysFunc, undefinedOnly) {
return function(obj) {
var length = arguments.length;
if (length < 2 || obj == null) return obj;
for (var index = 1; index < length; index++) {
var source = arguments[index],
keys = keysFunc(source),
l = keys.length;
for (var i = 0; i < l; i++) {
var key = keys[i];
if (!undefinedOnly || obj[key] === void 0) obj[key] = source[key];
}
}
return obj;
};
};
// An internal function for creating a new object that inherits from another.
var baseCreate = function(prototype) {
if (!_.isObject(prototype)) return {};
if (nativeCreate) return nativeCreate(prototype);
Ctor.prototype = prototype;
var result = new Ctor;
Ctor.prototype = null;
return result;
};
var property = function(key) {
return function(obj) {
return obj == null ? void 0 : obj[key];
};
};
// Helper for collection methods to determine whether a collection
// should be iterated as an array or as an object
// Related: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
// Avoids a very nasty iOS 8 JIT bug on ARM-64. #2094
var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
var getLength = property('length');
var isArrayLike = function(collection) {
var length = getLength(collection);
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
};
// Collection Functions
// --------------------
// The cornerstone, an `each` implementation, aka `forEach`.
// Handles raw objects in addition to array-likes. Treats all
// sparse array-likes as if they were dense.
_.each = _.forEach = function(obj, iteratee, context) {
iteratee = optimizeCb(iteratee, context);
var i, length;
if (isArrayLike(obj)) {
for (i = 0, length = obj.length; i < length; i++) {
iteratee(obj[i], i, obj);
}
} else {
var keys = _.keys(obj);
for (i = 0, length = keys.length; i < length; i++) {
iteratee(obj[keys[i]], keys[i], obj);
}
}
return obj;
};
// Return the results of applying the iteratee to each element.
_.map = _.collect = function(obj, iteratee, context) {
iteratee = cb(iteratee, context);
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length,
results = Array(length);
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
results[index] = iteratee(obj[currentKey], currentKey, obj);
}
return results;
};
// Create a reducing function iterating left or right.
function createReduce(dir) {
// Optimized iterator function as using arguments.length
// in the main function will deoptimize the, see #1991.
function iterator(obj, iteratee, memo, keys, index, length) {
for (; index >= 0 && index < length; index += dir) {
var currentKey = keys ? keys[index] : index;
memo = iteratee(memo, obj[currentKey], currentKey, obj);
}
return memo;
}
return function(obj, iteratee, memo, context) {
iteratee = optimizeCb(iteratee, context, 4);
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length,
index = dir > 0 ? 0 : length - 1;
// Determine the initial value if none is provided.
if (arguments.length < 3) {
memo = obj[keys ? keys[index] : index];
index += dir;
}
return iterator(obj, iteratee, memo, keys, index, length);
};
}
// **Reduce** builds up a single result from a list of values, aka `inject`,
// or `foldl`.
_.reduce = _.foldl = _.inject = createReduce(1);
// The right-associative version of reduce, also known as `foldr`.
_.reduceRight = _.foldr = createReduce(-1);
// Return the first value which passes a truth test. Aliased as `detect`.
_.find = _.detect = function(obj, predicate, context) {
var key;
if (isArrayLike(obj)) {
key = _.findIndex(obj, predicate, context);
} else {
key = _.findKey(obj, predicate, context);
}
if (key !== void 0 && key !== -1) return obj[key];
};
// Return all the elements that pass a truth test.
// Aliased as `select`.
_.filter = _.select = function(obj, predicate, context) {
var results = [];
predicate = cb(predicate, context);
_.each(obj, function(value, index, list) {
if (predicate(value, index, list)) results.push(value);
});
return results;
};
// Return all the elements for which a truth test fails.
_.reject = function(obj, predicate, context) {
return _.filter(obj, _.negate(cb(predicate)), context);
};
// Determine whether all of the elements match a truth test.
// Aliased as `all`.
_.every = _.all = function(obj, predicate, context) {
predicate = cb(predicate, context);
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length;
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
if (!predicate(obj[currentKey], currentKey, obj)) return false;
}
return true;
};
// Determine if at least one element in the object matches a truth test.
// Aliased as `any`.
_.some = _.any = function(obj, predicate, context) {
predicate = cb(predicate, context);
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length;
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
if (predicate(obj[currentKey], currentKey, obj)) return true;
}
return false;
};
// Determine if the array or object contains a given item (using `===`).
// Aliased as `includes` and `include`.
_.contains = _.includes = _.include = function(obj, item, fromIndex, guard) {
if (!isArrayLike(obj)) obj = _.values(obj);
if (typeof fromIndex != 'number' || guard) fromIndex = 0;
return _.indexOf(obj, item, fromIndex) >= 0;
};
// Invoke a method (with arguments) on every item in a collection.
_.invoke = function(obj, method) {
var args = slice.call(arguments, 2);
var isFunc = _.isFunction(method);
return _.map(obj, function(value) {
var func = isFunc ? method : value[method];
return func == null ? func : func.apply(value, args);
});
};
// Convenience version of a common use case of `map`: fetching a property.
_.pluck = function(obj, key) {
return _.map(obj, _.property(key));
};
// Convenience version of a common use case of `filter`: selecting only objects
// containing specific `key:value` pairs.
_.where = function(obj, attrs) {
return _.filter(obj, _.matcher(attrs));
};
// Convenience version of a common use case of `find`: getting the first object
// containing specific `key:value` pairs.
_.findWhere = function(obj, attrs) {
return _.find(obj, _.matcher(attrs));
};
// Return the maximum element (or element-based computation).
_.max = function(obj, iteratee, context) {
var result = -Infinity, lastComputed = -Infinity,
value, computed;
if (iteratee == null && obj != null) {
obj = isArrayLike(obj) ? obj : _.values(obj);
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value > result) {
result = value;
}
}
} else {
iteratee = cb(iteratee, context);
_.each(obj, function(value, index, list) {
computed = iteratee(value, index, list);
if (computed > lastComputed || computed === -Infinity && result === -Infinity) {
result = value;
lastComputed = computed;
}
});
}
return result;
};
// Return the minimum element (or element-based computation).
_.min = function(obj, iteratee, context) {
var result = Infinity, lastComputed = Infinity,
value, computed;
if (iteratee == null && obj != null) {
obj = isArrayLike(obj) ? obj : _.values(obj);
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value < result) {
result = value;
}
}
} else {
iteratee = cb(iteratee, context);
_.each(obj, function(value, index, list) {
computed = iteratee(value, index, list);
if (computed < lastComputed || computed === Infinity && result === Infinity) {
result = value;
lastComputed = computed;
}
});
}
return result;
};
// Shuffle a collection, using the modern version of the
// [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
_.shuffle = function(obj) {
var set = isArrayLike(obj) ? obj : _.values(obj);
var length = set.length;
var shuffled = Array(length);
for (var index = 0, rand; index < length; index++) {
rand = _.random(0, index);
if (rand !== index) shuffled[index] = shuffled[rand];
shuffled[rand] = set[index];
}
return shuffled;
};
// Sample **n** random values from a collection.
// If **n** is not specified, returns a single random element.
// The internal `guard` argument allows it to work with `map`.
_.sample = function(obj, n, guard) {
if (n == null || guard) {
if (!isArrayLike(obj)) obj = _.values(obj);
return obj[_.random(obj.length - 1)];
}
return _.shuffle(obj).slice(0, Math.max(0, n));
};
// Sort the object's values by a criterion produced by an iteratee.
_.sortBy = function(obj, iteratee, context) {
iteratee = cb(iteratee, context);
return _.pluck(_.map(obj, function(value, index, list) {
return {
value: value,
index: index,
criteria: iteratee(value, index, list)
};
}).sort(function(left, right) {
var a = left.criteria;
var b = right.criteria;
if (a !== b) {
if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1;
}
return left.index - right.index;
}), 'value');
};
// An internal function used for aggregate "group by" operations.
var group = function(behavior) {
return function(obj, iteratee, context) {
var result = {};
iteratee = cb(iteratee, context);
_.each(obj, function(value, index) {
var key = iteratee(value, index, obj);
behavior(result, value, key);
});
return result;
};
};
// Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion.
_.groupBy = group(function(result, value, key) {
if (_.has(result, key)) result[key].push(value); else result[key] = [value];
});
// Indexes the object's values by a criterion, similar to `groupBy`, but for
// when you know that your index values will be unique.
_.indexBy = group(function(result, value, key) {
result[key] = value;
});
// Counts instances of an object that group by a certain criterion. Pass
// either a string attribute to count by, or a function that returns the
// criterion.
_.countBy = group(function(result, value, key) {
if (_.has(result, key)) result[key]++; else result[key] = 1;
});
// Safely create a real, live array from anything iterable.
_.toArray = function(obj) {
if (!obj) return [];
if (_.isArray(obj)) return slice.call(obj);
if (isArrayLike(obj)) return _.map(obj, _.identity);
return _.values(obj);
};
// Return the number of elements in an object.
_.size = function(obj) {
if (obj == null) return 0;
return isArrayLike(obj) ? obj.length : _.keys(obj).length;
};
// Split a collection into two arrays: one whose elements all satisfy the given
// predicate, and one whose elements all do not satisfy the predicate.
_.partition = function(obj, predicate, context) {
predicate = cb(predicate, context);
var pass = [], fail = [];
_.each(obj, function(value, key, obj) {
(predicate(value, key, obj) ? pass : fail).push(value);
});
return [pass, fail];
};
// Array Functions
// ---------------
// Get the first element of an array. Passing **n** will return the first N
// values in the array. Aliased as `head` and `take`. The **guard** check
// allows it to work with `_.map`.
_.first = _.head = _.take = function(array, n, guard) {
if (array == null) return void 0;
if (n == null || guard) return array[0];
return _.initial(array, array.length - n);
};
// Returns everything but the last entry of the array. Especially useful on
// the arguments object. Passing **n** will return all the values in
// the array, excluding the last N.
_.initial = function(array, n, guard) {
return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n)));
};
// Get the last element of an array. Passing **n** will return the last N
// values in the array.
_.last = function(array, n, guard) {
if (array == null) return void 0;
if (n == null || guard) return array[array.length - 1];
return _.rest(array, Math.max(0, array.length - n));
};
// Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
// Especially useful on the arguments object. Passing an **n** will return
// the rest N values in the array.
_.rest = _.tail = _.drop = function(array, n, guard) {
return slice.call(array, n == null || guard ? 1 : n);
};
// Trim out all falsy values from an array.
_.compact = function(array) {
return _.filter(array, _.identity);
};
// Internal implementation of a recursive `flatten` function.
var flatten = function(input, shallow, strict, startIndex) {
var output = [], idx = 0;
for (var i = startIndex || 0, length = getLength(input); i < length; i++) {
var value = input[i];
if (isArrayLike(value) && (_.isArray(value) || _.isArguments(value))) {
//flatten current level of array or arguments object
if (!shallow) value = flatten(value, shallow, strict);
var j = 0, len = value.length;
output.length += len;
while (j < len) {
output[idx++] = value[j++];
}
} else if (!strict) {
output[idx++] = value;
}
}
return output;
};
// Flatten out an array, either recursively (by default), or just one level.
_.flatten = function(array, shallow) {
return flatten(array, shallow, false);
};
// Return a version of the array that does not contain the specified value(s).
_.without = function(array) {
return _.difference(array, slice.call(arguments, 1));
};
// Produce a duplicate-free version of the array. If the array has already
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
_.uniq = _.unique = function(array, isSorted, iteratee, context) {
if (!_.isBoolean(isSorted)) {
context = iteratee;
iteratee = isSorted;
isSorted = false;
}
if (iteratee != null) iteratee = cb(iteratee, context);
var result = [];
var seen = [];
for (var i = 0, length = getLength(array); i < length; i++) {
var value = array[i],
computed = iteratee ? iteratee(value, i, array) : value;
if (isSorted) {
if (!i || seen !== computed) result.push(value);
seen = computed;
} else if (iteratee) {
if (!_.contains(seen, computed)) {
seen.push(computed);
result.push(value);
}
} else if (!_.contains(result, value)) {
result.push(value);
}
}
return result;
};
// Produce an array that contains the union: each distinct element from all of
// the passed-in arrays.
_.union = function() {
return _.uniq(flatten(arguments, true, true));
};
// Produce an array that contains every item shared between all the
// passed-in arrays.
_.intersection = function(array) {
var result = [];
var argsLength = arguments.length;
for (var i = 0, length = getLength(array); i < length; i++) {
var item = array[i];
if (_.contains(result, item)) continue;
for (var j = 1; j < argsLength; j++) {
if (!_.contains(arguments[j], item)) break;
}
if (j === argsLength) result.push(item);
}
return result;
};
// Take the difference between one array and a number of other arrays.
// Only the elements present in just the first array will remain.
_.difference = function(array) {
var rest = flatten(arguments, true, true, 1);
return _.filter(array, function(value){
return !_.contains(rest, value);
});
};
// Zip together multiple lists into a single array -- elements that share
// an index go together.
_.zip = function() {
return _.unzip(arguments);
};
// Complement of _.zip. Unzip accepts an array of arrays and groups
// each array's elements on shared indices
_.unzip = function(array) {
var length = array && _.max(array, getLength).length || 0;
var result = Array(length);
for (var index = 0; index < length; index++) {
result[index] = _.pluck(array, index);
}
return result;
};
// Converts lists into objects. Pass either a single array of `[key, value]`
// pairs, or two parallel arrays of the same length -- one of keys, and one of
// the corresponding values.
_.object = function(list, values) {
var result = {};
for (var i = 0, length = getLength(list); i < length; i++) {
if (values) {
result[list[i]] = values[i];
} else {
result[list[i][0]] = list[i][1];
}
}
return result;
};
// Generator function to create the findIndex and findLastIndex functions
function createPredicateIndexFinder(dir) {
return function(array, predicate, context) {
predicate = cb(predicate, context);
var length = getLength(array);
var index = dir > 0 ? 0 : length - 1;
for (; index >= 0 && index < length; index += dir) {
if (predicate(array[index], index, array)) return index;
}
return -1;
};
}
// Returns the first index on an array-like that passes a predicate test
_.findIndex = createPredicateIndexFinder(1);
_.findLastIndex = createPredicateIndexFinder(-1);
// Use a comparator function to figure out the smallest index at which
// an object should be inserted so as to maintain order. Uses binary search.
_.sortedIndex = function(array, obj, iteratee, context) {
iteratee = cb(iteratee, context, 1);
var value = iteratee(obj);
var low = 0, high = getLength(array);
while (low < high) {
var mid = Math.floor((low + high) / 2);
if (iteratee(array[mid]) < value) low = mid + 1; else high = mid;
}
return low;
};
// Generator function to create the indexOf and lastIndexOf functions
function createIndexFinder(dir, predicateFind, sortedIndex) {
return function(array, item, idx) {
var i = 0, length = getLength(array);
if (typeof idx == 'number') {
if (dir > 0) {
i = idx >= 0 ? idx : Math.max(idx + length, i);
} else {
length = idx >= 0 ? Math.min(idx + 1, length) : idx + length + 1;
}
} else if (sortedIndex && idx && length) {
idx = sortedIndex(array, item);
return array[idx] === item ? idx : -1;
}
if (item !== item) {
idx = predicateFind(slice.call(array, i, length), _.isNaN);
return idx >= 0 ? idx + i : -1;
}
for (idx = dir > 0 ? i : length - 1; idx >= 0 && idx < length; idx += dir) {
if (array[idx] === item) return idx;
}
return -1;
};
}
// Return the position of the first occurrence of an item in an array,
// or -1 if the item is not included in the array.
// If the array is large and already in sort order, pass `true`
// for **isSorted** to use binary search.
_.indexOf = createIndexFinder(1, _.findIndex, _.sortedIndex);
_.lastIndexOf = createIndexFinder(-1, _.findLastIndex);
// Generate an integer Array containing an arithmetic progression. A port of
// the native Python `range()` function. See
// [the Python documentation](http://docs.python.org/library/functions.html#range).
_.range = function(start, stop, step) {
if (stop == null) {
stop = start || 0;
start = 0;
}
step = step || 1;
var length = Math.max(Math.ceil((stop - start) / step), 0);
var range = Array(length);
for (var idx = 0; idx < length; idx++, start += step) {
range[idx] = start;
}
return range;
};
// Function (ahem) Functions
// ------------------
// Determines whether to execute a function as a constructor
// or a normal function with the provided arguments
var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) {
if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args);
var self = baseCreate(sourceFunc.prototype);
var result = sourceFunc.apply(self, args);
if (_.isObject(result)) return result;
return self;
};
// Create a function bound to a given object (assigning `this`, and arguments,
// optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if
// available.
_.bind = function(func, context) {
if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
if (!_.isFunction(func)) throw new TypeError('Bind must be called on a function');
var args = slice.call(arguments, 2);
var bound = function() {
return executeBound(func, bound, context, this, args.concat(slice.call(arguments)));
};
return bound;
};
// Partially apply a function by creating a version that has had some of its
// arguments pre-filled, without changing its dynamic `this` context. _ acts
// as a placeholder, allowing any combination of arguments to be pre-filled.
_.partial = function(func) {
var boundArgs = slice.call(arguments, 1);
var bound = function() {
var position = 0, length = boundArgs.length;
var args = Array(length);
for (var i = 0; i < length; i++) {
args[i] = boundArgs[i] === _ ? arguments[position++] : boundArgs[i];
}
while (position < arguments.length) args.push(arguments[position++]);
return executeBound(func, bound, this, this, args);
};
return bound;
};
// Bind a number of an object's methods to that object. Remaining arguments
// are the method names to be bound. Useful for ensuring that all callbacks
// defined on an object belong to it.
_.bindAll = function(obj) {
var i, length = arguments.length, key;
if (length <= 1) throw new Error('bindAll must be passed function names');
for (i = 1; i < length; i++) {
key = arguments[i];
obj[key] = _.bind(obj[key], obj);
}
return obj;
};
// Memoize an expensive function by storing its results.
_.memoize = function(func, hasher) {
var memoize = function(key) {
var cache = memoize.cache;
var address = '' + (hasher ? hasher.apply(this, arguments) : key);
if (!_.has(cache, address)) cache[address] = func.apply(this, arguments);
return cache[address];
};
memoize.cache = {};
return memoize;
};
// Delays a function for the given number of milliseconds, and then calls
// it with the arguments supplied.
_.delay = function(func, wait) {
var args = slice.call(arguments, 2);
return setTimeout(function(){
return func.apply(null, args);
}, wait);
};
// Defers a function, scheduling it to run after the current call stack has
// cleared.
_.defer = _.partial(_.delay, _, 1);
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time. Normally, the throttled function will run
// as much as it can, without ever going more than once per `wait` duration;
// but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto.
_.throttle = function(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : _.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function() {
var now = _.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var last = _.now() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function() {
context = this;
args = arguments;
timestamp = _.now();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
// Returns the first function passed as an argument to the second,
// allowing you to adjust arguments, run code before and after, and
// conditionally execute the original function.
_.wrap = function(func, wrapper) {
return _.partial(wrapper, func);
};
// Returns a negated version of the passed-in predicate.
_.negate = function(predicate) {
return function() {
return !predicate.apply(this, arguments);
};
};
// Returns a function that is the composition of a list of functions, each
// consuming the return value of the function that follows.
_.compose = function() {
var args = arguments;
var start = args.length - 1;
return function() {
var i = start;
var result = args[start].apply(this, arguments);
while (i--) result = args[i].call(this, result);
return result;
};
};
// Returns a function that will only be executed on and after the Nth call.
_.after = function(times, func) {
return function() {
if (--times < 1) {
return func.apply(this, arguments);
}
};
};
// Returns a function that will only be executed up to (but not including) the Nth call.
_.before = function(times, func) {
var memo;
return function() {
if (--times > 0) {
memo = func.apply(this, arguments);
}
if (times <= 1) func = null;
return memo;
};
};
// Returns a function that will be executed at most one time, no matter how
// often you call it. Useful for lazy initialization.
_.once = _.partial(_.before, 2);
// Object Functions
// ----------------
// Keys in IE < 9 that won't be iterated by `for key in ...` and thus missed.
var hasEnumBug = !{toString: null}.propertyIsEnumerable('toString');
var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
function collectNonEnumProps(obj, keys) {
var nonEnumIdx = nonEnumerableProps.length;
var constructor = obj.constructor;
var proto = (_.isFunction(constructor) && constructor.prototype) || ObjProto;
// Constructor is a special case.
var prop = 'constructor';
if (_.has(obj, prop) && !_.contains(keys, prop)) keys.push(prop);
while (nonEnumIdx--) {
prop = nonEnumerableProps[nonEnumIdx];
if (prop in obj && obj[prop] !== proto[prop] && !_.contains(keys, prop)) {
keys.push(prop);
}
}
}
// Retrieve the names of an object's own properties.
// Delegates to **ECMAScript 5**'s native `Object.keys`
_.keys = function(obj) {
if (!_.isObject(obj)) return [];
if (nativeKeys) return nativeKeys(obj);
var keys = [];
for (var key in obj) if (_.has(obj, key)) keys.push(key);
// Ahem, IE < 9.
if (hasEnumBug) collectNonEnumProps(obj, keys);
return keys;
};
// Retrieve all the property names of an object.
_.allKeys = function(obj) {
if (!_.isObject(obj)) return [];
var keys = [];
for (var key in obj) keys.push(key);
// Ahem, IE < 9.
if (hasEnumBug) collectNonEnumProps(obj, keys);
return keys;
};
// Retrieve the values of an object's properties.
_.values = function(obj) {
var keys = _.keys(obj);
var length = keys.length;
var values = Array(length);
for (var i = 0; i < length; i++) {
values[i] = obj[keys[i]];
}
return values;
};
// Returns the results of applying the iteratee to each element of the object
// In contrast to _.map it returns an object
_.mapObject = function(obj, iteratee, context) {
iteratee = cb(iteratee, context);
var keys = _.keys(obj),
length = keys.length,
results = {},
currentKey;
for (var index = 0; index < length; index++) {
currentKey = keys[index];
results[currentKey] = iteratee(obj[currentKey], currentKey, obj);
}
return results;
};
// Convert an object into a list of `[key, value]` pairs.
_.pairs = function(obj) {
var keys = _.keys(obj);
var length = keys.length;
var pairs = Array(length);
for (var i = 0; i < length; i++) {
pairs[i] = [keys[i], obj[keys[i]]];
}
return pairs;
};
// Invert the keys and values of an object. The values must be serializable.
_.invert = function(obj) {
var result = {};
var keys = _.keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
result[obj[keys[i]]] = keys[i];
}
return result;
};
// Return a sorted list of the function names available on the object.
// Aliased as `methods`
_.functions = _.methods = function(obj) {
var names = [];
for (var key in obj) {
if (_.isFunction(obj[key])) names.push(key);
}
return names.sort();
};
// Extend a given object with all the properties in passed-in object(s).
_.extend = createAssigner(_.allKeys);
// Assigns a given object with all the own properties in the passed-in object(s)
// (https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
_.extendOwn = _.assign = createAssigner(_.keys);
// Returns the first key on an object that passes a predicate test
_.findKey = function(obj, predicate, context) {
predicate = cb(predicate, context);
var keys = _.keys(obj), key;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (predicate(obj[key], key, obj)) return key;
}
};
// Return a copy of the object only containing the whitelisted properties.
_.pick = function(object, oiteratee, context) {
var result = {}, obj = object, iteratee, keys;
if (obj == null) return result;
if (_.isFunction(oiteratee)) {
keys = _.allKeys(obj);
iteratee = optimizeCb(oiteratee, context);
} else {
keys = flatten(arguments, false, false, 1);
iteratee = function(value, key, obj) { return key in obj; };
obj = Object(obj);
}
for (var i = 0, length = keys.length; i < length; i++) {
var key = keys[i];
var value = obj[key];
if (iteratee(value, key, obj)) result[key] = value;
}
return result;
};
// Return a copy of the object without the blacklisted properties.
_.omit = function(obj, iteratee, context) {
if (_.isFunction(iteratee)) {
iteratee = _.negate(iteratee);
} else {
var keys = _.map(flatten(arguments, false, false, 1), String);
iteratee = function(value, key) {
return !_.contains(keys, key);
};
}
return _.pick(obj, iteratee, context);
};
// Fill in a given object with default properties.
_.defaults = createAssigner(_.allKeys, true);
// Creates an object that inherits from the given prototype object.
// If additional properties are provided then they will be added to the
// created object.
_.create = function(prototype, props) {
var result = baseCreate(prototype);
if (props) _.extendOwn(result, props);
return result;
};
// Create a (shallow-cloned) duplicate of an object.
_.clone = function(obj) {
if (!_.isObject(obj)) return obj;
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
};
// Invokes interceptor with the obj, and then returns obj.
// The primary purpose of this method is to "tap into" a method chain, in
// order to perform operations on intermediate results within the chain.
_.tap = function(obj, interceptor) {
interceptor(obj);
return obj;
};
// Returns whether an object has a given set of `key:value` pairs.
_.isMatch = function(object, attrs) {
var keys = _.keys(attrs), length = keys.length;
if (object == null) return !length;
var obj = Object(object);
for (var i = 0; i < length; i++) {
var key = keys[i];
if (attrs[key] !== obj[key] || !(key in obj)) return false;
}
return true;
};
// Internal recursive comparison function for `isEqual`.
var eq = function(a, b, aStack, bStack) {
// Identical objects are equal. `0 === -0`, but they aren't identical.
// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
if (a === b) return a !== 0 || 1 / a === 1 / b;
// A strict comparison is necessary because `null == undefined`.
if (a == null || b == null) return a === b;
// Unwrap any wrapped objects.
if (a instanceof _) a = a._wrapped;
if (b instanceof _) b = b._wrapped;
// Compare `[[Class]]` names.
var className = toString.call(a);
if (className !== toString.call(b)) return false;
switch (className) {
// Strings, numbers, regular expressions, dates, and booleans are compared by value.
case '[object RegExp]':
// RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i')
case '[object String]':
// Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
// equivalent to `new String("5")`.
return '' + a === '' + b;
case '[object Number]':
// `NaN`s are equivalent, but non-reflexive.
// Object(NaN) is equivalent to NaN
if (+a !== +a) return +b !== +b;
// An `egal` comparison is performed for other numeric values.
return +a === 0 ? 1 / +a === 1 / b : +a === +b;
case '[object Date]':
case '[object Boolean]':
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
// millisecond representations. Note that invalid dates with millisecond representations
// of `NaN` are not equivalent.
return +a === +b;
}
var areArrays = className === '[object Array]';
if (!areArrays) {
if (typeof a != 'object' || typeof b != 'object') return false;
// Objects with different constructors are not equivalent, but `Object`s or `Array`s
// from different frames are.
var aCtor = a.constructor, bCtor = b.constructor;
if (aCtor !== bCtor && !(_.isFunction(aCtor) && aCtor instanceof aCtor &&
_.isFunction(bCtor) && bCtor instanceof bCtor)
&& ('constructor' in a && 'constructor' in b)) {
return false;
}
}
// Assume equality for cyclic structures. The algorithm for detecting cyclic
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
// Initializing stack of traversed objects.
// It's done here since we only need them for objects and arrays comparison.
aStack = aStack || [];
bStack = bStack || [];
var length = aStack.length;
while (length--) {
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
if (aStack[length] === a) return bStack[length] === b;
}
// Add the first object to the stack of traversed objects.
aStack.push(a);
bStack.push(b);
// Recursively compare objects and arrays.
if (areArrays) {
// Compare array lengths to determine if a deep comparison is necessary.
length = a.length;
if (length !== b.length) return false;
// Deep compare the contents, ignoring non-numeric properties.
while (length--) {
if (!eq(a[length], b[length], aStack, bStack)) return false;
}
} else {
// Deep compare objects.
var keys = _.keys(a), key;
length = keys.length;
// Ensure that both objects contain the same number of properties before comparing deep equality.
if (_.keys(b).length !== length) return false;
while (length--) {
// Deep compare each member
key = keys[length];
if (!(_.has(b, key) && eq(a[key], b[key], aStack, bStack))) return false;
}
}
// Remove the first object from the stack of traversed objects.
aStack.pop();
bStack.pop();
return true;
};
// Perform a deep comparison to check if two objects are equal.
_.isEqual = function(a, b) {
return eq(a, b);
};
// Is a given array, string, or object empty?
// An "empty" object has no enumerable own-properties.
_.isEmpty = function(obj) {
if (obj == null) return true;
if (isArrayLike(obj) && (_.isArray(obj) || _.isString(obj) || _.isArguments(obj))) return obj.length === 0;
return _.keys(obj).length === 0;
};
// Is a given value a DOM element?
_.isElement = function(obj) {
return !!(obj && obj.nodeType === 1);
};
// Is a given value an array?
// Delegates to ECMA5's native Array.isArray
_.isArray = nativeIsArray || function(obj) {
return toString.call(obj) === '[object Array]';
};
// Is a given variable an object?
_.isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp, isError.
_.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error'], function(name) {
_['is' + name] = function(obj) {
return toString.call(obj) === '[object ' + name + ']';
};
});
// Define a fallback version of the method in browsers (ahem, IE < 9), where
// there isn't any inspectable "Arguments" type.
if (!_.isArguments(arguments)) {
_.isArguments = function(obj) {
return _.has(obj, 'callee');
};
}
// Optimize `isFunction` if appropriate. Work around some typeof bugs in old v8,
// IE 11 (#1621), and in Safari 8 (#1929).
if (typeof /./ != 'function' && typeof Int8Array != 'object') {
_.isFunction = function(obj) {
return typeof obj == 'function' || false;
};
}
// Is a given object a finite number?
_.isFinite = function(obj) {
return isFinite(obj) && !isNaN(parseFloat(obj));
};
// Is the given value `NaN`? (NaN is the only number which does not equal itself).
_.isNaN = function(obj) {
return _.isNumber(obj) && obj !== +obj;
};
// Is a given value a boolean?
_.isBoolean = function(obj) {
return obj === true || obj === false || toString.call(obj) === '[object Boolean]';
};
// Is a given value equal to null?
_.isNull = function(obj) {
return obj === null;
};
// Is a given variable undefined?
_.isUndefined = function(obj) {
return obj === void 0;
};
// Shortcut function for checking if an object has a given property directly
// on itself (in other words, not on a prototype).
_.has = function(obj, key) {
return obj != null && hasOwnProperty.call(obj, key);
};
// Utility Functions
// -----------------
// Run Underscore.js in *noConflict* mode, returning the `_` variable to its
// previous owner. Returns a reference to the Underscore object.
_.noConflict = function() {
root._ = previousUnderscore;
return this;
};
// Keep the identity function around for default iteratees.
_.identity = function(value) {
return value;
};
// Predicate-generating functions. Often useful outside of Underscore.
_.constant = function(value) {
return function() {
return value;
};
};
_.noop = function(){};
_.property = property;
// Generates a function for a given object that returns a given property.
_.propertyOf = function(obj) {
return obj == null ? function(){} : function(key) {
return obj[key];
};
};
// Returns a predicate for checking whether an object has a given set of
// `key:value` pairs.
_.matcher = _.matches = function(attrs) {
attrs = _.extendOwn({}, attrs);
return function(obj) {
return _.isMatch(obj, attrs);
};
};
// Run a function **n** times.
_.times = function(n, iteratee, context) {
var accum = Array(Math.max(0, n));
iteratee = optimizeCb(iteratee, context, 1);
for (var i = 0; i < n; i++) accum[i] = iteratee(i);
return accum;
};
// Return a random integer between min and max (inclusive).
_.random = function(min, max) {
if (max == null) {
max = min;
min = 0;
}
return min + Math.floor(Math.random() * (max - min + 1));
};
// A (possibly faster) way to get the current timestamp as an integer.
_.now = Date.now || function() {
return new Date().getTime();
};
// List of HTML entities for escaping.
var escapeMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'`': '`'
};
var unescapeMap = _.invert(escapeMap);
// Functions for escaping and unescaping strings to/from HTML interpolation.
var createEscaper = function(map) {
var escaper = function(match) {
return map[match];
};
// Regexes for identifying a key that needs to be escaped
var source = '(?:' + _.keys(map).join('|') + ')';
var testRegexp = RegExp(source);
var replaceRegexp = RegExp(source, 'g');
return function(string) {
string = string == null ? '' : '' + string;
return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
};
};
_.escape = createEscaper(escapeMap);
_.unescape = createEscaper(unescapeMap);
// If the value of the named `property` is a function then invoke it with the
// `object` as context; otherwise, return it.
_.result = function(object, property, fallback) {
var value = object == null ? void 0 : object[property];
if (value === void 0) {
value = fallback;
}
return _.isFunction(value) ? value.call(object) : value;
};
// Generate a unique integer id (unique within the entire client session).
// Useful for temporary DOM ids.
var idCounter = 0;
_.uniqueId = function(prefix) {
var id = ++idCounter + '';
return prefix ? prefix + id : id;
};
// By default, Underscore uses ERB-style template delimiters, change the
// following template settings to use alternative delimiters.
_.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
};
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
var noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var escapes = {
"'": "'",
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
var escaper = /\\|'|\r|\n|\u2028|\u2029/g;
var escapeChar = function(match) {
return '\\' + escapes[match];
};
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
// NB: `oldSettings` only exists for backwards compatibility.
_.template = function(text, settings, oldSettings) {
if (!settings && oldSettings) settings = oldSettings;
settings = _.defaults({}, settings, _.templateSettings);
// Combine delimiters into one regular expression via alternation.
var matcher = RegExp([
(settings.escape || noMatch).source,
(settings.interpolate || noMatch).source,
(settings.evaluate || noMatch).source
].join('|') + '|$', 'g');
// Compile the template source, escaping string literals appropriately.
var index = 0;
var source = "__p+='";
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset).replace(escaper, escapeChar);
index = offset + match.length;
if (escape) {
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
} else if (interpolate) {
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
} else if (evaluate) {
source += "';\n" + evaluate + "\n__p+='";
}
// Adobe VMs need the match returned to produce the correct offest.
return match;
});
source += "';\n";
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = "var __t,__p='',__j=Array.prototype.join," +
"print=function(){__p+=__j.call(arguments,'');};\n" +
source + 'return __p;\n';
try {
var render = new Function(settings.variable || 'obj', '_', source);
} catch (e) {
e.source = source;
throw e;
}
var template = function(data) {
return render.call(this, data, _);
};
// Provide the compiled source as a convenience for precompilation.
var argument = settings.variable || 'obj';
template.source = 'function(' + argument + '){\n' + source + '}';
return template;
};
// Add a "chain" function. Start chaining a wrapped Underscore object.
_.chain = function(obj) {
var instance = _(obj);
instance._chain = true;
return instance;
};
// OOP
// ---------------
// If Underscore is called as a function, it returns a wrapped object that
// can be used OO-style. This wrapper holds altered versions of all the
// underscore functions. Wrapped objects may be chained.
// Helper function to continue chaining intermediate results.
var result = function(instance, obj) {
return instance._chain ? _(obj).chain() : obj;
};
// Add your own custom functions to the Underscore object.
_.mixin = function(obj) {
_.each(_.functions(obj), function(name) {
var func = _[name] = obj[name];
_.prototype[name] = function() {
var args = [this._wrapped];
push.apply(args, arguments);
return result(this, func.apply(_, args));
};
});
};
// Add all of the Underscore functions to the wrapper object.
_.mixin(_);
// Add all mutator Array functions to the wrapper.
_.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
var method = ArrayProto[name];
_.prototype[name] = function() {
var obj = this._wrapped;
method.apply(obj, arguments);
if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0];
return result(this, obj);
};
});
// Add all accessor Array functions to the wrapper.
_.each(['concat', 'join', 'slice'], function(name) {
var method = ArrayProto[name];
_.prototype[name] = function() {
return result(this, method.apply(this._wrapped, arguments));
};
});
// Extracts the result from a wrapped and chained object.
_.prototype.value = function() {
return this._wrapped;
};
// Provide unwrapping proxy for some methods used in engine operations
// such as arithmetic and JSON stringification.
_.prototype.valueOf = _.prototype.toJSON = _.prototype.value;
_.prototype.toString = function() {
return '' + this._wrapped;
};
// AMD registration happens at the end for compatibility with AMD loaders
// that may not enforce next-turn semantics on modules. Even though general
// practice for AMD registration is to be anonymous, underscore registers
// as a named module because, like jQuery, it is a base library that is
// popular enough to be bundled in a third party lib, but not be part of
// an AMD load request. Those cases could generate an error when an
// anonymous define() is called outside of a loader request.
if (typeof define === 'function' && define.amd) {
define('underscore', [], function() {
return _;
});
}
}.call(this));
},{}],26:[function(require,module,exports){
arguments[4][19][0].apply(exports,arguments)
},{"dup":19}],27:[function(require,module,exports){
module.exports = function isBuffer(arg) {
return arg && typeof arg === 'object'
&& typeof arg.copy === 'function'
&& typeof arg.fill === 'function'
&& typeof arg.readUInt8 === 'function';
}
},{}],28:[function(require,module,exports){
(function (process,global){
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var formatRegExp = /%[sdj%]/g;
exports.format = function(f) {
if (!isString(f)) {
var objects = [];
for (var i = 0; i < arguments.length; i++) {
objects.push(inspect(arguments[i]));
}
return objects.join(' ');
}
var i = 1;
var args = arguments;
var len = args.length;
var str = String(f).replace(formatRegExp, function(x) {
if (x === '%%') return '%';
if (i >= len) return x;
switch (x) {
case '%s': return String(args[i++]);
case '%d': return Number(args[i++]);
case '%j':
try {
return JSON.stringify(args[i++]);
} catch (_) {
return '[Circular]';
}
default:
return x;
}
});
for (var x = args[i]; i < len; x = args[++i]) {
if (isNull(x) || !isObject(x)) {
str += ' ' + x;
} else {
str += ' ' + inspect(x);
}
}
return str;
};
// Mark that a method should not be used.
// Returns a modified function which warns once by default.
// If --no-deprecation is set, then it is a no-op.
exports.deprecate = function(fn, msg) {
// Allow for deprecating things in the process of starting up.
if (isUndefined(global.process)) {
return function() {
return exports.deprecate(fn, msg).apply(this, arguments);
};
}
if (process.noDeprecation === true) {
return fn;
}
var warned = false;
function deprecated() {
if (!warned) {
if (process.throwDeprecation) {
throw new Error(msg);
} else if (process.traceDeprecation) {
console.trace(msg);
} else {
console.error(msg);
}
warned = true;
}
return fn.apply(this, arguments);
}
return deprecated;
};
var debugs = {};
var debugEnviron;
exports.debuglog = function(set) {
if (isUndefined(debugEnviron))
debugEnviron = process.env.NODE_DEBUG || '';
set = set.toUpperCase();
if (!debugs[set]) {
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
var pid = process.pid;
debugs[set] = function() {
var msg = exports.format.apply(exports, arguments);
console.error('%s %d: %s', set, pid, msg);
};
} else {
debugs[set] = function() {};
}
}
return debugs[set];
};
/**
* Echos the value of a value. Trys to print the value out
* in the best way possible given the different types.
*
* @param {Object} obj The object to print out.
* @param {Object} opts Optional options object that alters the output.
*/
/* legacy: obj, showHidden, depth, colors*/
function inspect(obj, opts) {
// default options
var ctx = {
seen: [],
stylize: stylizeNoColor
};
// legacy...
if (arguments.length >= 3) ctx.depth = arguments[2];
if (arguments.length >= 4) ctx.colors = arguments[3];
if (isBoolean(opts)) {
// legacy...
ctx.showHidden = opts;
} else if (opts) {
// got an "options" object
exports._extend(ctx, opts);
}
// set default options
if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
if (isUndefined(ctx.depth)) ctx.depth = 2;
if (isUndefined(ctx.colors)) ctx.colors = false;
if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
if (ctx.colors) ctx.stylize = stylizeWithColor;
return formatValue(ctx, obj, ctx.depth);
}
exports.inspect = inspect;
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
inspect.colors = {
'bold' : [1, 22],
'italic' : [3, 23],
'underline' : [4, 24],
'inverse' : [7, 27],
'white' : [37, 39],
'grey' : [90, 39],
'black' : [30, 39],
'blue' : [34, 39],
'cyan' : [36, 39],
'green' : [32, 39],
'magenta' : [35, 39],
'red' : [31, 39],
'yellow' : [33, 39]
};
// Don't use 'blue' not visible on cmd.exe
inspect.styles = {
'special': 'cyan',
'number': 'yellow',
'boolean': 'yellow',
'undefined': 'grey',
'null': 'bold',
'string': 'green',
'date': 'magenta',
// "name": intentionally not styling
'regexp': 'red'
};
function stylizeWithColor(str, styleType) {
var style = inspect.styles[styleType];
if (style) {
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
'\u001b[' + inspect.colors[style][1] + 'm';
} else {
return str;
}
}
function stylizeNoColor(str, styleType) {
return str;
}
function arrayToHash(array) {
var hash = {};
array.forEach(function(val, idx) {
hash[val] = true;
});
return hash;
}
function formatValue(ctx, value, recurseTimes) {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (ctx.customInspect &&
value &&
isFunction(value.inspect) &&
// Filter out the util module, it's inspect function is special
value.inspect !== exports.inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
var ret = value.inspect(recurseTimes, ctx);
if (!isString(ret)) {
ret = formatValue(ctx, ret, recurseTimes);
}
return ret;
}
// Primitive types cannot have properties
var primitive = formatPrimitive(ctx, value);
if (primitive) {
return primitive;
}
// Look up the keys of the object.
var keys = Object.keys(value);
var visibleKeys = arrayToHash(keys);
if (ctx.showHidden) {
keys = Object.getOwnPropertyNames(value);
}
// IE doesn't make error fields non-enumerable
// http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
if (isError(value)
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
return formatError(value);
}
// Some type of object without properties can be shortcutted.
if (keys.length === 0) {
if (isFunction(value)) {
var name = value.name ? ': ' + value.name : '';
return ctx.stylize('[Function' + name + ']', 'special');
}
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
}
if (isDate(value)) {
return ctx.stylize(Date.prototype.toString.call(value), 'date');
}
if (isError(value)) {
return formatError(value);
}
}
var base = '', array = false, braces = ['{', '}'];
// Make Array say that they are Array
if (isArray(value)) {
array = true;
braces = ['[', ']'];
}
// Make functions say that they are functions
if (isFunction(value)) {
var n = value.name ? ': ' + value.name : '';
base = ' [Function' + n + ']';
}
// Make RegExps say that they are RegExps
if (isRegExp(value)) {
base = ' ' + RegExp.prototype.toString.call(value);
}
// Make dates with properties first say the date
if (isDate(value)) {
base = ' ' + Date.prototype.toUTCString.call(value);
}
// Make error with message first say the error
if (isError(value)) {
base = ' ' + formatError(value);
}
if (keys.length === 0 && (!array || value.length == 0)) {
return braces[0] + base + braces[1];
}
if (recurseTimes < 0) {
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
} else {
return ctx.stylize('[Object]', 'special');
}
}
ctx.seen.push(value);
var output;
if (array) {
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
} else {
output = keys.map(function(key) {
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
});
}
ctx.seen.pop();
return reduceToSingleString(output, base, braces);
}
function formatPrimitive(ctx, value) {
if (isUndefined(value))
return ctx.stylize('undefined', 'undefined');
if (isString(value)) {
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
return ctx.stylize(simple, 'string');
}
if (isNumber(value))
return ctx.stylize('' + value, 'number');
if (isBoolean(value))
return ctx.stylize('' + value, 'boolean');
// For some reason typeof null is "object", so special case here.
if (isNull(value))
return ctx.stylize('null', 'null');
}
function formatError(value) {
return '[' + Error.prototype.toString.call(value) + ']';
}
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
var output = [];
for (var i = 0, l = value.length; i < l; ++i) {
if (hasOwnProperty(value, String(i))) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
String(i), true));
} else {
output.push('');
}
}
keys.forEach(function(key) {
if (!key.match(/^\d+$/)) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
key, true));
}
});
return output;
}
function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
var name, str, desc;
desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
if (desc.get) {
if (desc.set) {
str = ctx.stylize('[Getter/Setter]', 'special');
} else {
str = ctx.stylize('[Getter]', 'special');
}
} else {
if (desc.set) {
str = ctx.stylize('[Setter]', 'special');
}
}
if (!hasOwnProperty(visibleKeys, key)) {
name = '[' + key + ']';
}
if (!str) {
if (ctx.seen.indexOf(desc.value) < 0) {
if (isNull(recurseTimes)) {
str = formatValue(ctx, desc.value, null);
} else {
str = formatValue(ctx, desc.value, recurseTimes - 1);
}
if (str.indexOf('\n') > -1) {
if (array) {
str = str.split('\n').map(function(line) {
return ' ' + line;
}).join('\n').substr(2);
} else {
str = '\n' + str.split('\n').map(function(line) {
return ' ' + line;
}).join('\n');
}
}
} else {
str = ctx.stylize('[Circular]', 'special');
}
}
if (isUndefined(name)) {
if (array && key.match(/^\d+$/)) {
return str;
}
name = JSON.stringify('' + key);
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
name = name.substr(1, name.length - 2);
name = ctx.stylize(name, 'name');
} else {
name = name.replace(/'/g, "\\'")
.replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'");
name = ctx.stylize(name, 'string');
}
}
return name + ': ' + str;
}
function reduceToSingleString(output, base, braces) {
var numLinesEst = 0;
var length = output.reduce(function(prev, cur) {
numLinesEst++;
if (cur.indexOf('\n') >= 0) numLinesEst++;
return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
}, 0);
if (length > 60) {
return braces[0] +
(base === '' ? '' : base + '\n ') +
' ' +
output.join(',\n ') +
' ' +
braces[1];
}
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
}
// NOTE: These type checking functions intentionally don't use `instanceof`
// because it is fragile and can be easily faked with `Object.create()`.
function isArray(ar) {
return Array.isArray(ar);
}
exports.isArray = isArray;
function isBoolean(arg) {
return typeof arg === 'boolean';
}
exports.isBoolean = isBoolean;
function isNull(arg) {
return arg === null;
}
exports.isNull = isNull;
function isNullOrUndefined(arg) {
return arg == null;
}
exports.isNullOrUndefined = isNullOrUndefined;
function isNumber(arg) {
return typeof arg === 'number';
}
exports.isNumber = isNumber;
function isString(arg) {
return typeof arg === 'string';
}
exports.isString = isString;
function isSymbol(arg) {
return typeof arg === 'symbol';
}
exports.isSymbol = isSymbol;
function isUndefined(arg) {
return arg === void 0;
}
exports.isUndefined = isUndefined;
function isRegExp(re) {
return isObject(re) && objectToString(re) === '[object RegExp]';
}
exports.isRegExp = isRegExp;
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
}
exports.isObject = isObject;
function isDate(d) {
return isObject(d) && objectToString(d) === '[object Date]';
}
exports.isDate = isDate;
function isError(e) {
return isObject(e) &&
(objectToString(e) === '[object Error]' || e instanceof Error);
}
exports.isError = isError;
function isFunction(arg) {
return typeof arg === 'function';
}
exports.isFunction = isFunction;
function isPrimitive(arg) {
return arg === null ||
typeof arg === 'boolean' ||
typeof arg === 'number' ||
typeof arg === 'string' ||
typeof arg === 'symbol' || // ES6 symbol
typeof arg === 'undefined';
}
exports.isPrimitive = isPrimitive;
exports.isBuffer = require('./support/isBuffer');
function objectToString(o) {
return Object.prototype.toString.call(o);
}
function pad(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
}
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];
// 26 Feb 16:19:34
function timestamp() {
var d = new Date();
var time = [pad(d.getHours()),
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
return [d.getDate(), months[d.getMonth()], time].join(' ');
}
// log is just a thin wrapper to console.log that prepends a timestamp
exports.log = function() {
console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
};
/**
* Inherit the prototype methods from one constructor into another.
*
* The Function.prototype.inherits from lang.js rewritten as a standalone
* function (not on Function.prototype). NOTE: If this file is to be loaded
* during bootstrapping this function needs to be rewritten using some native
* functions as prototype setup using normal JavaScript does not work as
* expected during bootstrapping (see mirror.js in r114903).
*
* @param {function} ctor Constructor function which needs to inherit the
* prototype.
* @param {function} superCtor Constructor function to inherit prototype from.
*/
exports.inherits = require('inherits');
exports._extend = function(origin, add) {
// Don't do anything if add isn't an object
if (!add || !isObject(add)) return origin;
var keys = Object.keys(add);
var i = keys.length;
while (i--) {
origin[keys[i]] = add[keys[i]];
}
return origin;
};
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./support/isBuffer":27,"_process":24,"inherits":26}],29:[function(require,module,exports){
// Returns a wrapper function that returns a wrapped callback
// The wrapper function should do some stuff, and return a
// presumably different callback function.
// This makes sure that own properties are retained, so that
// decorations and such are not lost along the way.
module.exports = wrappy
function wrappy (fn, cb) {
if (fn && cb) return wrappy(fn)(cb)
if (typeof fn !== 'function')
throw new TypeError('need wrapper function')
Object.keys(fn).forEach(function (k) {
wrapper[k] = fn[k]
})
return wrapper
function wrapper() {
var args = new Array(arguments.length)
for (var i = 0; i < args.length; i++) {
args[i] = arguments[i]
}
var ret = fn.apply(this, args)
var cb = args[args.length-1]
if (typeof ret === 'function' && ret !== cb) {
Object.keys(cb).forEach(function (k) {
ret[k] = cb[k]
})
}
return ret
}
}
},{}]},{},[7])(7)
});
================================================
FILE: site/book/index.html
================================================
100 Go Mistakes and How to Avoid Them - 100 Go Mistakes and How to Avoid Them
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.
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
Where to Buy?
100 Go Mistakes and How to Avoid Them (🇬🇧 edition: paper, digital, or audiobook)
Manning (please make sure to use my personal discount code for -35%: au35har)
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.
================================================
FILE: site/chapter-1/index.html
================================================
Read the First Chapter - 100 Go Mistakes and How to Avoid Them
What makes Go an efficient, scalable, and productive language
Exploring why Go is simple to learn but hard to master
Presenting the common types of mistakes made by developers
Making mistakes is part of everyone’s 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’t 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’t 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’ll discuss why, despite Go being considered simple to learn, mastering its nuances can be challenging. Finally, we’ll introduce the concepts this book covers.
Go outline
If you are reading this book, it’s likely that you’re 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—sometimes even hundreds, if not thousands. Nowadays, code must be readable, expressive, and maintainable to guarantee a system’s 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’t 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’s 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:
Stability—Even though Go receives frequent updates (including improvements and security patches), it remains a stable language. Some may even consider this one of the best features of the language.
Expressivity—We can define expressivity in a programming language by how naturally and intuitively we can write and read code. A reduced number of keywords and limited ways to solve common problems make Go an expressive language for large codebases.
Compilation—As developers, what can be more exasperating than having to wait for a build to test our application? Targeting fast compilation times has always been a conscious goal for the language designers. This, in turn, enables productivity.
Safety—Go is a strong, statically typed language. Hence, it has strict compiletime rules, which ensure the code is type-safe in most cases.
Go was built from the ground up with solid features such as outstanding concurrency primitives with goroutines and channels. There’s 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’t necessarily wrong. For example, a newcomer can learn the language’s main features in less than a day. So why read a book centered on the concept of mistakes if Go is simple?
Simple doesn’t mean easy
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’s 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’s not a question of confronting message passing versus sharing memory and determining the winner. However, it’s 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’t an easy topic in practice.
This leitmotif—simple doesn’t mean easy—can 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.
100 Go mistakes
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’re facing mistakes. 1 Haven’t 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’t 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
Bugs
Needless complexity
Weaker readability
Suboptimal or unidiomatic organization
Lack of API convenience
Under-optimized code
Lack of productivity
We introduce each mistake category next.
Bugs
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’t 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’s essential to make sure we avoid common bugs.
Needless complexity
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.
Weaker readability
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’t that important. However, today’s 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).
Suboptimal or unidiomatic organization
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’ll 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.
Lack of API convenience
Making common mistakes that weaken how convenient an API is for our clients is another type of mistake. If an API isn’t 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.
Under-optimized code
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.
Lack of productivity
In most cases, what’s the best language we can choose when working on a new project? The one we’re 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’ll 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’s time to delve into those 100 common Go mistakes.
Summary
Go is a modern programming language that enables developer productivity, which is crucial for most companies today.
Go is simple to learn but not easy to master. This is why we need to deepen our knowledge to make the most effective use of the language.
Learning via mistakes and concrete examples is a powerful way to be proficient in a language. This book will accelerate our path to proficiency by exploring 100 common mistakes.
J. S. Moser, H. S. Schroder, et al., “Mind Your Errors: Evidence for a Neural Mechanism Linking Growth Mindset to Adaptive Posterror Adjustments,” Psychological Science, vol. 22, no. 12, pp. 1484–1489, Dec. 2011. ↩
J. Metcalfe, “Learning from Errors,” Annual Review of Psychology, vol. 68, pp. 465–489, Jan. 2017. ↩
================================================
FILE: site/external/index.html
================================================
External Resources - 100 Go Mistakes and How to Avoid Them
================================================
FILE: site/index.html
================================================
Common Go Mistakes - 100 Go Mistakes and How to Avoid Them
If you enjoyed my book, you may be interested in my latest project: The Coder Cafe, a newsletter for coders.
Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve timeless concepts with your coffee. Written by a Google SWE and published author, we help you grow as an engineer, one coffee at a time.
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.
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:
Code and Project Organization
Unintended variable shadowing (#1)
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.
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’s see some different applications of this rule to optimize our code for readability:
When an if block returns, we should omit the else block in all cases. For example, we shouldn’t write:
iffoo(){// ...returntrue}else{// ...}
Instead, we omit the else block like this:
iffoo(){// ...returntrue}// ...
We can also follow this logic with a non-happy path:
Here, an empty s represents the non-happy path. Hence, we should flip the
condition like so:
ifs==""{returnerrors.New("empty string")}// ...
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’s readability.
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:
They can limit error management.
They can complicate how to implement tests (for example, an external dependency must be set up, which may not be necessary for the scope of unit tests).
If the initialization requires us to set a state, that has to be done through global variables.
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.
Forcing the use of getters and setters isn’t 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’t overwhelm our code with getters and setters on structs if they don’t 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’s nothing wrong with using them.
Interface pollution (#5)
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.
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’s not up to the producer to force a given abstraction for all the clients. Instead, it’s 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—not foresee—that 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.
To prevent being restricted in terms of flexibility, a function shouldn’t return interfaces but concrete implementations in most cases. Conversely, a function should accept interfaces whenever possible.
In most cases, we shouldn’t 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’t 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’s side.
any says nothing (#8)
TL;DR
Only use any if you need to accept or return any possible type, such as json.Marshal. Otherwise, any doesn’t 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.
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.
Not being aware of the possible problems with type embedding (#10)
TL;DR
Using type embedding can also help avoid boilerplate code; however, ensure that doing so doesn’t 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’t 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’s declared without a name. For example,
In the Foo struct, the Bar type is declared without an associated name; hence, it’s 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’s note that it’s 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:
It shouldn’t be used solely as some syntactic sugar to simplify accessing a field (such as Foo.Baz() instead of Foo.Bar.Baz()). If this is the only rationale, let’s not embed the inner type and use a field instead.
It shouldn’t promote data (fields) or a behavior (methods) we want to hide from the outside: for example, if it allows clients to access a locking behavior that should remain private to the struct.
Using type embedding consciously by keeping these constraints in mind can help avoid boilerplate code with additional forwarding methods. However, let’s make sure we don’t do it solely for cosmetics and not promote elements that should remain hidden.
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:
An unexported struct holds the configuration: options.
Each option is a function that returns the same type: type Option func(options *options) error. For example, WithPort accepts an int argument that represents the port and returns an Option type that represents how to update the options struct.
typeoptionsstruct{port*int}typeOptionfunc(options*options)errorfuncWithPort(portint)Option{returnfunc(options*options)error{ifport<0{returnerrors.New("port should be positive")}options.port=&portreturnnil}}funcNewServer(addrstring,opts...Option)(*http.Server,error){varoptionsoptionsfor_,opt:=rangeopts{err:=opt(&options)iferr!=nil{returnnil,err}}// At this stage, the options struct is built and contains the config// Therefore, we can implement our logic related to port configurationvarportintifoptions.port==nil{port=defaultHTTPPort}else{if*options.port==0{port=randomPort()}else{port=*options.port}}// ...}
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.
Project misorganization (project structure and package organization) (#12)
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’s 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’s 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’s 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’t straightforward, but following these rules should help make it easier to maintain. However, remember that consistency is also vital to ease maintainability. Therefore, let’s 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
Creating utility packages (#13)
TL;DR
Naming is a critical piece of application design. Creating packages such as common, util, and shared doesn’t 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.
To avoid naming collisions between variables and packages, leading to confusion or perhaps even bugs, use unique names for each one. If this isn’t 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.
Missing code documentation (#15)
TL;DR
To help clients and maintainers understand your code’s 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’s 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’t necessarily be public.
To help clients and maintainers understand a package’s 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’s because it will appear in the package. Then, we can provide all the information we need in the following lines.
Documenting our code shouldn’t be a constraint. We should take the opportunity to make sure it helps clients and maintainers to understand the purpose of our code.
Not using linters (#16)
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’t 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’re not a regular user of linters, here is a list that you may want to use daily:
Meanwhile, we should also look at golangci-lint (https://github.com/golangci/golangci-lint). It’s 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’s 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).
Data Types
Creating confusion with octal literals (#17)
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:
Binary—Uses a 0b or 0B prefix (for example, 0b100 is equal to 4 in base 10)
Hexadecimal—Uses an 0x or 0X prefix (for example, 0xF is equal to 15 in base 10)
Imaginary—Uses an i suffix (for example, 3i)
We can also use an underscore character (_) as a separator for readability. For example, we can write 1 billion this way: 1_000_000_000. We can also use the underscore character with other representations (for example, 0b00_00_01).
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,
varcounterint32=math.MaxInt32+1
constant2147483648overflowsint32
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).
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’ll look at a multiplication example:
varnfloat32=1.0001fmt.Println(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’s float32 and float64 types are approximations, we have to bear a few rules in mind:
When comparing two floating-point numbers, check that their difference is within an acceptable range.
When performing additions or subtractions, group operations with a similar order of magnitude for better accuracy.
To favor accuracy, if a sequence of operations requires addition, subtraction, multiplication, or division, perform the multiplication and division operations first.
Understanding the difference between slice length and capacity should be part of a Go developer’s 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.
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’s 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.
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’t 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’t necessarily nil. Meanwhile, a nil slice doesn’t require any allocation. We have seen throughout this section how to initialize a slice depending on the context by using
var s []string if we aren’t sure about the final length and the slice can be empty
[]string(nil) as syntactic sugar to create a nil and empty slice
make([]string, length) if the future length is known
The last option, []string{}, should be avoided if we initialize the slice without elements. Finally, let’s check whether the libraries we use make the distinctions between nil and empty slices to prevent unexpected behaviors.
To check if a slice doesn’t contain any element, check its length. This check works regardless of whether the slice is nil or empty. The same goes for maps. To design unambiguous APIs, you shouldn’t distinguish between nil and empty slices.
To determine whether a slice has elements, we can either do it by checking if the slice is nil or if its length is equal to 0. Checking the length is the best option to follow as it will cover both if the slice is empty or if the slice is nil.
Meanwhile, when designing interfaces, we should avoid distinguishing nil and empty slices, which leads to subtle programming errors. When returning slices, it should make neither a semantic nor a technical difference if we return a nil or empty slice. Both should mean the same thing for the callers. This principle is the same with maps. To check if a map is empty, check its length, not whether it’s nil.
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’s 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’ lengths. Also bear in mind that other alternatives exist to copy a slice, so we shouldn’t be surprised if we find them in a codebase.
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’s capacity is equal to max - low.
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.
Leaking capacity
Remember that slicing a large slice or array can lead to potential high memory consumption. The remaining space won’t 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.
When we use the slicing operation with pointers or structs with pointer fields, we need to know that the GC won’t 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.
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.
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.
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’s essential to understand how to use == and != to make comparisons effectively. We can use these operators on operands that are comparable:
Booleans—Compare whether two Booleans are equal.
Numerics (int, float, and complex types)—Compare whether two numerics are equal.
Strings—Compare whether two strings are equal.
Channels—Compare whether two channels were created by the same call to make or if both are nil.
Interfaces—Compare whether two interfaces have identical dynamic types and equal dynamic values or if both are nil.
Pointers—Compare whether two pointers point to the same value in memory or if both are nil.
Structs and arrays—Compare whether they are composed of similar types.
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’t reinvent the wheel.
Ignoring that elements are copied in range loops (#30)
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:
String
Array
Pointer to an array
Slice
Map
Receiving channel
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.
Ignoring how arguments are evaluated in range loops (channels and arrays) (#31)
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:
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:
The break statement doesn’t 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:
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.
Extracting loop logic inside a function leads to executing a defer statement at the end of each iteration.
The defer statement delays a call’s execution until the surrounding function returns. It’s 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:
funcreadFiles(ch<-chanstring)error{forpath:=rangech{file,err:=os.Open(path)iferr!=nil{returnerr}deferfile.Close()// Do something with file}returnnil}
The defer calls are executed not during each loop iteration but when the readFiles function returns. If readFiles doesn’t 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:
funcreadFiles(ch<-chanstring)error{forpath:=rangech{iferr:=readFile(path);err!=nil{returnerr}}returnnil}funcreadFile(pathstring)error{file,err:=os.Open(path)iferr!=nil{returnerr}deferfile.Close()// Do something with filereturnnil}
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.
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’s core knowledge to work accurately with strings.
As runes are everywhere in Go, it's important to understand the following:
A charset is a set of characters, whereas an encoding describes how to translate a charset into binary.
In Go, a string references an immutable slice of arbitrary bytes.
Go source code is encoded using UTF-8. Hence, all string literals are UTF-8 strings. But because a string can contain arbitrary bytes, if it’s obtained from somewhere else (not the source code), it isn’t guaranteed to be based on the UTF-8 encoding.
A rune corresponds to the concept of a Unicode code point, meaning an item represented by a single value.
Using UTF-8, a Unicode code point can be encoded into 1 to 4 bytes.
Using len() on a string in Go returns the number of bytes, not the number of runes.
Iterating on a string with the range operator iterates on the runes with the index corresponding to the starting index of the rune’s 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’s easy to get confused about how iteration works.
position 0: h
position 1: Ã
position 3: l
position 4: l
position 5: o
len=6
Let's highlight three points that might be confusing:
The second rune is à in the output instead of ê.
We jumped from position 1 to position 3: what is at position 2?
len returns a count of 6, whereas s contains only 5 runes.
Let’s 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 "ê" isn’t 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’t print the ith rune; it prints the UTF-8 representation of the byte at index i. Hence, we printed "hÃllo" instead of "hêllo".
If we want to print all the different runes, we can either use the value element of the range operator:
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’t have access to the rune index; rather, we know the starting index of a rune in the byte sequence.
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"))
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.
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’t 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:
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’s 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’t 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:
Let’s 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:
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.
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’re wondering whether we should work with strings or []byte, let’s recall that working with []byte isn’t 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’re 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.
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, “Slices and memory leaks,” 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.
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’t be copied, and how large the object is. When in doubt, use a pointer receiver.
Choosing between value and pointer receivers isn’t always straightforward. Let’s discuss some of the conditions to help us choose.
A receiver must be a pointer
If the method needs to mutate the receiver. This rule is also valid if the receiver is a slice and a method needs to append elements:
If the method receiver contains a field that cannot be copied: for example, a type part of the sync package (see #74, “Copying a sync type”).
A receiver should be a pointer
If the receiver is a large object. Using a pointer can make the call more efficient, as doing so prevents making an extensive copy. When in doubt about how large is large, benchmarking can be the solution; it’s pretty much impossible to state a specific size, because it depends on many factors.
A receiver must be a value
If we have to enforce a receiver’s immutability.
If the receiver is a map, function, or channel. Otherwise, a compilation error
occurs.
A receiver should be a value
If the receiver is a slice that doesn’t have to be mutated.
If the receiver is a small array or struct that is naturally a value type without mutable fields, such as time.Time.
If the receiver is a basic type such as int, float64, or string.
Of course, it’s impossible to be exhaustive, as there will always be edge cases, but this section’s goal was to provide guidance to cover most cases. By default, we can choose to go with a value receiver unless there’s a good reason not to do so. In doubt, we should use a pointer receiver.
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’s 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’s an example that uses a named result parameter b:
funcf(aint)(bint){b=areturn}
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’s a clear benefit.
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’re not careful enough. For example, can you spot what’s wrong with this code?
func(lloc)getCoordinates(ctxcontext.Context,addressstring)(lat,lngfloat32,errerror){isValid:=l.validateAddress(address)(1)if!isValid{return0,0,errors.New("invalid address")}ifctx.Err()!=nil{(2)return0,0,err}// Get and return coordinates}
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’t assigned any value to the err variable. It’s 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’t always straightforward to spot while reading code. Therefore, let’s remain cautious when using named result parameters, to avoid potential side effects.
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.
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.
Ignoring how defer arguments and receivers are evaluated (argument evaluation, pointer, and value receivers) (#47)
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.
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:
funcf()error{varstatusstringdefernotify(&status)deferincrementCounter(&status)// The rest of the function unchanged}
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’s another solution: calling a closure (an anonymous function value that references variables from outside its body) as a defer statement:
funcf()error{varstatusstringdeferfunc(){notify(status)incrementCounter(status)}()// The rest of the function unchanged}
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’t require notify and incrementCounter to change their signature.
Let's also note this behavior applies with method receiver: the receiver is evaluated immediately.
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:
This code prints a and then stops before printing b:
a
panic: foo
goroutine 1 [running]:
main.main()
main.go:7 +0xb3
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.
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’t 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:
Adding additional context to an error
Marking an error as a specific error
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’t 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’t use error wrapping but error transformation, for example, using fmt.Errorf with the %v directive.
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.
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"varErrFoo=errors.New("foo")
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:
Expected errors should be designed as error values (sentinel errors): var ErrFoo = errors.New("foo").
Unexpected errors should be designed as error types: type BarError struct { ... }, with BarError implementing the error interface.
If we use error wrapping in our application with the %w directive and fmt.Errorf, checking an error against a specific value should be done using errors.Is instead of ==. Thus, even if the sentinel error is wrapped, errors.Is can recursively unwrap it and compare each error in the chain against the provided value.
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.
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.
In many cases, you shouldn’t 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:
funcf(){// ...notify()// Error handling is omitted}funcnotify()error{// ...}
From a maintainability perspective, the code can lead to some issues. Let’s consider a new reader looking at it. This reader notices that notify returns an error but that the error isn’t 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
In terms of compilation and run time, this approach doesn’t change anything compared to the first piece of code. But this new version makes explicit that we aren’t interested in the error. Also, we can add a comment that indicates the rationale for why an error is ignored:
// At-most once delivery.// Hence, it's accepted to miss some of them in case of errors._=notify()
Understanding the fundamental differences between concurrency and parallelism is a cornerstone of the Go developer’s knowledge. Concurrency is about structure, whereas parallelism is about execution.
Concurrency and parallelism are not the same:
Concurrency is about structure. We can change a sequential implementation into a concurrent one by introducing different steps that separate concurrent goroutines can tackle.
Meanwhile, parallelism is about execution. We can use parallism at the steps level by adding more parallel goroutines.
In summary, concurrency provides a structure to solve a problem with parts that may be parallelized. Therefore, concurrency enables parallelism.
Thinking concurrency is always faster (#56)
TL;DR
To be a proficient developer, you must acknowledge that concurrency isn’t 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.
Being puzzled about when to use channels or mutexes (#57)
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:
G1 and G2 are parallel goroutines. They may be two goroutines executing the same function that keeps receiving messages from a channel, or perhaps two goroutines executing the same HTTP handler at the same time.
On the other hand, G1 and G3 are concurrent goroutines, as are G2 and G3. All the goroutines are part of an overall concurrent structure, but G1 and G2 perform the first step, whereas G3 does the next step.
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—therefore, channels.
Regarding concurrent goroutines, there’s 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’s important to know whether goroutines are parallel or concurrent because, in general, we need mutexes for parallel goroutines and channels for concurrent ones.
Not understanding race problems (data races vs. race conditions and the Go memory model) (#58)
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’t necessarily mean deterministic execution. When a behavior depends on the sequence or the timing of events that can’t 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.
Data Race
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:
Using the sync/atomic package
In synchronizing the two goroutines with an ad hoc data structure like a mutex
Using channels to make the two goroutines communicating to ensure that a variable is updated by only one goroutine at a time
Race Condition
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’t be controlled. Here, the timing of events is the goroutines’ execution order.
In summary, when we work in concurrent applications, it’s 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’t 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.
Not understanding the concurrency impacts of a workload type (#59)
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:
The speed of the CPU—For example, running a merge sort algorithm. The workload is called CPU-bound.
The speed of I/O—For example, making a REST call or a database query. The workload is called I/O-bound.
The amount of available memory—The workload is called memory-bound.
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.
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.
Deadline
A deadline refers to a specific point in time determined with one of the following:
A time.Duration from now (for example, in 250 ms)
A time.Time (for example, 2023-02-07 00:00:00 UTC)
The semantics of a deadline convey that an ongoing activity should be stopped if this deadline is met. An activity is, for example, an I/O request or a goroutine waiting to receive a message from a channel.
Cancellation signals
Another use case for Go contexts is to carry a cancellation signal. Let’s 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.
Context values
The last use case for Go contexts is to carry a key-value list. What’s 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.
Catching a context cancellation
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,
The Done channel related to a context created with context.WithCancel is closed when the cancel function is called.
The Done channel related to a context created with context.WithDeadline is closed when the deadline has expired.
One thing to note is that the internal channel should be closed when a context is canceled or has met a deadline, instead of when it receives a specific value, because the closure of a channel is the only channel action that all the consumer goroutines will receive. This way, all the consumers will be notified once a context is canceled or a deadline is reached.
In summary, to be a proficient Go developer, we have to understand what a context is and how to use it. In general, a function that users wait for should take a context, as doing so allows upstream callers to decide when calling this function should be aborted.
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’s 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’t 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:
funchandler(whttp.ResponseWriter,r*http.Request){response,err:=doSomeTask(r.Context(),r)iferr!=nil{http.Error(w,err.Error(),http.StatusInternalServerError)return}gofunc(){err:=publish(r.Context(),response)// Do something with err}()writeResponse(response)}
What’s wrong with this piece of code? We have to know that the context attached to an HTTP request can cancel in different conditions:
When the client’s connection closes
In the case of an HTTP/2 request, when the request is canceled
When the response has been written back to the client
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’s probably OK to call publish with a context already canceled so the message isn’t 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:
If the response is written after the Kafka publication, we both return a response and publish a message successfully
However, if the response is written before or during the Kafka publication, the message shouldn’t be published.
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.
Starting a goroutine without knowing when to stop it (#62)
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—so 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’s discuss a concrete example. We will design an application that needs to watch some external configuration (for example, using a database connection). Here’s a first implementation:
funcmain(){newWatcher()// Run the application}typewatcherstruct{/* Some resources */}funcnewWatcher(){w:=watcher{}gow.watch()// Creates a goroutine that watches some external configuration}
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’t 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:
funcmain(){ctx,cancel:=context.WithCancel(context.Background())defercancel()newWatcher(ctx)// Run the application}funcnewWatcher(ctxcontext.Context){w:=watcher{}gow.watch(ctx)}
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—and that’s a design flaw.
The problem is that we used signaling to convey that a goroutine had to be stopped. We didn’t block the parent goroutine until the resources had been closed. Let’s make sure we do:
funcmain(){w:=newWatcher()deferw.close()// Run the application}funcnewWatcher()watcher{w:=watcher{}gow.watch()returnw}func(wwatcher)close(){// Close the resources}
Instead of signaling watcher that it’s 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’s 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’s probably safer to wait for this goroutine to complete before exiting the application. This way, we can ensure that the resources can be freed.
Not being careful with goroutines and loop variables (#63)
Warning
This mistake isn't relevant anymore from Go 1.22 (details).
Expecting a deterministic behavior using select and channels (#64)
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):
Instead of consuming the 10 messages, we only received a few of them. What’s 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’s 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’s 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.
Channels are a mechanism for communicating across goroutines via signaling. A signal can be either with or without data.
Let’s 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(chanbool)
Now, let’s say we interact with an API that provides us with such a channel. Because it’s a channel of Booleans, we can receive either true or false messages. It’s probably clear what true conveys. But what does false mean? Does it mean we haven’t 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’s the case, meaning we don’t 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{}.
Not using nil channels (#66)
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?
varchchanint<-ch
ch is a chan int type. The zero value of a channel being nil, ch is nil. The goroutine won’t panic; however, it will block forever.
The principle is the same if we send a message to a nil channel. This goroutine blocks forever:
varchchanintch<-0
Then what’s 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:
funcmerge(ch1,ch2<-chanint)<-chanint{ch:=make(chanint,1)gofunc(){forch1!=nil||ch2!=nil{// Continue if at least one channel isn’t nilselect{casev,open:=<-ch1:if!open{ch1=nil// Assign ch1 to a nil channel once closedbreak}ch<-vcasev,open:=<-ch2:if!open{ch2=nil// Assigns ch2 to a nil channel once closedbreak}ch<-v}}close(ch)}()returnch}
This elegant solution relies on nil channels to somehow remove one case from the select statement.
Let’s keep this idea in mind: nil channels are useful in some conditions and should be part of the Go developer’s toolset when dealing with concurrent code.
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(chanint)ch2:=make(chanint,0)
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(chanint,1)
With a buffered channel, a sender can send messages while the channel isn’t full. Once the channel is full, it will block until a receiver goroutine receives a message:
The first send isn’t blocking, whereas the second one is, as the channel is full at this stage.
What's the main difference between unbuffered and buffered channels:
An unbuffered channel enables synchronization. We have the guarantee that two goroutines will be in a known state: one receiving and another sending a message.
A buffered channel doesn’t provide any strong synchronization. Indeed, a producer goroutine can send a message and then continue its execution if the channel isn’t full. The only guarantee is that a goroutine won’t receive a message before it is sent. But this is only a guarantee because of causality (you don’t drink your coffee before you prepare it).
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’s a list of possible cases where we should use another size:
While using a worker pooling-like pattern, meaning spinning a fixed number of goroutines that need to send data to a shared channel. In that case, we can tie the channel size to the number of goroutines created.
When using channels for rate-limiting problems. For example, if we need to enforce resource utilization by bounding the number of requests, we should set up the channel size according to the limit.
If we are outside of these cases, using a different channel size should be done cautiously. Let’s bear in mind that deciding about an accurate queue size isn’t 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.
Forgetting about possible side effects with string formatting (#68)
TL;DR
Being aware that string formatting may lead to calling existing functions means watching out for possible deadlocks and other data races.
It’s pretty easy to forget the potential side effects of string formatting while working in a concurrent application.
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.
Deadlock
Can you see what the problem is in this code with a Customer struct exposing an UpdateAge method and implementing the fmt.Stringer interface?
typeCustomerstruct{mutexsync.RWMutex// Uses a sync.RWMutex to protect concurrent accessesidstringageint}func(c*Customer)UpdateAge(ageint)error{c.mutex.Lock()// Locks and defers unlock as we update Customerdeferc.mutex.Unlock()ifage<0{// Returns an error if age is negativereturnfmt.Errorf("age should be positive for customer %v",c)}c.age=agereturnnil}func(c*Customer)String()string{c.mutex.RLock()// Locks and defers unlock as we read Customerdeferc.mutex.RUnlock()returnfmt.Sprintf("id %s, age %d",c.id,c.age)}
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’t 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(ageint)error{ifage<0{returnfmt.Errorf("age should be positive for customer %v",c)}c.mutex.Lock()deferc.mutex.Unlock()c.age=agereturnnil}
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(ageint)error{c.mutex.Lock()deferc.mutex.Unlock()ifage<0{returnfmt.Errorf("age should be positive for customer id %s",c.id)}c.age=agereturnnil}
In concurrent applications, we should remain cautious about the possible side effects of string formatting.
Calling append isn’t always data-race-free; hence, it shouldn’t 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)gofunc(){// In a new goroutine, appends a new element on ss1:=append(s,1)fmt.Println(s1)}()gofunc(){// Sames2:=append(s,1)fmt.Println(s2)}()
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’t mutate the existing array; hence, it doesn’t lead to a data race.
Now, let’s 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?
The answer is yes. We create a slice with make([]int, 0, 1). Therefore, the array isn’t 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.
Using mutexes inaccurately with slices and maps (#70)
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:
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{c.mu.RLock()balances:=c.balances// Creates a copy of the balances mapc.mu.RUnlock()sum:=0.for_,balance:=rangebalances{// Iterates over the copy, outside of the critical sectionsum+=balance}returnsum/float64(len(balances))}
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’s 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’t 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{c.mu.RLock()deferc.mu.RUnlock()// Unlocks when the function returnssum:=0.for_,balance:=rangec.balances{sum+=balance}returnsum/float64(len(c.balances))}
Another option, if the iteration operation isn’t lightweight, is to work on an actual copy of the data and protect only the copy:
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’t 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’s be cautious when designing critical sections and make sure the boundaries are accurately defined.
To accurately use sync.WaitGroup, call the Add method before spinning up goroutines.
In the following example, we will initialize a wait group, start three goroutines that will update a counter atomically, and then wait for them to complete. We want to wait for these three goroutines to print the value of the counter (which should be 3):
If we run this example, we get a non-deterministic value: the code can print any value from 0 to 3. Also, if we enable the -race flag, Go will even catch a data race.
The problem is that wg.Add(1) is called within the newly created goroutine, not in the parent goroutine. Hence, there is no guarantee that we have indicated to the wait group that we want to wait for three goroutines before calling wg.Wait().
To fix this issue, we can call wg.Add before the loop:
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)for{select{case<-ticker.C:// Do something}}
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:
This mistake isn't relevant anymore from Go 1.23 (details).
JSON handling common mistakes (#77)
Unexpected behavior because of type embedding
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.
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.
Forgetting the return statement after replying to an HTTP request (#80)
TL;DR
To avoid unexpected behaviors in HTTP handler implementations, make sure you don’t 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:
If we run this code and err != nil, the HTTP response would be:
foo
all good
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)
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:
funchandler(whttp.ResponseWriter,req*http.Request){err:=foo(req)iferr!=nil{http.Error(w,"foo",http.StatusInternalServerError)return// Adds the return statement}_,_=w.Write([]byte("all good"))w.WriteHeader(http.StatusCreated)}
For production-grade applications, don’t use the default HTTP client and server implementations. These implementations are missing timeouts and behaviors that should be mandatory in production.
Not categorizing tests (build tags, environment variables, and short mode) (#82)
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.
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’t a static analysis tool used during compilation; instead, it’s 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:
gotest-race./...
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:
Not using test execution modes (parallel and shuffle) (#84)
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’t rely on wrong assumptions that could hide bugs.
Not using table-driven tests (#85)
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.
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.
Use time methods to preserve the accuracy of a benchmark.
Increasing benchtime or using tools such as benchstat can be helpful when dealing with micro-benchmarks.
Be careful with the results of a micro-benchmark if the system that ends up running the application is different from the one running the micro-benchmark.
Make sure the function under test leads to a side effect, to prevent compiler optimizations from fooling you about the benchmark results.
To prevent the observer effect, force a benchmark to re-create the data used by a CPU-bound function.
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.
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.
Cache line
Being conscious of the cache line concept is critical to understanding how to organize data in data-intensive applications. A CPU doesn’t 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.
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’t predictable.
To avoid a critical stride, hence utilizing only a tiny portion of the cache, be aware that caches are partitioned.
Writing concurrent code that leads to false sharing (#92)
TL;DR
Knowing that lower levels of CPU caches aren’t shared across all the cores helps avoid performance-degrading patterns such as false sharing while writing concurrency code. Sharing memory is an illusion.
Not taking into account instruction-level parallelism (#93)
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.
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).
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.
Not knowing how to reduce allocations (API change, compiler optimizations, and sync.Pool) (#96)
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.
Understanding how to tune the GC can lead to multiple benefits such as handling sudden load increases more efficiently.
Not understanding the impacts of running Go in Docker and Kubernetes (#100)
Warning
This mistake isn't relevant anymore from Go 1.25 (details).
Community
Thanks to all the contributors:
Powered by
Comments
================================================
FILE: site/ja/index.html
================================================
Japanese Version - 100 Go Mistakes and How to Avoid Them
Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve one essential concept for coders. Written by a senior software engineer at Google, it's perfectly brewed for your morning coffee, helping you grow your skills deeply.
このページは『100 Go Mistakes』の内容をまとめたものです。一方で、コミュニティに開かれたページでもあります。「ありがちな間違い」が新たに追加されるべきだとお考えでしたら community mistake issue を作成してください。
インタフェースは、オブジェクトの動作を指定する方法を提供します。複数のオブジェクトが実装できる共通項を抽出するために、インタフェースは使用されます。Go言語のインタフェースが大きく異なるのは、暗黙的に満たされることです。オブジェクト X がインタフェース Y を実装していることを示す implements のような明示的なキーワードはありません。
typeoptionsstruct{port*int}typeOptionfunc(options*options)errorfuncWithPort(portint)Option{returnfunc(options*options)error{ifport<0{returnerrors.New("port should be positive")}options.port=&portreturnnil}}funcNewServer(addrstring,opts...Option)(*http.Server,error){varoptionsoptionsfor_,opt:=rangeopts{err:=opt(&options)iferr!=nil{returnnil,err}}// この段階で、options 構造体が構築され、構成が含まれます。// したがって、ポート設定に関連するロジックを実装できます。varportintifoptions.port==nil{port=defaultHTTPPort}else{if*options.port==0{port=randomPort()}else{port=*options.port}}// ...}
Functional Options パターンは、オプションを処理するための手軽で API フレンドリーな方法を提供します。 Builder パターンは有効なオプションですが、いくつかの小さな欠点(空の可能性がある構成構造体を渡さなければならない、またはエラーを処理する方法があまり便利ではない)があり、この種の問題において Functional Options パターンがGo言語における慣用的な対処方法になる傾向があります。
パッケージの名前付けも注意して行う必要があります。(開発者なら)誰もが知っているように、名前を付けるのは難しいです。クライアントが Go プロジェクトを理解しやすいように、パッケージに含まれるものではなく、提供するものに基づいてパッケージに名前を付ける必要があります。また、ネーミングには意味のあるものを付ける必要があります。したがって、パッケージ名は短く、簡潔で、表現力豊かで、慣例により単一の小文字にする必要があります。
リンターは、コードを分析してエラーを検出する自動ツールです。このセクションの目的は、既存のリンターの完全なリストを提供することではありません。そうした場合、すぐに使い物にならなくなってしまうからです。ただし、ほとんどの Go プロジェクトにリンターが不可欠であるということは理解し、覚えておきましょう。
make を使用してスライスを初期化するときに、長さとオプションの容量を指定できます。これらのパラメータの両方に適切な値を渡すことが適当であるにもかかわらず、それを忘れるのはよくある間違いです。実際、複数のコピーが必要になり、一時的なバッキング配列をクリーンアップするために GC に追加の労力がかかる可能性があります。パフォーマンスの観点から言えば、Go ランタイムに手を差し伸べない理由はありません。
range ループ内の value 要素はコピーです。したがって、たとえば構造体を変更するには、そのインデックスを介してアクセスするか、従来の for ループを介してアクセスしましょう(変更する要素またはフィールドがポインタである場合を除く)。
range ループを使用すると、さまざまなデータ構造に反復処理を行うことができます。
文字列
配列
配列へのポインタ
スライス
マップ
受信チャネル
古典的な for ループと比較すると、range ループはその簡潔な構文のおかげで、これらのデータ構造のすべての要素に反復処理をするのに便利です。
ただし、range ループ内の値要素はコピーであることを覚えておく必要があります。したがって、値を変更する必要がある構造体の場合、変更する値またはフィールドがポインタでない限り、要素自体ではなくコピーのみを更新します。range ループまたは従来の for ループを使用してインデックス経由で要素にアクセスすることが推奨されます。
range ループを使用してデータ構造に反復処理を施す場合、すべての値が単一の一意のアドレスを持つ一意の変数に割り当てられることを思い出してください。ゆえに、各反復処理中にこの変数を参照するポインタを保存すると、同じ要素、つまり最新の要素を参照する同じポインタを保存することになります。この問題は、ループのスコープ内にローカル変数を強制的に作成するか、インデックスを介してスライス要素を参照するポインタを作成することで解決できます。どちらの解決策でも問題ありません。
この解決策では、以前の解決策と比較して実行時のオーバーヘッドが発生することに注意してください。実際、文字列をルーンのスライスに変換するには、追加のスライスを割り当て、バイトをルーンに変換する必要があります。文字列のバイト数を n とすると、時間計算量は O(n) になります。したがって、すべてのルーンを反復処理する場合は、最初の解決策を使用するべきです。
ただし、最初の方法を使用して文字列の i 番目のルーンにアクセスしたい場合は、ルーンインデックスにアクセスできません。代わりに、バイトシーケンス内のルーンの開始インデックスがわかります。
各反復中に、 += 演算子は s と value 文字列を連結します。一見すると、この関数は間違っていないように見えるかもしれません。しかし、この実装は、文字列の核となる特性の1つである不変性を忘れています。したがって、各反復では s は更新されません。メモリ内に新しい文字列を再割り当てするため、この関数のパフォーマンスに大きな影響を与えます。
ワーカーによって実行されるワークロードが I/O バウンドである場合、値は主に外部システムに依存します。逆に、ワークロードが CPU に依存している場合、ゴルーチンの最適な数は利用可能な CPU コアの数に近くなります(ベストプラクティスは runtime.GOMAXPROCS を使用することです)。並行処理のアプリケーションを設計する場合、ワークロードのタイプ( I/O あるいは CPU )を知ることが重要です。
Go Context の最後の使用例は、キーと値のリストを運ぶことです。 Context にキーと値のリストを含める意味は何でしょうか。Go Context は汎用的であるため、使用例は無限にあります。
たとえば、トレースを使用する場合、異なるサブ関数の間で同じ相関 ID を共有したいことがあるかもしれません。一部の開発者は、この ID を関数シグネチャの一部にするにはあまりに侵略的だと考えるかもしれません。この点に関して、与えられた Context の一部としてそれを含めることを決定することもできます。
Docker と Kubernetes にデプロイする際の CPU スロットリングを回避するには、Go言語が CFS 対応ではないことに留意してください。
Comments
================================================
FILE: site/pt-br/index.html
================================================
Brazilian Portuguese Version - 100 Go Mistakes and How to Avoid Them
Se você gostou do meu livro, talvez se interesse pelo meu novo projeto: The Coder Cafe, uma newsletter diária para programadores.
Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve one essential concept for coders. Written by a senior software engineer at Google, it's perfectly brewed for your morning coffee, helping you grow your skills deeply.
Esta página é um resumo dos erros do 100 Go Mistakes and How to Avoid Them book. Enquanto isso, também está aberto à comunidade. Se você acredita que um erro comum do Go deve ser adicionado, crie uma issue.
Beta
Você está visualizando uma versão beta enriquecida com muito mais conteúdo. No entanto, esta versão ainda não está completa e estou procurando voluntários para me ajudar a resumir os erros restantes (GitHub issue #43).
Progresso:
Código e Organização do Projeto
Sombreamento não intencional de variável (#1)
TL;DR
Evitar variáveis sombreadas pode ajudar a evitar erros, como fazer referência à variável errada ou confundir os desenvolvedores.
O sombreamento de variável ocorre quando um nome de variável é redeclarado em um bloco interno, mas essa prática está sujeita a erros. A imposição de uma regra para proibir variáveis obscuras depende do gosto pessoal. Por exemplo, às vezes pode ser conveniente reutilizar um nome de variável existente, como err no caso de erros. Porém, em geral, devemos ser cautelosos porque agora sabemos que podemos enfrentar um cenário onde o código compila, mas a variável que recebe o valor não é a esperada.
Evitar níveis aninhados e manter o caminho feliz alinhado à esquerda facilita a construção de um modelo de código mental.
Em geral, quanto mais níveis aninhados uma função exigir, mais complexa será sua leitura e compreensão. Vamos ver algumas aplicações diferentes desta regra para otimizar a legibilidade do nosso código:
Quando um bloco if retorna, devemos omitir o else em todos os casos. Por exemplo, não deveríamos escrever:
iffoo(){// ...returntrue}else{// ...}
Em vez disso, omitimos o bloco else assim:
iffoo(){// ...returntrue}// ...
Também podemos seguir esta lógica com um caminho não feliz:
Aqui, um s vazio representa o caminho não feliz. Portanto, devemos inverter a condição assim:
ifs==""{returnerrors.New("empty string")}// ...
Escrever código legível é um desafio importante para todo desenvolvedor. Esforçar-se para reduzir o número de blocos aninhados, alinhar o caminho feliz à esquerda e retornar o mais cedo possível são meios concretos para melhorar a legibilidade do nosso código.
Ao inicializar variáveis, lembre-se de que as funções init têm tratamento de erros limitado e tornam o tratamento de estado e os testes mais complexos. Na maioria dos casos, as inicializações devem ser tratadas como funções específicas.
Uma função init é uma função usada para inicializar o estado de um aplicativo. Não aceita argumentos e não retorna nenhum resultado (uma função func()). Quando um pacote é inicializado, todas as declarações de constantes e variáveis do pacote são avaliadas. Então, as funções init são executadas.
As funções de inicialização podem levar a alguns problemas:
Elas podem limitar o gerenciamento de erros.
Elas podem complicar a implementação de testes (por exemplo, uma dependência externa deve ser configurada, o que pode não ser necessário para o escopo dos testes unitários).
Se a inicialização exigir que definamos um estado, isso deverá ser feito por meio de variáveis globais.
Devemos ser cautelosos com as funções init. No entanto, elas podem ser úteis em algumas situações, como na definição de configuração estática. Caso contrário, e na maioria dos casos, devemos tratar as inicializações através de funções ad hoc.
Forcing the use of getters and setters isn’t 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ão meios de habilitar o encapsulamento, fornecendo métodos exportados sobre campos de objetos não exportados.
No Go, não há suporte automático para getters e setters como vemos em algumas linguagens. Também não é considerado obrigatório nem idiomático o uso de getters e setters para acessar campos struct. Não devemos sobrecarregar nosso código com getters e setters em structs se eles não trouxerem nenhum valor. Deveríamos ser pragmáticos e nos esforçar para encontrar o equilíbrio certo entre eficiência e seguir expressões que às vezes são consideradas indiscutíveis em outros paradigmas de programação.
Lembre-se de que Go é uma linguagem única projetada para muitas características, 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ão há nada de errado em usá-los.
Interface poluidas (#5)
TL;DR
Abstrações devem ser descobertas, não criadas. Para evitar complexidade desnecessária, crie uma interface quando precisar dela e não quando você prevêr que será necessária, ou se puder pelo menos provar que a abstração é válida.
Manter interfaces no lado do cliente evita abstrações desnecessárias.
As interfaces são satisfeitas implicitamente em Go, o que tende a ser um divisor de águas em comparação com linguagens com implementação explícita. Na maioria dos casos, a abordagem a seguir é semelhante à que descrevemos na seção anterior: as abstrações devem ser descobertas, não criadas. Isso significa que não cabe ao producer forçar uma determinada abstração para todos os clientes. Em vez disso, cabe ao cliente decidir se precisa de alguma forma de abstração e então determinar o melhor nível de abstração para suas necessidades.
Uma interface deve residir no lado do consumidor na maioria dos casos. Contudo, em contextos específicos (por exemplo, quando sabemos – e não prevemos – que uma abstração será útil para os consumidores), podemos querer tê-la do lado do procuder. Se o fizermos, devemos nos esforçar para mantê-lo o mínimo possível, aumentando o seu potencial de reutilização e tornando-o mais facilmente combinável.
Para evitar restrições em termos de flexibilidade, uma função não deve retornar interfaces, mas implementações concretas na maioria dos casos. Por outro lado, uma função deve aceitar interfaces sempre que possível.
Na maioria dos casos, não devemos retornar interfaces, mas implementações concretas. Caso contrário, isso pode tornar nosso design mais complexo devido às dependências do pacote e pode restringir a flexibilidade porque todos os clientes teriam que contar com a mesma abstração. Novamente, a conclusão é semelhante às seções anteriores: se sabemos (não prevemos) que uma abstração será útil para os clientes, podemos considerar o retorno de uma interface. Caso contrário, não deveríamos forçar abstrações; eles devem ser descobertas pelos clientes. Se um cliente precisar abstrair uma implementação por qualquer motivo, ele ainda poderá fazer isso do lado do cliente.
any não diz nada (#8)
TL;DR
Use apenas any se precisar aceitar ou retornar qualquer tipo possível, como json.Marshal. Caso contrário, any não fornece informações significativas e pode levar a problemas de tempo de compilação, permitindo que um chamador chame métodos com qualquer tipo de dados.
O tipo any pode ser útil se houver uma necessidade genuína de aceitar ou retornar qualquer tipo possível (por exemplo, quando se trata de empacotamento ou formatação). Em geral, devemos evitar a todo custo generalizar demais o código que escrevemos. Talvez um pouco de código duplicado possa ocasionalmente ser melhor se melhorar outros aspectos, como a expressividade do código.
Depender de parâmetros genéricos e de tipo pode impedir a gravação de código clichê (boilerplate) para fatorar elementos ou comportamentos. No entanto, não use parâmetros de tipo prematuramente, mas somente quando você perceber uma necessidade concreta deles. Caso contrário, introduzem abstrações e complexidade desnecessárias.
Não estar ciente dos possíveis problemas com a incorporação de tipos (#10)
TL;DR
Usar a incorporação de tipo (type embedding) também pode ajudar a evitar código clichê (boilerplate); no entanto, certifique-se de que isso não leve a problemas de visibilidade onde alguns campos deveriam ter permanecido ocultos.
Ao criar uma struct, Go oferece a opção de incorporar tipos. Mas isso às vezes pode levar a comportamentos inesperados se não compreendermos todas as implicações da incorporação de tipos. Ao longo desta seção, veremos como incorporar tipos, o que eles trazem e os possíveis problemas.
No Go, um campo struct é chamado de incorporado se for declarado sem nome. Por exemplo,
Na estrutura Foo, o tipo Bar é declarado sem nome associado; portanto, é um campo incorporado.
Usamos incorporação para promover os campos e métodos de um tipo incorporado. Como Bar contém um campo Baz, esse campo é promovido para Foo. Portanto, Baz fica disponível a partir de Foo.
O que podemos dizer sobre a incorporação de tipos? Primeiro, observemos que raramente é uma necessidade e significa que, qualquer que seja o caso de uso, provavelmente também poderemos resolvê-lo sem incorporação de tipo. A incorporação de tipos é usada principalmente por conveniência: na maioria dos casos, para promover comportamentos.
Se decidirmos usar incorporação de tipo, precisamos ter em mente duas restrições principais:
Não deve ser usado apenas como um açúcar sintático para simplificar o acesso a um campo (como Foo.Baz() em vez de Foo.Bar.Baz()). Se esta for a única justificativa, não vamos incorporar o tipo interno e usar um campo.
Não deve promover dados (campos) ou um comportamento (métodos) que queremos ocultar do exterior: por exemplo, se permitir que os clientes acessem um comportamento de bloqueio que deve permanecer privado da struct.
Usar a incorporação de tipo de forma consciente, mantendo essas restrições em mente, pode ajudar a evitar código clichê (boilerplate) com métodos de encaminhamento adicionais. No entanto, vamos garantir que não o fazemos apenas por motivos cosméticos e não promovemos elementos que deveriam permanecer ocultos.
Não usar o padrão de opções funcionais (functional options pattern) (#11)
TL;DR
Para lidar com opções de maneira conveniente e amigável à API, use o padrão de opções funcionais.
Embora existam diferentes implementações com pequenas variações, a ideia principal é a seguinte:
Uma estrutura não exportada contém a configuração: opções.
Cada opção é uma função que retorna o mesmo tipo: type Option func(options *options) error. Por exemplo, WithPort aceita um argumento int que representa a porta e retorna um tipo Option que representa como atualizar a struct options.
typeoptionsstruct{port*int}typeOptionfunc(options*options)errorfuncWithPort(portint)Option{returnfunc(options*options)error{ifport<0{returnerrors.New("port should be positive")}options.port=&portreturnnil}}funcNewServer(addrstring,opts...Option)(*http.Server,error){varoptionsoptionsfor_,opt:=rangeopts{err:=opt(&options)iferr!=nil{returnnil,err}}// At this stage, the options struct is built and contains the config// Therefore, we can implement our logic related to port configurationvarportintifoptions.port==nil{port=defaultHTTPPort}else{if*options.port==0{port=randomPort()}else{port=*options.port}}// ...}
O padrão de opções funcionais fornece uma maneira prática e amigável à API de lidar com opções. Embora o padrão do construtor possa ser uma opção válida, ele tem algumas desvantagens menores (ter que passar uma estrutura de configuração que pode estar vazia ou uma maneira menos prática de lidar com o gerenciamento de erros) que tendem a tornar o padrão de opções funcionais a maneira idiomática de lidar com esse tipo de problema no Go.
Desorganização do projeto (estrutura do projeto e organização do pacote) (#12)
No que diz respeito à organização geral, existem diferentes escolas de pensamento. Por exemplo, devemos organizar a nossa aplicação por contexto ou por camada? Depende de nossas preferências. Podemos preferir agrupar o código por contexto (como o contexto do cliente, o contexto do contrato, etc.), ou podemos preferir seguir os princípios da arquitetura hexagonal e agrupar por camada técnica. Se a decisão que tomarmos se adequar ao nosso caso de uso, não pode ser uma decisão errada, desde que permaneçamos consistentes com ela.
Em relação aos pacotes, existem várias práticas recomendadas que devemos seguir. Primeiro, devemos evitar pacotes prematuros porque podem complicar demais um projeto. Às vezes, é melhor usar uma organização simples e fazer nosso projeto evoluir quando entendemos o que ele contém, em vez de nos forçarmos a fazer a estrutura perfeita desde o início. A granularidade é outra coisa essencial a considerar. Devemos evitar dezenas de pacotes nano contendo apenas um ou dois arquivos. Se o fizermos, é porque provavelmente perdemos algumas conexões lógicas entre esses pacotes, tornando nosso projeto mais difícil de ser compreendido pelos leitores. Por outro lado, também devemos evitar pacotes grandes que diluem o significado do nome de um pacote.
A nomenclatura dos pacotes também deve ser considerada com cuidado. Como todos sabemos (como desenvolvedores), nomear é difícil. Para ajudar os clientes a entender um projeto Go, devemos nomear nossos pacotes de acordo com o que eles fornecem, não com o que contêm. Além disso, a nomenclatura deve ser significativa. Portanto, o nome de um pacote deve ser curto, conciso, expressivo e, por convenção, uma única palavra minúscula.
Quanto ao que exportar, a regra é bastante simples. Devemos minimizar o que deve ser exportado tanto quanto possível para reduzir o acoplamento entre pacotes e manter ocultos os elementos exportados desnecessários. Se não tivermos certeza se devemos ou não exportar um elemento, devemos optar por não exportá-lo. Mais tarde, se descobrirmos que precisamos exportá-lo, poderemos ajustar nosso código. Vamos também ter em mente algumas exceções, como fazer com que os campos sejam exportados para que uma estrutura possa ser desempacotada com encoding/json.
Organizar um projeto não é simples, mas seguir essas regras deve ajudar a facilitar sua manutenção. No entanto, lembre-se de que a consistência também é vital para facilitar a manutenção. Portanto, vamos nos certificar de manter as coisas o mais consistentes possível dentro de uma base de código.
Note
Em 2023, a equipe Go publicou uma diretriz oficial para organizar/estruturar um projeto Go: go.dev/doc/modules/layout
Criando pacotes de utilitários (#13)
TL;DR
A nomenclatura é uma parte crítica do design do aplicativo. Criar pacotes como common, util e shared não traz muito valor para o leitor. Refatore esses pacotes em nomes de pacotes significativos e específicos.
Além disso, tenha em mente que nomear um pacote com base no que ele fornece e não no que ele contém pode ser uma forma eficiente de aumentar sua expressividade.
Para evitar colisões de nomes entre variáveis e pacotes, levando a confusão ou talvez até bugs, use nomes exclusivos para cada um. Se isso não for viável, use um alias de importação para alterar o qualificador para diferenciar o nome do pacote do nome da variável ou pense em um nome melhor.
As colisões de pacotes ocorrem quando um nome de variável colide com um nome de pacote existente, impedindo que o pacote seja reutilizado. Devemos evitar colisões de nomes de variáveis para evitar ambiguidade. Se enfrentarmos uma colisão, devemos encontrar outro nome significativo ou usar um alias de importação.
Documentação de código ausente (#15)
TL;DR
Para ajudar clientes e mantenedores a entender a finalidade do seu código, documente os elementos exportados.
A documentação é um aspecto importante da programação. Simplifica como os clientes podem consumir uma API, mas também pode ajudar na manutenção de um projeto. No Go, devemos seguir algumas regras para tornar nosso código idiomático:
Primeiro, cada elemento exportado deve ser documentado. Seja uma estrutura, uma interface, uma função ou qualquer outra coisa, se for exportado deve ser documentado. A convenção é adicionar comentários, começando com o nome do elemento exportado.
Por convenção, cada comentário deve ser uma frase completa que termina com pontuação. Tenha também em mente que quando documentamos uma função (ou um método), devemos destacar o que a função pretende fazer, não como o faz; isso pertence ao núcleo de uma função e comentários, não à documentação. Além disso, o ideal é que a documentação forneça informações suficientes para que o consumidor não precise olhar nosso código para entender como usar um elemento exportado.
Quando se trata de documentar uma variável ou constante, podemos estar interessados em transmitir dois aspectos: sua finalidade e seu conteúdo. O primeiro deve funcionar como documentação de código para ser útil para clientes externos. Este último, porém, não deveria ser necessariamente público.
Para ajudar clientes e mantenedores a entender o escopo de um pacote, devemos também documentar cada pacote. A convenção é iniciar o comentário com // Package seguido do nome do pacote. A primeira linha de um comentário de pacote deve ser concisa. Isso porque ele aparecerá no pacote. Então, podemos fornecer todas as informações que precisamos nas linhas seguintes.
Documentar nosso código não deve ser uma restrição. Devemos aproveitar a oportunidade para garantir que isso ajude os clientes e mantenedores a entender o propósito do nosso código.
Não usando linters (#16)
TL;DR
Para melhorar a qualidade e consistência do código, use linters e formatadores.
Um linter é uma ferramenta automática para analisar código e detectar erros. O escopo desta seção não é fornecer uma lista exaustiva dos linters existentes; caso contrário, ele ficará obsoleto rapidamente. Mas devemos entender e lembrar por que os linters são essenciais para a maioria dos projetos Go.
No entanto, se você não é um usuário regular de linters, aqui está uma lista que você pode usar diariamente:
Além dos linters, também devemos usar formatadores de código para corrigir o estilo do código. Aqui está uma lista de alguns formatadores de código para você experimentar:
Enquanto isso, devemos também dar uma olhada em golangci-lint (https://github.com/golangci/golangci-lint).
É uma ferramenta de linting que fornece uma fachada sobre muitos linters e formatadores úteis. Além disso, permite executar os linters em paralelo para melhorar a velocidade de análise, o que é bastante útil.
Linters e formatadores são uma forma poderosa de melhorar a qualidade e consistência de nossa base de código. Vamos dedicar um tempo para entender qual deles devemos usar e garantir que automatizamos sua execução (como um precommit hook de CI ou Git).
Tipos de dados
Criando confusão com literais octais (#17)
TL;DR
Ao ler o código existente, lembre-se de que literais inteiros começando com 0 são números octais. Além disso, para melhorar a legibilidade, torne os inteiros octais explícitos prefixando-os com 0o.
Os números octais começam com 0 (por exemplo, 010 é igual a 8 na base 10). Para melhorar a legibilidade e evitar possíveis erros para futuros leitores de código, devemos tornar os números octais explícitos usando o prefixo 0o (por exemplo, 0o10).
Devemos também observar as outras representações literais inteiras:
Binário—Usa um prefixo 0b ou 0B(por exemplo, 0b100 é igual a 4 na base 10)
Hexadecimal—Usa um prefixo 0x ou 0X (por exemplo, 0xF é igual a 15 na base 10)
Imaginário—Usa um i sufixo (por exemplo, 3i)
Também podemos usar um caractere de sublinhado (_) como separador para facilitar a leitura. Por exemplo, podemos escrever 1 bilhão desta forma: 1_000_000_000. Também podemos usar o caractere sublinhado com outras representações (por exemplo, 0b00_00_01).
Como os overflows e underflows de números inteiros são tratados silenciosamente no Go, você pode implementar suas próprias funções para capturá-los.
No Go, um estouro de número inteiro que pode ser detectado em tempo de compilação gera um erro de compilação. Por exemplo,
varcounterint32=math.MaxInt32+1
constant2147483648overflowsint32
No entanto, em tempo de execução, um overflow ou underflow de inteiro é silencioso; isso não leva ao pânico do aplicativo. É essencial ter esse comportamento em mente, pois ele pode levar a bugs sorrateiros (por exemplo, um incremento de número inteiro ou adição de números inteiros positivos que leva a um resultado negativo).
Fazer comparações de ponto flutuante dentro de um determinado delta pode garantir que seu código seja portátil. Ao realizar adição ou subtração, agrupe as operações com ordem de grandeza semelhante para favorecer a precisão. Além disso, execute multiplicação e divisão antes da adição e subtração.
Em Go, existem dois tipos de ponto flutuante (se omitirmos os números imaginários): float32 e float64. O conceito de ponto flutuante foi inventado para resolver o principal problema dos números inteiros: sua incapacidade de representar valores fracionários. Para evitar surpresas desagradáveis, precisamos saber que a aritmética de ponto flutuante é uma aproximação da aritmética real.
Para isso, veremos um exemplo de multiplicação:
varnfloat32=1.0001fmt.Println(n*n)
Podemos esperar que este código imprima o resultado de 1.0001 * 1.0001 = 1,00020001, certo? No entanto, executá-lo na maioria dos processadores x86 imprime 1.0002.
Como os tipos float32 e float64 em Go são aproximações, temos que ter algumas regras em mente:
Ao comparar dois números de ponto flutuante, verifique se a diferença está dentro de um intervalo aceitável.
Ao realizar adições ou subtrações, agrupe operações com ordem de magnitude semelhante para melhor precisão.
Para favorecer a precisão, se uma sequência de operações exigir adição, subtração, multiplicação ou divisão, execute primeiro as operações de multiplicação e divisão.
Não entendendo o comprimento e a capacidade de slice (#20)
TL;DR
Compreender a diferença entre comprimento e capacidade da slice deve fazer parte do conhecimento básico de um desenvolvedor Go. O comprimento de slice é o número de elementos disponíveis na slice, enquanto a capacidade de slice é o número de elementos na matriz de apoio.
Ao criar uma fatia, inicialize-a com um determinado comprimento ou capacidade se o seu comprimento já for conhecido. Isso reduz o número de alocações 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âmetros quando faz sentido é um erro generalizado. Na verdade, isso pode levar a múltiplas cópias e esforço adicional para o GC limpar as matrizes de apoio temporárias. Em termos de desempenho, não há uma boa razão para não ajudar o tempo de execução do Go.
Nossas opções são alocar uma fatia com determinada capacidade ou comprimento. Destas duas soluções, vimos que a segunda tende a ser um pouco mais rápida. Mas usar uma determinada capacidade e anexar pode ser mais fácil de implementar e ler em alguns contextos.
Estar confuso sobre slice nula vs. slice vazia (#22)
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’t require allocation.
No Go, há uma distinção entre slices nulas e vazias. Uma slice nula é igual a nil, enquanto uma slice vazia tem comprimento zero. Uma slice nula está vazia, mas uma slice vazia não é necessariamente nil. Enquanto isso, uma slice nula não requer nenhuma alocação. Vimos ao longo desta seção como inicializar uma slice dependendo do contexto usando
var s []string se não tivermos certeza sobre o comprimento final e a fatia pode estar vazia
[]string(nil) como açúcar sintático para criar uma fatia nula e vazia
make([]string, length) se o comprimento futuro for conhecido
A última opção, []string{} deve ser evitada se inicializarmos a fatia sem elementos. Finalmente, vamos verificar se as bibliotecas que usamos fazem distinções entre fatias nulas e vazias para evitar comportamentos inesperados.
Não verificar corretamente se um slice está vazio (#23)
TL;DR
Para verificar se uma fatia não contém nenhum elemento, verifique seu comprimento. Esta verificação funciona independentemente de o slice estar nil ou vazio. O mesmo vale para maps. Para projetar APIs inequívocas, você não deve distinguir entre slice nulos e vazios.
Para determinar se um slice possui elementos, podemos fazê-lo verificando se o slice é nulo ou se seu comprimento é igual a 0. Verificar o comprimento é a melhor opção a seguir, pois cobrirá ambos se o slice estiver vazio ou se o slice é nulo.
Enquanto isso, ao projetar interfaces, devemos evitar distinguir slices nulos e vazios, o que leva a erros sutis de programação. Ao retornar slices, não deve haver diferença semântica nem técnica se retornarmos um slice nulo ou vazio. Ambos devem significar a mesma coisa para quem liga. Este princípio é o mesmo com maps. Para verificar se um map está vazio, verifique seu comprimento, não se é nulo.
Para copiar um slice para outro usando a função copy, lembre-se que o número de elementos copiados corresponde ao mínimo entre os comprimentos dos dois slices.
Copiar elementos de um slice para outro é uma operação razoavelmente frequente. Ao utilizar a cópia, devemos lembrar que o número de elementos copiados para o destino corresponde ao mínimo entre os comprimentos dos dois slices. Tenha também em mente que existem outras alternativas para copiar um slice, por isso não devemos nos surpreender se as encontrarmos em uma base de código.
Efeitos colaterais inesperados usando o slice append (#25)
TL;DR
Usar copy ou a expressão de slice completa é uma forma de evitar que append crie conflitos se duas funções diferentes usarem slices apoiados pela mesmo array. No entanto, apenas uma cópia de slice evita vazamentos de memória se você quiser reduzir um slice grande.
Ao usar o slicing, devemos lembrar que podemos enfrentar uma situação que leva a efeitos colaterais não intencionais. Se o slice resultante tiver um comprimento menor que sua capacidade, o acréscimo poderá alterar o slice original. Se quisermos restringir a gama de possíveis efeitos colaterais, podemos usar uma cópia de slice ou a expressão de slice completa, o que nos impede de fazer uma cópia.
Note
s[low:high:max](expressão de slice completo): Esta instrução cria um slice semelhante àquele criado com s[low:high], exceto que a capacidade de slice resultante é igual a max - low.
Trabalhando com um slice de ponteiros ou estruturas com campos de ponteiro, você pode evitar vazamentos de memória marcando como nulos os elementos excluídos por uma operação de fatiamento.
Vazamento de capacidade
Lembre-se de que fatiar um slice ou array grande pode levar a um potencial alto consumo de memória. O espaço restante não será recuperado pelo GC e podemos manter um grande array de apoio, apesar de usarmos apenas alguns elementos. Usar uma cópia em slice é a solução para evitar tal caso.
Quando usamos a operação de fatiamento com ponteiros ou estruturas com campos de ponteiro, precisamos saber que o GC não recuperará esses elementos. Nesse caso, as duas opções são realizar uma cópia ou marcar explicitamente os elementos restantes ou seus campos como nil.
Ao criar um mapa, inicialize-o com um determinado comprimento se o seu comprimento já for conhecido. Isso reduz o número de alocações e melhora o desempenho.
Um mapa fornece uma coleção não ordenada de pares chave-valor em que todas as chaves são distintas. No Go, um mapa é baseado na estrutura de dados da tabela hash. Internamente, uma tabela hash é uma matriz de intervalos e cada intervalo é um ponteiro para uma matriz de pares de valores-chave.
Se soubermos de antemão o número de elementos que um mapa conterá, devemos criá-lo fornecendo um tamanho inicial. Fazer isso evita o crescimento potencial do mapa, o que é bastante pesado em termos de computação porque requer a realocação de espaço suficiente e o reequilíbrio de todos os elementos.
Um mapa sempre pode crescer na memória, mas nunca diminui. Portanto, se isso causar alguns problemas de memória, você pode tentar diferentes opções, como forçar Go a recriar o mapa ou usar ponteiros.
Para comparar tipos em Go, você pode usar os operadores == e != se dois tipos forem comparáveis: booleanos, numerais, strings, ponteiros, canais e estruturas são compostos inteiramente de tipos comparáveis. Caso contrário, você pode usar reflect.DeepEquale pagar o preço da reflexão ou usar implementações e bibliotecas personalizadas.
É essencial entender como usar == e != para fazer comparações de forma eficaz. Podemos usar esses operadores em operandos comparáveis:
Booleans—Compara se dois booleanos são iguais.
Numerics (int, float, and complex types)—Compare se dois números são iguais.
Strings—Compare se duas strings são iguais.
Channels—Compare se dois canais foram criados pela mesma chamada a ser feita ou se ambos são nulos.
Interfaces—Compare se duas interfaces têm tipos dinâmicos idênticos e valores dinâmicos iguais ou se ambas são nulas.
Pointers—Compare se dois ponteiros apontam para o mesmo valor na memória ou se ambos são nulos.
Structs and arrays—Compare se são compostas de tipos semelhantes.
Note
Também podemos usar os operadores ?, >=, < e > com tipos numéricos para comparar valores e com strings para comparar sua ordem lexical.
Se os operandos não forem comparáveis (por exemplo, slices e mapas), teremos que usar outras opções, como reflexão. A reflexão é uma forma de metaprogramação e se refere à capacidade de um aplicativo de introspectar e modificar sua estrutura e comportamento. Por exemplo, em Go, podemos usar reflect.DeepEqual. Esta função informa se dois elementos são profundamente iguais percorrendo recursivamente dois valores. Os elementos que ele aceita são tipos básicos mais arrays, estruturas, slices, mapas, ponteiros, interfaces e funções. No entanto, o principal problema é a penalidade de desempenho.
Se o desempenho for crucial em tempo de execução, implementar nosso método customizado pode ser a melhor solução. Uma observação adicional: devemos lembrar que a biblioteca padrão possui alguns métodos de comparação existentes. Por exemplo, podemos usar a função bytes.Compare otimizada para comparar duas slices de bytes. Antes de implementar um método customizado, precisamos ter certeza de não reinventar a roda.
Ignorando que os elementos são copiados em loops de range (#30)
TL;DR
O elemento de valor em um loop de range é uma cópia. Portanto, para modificar uma struct, por exemplo, acesse-a através de seu índice ou através de um loop for clássico (a menos que o elemento ou campo que você deseja modificar seja um ponteiro).
Um range loop permite iterar em diferentes estruturas de dados:
String
Array
Pointer to an array
Slice
Map
Receiving channel
Comparado a um for loop clássico, um loop range é uma maneira conveniente de iterar todos os elementos de uma dessas estruturas de dados, graças à sua sintaxe concisa.
Ainda assim, devemos lembrar que o elemento de valor em um range loop é uma cópia. Portanto, se o valor for uma estrutura que precisamos sofrer mutação, atualizaremos apenas a cópia, não o elemento em si, a menos que o valor ou campo que modificamos seja um ponteiro. As opções preferidas são acessar o elemento através do índice usando um range loop ou um loop for clássico.
Ignorando como os argumentos são avaliados em range loops (canais e arrays) (#31)
TL;DR
Entender que a expressão passada ao operador range é avaliada apenas uma vez antes do início do loop pode ajudar a evitar erros comuns, como atribuição ineficiente em canal ou iteração de slice.
O range loop avalia a expressão fornecida apenas uma vez, antes do início do loop, fazendo uma cópia (independentemente do tipo). Devemos lembrar deste comportamento para evitar erros comuns que podem, por exemplo, nos levar a acessar o elemento errado. Por exemplo:
Usar break ou continue com um rótulo impõe a quebra de uma instrução específica. Isso pode ser útil com instruções switch ou select dentro de loops.
Uma instrução break é comumente usada para encerrar a execução de um loop. Quando loops são usados em conjunto com switch ou select, os desenvolvedores frequentemente cometem o erro de quebrar a instrução errada. Por exemplo:
A instrução break não encerra o loop for: em vez disso, ela encerra a instrução switch. Portanto, em vez de iterar de 0 a 2, este código itera de 0 a 4: 0 1 2 3 4.
Uma regra essencial a ter em mente é que uma instrução break encerra a execução da instrução for, switch, ou mais interna select. No exemplo anterior, ele encerra a instrução switch.
Para quebrar o loop em vez da instrução switch, a maneira mais idiomática é usar um rótulo:
Aqui, associamos o looprótulo ao for loop. Então, como fornecemos o loop rótulo para a instrução break, ela interrompe o loop, não a opção. Portanto, esta nova versão será impressa 0 1 2, como esperávamos.
Extrair a lógica do loop dentro de uma função leva à execução de uma instrução defer no final de cada iteração.
A instrução defer atrasa a execução de uma chamada até que a função circundante retorne. É usado principalmente para reduzir o código padrão. Por exemplo, se um recurso precisar ser fechado eventualmente, podemos usar defer para evitar a repetição das chamadas de fechamento antes de cada return.
Um erro comum com defer é esquecer que ele agenda uma chamada de função quando a função circundante retorna. Por exemplo:
funcreadFiles(ch<-chanstring)error{forpath:=rangech{file,err:=os.Open(path)iferr!=nil{returnerr}deferfile.Close()// Do something with file}returnnil}
As chamadas defer não são executadas durante cada iteração do loop, mas quando a função readFiles retorna. Se readFiles não retornar, os descritores de arquivos ficarão abertos para sempre, causando vazamentos.
Uma opção comum para corrigir esse problema é criar uma função circundante após defer, chamada durante cada iteração:
funcreadFiles(ch<-chanstring)error{forpath:=rangech{iferr:=readFile(path);err!=nil{returnerr}}returnnil}funcreadFile(pathstring)error{file,err:=os.Open(path)iferr!=nil{returnerr}deferfile.Close()// Do something with filereturnnil}
Outra solução é tornar a função readFile um encerramento, mas intrinsecamente, esta permanece a mesma solução: adicionar outra função circundante para executar as chamadas defer durante cada iteração.
Entender que uma runa corresponde ao conceito de um ponto de código Unicode e que pode ser composta de múltiplos bytes deve fazer parte do conhecimento básico do desenvolvedor Go para trabalhar com precisão com strings.
Como as runas estão por toda parte no Go, é importante entender o seguinte:
Um conjunto de caracteres é um conjunto de caracteres, enquanto uma codificação descreve como traduzir um conjunto de caracteres em binário.
No Go, uma string faz referência a uma fatia imutável de bytes arbitrários.
O código-fonte Go é codificado usando UTF-8. Portanto, todos os literais de string são strings UTF-8. Mas como uma string pode conter bytes arbitrários, se for obtida de outro lugar (não do código-fonte), não há garantia de que seja baseada na codificação UTF-8.
A rune corresponde ao conceito de ponto de código Unicode, significando um item representado por um único valor.
Usando UTF-8, um ponto de código Unicode pode ser codificado em 1 a 4 bytes.
Usar len() na string em Go retorna o número de bytes, não o número de runas.
Iterar em uma string com o operador range itera nas runas com o índice correspondente ao índice inicial da sequência de bytes da runa. Para acessar um índice de runa específico (como a terceira runa), converta a string em um arquivo []rune.
Iterar em uma string é uma operação comum para desenvolvedores. Talvez queiramos realizar uma operação para cada runa na string ou implementar uma função personalizada para procurar uma substring específica. Em ambos os casos, temos que iterar nas diferentes runas de uma string. Mas é fácil ficar confuso sobre como funciona a iteração.
position 0: h
position 1: Ã
position 3: l
position 4: l
position 5: o
len=6
Vamos destacar três pontos que podem ser confusos:
A segunda runa é Ã na saída em vez de ê.
Saltamos da posição 1 para a posição 3: o que há na posição 2?
len retorna uma contagem de 6, enquanto s contém apenas 5 runas.
Vamos começar com a última observação. Já mencionamos que len retorna o número de bytes em uma string, não o número de runas. Como atribuímos uma string literal a s, s é uma string UTF-8. Enquanto isso, o caractere especial “ê” não é codificado em um único byte; requer 2 bytes. Portanto, chamar len(s) retorna 6.
Enquanto isso, no exemplo anterior, temos que entender que não repetimos cada runa; em vez disso, iteramos sobre cada índice inicial de uma runa:
Imprimir s[i] não imprime a i-ésima runa; imprime a representação UTF-8 do byte em index i. Portanto, imprimimos "hÃllo" em vez de "hêllo".
Se quisermos imprimir todas as diferentes runas, podemos usar o elemento value do operador range:
Observe que esta solução introduz uma sobrecarga de tempo de execução em comparação com a anterior. Na verdade, converter uma string em uma fatia de runas requer a alocação de uma fatia adicional e a conversão dos bytes em runas: uma complexidade de tempo O(n) com n o número de bytes na string. Portanto, se quisermos iterar todas as runas, devemos usar a primeira solução.
Porém, se quisermos acessar a i-ésima runa de uma string com a primeira opção, não teremos acesso ao índice da runa; em vez disso, conhecemos o índice inicial de uma runa na sequência de bytes.
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"))
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.
Durante cada iteração, o operador += concatena com s a sequência de valores. À primeira vista, esta função pode não parecer errada. Mas com esta implementação, esquecemos uma das principais características de uma string: a sua imutabilidade. Portanto, cada iteração não é atualizada s; ele realoca uma nova string na memória, o que impacta significativamente o desempenho desta função.
Felizmente, existe uma solução para lidar com esse problema, usando strings.Builder:
Durante cada iteração, construímos a string resultante chamando o método WriteString que anexa o conteúdo do valor ao seu buffer interno, minimizando assim a cópia da memória.
Note
WriteString retorna um erro como segunda saída, mas nós o ignoramos propositalmente. Na verdade, este método nunca retornará um erro diferente de zero. Então, qual é o propósito deste método retornar um erro como parte de sua assinatura? strings.Builder implementa a io.StringWriter interface, que contém um único método: WriteString(s string) (n int, err error). Portanto, para estar em conformidade com esta interface, WriteString deve retornar um erro.
Internamente, strings.Builder contém uma fatia de bytes. Cada chamada para WriteString resulta em uma chamada para anexar nesta fatia. Existem dois impactos. Primeiro, esta estrutura não deve ser usada simultaneamente, pois as chamadas append levariam a condições de corrida. O segundo impacto é algo que vimos no mistake #21, "Inicialização de slice ineficiente": se o comprimento futuro de uma slice já for conhecido, devemos pré-alocá-la. Para isso, strings.Builder expõe um método Grow(n int) para garantir espaço para outros n bytes:
Vamos executar um benchmark para comparar as três versões (v1 usando +=; v2 usando strings.Builder{} sem pré-alocação; e v3 usando strings.Builder{} com pré-alocação). A slice de entrada contém 1.000 strings e cada string contém 1.000 bytes:
Como podemos ver, a versão mais recente é de longe a mais eficiente: 99% mais rápida que a v1 e 78% mais rápida que a v2.
strings.Builder é a solução recomendada para concatenar uma lista de strings. Normalmente, esta solução 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ão é recomendado, pois isso tornará o código um pouco menos legível do que usar o operador += or fmt.Sprintf.
Lembrar que o pacote bytes oferece as mesmas operações que o pacote strings pode ajudar a evitar conversões extras de bytes/string.
Ao optar por trabalhar com uma string ou um []byte, a maioria dos programadores tende a preferir strings por conveniência. Mas a maior parte da E/S é realmente feita com []byte. Por exemplo, io.Reader, io.Writer e io.ReadAll trabalham com []byte, não com strings.
Quando nos perguntamos se devemos trabalhar com strings ou []byte, lembremos que trabalhar com []bytenão é necessariamente menos conveniente. Na verdade, todas as funções exportadas do pacote strings também possuem alternativas no pacote bytes: Split, Count, Contains, Index e assim por diante. Portanto, estejamos fazendo I/O ou não, devemos primeiro verificar se poderíamos implementar um fluxo de trabalho completo usando bytes em vez de strings e evitar o preço de conversões adicionais.
Usar cópias em vez de substrings pode evitar vazamentos de memória, pois a string retornada por uma operação de substring será apoiada pela mesma matriz de bytes.
In mistake #26, “Slices and memory leaks,” 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.
A decisão de usar um valor ou um receptor de ponteiro deve ser tomada com base em fatores como o tipo, se deve sofrer mutação, se contém um campo que não pode ser copiado e o tamanho do objeto. Em caso de dúvida, use um receptor de ponteiro.
Choosing between value and pointer receivers isn’t always straightforward. Let’s discuss some of the conditions to help us choose.
A receiver must be a pointer
If the method needs to mutate the receiver. This rule is also valid if the receiver is a slice and a method needs to append elements:
If the method receiver contains a field that cannot be copied: for example, a type part of the sync package (see #74, “Copying a sync type”).
A receiver should be a pointer
If the receiver is a large object. Using a pointer can make the call more efficient, as doing so prevents making an extensive copy. When in doubt about how large is large, benchmarking can be the solution; it’s pretty much impossible to state a specific size, because it depends on many factors.
A receiver must be a value
If we have to enforce a receiver’s immutability.
If the receiver is a map, function, or channel. Otherwise, a compilation error
occurs.
A receiver should be a value
If the receiver is a slice that doesn’t have to be mutated.
If the receiver is a small array or struct that is naturally a value type without mutable fields, such as time.Time.
If the receiver is a basic type such as int, float64, or string.
Of course, it’s impossible to be exhaustive, as there will always be edge cases, but this section’s goal was to provide guidance to cover most cases. By default, we can choose to go with a value receiver unless there’s a good reason not to do so. In doubt, we should use a pointer receiver.
Nunca usando parâmetros de resultado nomeados (#43)
TL;DR
Usar parâmetros de resultado nomeados pode ser uma maneira eficiente de melhorar a legibilidade de uma função/método, especialmente se vários parâmetros de resultado tiverem o mesmo tipo. Em alguns casos, esta abordagem também pode ser conveniente porque os parâmetros de resultado nomeados são inicializados com seu valor zero. Mas tenha cuidado com os possíveis 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’s 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’s an example that uses a named result parameter b:
funcf(aint)(bint){b=areturn}
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’s a clear benefit.
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’re not careful enough. For example, can you spot what’s wrong with this code?
func(lloc)getCoordinates(ctxcontext.Context,addressstring)(lat,lngfloat32,errerror){isValid:=l.validateAddress(address)(1)if!isValid{return0,0,errors.New("invalid address")}ifctx.Err()!=nil{(2)return0,0,err}// Get and return coordinates}
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’t assigned any value to the err variable. It’s 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’t always straightforward to spot while reading code. Therefore, let’s remain cautious when using named result parameters, to avoid potential side effects.
Ao retornar uma interface, tenha cuidado para não retornar um ponteiro nulo, mas um valor nulo explícito. Caso contrário, poderão ocorrer consequências não intencionais e o chamador receberá um valor diferente de zero.
Usando um nome de arquivo como entrada de função (#46)
TL;DR
Projetar funções para receber tipos io.Reader em vez de nomes de arquivos melhora a capacidade de reutilização de uma função 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.
Ignorando como argumentos defer e receptores são avaliados (avaliação de argumentos, ponteiros e receptores de valor) (#47)
TL;DR
Passar um ponteiro para uma função defer e agrupar uma chamada dentro de um closure são duas soluções possíveis para superar a avaliação 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.
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:
funcf()error{varstatusstringdefernotify(&status)deferincrementCounter(&status)// The rest of the function unchanged}
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’s another solution: calling a closure (an anonymous function value that references variables from outside its body) as a defer statement:
funcf()error{varstatusstringdeferfunc(){notify(status)incrementCounter(status)}()// The rest of the function unchanged}
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’t require notify and incrementCounter to change their signature.
Let's also note this behavior applies with method receiver: the receiver is evaluated immediately.
Usar panic é uma opção para lidar com erros no Go. No entanto, só deve ser usado com moderação em condições irrecuperáveis: por exemplo, para sinalizar um erro do programador ou quando você não consegue carregar uma dependência obrigatória.
In Go, panic is a built-in function that stops the ordinary flow:
This code prints a and then stops before printing b:
a
panic: foo
goroutine 1 [running]:
main.main()
main.go:7 +0xb3
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.
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ê quiser evitar isso, não use a agrupamento automático 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:
Adding additional context to an error
Marking an error as a specific error
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’t 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’t use error wrapping but error transformation, for example, using fmt.Errorf with the %v directive.
Comparando um tipo de erro de forma imprecisa (#50)
TL;DR
Se você usar o agrupamento de erros do Go 1.13 com a diretiva %w e fmt.Errorf, a comparação de um erro com um tipo deverá ser feita usando errors.As. Caso contrário, se o erro retornado que você deseja verificar for embrulhado, as verificações falharão.
Se você usar o agrupamento de erros do Go 1.13 com a diretiva %w e fmt.Errorf, a comparação de um erro ou de um valor deverá ser feita usando errors.As. Caso contrário, se o erro retornado que você deseja verificar for embrulhado, as verificações falharão.
A sentinel error is an error defined as a global variable:
import"errors"varErrFoo=errors.New("foo")
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:
Expected errors should be designed as error values (sentinel errors): var ErrFoo = errors.New("foo").
Unexpected errors should be designed as error types: type BarError struct { ... }, with BarError implementing the error interface.
If we use error wrapping in our application with the %w directive and fmt.Errorf, checking an error against a specific value should be done using errors.Is instead of ==. Thus, even if the sentinel error is wrapped, errors.Is can recursively unwrap it and compare each error in the chain against the provided value.
Na maioria das situações, um erro deve ser tratado apenas uma vez. Registrar um erro é tratar um erro. Portanto, você deve escolher entre registrar ou retornar um erro. Em muitos casos, o embrulho automático de erros é a solução, 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.
Ignorar um erro, seja durante uma chamada de função ou em uma função defer, deve ser feito explicitamente usando o identificador em branco. Caso contrário, os futuros leitores poderão ficar confusos sobre se foi intencional ou um erro.
Em muitos casos, você não deve ignorar um erro retornado por uma função defer. Manipule-o diretamente ou propague-o para o chamador, dependendo do contexto. Se você quiser ignorá-lo, use o identificador em branco.
Consider the following code:
funcf(){// ...notify()// Error handling is omitted}funcnotify()error{// ...}
From a maintainability perspective, the code can lead to some issues. Let’s consider a new reader looking at it. This reader notices that notify returns an error but that the error isn’t 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
In terms of compilation and run time, this approach doesn’t change anything compared to the first piece of code. But this new version makes explicit that we aren’t interested in the error. Also, we can add a comment that indicates the rationale for why an error is ignored:
// At-most once delivery.// Hence, it's accepted to miss some of them in case of errors._=notify()
Compreender as diferenças fundamentais entre simultaneidade e paralelismo é a base do conhecimento do desenvolvedor Go. A simultaneidade tem a ver com estrutura, enquanto o paralelismo tem a ver com execução.
Concurrency and parallelism are not the same:
Concurrency is about structure. We can change a sequential implementation into a concurrent one by introducing different steps that separate concurrent goroutines can tackle.
Meanwhile, parallelism is about execution. We can use parallism at the steps level by adding more parallel goroutines.
In summary, concurrency provides a structure to solve a problem with parts that may be parallelized. Therefore, concurrency enables parallelism.
Pensar que a simultaneidade é sempre mais rápida (#56)
TL;DR
Para ser um desenvolvedor proficiente, você deve reconhecer que a simultaneidade nem sempre é mais rápida. As soluções que envolvem a paralelização de cargas de trabalho mínimas podem não ser necessariamente mais rápidas do que uma implementação sequencial. A avaliação comparativa de soluções sequenciais versus soluções simultâneas deve ser a forma de validar suposições.
Ficar confuso sobre quando usar canais ou mutexes (#57)
TL;DR
Estar ciente das interações goroutine também pode ser útil ao decidir entre canais e mutexes. Em geral, goroutines paralelas requerem sincronização e, portanto, mutexes. Por outro lado, goroutines simultâneas geralmente requerem coordenação e orquestração 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:
G1 and G2 are parallel goroutines. They may be two goroutines executing the same function that keeps receiving messages from a channel, or perhaps two goroutines executing the same HTTP handler at the same time.
On the other hand, G1 and G3 are concurrent goroutines, as are G2 and G3. All the goroutines are part of an overall concurrent structure, but G1 and G2 perform the first step, whereas G3 does the next step.
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—therefore, channels.
Regarding concurrent goroutines, there’s 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’s important to know whether goroutines are parallel or concurrent because, in general, we need mutexes for parallel goroutines and channels for concurrent ones.
Não entender os problemas de corrida (corridas de dados vs. condições de corrida e o modelo de memória Go) (#58)
TL;DR
Ser proficiente em simultaneidade também significa compreender que corridas de dados e condições de corrida são conceitos diferentes. As corridas de dados ocorrem quando várias goroutines acessam simultaneamente o mesmo local de memória e pelo menos uma delas está gravando. Enquanto isso, estar livre de disputa de dados não significa necessariamente execução determinística. Quando um comportamento depende da sequência ou do tempo de eventos que não podem ser controlados, esta é uma condição 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.
Data Race
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:
Using the sync/atomic package
In synchronizing the two goroutines with an ad hoc data structure like a mutex
Using channels to make the two goroutines communicating to ensure that a variable is updated by only one goroutine at a time
Race Condition
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’t be controlled. Here, the timing of events is the goroutines’ execution order.
In summary, when we work in concurrent applications, it’s 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’t 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.
Não compreender os impactos de simultaneidade de um tipo de carga de trabalho (#59)
TL;DR
Ao criar um determinado número de goroutines, considere o tipo de carga de trabalho. Criar goroutines vinculadas à CPU significa limitar esse número próximo à variável GOMAXPROCS (baseado por padrão no número de núcleos de CPU no host). A criação 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:
The speed of the CPU—For example, running a merge sort algorithm. The workload is called CPU-bound.
The speed of I/O—For example, making a REST call or a database query. The workload is called I/O-bound.
The amount of available memory—The workload is called memory-bound.
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.
Os contextos Go também são um dos pilares da simultaneidade em Go. Um contexto permite que você 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.
Deadline
A deadline refers to a specific point in time determined with one of the following:
A time.Duration from now (for example, in 250 ms)
A time.Time (for example, 2023-02-07 00:00:00 UTC)
The semantics of a deadline convey that an ongoing activity should be stopped if this deadline is met. An activity is, for example, an I/O request or a goroutine waiting to receive a message from a channel.
Cancellation signals
Another use case for Go contexts is to carry a cancellation signal. Let’s 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.
Context values
The last use case for Go contexts is to carry a key-value list. What’s 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.
Catching a context cancellation
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,
The Done channel related to a context created with context.WithCancel is closed when the cancel function is called.
The Done channel related to a context created with context.WithDeadline is closed when the deadline has expired.
One thing to note is that the internal channel should be closed when a context is canceled or has met a deadline, instead of when it receives a specific value, because the closure of a channel is the only channel action that all the consumer goroutines will receive. This way, all the consumers will be notified once a context is canceled or a deadline is reached.
In summary, to be a proficient Go developer, we have to understand what a context is and how to use it. In general, a function that users wait for should take a context, as doing so allows upstream callers to decide when calling this function should be aborted.
Compreender as condições em que um contexto pode ser cancelado deve ser importante ao propagá-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’s 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’t 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:
funchandler(whttp.ResponseWriter,r*http.Request){response,err:=doSomeTask(r.Context(),r)iferr!=nil{http.Error(w,err.Error(),http.StatusInternalServerError)return}gofunc(){err:=publish(r.Context(),response)// Do something with err}()writeResponse(response)}
What’s wrong with this piece of code? We have to know that the context attached to an HTTP request can cancel in different conditions:
When the client’s connection closes
In the case of an HTTP/2 request, when the request is canceled
When the response has been written back to the client
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’s probably OK to call publish with a context already canceled so the message isn’t 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:
If the response is written after the Kafka publication, we both return a response and publish a message successfully
However, if the response is written before or during the Kafka publication, the message shouldn’t be published.
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.
Iniciando uma goroutine sem saber quando interrompê-la (#62)
TL;DR
Evitar vazamentos significa estar ciente de que sempre que uma goroutine for iniciada, você deve ter um plano para interrompê-la eventualmente.
Goroutines are easy and cheap to start—so 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’s discuss a concrete example. We will design an application that needs to watch some external configuration (for example, using a database connection). Here’s a first implementation:
funcmain(){newWatcher()// Run the application}typewatcherstruct{/* Some resources */}funcnewWatcher(){w:=watcher{}gow.watch()// Creates a goroutine that watches some external configuration}
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’t 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:
funcmain(){ctx,cancel:=context.WithCancel(context.Background())defercancel()newWatcher(ctx)// Run the application}funcnewWatcher(ctxcontext.Context){w:=watcher{}gow.watch(ctx)}
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—and that’s a design flaw.
The problem is that we used signaling to convey that a goroutine had to be stopped. We didn’t block the parent goroutine until the resources had been closed. Let’s make sure we do:
funcmain(){w:=newWatcher()deferw.close()// Run the application}funcnewWatcher()watcher{w:=watcher{}gow.watch()returnw}func(wwatcher)close(){// Close the resources}
Instead of signaling watcher that it’s 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’s 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’s probably safer to wait for this goroutine to complete before exiting the application. This way, we can ensure that the resources can be freed.
Não ter cuidado com goroutines e variáveis de loop (#63)
Warning
Este erro não é mais relevante no Go 1.22 (detalhes).
Esperando um comportamento determinístico usando seleção e canais (#64)
TL;DR
Compreender que com select vários canais escolhe o caso aleatoriamente se múltiplas opções forem possíveis evita fazer suposições 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):
Instead of consuming the 10 messages, we only received a few of them. What’s 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’s 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’s 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.
Channels are a mechanism for communicating across goroutines via signaling. A signal can be either with or without data.
Let’s 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(chanbool)
Now, let’s say we interact with an API that provides us with such a channel. Because it’s a channel of Booleans, we can receive either true or false messages. It’s probably clear what true conveys. But what does false mean? Does it mean we haven’t 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’s the case, meaning we don’t 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{}.
Não usar canais nulos (#66)
TL;DR
O uso de canais nulos deve fazer parte do seu conjunto de ferramentas de simultaneidade porque permite remover casos de instruções select, por exemplo.
What should this code do?
varchchanint<-ch
ch is a chan int type. The zero value of a channel being nil, ch is nil. The goroutine won’t panic; however, it will block forever.
The principle is the same if we send a message to a nil channel. This goroutine blocks forever:
varchchanintch<-0
Then what’s 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:
funcmerge(ch1,ch2<-chanint)<-chanint{ch:=make(chanint,1)gofunc(){forch1!=nil||ch2!=nil{// Continue if at least one channel isn’t nilselect{casev,open:=<-ch1:if!open{ch1=nil// Assign ch1 to a nil channel once closedbreak}ch<-vcasev,open:=<-ch2:if!open{ch2=nil// Assigns ch2 to a nil channel once closedbreak}ch<-v}}close(ch)}()returnch}
This elegant solution relies on nil channels to somehow remove one case from the select statement.
Let’s keep this idea in mind: nil channels are useful in some conditions and should be part of the Go developer’s toolset when dealing with concurrent code.
Decida cuidadosamente o tipo de canal correto a ser usado, considerando o problema. Somente canais sem buffer oferecem fortes garantias de sincronização. Para canais em buffer, você 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(chanint)ch2:=make(chanint,0)
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(chanint,1)
With a buffered channel, a sender can send messages while the channel isn’t full. Once the channel is full, it will block until a receiver goroutine receives a message:
The first send isn’t blocking, whereas the second one is, as the channel is full at this stage.
What's the main difference between unbuffered and buffered channels:
An unbuffered channel enables synchronization. We have the guarantee that two goroutines will be in a known state: one receiving and another sending a message.
A buffered channel doesn’t provide any strong synchronization. Indeed, a producer goroutine can send a message and then continue its execution if the channel isn’t full. The only guarantee is that a goroutine won’t receive a message before it is sent. But this is only a guarantee because of causality (you don’t drink your coffee before you prepare it).
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’s a list of possible cases where we should use another size:
While using a worker pooling-like pattern, meaning spinning a fixed number of goroutines that need to send data to a shared channel. In that case, we can tie the channel size to the number of goroutines created.
When using channels for rate-limiting problems. For example, if we need to enforce resource utilization by bounding the number of requests, we should set up the channel size according to the limit.
If we are outside of these cases, using a different channel size should be done cautiously. Let’s bear in mind that deciding about an accurate queue size isn’t 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.
Esquecendo os possíveis efeitos colaterais da formatação de strings (#68)
TL;DR
Estar ciente de que a formatação de strings pode levar à chamada de funções existentes significa estar atento a possíveis impasses e outras disputas de dados.
It’s pretty easy to forget the potential side effects of string formatting while working in a concurrent application.
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.
Deadlock
Can you see what the problem is in this code with a Customer struct exposing an UpdateAge method and implementing the fmt.Stringer interface?
typeCustomerstruct{mutexsync.RWMutex// Uses a sync.RWMutex to protect concurrent accessesidstringageint}func(c*Customer)UpdateAge(ageint)error{c.mutex.Lock()// Locks and defers unlock as we update Customerdeferc.mutex.Unlock()ifage<0{// Returns an error if age is negativereturnfmt.Errorf("age should be positive for customer %v",c)}c.age=agereturnnil}func(c*Customer)String()string{c.mutex.RLock()// Locks and defers unlock as we read Customerdeferc.mutex.RUnlock()returnfmt.Sprintf("id %s, age %d",c.id,c.age)}
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’t 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(ageint)error{ifage<0{returnfmt.Errorf("age should be positive for customer %v",c)}c.mutex.Lock()<1>deferc.mutex.Unlock()c.age=agereturnnil}
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(ageint)error{c.mutex.Lock()deferc.mutex.Unlock()ifage<0{returnfmt.Errorf("age should be positive for customer id %s",c.id)}c.age=agereturnnil}
In concurrent applications, we should remain cautious about the possible side effects of string formatting.
As chamadas append nem sempre são isentas de disputa de dados; portanto, não 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)gofunc(){// In a new goroutine, appends a new element on ss1:=append(s,1)fmt.Println(s1)}()gofunc(){// Sames2:=append(s,1)fmt.Println(s2)}()
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’t mutate the existing array; hence, it doesn’t lead to a data race.
Now, let’s 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?
The answer is yes. We create a slice with make([]int, 0, 1). Therefore, the array isn’t 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.
Usando mutexes imprecisamente com slices e maps (#70)
TL;DR
Lembrar que slices e maps são 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:
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{c.mu.RLock()balances:=c.balances// Creates a copy of the balances mapc.mu.RUnlock()sum:=0.for_,balance:=rangebalances{// Iterates over the copy, outside of the critical sectionsum+=balance}returnsum/float64(len(balances))}
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’s 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’t 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{c.mu.RLock()deferc.mu.RUnlock()// Unlocks when the function returnssum:=0.for_,balance:=rangec.balances{sum+=balance}returnsum/float64(len(c.balances))}
Another option, if the iteration operation isn’t lightweight, is to work on an actual copy of the data and protect only the copy:
func(c*Cache)AverageBalance()float64{c.mu.RLock()m:=make(map[string]float64,len(c.balances))// Copies the mapfork,v:=rangec.balances{m[k]=v}c.mu.RUnlock()sum:=0.for_,balance:=rangem{sum+=balance}returnsum/float64(len(m))}
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’t 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’s be cautious when designing critical sections and make sure the boundaries are accurately defined.
Seja cauteloso com funções que aceitam um arquivo time.Duration. Mesmo que a passagem de um número inteiro seja permitida, tente usar a API time para evitar qualquer possível confusão.
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)for{select{case<-ticker.C:// Do something}}
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:
Evitar chamadas para funções time.After repetidas (como loops ou manipuladores HTTP) pode evitar pico de consumo de memória. Os recursos criados por time.After são liberados somente quando o cronômetro 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:
funcconsumer(ch<-chanEvent){for{select{caseevent:=<-ch:handle(event)case<-time.After(time.Hour):log.Println("warning: no messages received")}}}
The source code of the function time.After is as follows:
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:
funcconsumer(ch<-chanEvent){for{ctx,cancel:=context.WithTimeout(context.Background(),time.Hour)select{caseevent:=<-ch:cancel()handle(event)case<-ctx.Done():log.Println("warning: no messages received")}}}
We can also use time.NewTimer like so:
funcconsumer(ch<-chanEvent){timerDuration:=1*time.Hourtimer:=time.NewTimer(timerDuration)for{timer.Reset(timerDuration)select{caseevent:=<-ch:handle(event)case<-timer.C:log.Println("warning: no messages received")}}}
Comportamento inesperado devido à incorporação de tipo
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ão.
Ao comparar duas estruturas time.Time, lembre-se de que time.Time contém um relógio de parede e um relógio monotônico, e a comparação usando o operador == é feita em ambos os relógios.
Para evitar suposições erradas ao fornecer um map ao desempacotar (unmarshaling) dados JSON, lembre-se de que os valores numéricos são convertidos para float64 por padrão.
Esquecer sql.Open não necessariamente estabelece conexões com um banco de dados
Esquecer sql.Open não necessariamente estabelece conexões com um banco de dados
Chame o método Ping ou PingContext se precisar testar sua configuração e garantir que um banco de dados esteja acessível.
Esquecendo a instrução return após responder a uma solicitação HTTP (#80)
TL;DR
Para evitar comportamentos inesperados nas implementações do manipulador HTTP, certifique-se de não perder a instrução return se quiser que um manipulador pare após http.Error.
Consider the following HTTP handler that handles an error from foo using http.Error:
If we run this code and err != nil, the HTTP response would be:
foo
all good
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)
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:
funchandler(whttp.ResponseWriter,req*http.Request){err:=foo(req)iferr!=nil{http.Error(w,"foo",http.StatusInternalServerError)return// Adds the return statement}_,_=w.Write([]byte("all good"))w.WriteHeader(http.StatusCreated)}
Para aplicativos de nível de produção, não use as implementações de cliente e servidor HTTP padrão. Essas implementações não possuem tempos limite e comportamentos que deveriam ser obrigatórios na produção.
Não categorizar testes (tags de construção, variáveis de ambiente e modo abreviado) (#82)
TL;DR
Categorizar testes usando sinalizadores de construção, variáveis de ambiente ou modo curto torna o processo de teste mais eficiente. Você pode criar categorias de teste usando sinalizadores de construção ou variáveis de ambiente (por exemplo, testes de unidade versus testes de integração) e diferenciar testes curtos de testes de longa duração para decidir quais tipos de testes executar.
A ativação do sinalizador -race é altamente recomendada ao escrever aplicativos simultâneos. Isso permite que você detecte possíveis corridas de dados que podem levar a bugs de software.
In Go, the race detector isn’t a static analysis tool used during compilation; instead, it’s 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:
gotest-race./...
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:
Não usar modos de execução de teste (paralelo e aleatório) (#84)
TL;DR
Usar o sinalizador -parallel é uma forma eficiente de acelerar testes, especialmente os de longa duração. Use o sinalizador -shuffle para ajudar a garantir que um conjunto de testes não se baseie em suposições erradas que possam ocultar bugs.
Não usar testes baseados em tabela (#85)
TL;DR
Os testes baseados em tabelas são uma maneira eficiente de agrupar um conjunto de testes semelhantes para evitar a duplicação de código e facilitar o manuseio de atualizações futuras.
Evite interrupções usando a sincronização para tornar o teste menos instável e mais robusto. Se a sincronização não for possível, considere uma abordagem de nova tentativa.
Não lidar com a API de tempo de forma eficiente (#87)
TL;DR
Entender como lidar com funções usando a API time é outra maneira de tornar um teste menos complicado. Você pode usar técnicas padrão, como lidar com o tempo como parte de uma dependência oculta ou solicitar que os clientes o forneçam.
Use métodos de tempo para preservar a precisão de um benchmark.
Aumentar o tempo de teste ou usar ferramentas como o benchstat pode ser útil ao lidar com micro-benchmarks.
Tenha cuidado com os resultados de um micro-benchmark se o sistema que executa o aplicativo for diferente daquele que executa o micro-benchmark.
Certifique-se de que a função em teste cause um efeito colateral, para evitar que as otimizações do compilador enganem você sobre os resultados do benchmark.
Para evitar o efeito observador, force um benchmark a recriar os dados usados por uma função vinculada à CPU.
Fuzzing é uma estratégia eficiente para detectar entradas aleatórias, inesperadas ou malformadas em funções e métodos complexos, a fim de descobrir vulnerabilidades, bugs ou até mesmo travamentos potenciais.
Compreender como usar caches de CPU é importante para otimizar aplicativos vinculados à CPU porque o cache L1 é cerca de 50 a 100 vezes mais rápido que a memória principal.
Linha de cache
Estar consciente do conceito de linha de cache é fundamental para entender como organizar dados em aplicativos com uso intensivo de dados. Uma CPU não busca memória palavra por palavra; em vez disso, geralmente copia um bloco de memória para uma linha de cache de 64 bytes. Para aproveitar ao máximo cada linha de cache individual, imponha a localidade espacial.
Tornar o código previsível para a CPU também pode ser uma forma eficiente de otimizar certas funções. Por exemplo, uma passada unitária ou constante é previsível para a CPU, mas uma passada não unitária (por exemplo, uma lista vinculada) não é previsível.
Para evitar um avanço crítico e, portanto, utilizar apenas uma pequena parte do cache, esteja ciente de que os caches são particionados.
Escrevendo código simultâneo que leva a compartilhamento falso (#92)
TL;DR
Saber que níveis mais baixos de caches de CPU não são compartilhados entre todos os núcleos ajuda a evitar padrões que degradam o desempenho, como compartilhamento falso ao escrever código de simultaneidade. Compartilhar memória é uma ilusão.
Não levando em consideração o paralelismo no nível de instrução (#93)
TL;DR
Use o ILP para otimizar partes específicas do seu código para permitir que uma CPU execute tantas instruções paralelas quanto possível. Identificar perigos nos dados é uma das etapas principais.
Você pode evitar erros comuns lembrando que no Go os tipos básicos são alinhados com seu próprio 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ção de memória e potencialmente uma melhor localidade espacial).
Compreender as diferenças fundamentais entre heap e pilha também deve fazer parte do seu conhecimento básico ao otimizar um aplicativo Go. As alocações de pilha são quase gratuitas, enquanto as alocações de heap são mais lentas e dependem do GC para limpar a memória.
Não saber como reduzir alocações (mudança de API, otimizações de compilador e sync.Pool) (#96)
TL;DR
A redução das alocações também é um aspecto essencial da otimização de um aplicativo Go. Isso pode ser feito de diferentes maneiras, como projetar a API cuidadosamente para evitar compartilhamento, compreender as otimizações comuns do compilador Go e usar sync.Pool.
Compreender como ajustar o GC pode levar a vários benefícios, como lidar com aumentos repentinos de carga com mais eficiência.
Não entendendo os impactos da execução do Go no Docker e Kubernetes (#100)
TL;DR
Para ajudar a evitar a limitação da CPU quando implantado no Docker e no Kubernetes, lembre-se de que Go não 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.
Community
Thanks to all the contributors:
Comments
================================================
FILE: site/search/search_index.json
================================================
{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Common Go Mistakes","text":""},{"location":"#common-go-mistakes","title":"Common Go Mistakes","text":"The Coder Cafe
If you enjoyed my book, you may be interested in my latest project: The Coder Cafe, a newsletter for coders.
Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve timeless concepts with your coffee. Written by a Google SWE and published author, we help you grow as an engineer, one coffee at a time.
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.
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.
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:
When an if block returns, we should omit the else block in all cases. For example, we shouldn\u2019t write:
We can also follow this logic with a non-happy path:
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.
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:
They can limit error management.
They can complicate how to implement tests (for example, an external dependency must be set up, which may not be necessary for the scope of unit tests).
If the initialization requires us to set a state, that has to be done through global variables.
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.
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.
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.
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:
It shouldn\u2019t be used solely as some syntactic sugar to simplify accessing a field (such as Foo.Baz() instead of Foo.Bar.Baz()). If this is the only rationale, let\u2019s not embed the inner type and use a field instead.
It shouldn\u2019t promote data (fields) or a behavior (methods) we want to hide from the outside: for example, if it allows clients to access a locking behavior that should remain private to the struct.
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:
An unexported struct holds the configuration: options.
Each option is a function that returns the same type: type Option func(options *options) error. For example, WithPort accepts an int argument that represents the port and returns an Option type that represents how to update the options struct.
type options struct {\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
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.
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:
https://golang.org/cmd/vet\u2014A standard Go analyzer
Besides linters, we should also use code formatters to fix code style. Here is a list of some code formatters for you to try:
https://golang.org/cmd/gofmt\u2014A standard Go code formatter
https://godoc.org/golang.org/x/tools/cmd/goimports\u2014A standard Go imports formatter
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:
Binary\u2014Uses a 0b or 0B prefix (for example, 0b100 is equal to 4 in base 10)
Hexadecimal\u2014Uses an 0x or 0X prefix (for example, 0xF is equal to 15 in base 10)
Imaginary\u2014Uses an i suffix (for example, 3i)
We can also use an underscore character (_) as a separator for readability. For example, we can write 1 billion this way: 1_000_000_000. We can also use the underscore character with other representations (for example, 0b00_00_01).
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).
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:
When comparing two floating-point numbers, check that their difference is within an acceptable range.
When performing additions or subtractions, group operations with a similar order of magnitude for better accuracy.
To favor accuracy, if a sequence of operations requires addition, subtraction, multiplication, or division, perform the multiplication and division operations first.
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.
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
var s []string if we aren\u2019t sure about the final length and the slice can be empty
[]string(nil) as syntactic sugar to create a nil and empty slice
make([]string, length) if the future length is known
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.
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.
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.
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:
Booleans\u2014Compare whether two Booleans are equal.
Numerics (int, float, and complex types)\u2014Compare whether two numerics are equal.
Strings\u2014Compare whether two strings are equal.
Channels\u2014Compare whether two channels were created by the same call to make or if both are nil.
Interfaces\u2014Compare whether two interfaces have identical dynamic types and equal dynamic values or if both are nil.
Pointers\u2014Compare whether two pointers point to the same value in memory or if both are nil.
Structs and arrays\u2014Compare whether they are composed of similar types.
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:
String
Array
Pointer to an array
Slice
Map
Receiving channel
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:
Doesn\u2019t order the data by keys
Doesn\u2019t preserve the insertion order
Doesn\u2019t have a deterministic iteration order
Doesn\u2019t guarantee that an element added during an iteration will be produced during this iteration
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:
A charset is a set of characters, whereas an encoding describes how to translate a charset into binary.
In Go, a string references an immutable slice of arbitrary bytes.
Go source code is encoded using UTF-8. Hence, all string literals are UTF-8 strings. But because a string can contain arbitrary bytes, if it\u2019s obtained from somewhere else (not the source code), it isn\u2019t guaranteed to be based on the UTF-8 encoding.
A rune corresponds to the concept of a Unicode code point, meaning an item represented by a single value.
Using UTF-8, a Unicode code point can be encoded into 1 to 4 bytes.
Using len() on a string in Go returns the number of bytes, not the number of runes.
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
Let's highlight three points that might be confusing:
The second rune is \u00c3 in the output instead of \u00ea.
We jumped from position 1 to position 3: what is at position 2?
len returns a count of 6, whereas s contains only 5 runes.
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.
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:
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.
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
If the method needs to mutate the receiver. This rule is also valid if the receiver is a slice and a method needs to append elements:
type slice []int\n\nfunc (s *slice) add(element int) {\n *s = append(*s, element)\n}\n
If the method receiver contains a field that cannot be copied: for example, a type part of the sync package (see #74, \u201cCopying a sync type\u201d).
A receiver should be a pointer
If the receiver is a large object. Using a pointer can make the call more efficient, as doing so prevents making an extensive copy. When in doubt about how large is large, benchmarking can be the solution; it\u2019s pretty much impossible to state a specific size, because it depends on many factors.
A receiver must be a value
If we have to enforce a receiver\u2019s immutability.
If the receiver is a map, function, or channel. Otherwise, a compilation error occurs.
A receiver should be a value
If the receiver is a slice that doesn\u2019t have to be mutated.
If the receiver is a small array or struct that is naturally a value type without mutable fields, such as time.Time.
If the receiver is a basic type such as int, float64, or string.
Of course, it\u2019s impossible to be exhaustive, as there will always be edge cases, but this section\u2019s goal was to provide guidance to cover most cases. By default, we can choose to go with a value receiver unless there\u2019s a good reason not to do so. In doubt, we should use a pointer receiver.
Source code
"},{"location":"#never-using-named-result-parameters-43","title":"Never using named result parameters (#43)","text":"TL;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.
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:
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:
Adding additional context to an error
Marking an error as a specific error
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:
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:
Expected errors should be designed as error values (sentinel errors): var ErrFoo = errors.New(\"foo\").
Unexpected errors should be designed as error types: type BarError struct { ... }, with BarError implementing the error interface.
If we use error wrapping in our application with the %w directive and fmt.Errorf, checking an error against a specific value should be done using errors.Is instead of ==. Thus, even if the sentinel error is wrapped, errors.Is can recursively unwrap it and compare each error in the chain against the provided value.
Source code
"},{"location":"#handling-an-error-twice-52","title":"Handling an error twice (#52)","text":"TL;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.
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.
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:
Concurrency is about structure. We can change a sequential implementation into a concurrent one by introducing different steps that separate concurrent goroutines can tackle.
Meanwhile, parallelism is about execution. We can use parallism at the steps level by adding more parallel goroutines.
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:
G1 and G2 are parallel goroutines. They may be two goroutines executing the same function that keeps receiving messages from a channel, or perhaps two goroutines executing the same HTTP handler at the same time.
On the other hand, G1 and G3 are concurrent goroutines, as are G2 and G3. All the goroutines are part of an overall concurrent structure, but G1 and G2 perform the first step, whereas G3 does the next step.
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.
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:
Using the sync/atomic package
In synchronizing the two goroutines with an ad hoc data structure like a mutex
Using channels to make the two goroutines communicating to ensure that a variable is updated by only one goroutine at a time
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:
The speed of the CPU\u2014For example, running a merge sort algorithm. The workload is called CPU-bound.
The speed of I/O\u2014For example, making a REST call or a database query. The workload is called I/O-bound.
The amount of available memory\u2014The workload is called memory-bound.
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.
A deadline refers to a specific point in time determined with one of the following:
A time.Duration from now (for example, in 250 ms)
A time.Time (for example, 2023-02-07 00:00:00 UTC)
The semantics of a deadline convey that an ongoing activity should be stopped if this deadline is met. An activity is, for example, an I/O request or a goroutine waiting to receive a message from a channel.
Another use case for Go contexts is to carry a cancellation signal. Let\u2019s imagine that we want to create an application that calls CreateFileWatcher(ctx context.Context, filename string) within another goroutine. This function creates a specific file watcher that keeps reading from a file and catches updates. When the provided context expires or is canceled, this function handles it to close the file descriptor.
The last use case for Go contexts is to carry a key-value list. What\u2019s the point of having a context carrying a key-value list? Because Go contexts are generic and mainstream, there are infinite use cases.
For example, if we use tracing, we may want different subfunctions to share the same correlation ID. Some developers may consider this ID too invasive to be part of the function signature. In this regard, we could also decide to include it as part of the provided context.
"},{"location":"#catching-a-context-cancellation","title":"Catching a context cancellation","text":"
The context.Context type exports a Done method that returns a receive-only notification channel: <-chan struct{}. This channel is closed when the work associated with the context should be canceled. For example,
The Done channel related to a context created with context.WithCancel is closed when the cancel function is called.
The Done channel related to a context created with context.WithDeadline is closed when the deadline has expired.
One thing to note is that the internal channel should be closed when a context is canceled or has met a deadline, instead of when it receives a specific value, because the closure of a channel is the only channel action that all the consumer goroutines will receive. This way, all the consumers will be notified once a context is canceled or a deadline is reached.
In summary, to be a proficient Go developer, we have to understand what a context is and how to use it. In general, a function that users wait for should take a context, as doing so allows upstream callers to decide when calling this function should be aborted.
Source code
"},{"location":"#concurrency-practice","title":"Concurrency: Practice","text":""},{"location":"#propagating-an-inappropriate-context-61","title":"Propagating an inappropriate context (#61)","text":"TL;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:
When the client\u2019s connection closes
In the case of an HTTP/2 request, when the request is canceled
When the response has been written back to the client
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:
If the response is written after the Kafka publication, we both return a response and publish a message successfully
However, if the response is written before or during the Kafka publication, the message shouldn\u2019t be published.
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:
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:
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:
An unbuffered channel enables synchronization. We have the guarantee that two goroutines will be in a known state: one receiving and another sending a message.
A buffered channel doesn\u2019t provide any strong synchronization. Indeed, a producer goroutine can send a message and then continue its execution if the channel isn\u2019t full. The only guarantee is that a goroutine won\u2019t receive a message before it is sent. But this is only a guarantee because of causality (you don\u2019t drink your coffee before you prepare it).
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:
While using a worker pooling-like pattern, meaning spinning a fixed number of goroutines that need to send data to a shared channel. In that case, we can tie the channel size to the number of goroutines created.
When using channels for rate-limiting problems. For example, if we need to enforce resource utilization by bounding the number of requests, we should set up the channel size according to the limit.
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.
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?
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):
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 := maps.Clone(c.balances)\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.
To accurately use sync.WaitGroup, call the Add method before spinning up goroutines.
In the following example, we will initialize a wait group, start three goroutines that will update a counter atomically, and then wait for them to complete. We want to wait for these three goroutines to print the value of the counter (which should be 3):
wg := sync.WaitGroup{}\nvar v uint64\nfor i := 0; i < 3; i++ {\n go func() {\n wg.Add(1)\n atomic.AddUint64(&v, 1)\n wg.Done()\n }()\n}\nwg.Wait()\nfmt.Println(v)\n
If we run this example, we get a non-deterministic value: the code can print any value from 0 to 3. Also, if we enable the -race flag, Go will even catch a data race.
The problem is that wg.Add(1) is called within the newly created goroutine, not in the parent goroutine. Hence, there is no guarantee that we have indicated to the wait group that we want to wait for three goroutines before calling wg.Wait().
To fix this issue, we can call wg.Add before the loop:
wg := sync.WaitGroup{}\nvar v uint64\nwg.Add(3)\nfor i := 0; i < 3; i++ {\n go func() {\n atomic.AddUint64(&v, 1)\n wg.Done()\n }()\n}\nwg.Wait()\nfmt.Println(v)\n
Or inside the loop but not in the newly created goroutine:
wg := sync.WaitGroup{}\nvar v uint64\nfor i := 0; i < 3; i++ {\n wg.Add(1)\n go func() {\n atomic.AddUint64(&v, 1)\n wg.Done()\n }()\n}\nwg.Wait()\nfmt.Println(v)\n
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:
"},{"location":"#timeafter-and-memory-leaks-76","title":"time.After and memory leaks (#76)","text":"Warning
This mistake isn't relevant anymore from Go 1.23 (details).
"},{"location":"#json-handling-common-mistakes-77","title":"JSON handling common mistakes (#77)","text":"
Unexpected behavior because of type embedding
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
JSON and the monotonic clock
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
Map of any
To avoid wrong assumptions when you provide a map while unmarshaling JSON data, remember that numerics are converted to float64 by default.
Forgetting that sql.Open doesn't necessarily establish connections to a database
Call the Ping or PingContext method if you need to test your configuration and make sure a database is reachable.
Source code
Forgetting about connections pooling
Configure the database connection parameters for production-grade applications.
Not using prepared statements
Using SQL prepared statements makes queries more efficient and more secure.
Source code
Mishandling null values
Deal with nullable columns in tables using pointers or sql.NullXXX types.
Source code
Not handling rows iteration errors
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:
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:
"},{"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
Indicates that goroutine 7 was writing
Indicates that the main goroutine was reading
Indicates when the goroutine 7 was created
Let\u2019s make sure we are comfortable reading these messages. Go always logs the following:
The concurrent goroutines that are incriminated: here, the main goroutine and goroutine 7.
Where accesses occur in the code: in this case, lines 9 and 10.
When these goroutines were created: goroutine 7 was created in main().
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:
"},{"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":"
The httptest package is helpful for dealing with HTTP applications. It provides a set of utilities to test both clients and servers.
Source code
The iotest package helps write io.Reader and test that an application is tolerant to errors.
Use time methods to preserve the accuracy of a benchmark.
Increasing benchtime or using tools such as benchstat can be helpful when dealing with micro-benchmarks.
Be careful with the results of a micro-benchmark if the system that ends up running the application is different from the one running the micro-benchmark.
Make sure the function under test leads to a side effect, to prevent compiler optimizations from fooling you about the benchmark results.
To prevent the observer effect, force a benchmark to re-create the data used by a CPU-bound function.
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":"
Code coverage
Use code coverage with the -coverprofile flag to quickly see which part of the code needs more attention.
Testing from a different package
Place unit tests in a different package to enforce writing tests that focus on an exposed behavior, not internals.
Source code
Utility functions
Handling errors using the *testing.T variable instead of the classic if err != nil makes code shorter and easier to read.
Source code
Setup and teardown
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":"
CPU architecture
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.
Cache line
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
Slice of structs vs. struct of slices
Source code
Predictability
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
Cache placement policy
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":"Warning
This mistake isn't relevant anymore from Go 1.25 (details).
"},{"location":"#powered-by","title":"Powered by","text":""},{"location":"20-slice/","title":"Not understanding slice length and capacity (#20)","text":""},{"location":"20-slice/#not-understanding-slice-length-and-capacity","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 (#28)","text":""},{"location":"28-maps-memory-leaks/#maps-and-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:
Allocate an empty map.
Add 1 million elements.
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]bytemap[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.
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.
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:
io.Reader reads data from a source
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\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:
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:
Retrieving the number of elements in the collection
Reporting whether one element must be sorted before another
Swapping two elements
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.
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:
Use the concrete implementation via integration tests
Use a mock (or any kind of test double) via unit tests
Or both
Let\u2019s now discuss another use case: to restrict a behavior.
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:
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:
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.
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 (#56)","text":""},{"location":"56-concurrency-faster/#thinking-concurrency-is-always-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.
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:
Concurrent \u2014 Two or more threads can start, run, and complete in overlapping time periods.
Parallel \u2014 The same task can be executed multiple times at once.
The OS is responsible for scheduling the thread\u2019s processes optimally so that:
All the threads can consume CPU cycles without being starved for too much time.
The workload is distributed as evenly as possible among the different CPU cores.
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):
G \u2014 Goroutine
M \u2014 OS thread (stands for machine)
P \u2014 CPU core (stands for processor)
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:
Executing \u2014 The goroutine is scheduled on an M and executing its instructions.
Runnable \u2014 The goroutine is waiting to be in an executing state.
Waiting \u2014 The goroutine is stopped and pending something completing, such as a system call or a synchronization operation (such as acquiring a mutex).
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.
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:
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:
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.
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.
In general, we should never guess about performance. When writing optimizations, so many factors may come into play that even if we have a strong opinion about the results, it\u2019s rarely a bad idea to test them. However, writing benchmarks isn\u2019t straightforward. It can be pretty simple to write inaccurate benchmarks and make wrong assumptions based on them. The goal of this post is to examine four common and concrete traps leading to inaccuracy:
Before discussing these traps, let\u2019s briefly review how benchmarks work in Go. The skeleton of a benchmark is as follows:
func BenchmarkFoo(b *testing.B) {\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:
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:
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
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):
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:
During each loop iteration, assign the result to a local variable (local in the context of the benchmark function).
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:
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):
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:
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 (#9)","text":""},{"location":"9-generics/#being-confused-about-when-to-use-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.
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:
First, it increases boilerplate code. Indeed, whenever we want to add a case, it will require duplicating the range loop.
Meanwhile, the function now accepts an empty interface, which means we are losing some of the benefits of Go being a typed language. Indeed, checking whether a type is supported is done at runtime instead of compile-time. Hence, we also need to return an error if the provided type is unknown.
Last but not least, as the key type can be either int or string, we are obliged to return a slice of empty interfaces to factor out key types. This approach increases the effort on the caller-side as the client may also have to perform a type check of the keys or extra conversion.
Thanks to generics, we can now refactor this code using type parameters.
Type parameters are generic types we can use with functions and types. For example, the following function accepts a type parameter:
func foo[T any](t T) {\n // ...\n}\n
When calling foo, we will pass a type argument of any type. Passing a type argument is called instantiation because the work is done at compile time which keeps type safety as part of the core language features and avoids runtime overheads.
Let\u2019s get back to the getKeys function and use type parameters to write a generic version that would accept any kind of map:
func getKeys[K comparable, V any](m map[K]V) []K {\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:
A set of behaviors (methods)
But also arbitrary type
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:
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:
Data structures. For example, we can use generics to factor out the element type if we implement a binary tree, a linked list, or a heap.
Functions working with slices, maps, and channels of any type. For example, a function to merge two channels would work with any channel type. Hence, we could use type parameters to factor out the channel type:
Meanwhile, instead of factoring out a type, we can factor out behaviors. For example, the sort package contains functions to sort different slice types such as sort.Ints or sort.Float64s. Using type parameters, we can factor out the sorting behaviors that rely on three methods, Len, Less, and Swap:
type sliceFn[T any] struct { // Use type parameter\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?
When just calling a method of the type argument. For example, consider a function that receives an io.Writer and call the Write method:
When it makes our code more complex. Generics are never mandatory, and as Go developers, we have been able to live without them for more than a decade. If writing generic functions or structures we figure out that it doesn\u2019t make our code clearer, we should probably reconsider our decision for this particular use case.
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 (#92)","text":""},{"location":"92-false-sharing/#writing-concurrent-code-that-leads-to-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:
The sum of all the Input.a fields into Result.sumA
The sum of all the Input.b fields into Result.sumB
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 (#98)","text":""},{"location":"98-profiling-execution-tracing/#not-using-go-diagnostics-tooling","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.
Profiling provides insights into the execution of an application. It allows us to resolve performance issues, detect contention, locate memory leaks, and more. These insights can be collected via several profiles:
CPU\u2014 Determines where an application spends its time
Goroutine\u2014 Reports the stack traces of the ongoing goroutines
Heap\u2014 Reports heap memory allocation to monitor current memory usage and check for possible memory leaks
Mutex\u2014 Reports lock contentions to see the behaviors of the mutexes used in our code and whether an application spends too much time in locking calls
Block\u2014 Shows where goroutines block waiting on synchronization primitives
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.
Importing net/http/pprof leads to a side effect that allows us to reach the pprof URL: http://host/debug/pprof. Note that enabling pprof is safe even in production (https://go.dev/doc/diagnostics#profiling). The profiles that impact performance, such as CPU profiling, aren\u2019t enabled by default, nor do they run continuously: they are activated only for a specific period.
Now that we have seen how to expose a pprof endpoint, let\u2019s discuss the most common profiles.
The CPU profiler relies on the OS and signaling. When it is activated, the application asks the OS to interrupt it every 10 ms by default via a SIGPROF signal. When the application receives a SIGPROF, it suspends the current activity and transfers the execution to the profiler. The profiler collects data such as the current goroutine activity and aggregates execution statistics that we can retrieve. Then it stops, and the execution resumes until the next SIGPROF.
We can access the /debug/pprof/profile endpoint to activate CPU profiling. Accessing this endpoint executes CPU profiling for 30 seconds by default. For 30 seconds, our application is interrupted every 10 ms. Note that we can change these two default values: we can use the seconds parameter to pass to the endpoint how long the profiling should last (for example, /debug/pprof/profile?seconds=15), and we can change the interruption rate (even to less than 10 ms). But in most cases, 10 ms should be enough, and in decreasing this value (meaning increasing the rate), we should be careful not to harm performance. After 30 seconds, we download the results of the CPU profiler.
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:
Too many calls to runtime.mallogc can mean an excessive number of small heap allocations that we can try to minimize.
Too much time spent in channel operations or mutex locks can indicate excessive contention that is harming the application\u2019s performance.
Too much time spent on syscall.Read or syscall.Write means the application spends a significant amount of time in Kernel mode. Working on I/O buffering may be an avenue for improvement.
These are the kinds of insights we can get from the CPU profiler. It\u2019s valuable to understand the hottest code path and identify bottlenecks. But it won\u2019t determine more than the configured rate because the CPU profiler is executed at a fixed pace (by default, 10 ms). To get finer-grained insights, we should use tracing, which we discuss later in this post.
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.
Heap profiling allows us to get statistics about the current heap usage. Like CPU profiling, heap profiling is sample-based. We can change this rate, but we shouldn\u2019t be too granular because the more we decrease the rate, the more effort heap profiling will require to collect data. By default, samples are profiled at one allocation for every 512 KB of heap allocation.
If we reach /debug/pprof/heap/, we get raw data that can be hard to read. However, we can download a heap profile using /debug/pprof/heap/?debug=0 and then open it with go tool (the same command as in the previous section) to navigate into the data using the web UI.
The next figure shows an example of a heap graph. Calling the MetadataResponse.decode method leads to allocating 1536 KB of heap data (which represents 6.32% of the total heap). However, 0 out of these 1536 KB were allocated by this function directly, so we need to inspect the second call. The TopicMetadata.decode method allocated 512 KB out of the 1536 KB; the rest \u2014 1024 KB \u2014 were allocated in another method.
Figure 3: A heap graph.
This is how we can navigate the call chain to understand what part of an application is responsible for most of the heap allocations. We can also look at different sample types:
alloc_objects\u2014 Total number of objects allocated
alloc_space\u2014 Total amount of memory allocated
inuse_objects \u2014 Number of objects allocated and not yet released
inuse_space\u2014 Amount of memory allocated and not yet released
Another very helpful capability with heap profiling is tracking memory leaks. With a GC-based language, the usual procedure is the following:
Trigger a GC.
Download heap data.
Wait for a few seconds/minutes.
Trigger another GC.
Download another heap data.
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:
Go to /debug/pprof/heap?gc=1 (trigger the GC and download the heap profile).
Wait for a few seconds/minutes.
Go to /debug/pprof/heap?gc=1 again.
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.
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.
The block profile reports where ongoing goroutines block waiting on synchronization primitives. Possibilities include
Sending or receiving on an unbuffered channel
Sending to a full channel
Receiving from an empty channel
Mutex contention
Network or filesystem waits
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:
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 haven\u2019t mentioned the threadcreate profile because it\u2019s been broken since 2013 (https://github.com/golang/go/issues/6104).
Be sure to enable only one profiler at a time: for example, do not enable CPU and heap profiling simultaneously. Doing so can lead to erroneous observations.
pprof is extensible, and we can create our own custom profiles using pprof.Profile.
We have seen the most important profiles that we can enable to help us understand how an application performs and possible avenues for optimization. In general, enabling pprof is recommended, even in production, because in most cases it offers an excellent balance between its footprint and the amount of insight we can get from it. Some profiles, such as the CPU profile, lead to performance penalties but only during the time they are enabled.
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:
Understanding runtime events such as how the GC performs
Understanding how goroutines execute
Identifying poorly parallelized execution
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?
CPU profiling:
Sample-based
Per function
Doesn\u2019t go below the sampling rate (10 ms by default)
User-level traces:
Not sample-based
Per-goroutine execution (unless we use the runtime/trace package)
Time executions aren\u2019t bound by any rate
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/#100-go-mistakes-and-how-to-avoid-them","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":"
100 Go Mistakes and How to Avoid Them (\ud83c\uddec\ud83c\udde7 edition: paper, digital, or audiobook)
Manning (please make sure to use my personal discount code for -35%: au35har)
Go\u8a00\u8a9e100Tips \u958b\u767a\u8005\u306b\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044\u3078\u306e\u5bfe\u51e6\u6cd5 (\ud83c\uddef\ud83c\uddf5 edition: paper or digital)
Amazon: .co.jp
100\u4e2aGo\u8bed\u8a00\u5178\u578b\u9519\u8bef (\ud83c\udde8\ud83c\uddf3 edition: paper or digital)
Douban.com
Go 100\uac00\uc9c0 \uc2e4\uc218 \ud328\ud134\uacfc \uc194\ub8e8\uc158 (\ud83c\uddf0\ud83c\uddf7 edition: paper or digital)
Yes24.com
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":"Read the First Chapter","text":""},{"location":"chapter-1/#go-simple-to-learn-but-hard-to-master","title":"Go: Simple to learn but hard to master","text":"
This chapter covers
What makes Go an efficient, scalable, and productive language
Exploring why Go is simple to learn but hard to master
Presenting the common types of mistakes made by developers
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.
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:
Stability\u2014Even though Go receives frequent updates (including improvements and security patches), it remains a stable language. Some may even consider this one of the best features of the language.
Expressivity\u2014We can define expressivity in a programming language by how naturally and intuitively we can write and read code. A reduced number of keywords and limited ways to solve common problems make Go an expressive language for large codebases.
Compilation\u2014As developers, what can be more exasperating than having to wait for a build to test our application? Targeting fast compilation times has always been a conscious goal for the language designers. This, in turn, enables productivity.
Safety\u2014Go is a strong, statically typed language. Hence, it has strict compiletime rules, which ensure the code is type-safe in most cases.
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
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.
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.
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.
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.
Go is a modern programming language that enables developer productivity, which is crucial for most companies today.
Go is simple to learn but not easy to master. This is why we need to deepen our knowledge to make the most effective use of the language.
Learning via mistakes and concrete examples is a powerful way to be proficient in a language. This book will accelerate our path to proficiency by exploring 100 common mistakes.
J. S. Moser, H. S. Schroder, et al., \u201cMind Your Errors: Evidence for a Neural Mechanism Linking Growth Mindset to Adaptive Posterror Adjustments,\u201d Psychological Science, vol. 22, no. 12, pp. 1484\u20131489, Dec. 2011.\u00a0\u21a9
J. Metcalfe, \u201cLearning from Errors,\u201d Annual Review of Psychology, vol. 68, pp. 465\u2013489, Jan. 2017.\u00a0\u21a9
Synopsys, \u201cThe Cost of Poor Software Quality in the US: A 2020 Report.\u201d 2020. https://news.synopsys.com/2021-01-06-Synopsys-Sponsored-CISQ-Research-Estimates-Cost-of-Poor-Software-Quality-in-the-US-2-08-Trillion-in-2020.\u00a0\u21a9
"},{"location":"external/","title":"External Resources","text":""},{"location":"external/#external-resources","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":"
Episode
Spotify
"},{"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":"
"},{"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":"Japanese Version","text":""},{"location":"ja/#go","title":"Go\u8a00\u8a9e\u3067\u3042\u308a\u304c\u3061\u306a\u9593\u9055\u3044","text":"The Coder Cafe
Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve one essential concept for coders. Written by a senior software engineer at Google, it's perfectly brewed for your morning coffee, helping you grow your skills deeply.
\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
if \u30d6\u30ed\u30c3\u30af\u304c\u8fd4\u3055\u308c\u308b\u3068\u304d\u3001\u3059\u3079\u3066\u306e\u5834\u5408\u306b\u304a\u3044\u3066 else \u30d6\u30ed\u30c3\u30af\u3092\u7701\u7565\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 \u305f\u3068\u3048\u3070\u3001\u6b21\u306e\u3088\u3046\u306b\u66f8\u304f\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002
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
\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
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
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
\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
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
\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
\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\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
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
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
"},{"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
\u6587\u5b57\u5217
\u914d\u5217
\u914d\u5217\u3078\u306e\u30dd\u30a4\u30f3\u30bf
\u30b9\u30e9\u30a4\u30b9
\u30de\u30c3\u30d7
\u53d7\u4fe1\u30c1\u30e3\u30cd\u30eb
\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
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
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
\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
position 1 \u304b\u3089 position 3 \u306b\u30b8\u30e3\u30f3\u30d7\u3057\u307e\u3057\u305f\u3002 position 2 \u306b\u306f\u4f55\u304c\u3042\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002
len \u306f 6 \u3092\u8fd4\u3057\u307e\u3059\u304c\u3001s \u306b\u306f 5 \u3064\u306e\u30eb\u30fc\u30f3\u3057\u304b\u542b\u307e\u308c\u3066\u3044\u307e\u305b\u3093\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
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
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
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
\u4e88\u671f\u3055\u308c\u308b\u30a8\u30e9\u30fc\u306f\u30a8\u30e9\u30fc\u5024\uff08\u30bb\u30f3\u30c1\u30cd\u30eb\u30a8\u30e9\u30fc\uff09\u3068\u3057\u3066\u8a2d\u8a08\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\uff1a var ErrFoo =errors.New(\"foo\")\u3002
\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
CPU \u306e\u901f\u5ea6 - \u305f\u3068\u3048\u3070\u3001\u30de\u30fc\u30b8\u30bd\u30fc\u30c8\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5b9f\u884c\u304c\u3053\u308c\u306b\u3042\u305f\u308a\u307e\u3059\u3002\u3053\u306e\u30ef\u30fc\u30af\u30ed\u30fc\u30c9\u306f CPU \u30d0\u30a6\u30f3\u30c9\u3068\u547c\u3070\u308c\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
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
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
\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
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
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
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
\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
"},{"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
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
\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
"},{"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
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":"Brazilian Portuguese Version","text":""},{"location":"pt-br/#erros-comuns-de-go","title":"Erros comuns de Go","text":"The Coder Cafe
Se voc\u00ea gostou do meu livro, talvez se interesse pelo meu novo projeto: The Coder Cafe, uma newsletter di\u00e1ria para programadores.
Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve one essential concept for coders. Written by a senior software engineer at Google, it's perfectly brewed for your morning coffee, helping you grow your skills deeply.
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.
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.
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:
Quando um bloco if retorna, devemos omitir o else em todos os casos. Por exemplo, n\u00e3o dever\u00edamos escrever:
Tamb\u00e9m podemos seguir esta l\u00f3gica com um caminho n\u00e3o feliz:
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:
Elas podem limitar o gerenciamento de erros.
Elas podem complicar a implementa\u00e7\u00e3o de testes (por exemplo, uma depend\u00eancia externa deve ser configurada, o que pode n\u00e3o ser necess\u00e1rio para o escopo dos testes unit\u00e1rios).
Se a inicializa\u00e7\u00e3o exigir que definamos um estado, isso dever\u00e1 ser feito por meio de vari\u00e1veis \u200b\u200bglobais.
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.
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:
N\u00e3o deve ser usado apenas como um a\u00e7\u00facar sint\u00e1tico para simplificar o acesso a um campo (como Foo.Baz() em vez de Foo.Bar.Baz()). Se esta for a \u00fanica justificativa, n\u00e3o vamos incorporar o tipo interno e usar um campo.
N\u00e3o deve promover dados (campos) ou um comportamento (m\u00e9todos) que queremos ocultar do exterior: por exemplo, se permitir que os clientes acessem um comportamento de bloqueio que deve permanecer privado da struct.
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:
Uma estrutura n\u00e3o exportada cont\u00e9m a configura\u00e7\u00e3o: op\u00e7\u00f5es.
Cada op\u00e7\u00e3o \u00e9 uma fun\u00e7\u00e3o que retorna o mesmo tipo: type Option func(options *options) error. Por exemplo, WithPort aceita um argumento int que representa a porta e retorna um tipo Option que representa como atualizar a struct options.
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.
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:
https://golang.org/cmd/vet\u2014A standard Go analyzer
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:
https://golang.org/cmd/gofmt\u2014A standard Go code formatter
https://godoc.org/golang.org/x/tools/cmd/goimports\u2014A standard Go imports formatter
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:
Bin\u00e1rio\u2014Usa um prefixo 0b ou 0B(por exemplo, 0b100 \u00e9 igual a 4 na base 10)
Hexadecimal\u2014Usa um prefixo 0x ou 0X (por exemplo, 0xF \u00e9 igual a 15 na base 10)
Imagin\u00e1rio\u2014Usa um i sufixo (por exemplo, 3i)
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:
Ao comparar dois n\u00fameros de ponto flutuante, verifique se a diferen\u00e7a est\u00e1 dentro de um intervalo aceit\u00e1vel.
Ao realizar adi\u00e7\u00f5es ou subtra\u00e7\u00f5es, agrupe opera\u00e7\u00f5es com ordem de magnitude semelhante para melhor precis\u00e3o.
Para favorecer a precis\u00e3o, se uma sequ\u00eancia de opera\u00e7\u00f5es exigir adi\u00e7\u00e3o, subtra\u00e7\u00e3o, multiplica\u00e7\u00e3o ou divis\u00e3o, execute primeiro as opera\u00e7\u00f5es de multiplica\u00e7\u00e3o e divis\u00e3o.
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
var s []string se n\u00e3o tivermos certeza sobre o comprimento final e a fatia pode estar vazia
[]string(nil) como a\u00e7\u00facar sint\u00e1tico para criar uma fatia nula e vazia
make([]string, length) se o comprimento futuro for conhecido
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.
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:
Booleans\u2014Compara se dois booleanos s\u00e3o iguais.
Numerics (int, float, and complex types)\u2014Compare se dois n\u00fameros s\u00e3o iguais.
Strings\u2014Compare se duas strings s\u00e3o iguais.
Channels\u2014Compare se dois canais foram criados pela mesma chamada a ser feita ou se ambos s\u00e3o nulos.
Interfaces\u2014Compare se duas interfaces t\u00eam tipos din\u00e2micos id\u00eanticos e valores din\u00e2micos iguais ou se ambas s\u00e3o nulas.
Pointers\u2014Compare se dois ponteiros apontam para o mesmo valor na mem\u00f3ria ou se ambos s\u00e3o nulos.
Structs and arrays\u2014Compare se s\u00e3o compostas de tipos semelhantes.
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:
String
Array
Pointer to an array
Slice
Map
Receiving channel
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:
N\u00e3o ordena os dados por chaves
N\u00e3o preserva o pedido de inser\u00e7\u00e3o
N\u00e3o tem uma ordem de itera\u00e7\u00e3o determin\u00edstica
N\u00e3o garante que um elemento adicionado durante uma itera\u00e7\u00e3o ser\u00e1 produzido durante esta itera\u00e7\u00e3o
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:
Um conjunto de caracteres \u00e9 um conjunto de caracteres, enquanto uma codifica\u00e7\u00e3o descreve como traduzir um conjunto de caracteres em bin\u00e1rio.
No Go, uma string faz refer\u00eancia a uma fatia imut\u00e1vel de bytes arbitr\u00e1rios.
O c\u00f3digo-fonte Go \u00e9 codificado usando UTF-8. Portanto, todos os literais de string s\u00e3o strings UTF-8. Mas como uma string pode conter bytes arbitr\u00e1rios, se for obtida de outro lugar (n\u00e3o do c\u00f3digo-fonte), n\u00e3o h\u00e1 garantia de que seja baseada na codifica\u00e7\u00e3o UTF-8.
A rune corresponde ao conceito de ponto de c\u00f3digo Unicode, significando um item representado por um \u00fanico valor.
Usando UTF-8, um ponto de c\u00f3digo Unicode pode ser codificado em 1 a 4 bytes.
Usar len() na string em Go retorna o n\u00famero de bytes, n\u00e3o o n\u00famero de runas.
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
Vamos destacar tr\u00eas pontos que podem ser confusos:
A segunda runa \u00e9 \u00c3 na sa\u00edda em vez de \u00ea.
Saltamos da posi\u00e7\u00e3o 1 para a posi\u00e7\u00e3o 3: o que h\u00e1 na posi\u00e7\u00e3o 2?
len retorna uma contagem de 6, enquanto s cont\u00e9m apenas 5 runas.
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 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:
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
If the method needs to mutate the receiver. This rule is also valid if the receiver is a slice and a method needs to append elements:
type slice []int\n\nfunc (s *slice) add(element int) {\n *s = append(*s, element)\n}\n
If the method receiver contains a field that cannot be copied: for example, a type part of the sync package (see #74, \u201cCopying a sync type\u201d).
A receiver should be a pointer
If the receiver is a large object. Using a pointer can make the call more efficient, as doing so prevents making an extensive copy. When in doubt about how large is large, benchmarking can be the solution; it\u2019s pretty much impossible to state a specific size, because it depends on many factors.
A receiver must be a value
If we have to enforce a receiver\u2019s immutability.
If the receiver is a map, function, or channel. Otherwise, a compilation error occurs.
A receiver should be a value
If the receiver is a slice that doesn\u2019t have to be mutated.
If the receiver is a small array or struct that is naturally a value type without mutable fields, such as time.Time.
If the receiver is a basic type such as int, float64, or string.
Of course, it\u2019s impossible to be exhaustive, as there will always be edge cases, but this section\u2019s goal was to provide guidance to cover most cases. By default, we can choose to go with a value receiver unless there\u2019s a good reason not to do so. In doubt, we should use a pointer receiver.
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.
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:
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:
Adding additional context to an error
Marking an error as a specific error
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:
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:
Expected errors should be designed as error values (sentinel errors): var ErrFoo = errors.New(\"foo\").
Unexpected errors should be designed as error types: type BarError struct { ... }, with BarError implementing the error interface.
If we use error wrapping in our application with the %w directive and fmt.Errorf, checking an error against a specific value should be done using errors.Is instead of ==. Thus, even if the sentinel error is wrapped, errors.Is can recursively unwrap it and compare each error in the chain against the provided value.
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.
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:
Concurrency is about structure. We can change a sequential implementation into a concurrent one by introducing different steps that separate concurrent goroutines can tackle.
Meanwhile, parallelism is about execution. We can use parallism at the steps level by adding more parallel goroutines.
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:
G1 and G2 are parallel goroutines. They may be two goroutines executing the same function that keeps receiving messages from a channel, or perhaps two goroutines executing the same HTTP handler at the same time.
On the other hand, G1 and G3 are concurrent goroutines, as are G2 and G3. All the goroutines are part of an overall concurrent structure, but G1 and G2 perform the first step, whereas G3 does the next step.
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.
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:
Using the sync/atomic package
In synchronizing the two goroutines with an ad hoc data structure like a mutex
Using channels to make the two goroutines communicating to ensure that a variable is updated by only one goroutine at a time
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:
The speed of the CPU\u2014For example, running a merge sort algorithm. The workload is called CPU-bound.
The speed of I/O\u2014For example, making a REST call or a database query. The workload is called I/O-bound.
The amount of available memory\u2014The workload is called memory-bound.
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.
A deadline refers to a specific point in time determined with one of the following:
A time.Duration from now (for example, in 250 ms)
A time.Time (for example, 2023-02-07 00:00:00 UTC)
The semantics of a deadline convey that an ongoing activity should be stopped if this deadline is met. An activity is, for example, an I/O request or a goroutine waiting to receive a message from a channel.
Another use case for Go contexts is to carry a cancellation signal. Let\u2019s imagine that we want to create an application that calls CreateFileWatcher(ctx context.Context, filename string) within another goroutine. This function creates a specific file watcher that keeps reading from a file and catches updates. When the provided context expires or is canceled, this function handles it to close the file descriptor.
The last use case for Go contexts is to carry a key-value list. What\u2019s the point of having a context carrying a key-value list? Because Go contexts are generic and mainstream, there are infinite use cases.
For example, if we use tracing, we may want different subfunctions to share the same correlation ID. Some developers may consider this ID too invasive to be part of the function signature. In this regard, we could also decide to include it as part of the provided context.
"},{"location":"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,
The Done channel related to a context created with context.WithCancel is closed when the cancel function is called.
The Done channel related to a context created with context.WithDeadline is closed when the deadline has expired.
One thing to note is that the internal channel should be closed when a context is canceled or has met a deadline, instead of when it receives a specific value, because the closure of a channel is the only channel action that all the consumer goroutines will receive. This way, all the consumers will be notified once a context is canceled or a deadline is reached.
In summary, to be a proficient Go developer, we have to understand what a context is and how to use it. In general, a function that users wait for should take a context, as doing so allows upstream callers to decide when calling this function should be aborted.
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:
When the client\u2019s connection closes
In the case of an HTTP/2 request, when the request is canceled
When the response has been written back to the client
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:
If the response is written after the Kafka publication, we both return a response and publish a message successfully
However, if the response is written before or during the Kafka publication, the message shouldn\u2019t be published.
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:
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{}.
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:
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:
An unbuffered channel enables synchronization. We have the guarantee that two goroutines will be in a known state: one receiving and another sending a message.
A buffered channel doesn\u2019t provide any strong synchronization. Indeed, a producer goroutine can send a message and then continue its execution if the channel isn\u2019t full. The only guarantee is that a goroutine won\u2019t receive a message before it is sent. But this is only a guarantee because of causality (you don\u2019t drink your coffee before you prepare it).
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:
While using a worker pooling-like pattern, meaning spinning a fixed number of goroutines that need to send data to a shared channel. In that case, we can tie the channel size to the number of goroutines created.
When using channels for rate-limiting problems. For example, if we need to enforce resource utilization by bounding the number of requests, we should set up the channel size according to the limit.
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.
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?
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):
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.
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:
"},{"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":"
Comportamento inesperado devido \u00e0 incorpora\u00e7\u00e3o de tipo
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
JSON e o rel\u00f3gio monot\u00f4nico
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
Map de any
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
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
Esquecendo o pool de conex\u00f5es
Configure os par\u00e2metros de conex\u00e3o do banco de dados para aplicativos de n\u00edvel de produ\u00e7\u00e3o.
N\u00e3o usar declara\u00e7\u00f5es preparadas
O uso de instru\u00e7\u00f5es preparadas em SQL torna as consultas mais eficientes e seguras.
C\u00f3digo fonte
Tratamento incorreto de valores nulos
Lide com colunas anul\u00e1veis \u200b\u200bem tabelas usando ponteiros ou tipos sql.NullXXX.
C\u00f3digo fonte
N\u00e3o tratando de erros de itera\u00e7\u00e3o de linhas
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:
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:
"},{"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
Indicates that goroutine 7 was writing
Indicates that the main goroutine was reading
Indicates when the goroutine 7 was created
Let\u2019s make sure we are comfortable reading these messages. Go always logs the following:
The concurrent goroutines that are incriminated: here, the main goroutine and goroutine 7.
Where accesses occur in the code: in this case, lines 9 and 10.
When these goroutines were created: goroutine 7 was created in main().
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:
"},{"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":"
O pacote httptest \u00e9 \u00fatil para lidar com aplicativos HTTP. Ele fornece um conjunto de utilit\u00e1rios para testar clientes e servidores.
C\u00f3digo fonte
O pacote iotest ajuda a escrever io.Reader e testar se um aplicativo \u00e9 tolerante a erros.
Use m\u00e9todos de tempo para preservar a precis\u00e3o de um benchmark.
Aumentar o tempo de teste ou usar ferramentas como o benchstat pode ser \u00fatil ao lidar com micro-benchmarks.
Tenha cuidado com os resultados de um micro-benchmark se o sistema que executa o aplicativo for diferente daquele que executa o micro-benchmark.
Certifique-se de que a fun\u00e7\u00e3o em teste cause um efeito colateral, para evitar que as otimiza\u00e7\u00f5es do compilador enganem voc\u00ea sobre os resultados do benchmark.
Para evitar o efeito observador, force um benchmark a recriar os dados usados \u200b\u200bpor uma fun\u00e7\u00e3o vinculada \u00e0 CPU.
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":"
Cobertura de c\u00f3digo
Use a cobertura de c\u00f3digo com o sinalizador -coverprofile para ver rapidamente qual parte do c\u00f3digo precisa de mais aten\u00e7\u00e3o.
Testando de um pacote diferente
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
Fun\u00e7\u00f5es utilit\u00e1rias
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
Configura\u00e7\u00e3o e desmontagem
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":"
Arquitetura da CPU
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.
Linha de cache
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
Slice de estruturas vs. estrutura de slices
C\u00f3digo fonte
Previsibilidade
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
Pol\u00edtica de posicionamento de cache
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.
Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve one essential concept for coders. Written by a senior software engineer at Google, it's perfectly brewed for your morning coffee, helping you grow your skills deeply.
\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
\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/#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
\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
\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
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
\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
\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
\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/#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":"
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/#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
\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
\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/#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/#_12","title":"\u4f18\u5316\u6280\u672f","text":""},{"location":"zh/#cpu-cache-91","title":"\u4e0d\u7406\u89e3 CPU cache (#91)","text":"
CPU \u67b6\u6784
\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
Cache line
\u610f\u8bc6\u5230 cache line \u6982\u5ff5\u5bf9\u4e8e\u7406\u89e3\u5982\u4f55\u5728\u6570\u636e\u5bc6\u96c6\u578b\u5e94\u7528\u4e2d\u7ec4\u7ec7\u6570\u636e\u975e\u5e38\u5173\u952e\u3002CPU \u5e76\u4e0d\u662f\u4e00\u4e2a\u4e00\u4e2a\u5b57\u6765\u83b7\u53d6\u5185\u5b58\u3002\u76f8\u53cd\uff0c\u5b83\u901a\u5e38\u590d\u5236\u4e00\u4e2a 64 \u5b57\u8282\u957f\u5ea6\u7684 cache line\u3002\u4e3a\u4e86\u83b7\u5f97\u6bcf\u4e2a cache line \u7684\u6700\u5927\u6548\u7528\uff0c\u9700\u8981\u5b9e\u65bd\u7a7a\u95f4\u5c40\u90e8\u6027\u3002
\u4e00\u7cfb\u5217 struct \u5143\u7d20\u6784\u6210\u7684 slice vs. \u591a\u4e2a slice \u5b57\u6bb5\u6784\u6210\u7684 struct
\u6982\u7387\u6027\u7684\u95ee\u9898
\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
\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
\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
\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
Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve one essential concept for coders. Written by a senior software engineer at Google, it's perfectly brewed for your morning coffee, helping you grow your skills deeply.
Go 中比较两个类型值时,如果是可比较类型,那么可以使用 == 或者 != 运算符进行比较,比如:booleans、numerals、strings、pointers、channels,以及字段全部是可比较类型的 structs。其他情况下,你可以使用 reflect.DeepEqual 来比较,用反射的话会牺牲一点性能,也可以使用自定义的实现和其他库来完成。
控制结构
忽略了 range 循环变量是一个拷贝 (#30)
range 循环中的循环变量是遍历容器中元素值的一个拷贝。因此,如果元素值是一个 struct 并且想在 range 中修改它,可以通过索引值来访问并修改它,或者使用经典的 for 循环+索引值的写法(除非遍历的元素是一个指针)。
忽略了 range 循环中迭代目标值的计算方式 (channels 和 arrays) (#31)
传递给 range 操作的迭代目标对应的表达式的值,只会在循环执行前被计算一次,理解这个有助于避免犯一些常见的错误,例如不高效的 channel 赋值操作和 slice 迭代操作。
忽略了 range 循环中指针元素的影响 (#32)
这里其实强调的是 range 迭代过程中,迭代变量实际上是一个拷贝。假设给另外一个容器元素(指针类型)赋值,且需要对迭代变量取地址转换成指针再赋值的话,这里潜藏着一个错误,就是 for 循环迭代变量是 per-variable-per-loop 而不是 per-variable-per-iteration。如果是通过局部变量(用迭代变量来初始化)或者使用索引值来直接引用迭代的元素,将有助于避免拷贝指针(迭代变量的地址)之类的 bug。