Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
245 commits
Select commit Hold shift + click to select a range
7823e44
Update timeouts.go: Fix typo
szTheory Jun 13, 2020
25cf6d9
Merge branch 'szTheory-patch-1'
mmcgrana Aug 23, 2020
7d87b1b
Rebuild
mmcgrana Aug 23, 2020
71719ea
Change t.Fail to t.Fatal
legonian Oct 18, 2020
1e3ec28
Merge pull request #342 from legonian/patch-1
mmcgrana Oct 19, 2020
f72f11e
Rebuild
mmcgrana Oct 19, 2020
9e216da
go.mod: add go.mod and move pygments to third_party
hyangah Feb 15, 2021
c8b3ae7
templates: recommend the Go getting started doc
hyangah Feb 15, 2021
fcd052e
LICENSE: add CC-BY-3.0 license
hyangah Feb 15, 2021
7f73668
.travis.yml: update to go1.15
hyangah Feb 16, 2021
fb4e31b
Revert "LICENSE: add CC-BY-3.0 license"
hyangah Feb 19, 2021
e39e6f1
remove unnecessary go get
hyangah Feb 19, 2021
1ecc5bf
Fix a typo
yurivish Feb 19, 2021
b76ff6c
Merge branch 'yurivish-patch-1'
mmcgrana Feb 19, 2021
fff24f8
Rebuild for typo fix
mmcgrana Feb 19, 2021
81ab859
Upgrade blackfriday to v2 and fix underscore italics (#350)
eliben Feb 20, 2021
58e66a8
Use chroma (#346)
TechplexEngineer Feb 27, 2021
7a5cb99
Add myself to footer (#386)
eliben Aug 26, 2021
8627d85
Test with both Go 1.17 and 1.16 (#385)
eliben Aug 27, 2021
c5ee569
Fix typo in meta tag attribute (#376)
TechplexEngineer Aug 27, 2021
eb450ec
Add public/404.html as generated in the previous commit
eliben Aug 27, 2021
9524538
Update Travis badge Markdown per Travis app (#388)
mmcgrana Aug 28, 2021
ac94809
Replace deprecated ioutil functions with others (#374)
eliben Aug 28, 2021
a9507f5
Clarify comment for the goroutines sample (#366)
eliben Aug 30, 2021
df8a378
Remove outdated link from an example
eliben Aug 30, 2021
69efd4b
Add note to waitgroups example about errgroup
eliben Aug 30, 2021
95a1002
Create initial GitHub actions workflow
eliben Aug 31, 2021
5525fed
Remove Travis and fix badge link in README
eliben Aug 31, 2021
91c8cee
Adding a new example: recover
eliben Sep 1, 2021
2acace9
Clarify the panic example with a comment about unreachable code
eliben Sep 2, 2021
d037acd
Update Recursion example to demonstrate recursive closures
eliben Sep 2, 2021
364c664
Initial version of the Go uploader to S3
eliben Sep 2, 2021
198349f
Add comments and clean up code
eliben Sep 2, 2021
68cc690
Add a wrapper script for tools/upload.go
eliben Sep 2, 2021
b4568f3
Refactor page footer to separate template
eliben Sep 2, 2021
2e6a5ad
Clarify the comment on the recursion example
eliben Sep 2, 2021
33985af
Added prefixes with purpose to find output result easier. (#391)
cn0047 Sep 8, 2021
cf0d050
Rename some string formatting outputs
eliben Sep 8, 2021
2570a9c
Remove old Ruby-based upload script
eliben Sep 8, 2021
4de485a
Add partial prefixes in some places to regular-expressions
eliben Sep 10, 2021
b1ef499
Rewrite the WaitGroup example to be more idiomatic
eliben Sep 10, 2021
8367c3e
Update .gitattributes
eliben Sep 10, 2021
eac6776
Clarify comment
eliben Sep 10, 2021
15c92ee
Minor fixes (#396)
gkotian Sep 20, 2021
e10011f
Rebuild example to public after previous PR's change
eliben Sep 20, 2021
f09cadc
Change mutexes example to make it simpler and more predictable
eliben Oct 30, 2021
35ad9cc
Add example of struct embedding in Go
eliben Nov 22, 2021
2621c73
don't embed the mutex (#400)
natefinch Nov 22, 2021
c93e7c1
Generate public/* for mutex
eliben Nov 22, 2021
dad9113
Fix incorrect link in README.md (#404)
alaq Dec 1, 2021
80fb5eb
Clarify the signals example slightly
eliben Dec 2, 2021
dadb6bd
feat: Add benchmark example (#403)
jidicula Dec 2, 2021
99902be
Slightly tweak comments after #403
eliben Dec 2, 2021
061b2f6
Rename testing --> testing-and-benchmarking
eliben Dec 2, 2021
3397ebc
Remove old public/testing, since the source was renamed
eliben Dec 2, 2021
668ecb9
Add brief FAQ section in the README to clarify common questions
eliben Dec 17, 2021
1c7180a
Update `Epoch` example to add UnixMilli from Time package (#409)
sandeepkru Jan 10, 2022
63ed623
Slight comment rewording following the previous commit
eliben Jan 10, 2022
364f0c2
Run CI on latest version of Go only
eliben Jan 10, 2022
57ac312
Update README.md
eliben Jan 10, 2022
2885fc2
Move around commend in recursion sample for better rendering
eliben Jan 28, 2022
ff399c7
Starting a strings-runes example
eliben Feb 5, 2022
b2057cc
strings-and-runes: added comments, output and listed in examples
eliben Feb 5, 2022
e604254
Tweaks to other samples to accommodate strings-and-runes
eliben Feb 5, 2022
35018ab
Update README.md
eliben Feb 5, 2022
2c7b384
Clean up last printout in string-functions
eliben Feb 5, 2022
3e51f69
Merge branch 'master' of github.com:mmcgrana/gobyexample
eliben Feb 5, 2022
0f801fd
Fix typo in strings-and-runes (#414)
kovshenin Feb 10, 2022
5fa0d40
Re-generate output after #414
eliben Feb 10, 2022
ccfd710
Templates: added comments, output and listed in examples (#413)
ArashSameni Feb 10, 2022
0751f43
Round of fixes for the new text-template example
eliben Feb 10, 2022
1fa5cff
Reorder examples slightly
eliben Feb 11, 2022
5c36a19
Try rc1 1.18 version for go testing
eliben Mar 14, 2022
3c2447b
Add example of Go generics (#415)
eliben Mar 15, 2022
881bb5d
Remove collection-functions
eliben Mar 15, 2022
9a8d0aa
Remove collection-functions from public/ as well
eliben Mar 15, 2022
1eba71e
Set go-version to 1.18.0 in the builder (#416)
eliben Mar 16, 2022
e6da679
Update GH actions workflow badge in README
eliben Mar 17, 2022
ac01d2d
Update SHA1 example to SHA256 (#418)
Hayden-IO Mar 31, 2022
ed7a093
Clean up description in SHA256 example
eliben Mar 31, 2022
e6c5a8f
Fix typo in embedding example's comment
eliben May 2, 2022
6b91c38
Rebuild embedding example and publish public/
eliben May 2, 2022
9deadb7
Improve spawning-process example with error-checking
eliben May 6, 2022
a19c5b8
Rename embedding to struct embedding to be separated from //go:embed …
peterzhu1992 May 17, 2022
7e45336
Add embed-directive example (#423)
peterzhu1992 May 24, 2022
d4bbdc2
Reformat tools/ files
eliben May 24, 2022
fc88db1
Fix up embed-directive example and rename files to be shorter
eliben May 24, 2022
5f2ab6c
More comment tweaks to the new embed-directive
eliben May 24, 2022
52fd882
Update golang.org links to point directly to go.dev
eliben May 25, 2022
3646485
Rebuild to have updated index.html in public/
eliben May 25, 2022
625ea80
Render 404 page with templates to reuse the footer
eliben May 26, 2022
d943933
Clean up template rendering code
eliben May 26, 2022
25d2811
Remove unused code
eliben May 26, 2022
da13e2f
Improving links (http -> https) (#425)
deining Jun 6, 2022
c662818
Changes 'the' to 'then' in stateful goroutine example (#427)
Jun 7, 2022
f197dfc
Clarify the type of variadic params with a comment
eliben Jun 24, 2022
0eaaca2
Fix typo in xml example
eliben Jun 28, 2022
73cd7a7
Fix typo in Println statement (#432)
mcncl Jul 11, 2022
279163e
Update README.md
eliben Aug 22, 2022
abfc2c2
Remove superfluous punctuation
eliben Aug 22, 2022
1b4dac7
Mention that environment variable set by program is also listed by `o…
AndiDog Aug 22, 2022
742fab3
Clarify slices vs. arrays a bit
eliben Sep 8, 2022
b4f289f
Adjust for gofmt changes in 1.19
eliben Sep 8, 2022
00543ca
Update Go version to 1.19 in go.mod and GH actions
eliben Sep 8, 2022
cc804d7
Clarify determinism of Go playground w.r.t. random numbers
eliben Sep 22, 2022
840541a
Rename http clients/servers to singular language: client/server
eliben Sep 22, 2022
2f242db
Remove old public html
eliben Sep 22, 2022
c40fcde
Add missing command line example for recover.sh (#444)
Naoya9922 Sep 23, 2022
4f668ee
Re-generate output following previous change updating recover.go
eliben Sep 23, 2022
9ba0fd0
Minor addition to if-else text on declarations (#450)
Aditya-Sood Oct 25, 2022
0650d74
Slight rewording
eliben Oct 25, 2022
bc9700a
Add missing `$ go run ...` line on top of generics.sh
eliben Nov 3, 2022
7b502fa
Update CONTRIBUTING.md
eliben Dec 12, 2022
f783c41
Update CONTRIBUTING.md
eliben Dec 12, 2022
30a235b
docs: remove typo (#454)
jpacareu Dec 12, 2022
4944c3b
Added more information about shorthand variable declaration (#457)
grejojoby Dec 20, 2022
b6baa3b
Minor README tweaks
eliben Jan 9, 2023
24b5e9a
Tweak CSS to decrease the font size in the footer
eliben Jan 31, 2023
38eb223
`dependes` -> `depends` (#462)
siiky Feb 4, 2023
0812429
Clarify zero value for map values when keys don't exist
eliben Mar 20, 2023
4280f94
Added Brazilian portuguese (PT-BR) to translations list. (#467)
LCSLITX Mar 27, 2023
ea61a84
Revert "Added Brazilian portuguese (PT-BR) to translations list. (#467)"
eliben Apr 3, 2023
4b9ed3f
Remove dead links to translations in README
eliben Apr 3, 2023
0a1da7e
Added Brazilian portuguese (PT-BR) to translations list, updated link…
LCSLITX Apr 3, 2023
363849f
link embed directive (#469)
niyumard Apr 19, 2023
cb2d0d9
/bin/bash -> /usr/bin/env bash (#471)
niyumard Apr 19, 2023
fcc06e2
Add Vietnamese version translation (#472)
benhaq Apr 26, 2023
fd4d72b
add unassigned slice example (#475)
jiangbo0216 May 16, 2023
1512fb0
slices: tweak the recent changes for clarity and grammar
eliben May 16, 2023
4b94115
Introduce anonymous structs (#474)
niyumard Jun 5, 2023
6bd7615
Reword comment in new anon struct sample
eliben Jun 5, 2023
ef81b2a
Fix typo in a comment
eliben Jun 5, 2023
1ee3369
Add link from structs-->testing-and-benchmarking
eliben Jun 5, 2023
d51586e
Add example of the new clear() builtin for maps in 1.21
eliben Aug 21, 2023
4dc83f3
Update chroma from v0.8 to v2 (v2.8.0) and fix up formatting
eliben Aug 21, 2023
83763d9
Bump Go version for GH actions
eliben Aug 21, 2023
57db236
Properly fix extra newline rendered at the beginning of code segments
eliben Aug 21, 2023
655a3b6
Update sorting sample to use slices.Sort instead of sort package
eliben Aug 21, 2023
2166e61
Update sorting-by-functions example to use slices.SortFunc
eliben Aug 21, 2023
146bd9c
Add examples of maps/slices packages
eliben Aug 22, 2023
15d8fe7
Add new example: logging with the log and log/slog packages
eliben Aug 22, 2023
d1ca2ce
Update Atomic Counters example to use atomic.Uint64 (#490)
erazemk Oct 2, 2023
edab962
Small comment updates to atomic-counters
eliben Oct 2, 2023
41dd5d9
Add sample of logical operators in `if` statements
eliben Oct 28, 2023
8a64f89
Update directories sample to use WalkDir
eliben Oct 28, 2023
bc2c65c
Update chroma dependency
eliben Oct 28, 2023
2943876
Update aws-adk-go dependencies
eliben Oct 28, 2023
991f893
Added example of reading from closed channel (#498)
zhuboris Nov 30, 2023
461f695
Fix up formatting and phrasing for closed chan read
eliben Nov 30, 2023
6fdb46e
Update generics.sh with correct response (#500)
akhilpenugonda Dec 9, 2023
ef79989
Fix up comment length and text, and generate public/
eliben Dec 9, 2023
0c31ca8
typo fix - if-else.sh (#502)
Deftly Dec 29, 2023
da72468
Reorder text to conform to output
eliben Dec 29, 2023
f7256de
Update random number examples to match new 1.20 semantics
eliben Jan 31, 2024
ef87d65
Update building Go versions to 1.22.0
eliben Feb 7, 2024
1143b54
Add Go 1.22's range-over-int example to `for`
eliben Feb 7, 2024
5aa9ad7
Update random-numbers example to use Go 1.22's math/rand/v2
eliben Feb 7, 2024
a3fb3a3
Update waitgroups example to use go 1.22 (#508)
pzerone Feb 9, 2024
9c71a85
Fix verbiage for Strings and Runes (noissue) (#510)
aannirajpatel Feb 22, 2024
13d2e9a
added: a dark mode in css for us night dwellers
ChrisMcKenzie Mar 14, 2024
c7e9d6f
changed: to github-dark and a different background and more gray text
ChrisMcKenzie Mar 15, 2024
26c4409
Darkmode style refactoring and tweaking
mmcgrana Mar 17, 2024
fae3af5
Try a slightly lighter dark
mmcgrana Mar 17, 2024
4a0a0c0
Text adjusted for lighter backgrounds
mmcgrana Mar 17, 2024
3dd6791
Slight tweaks to foreground colors for lightened background
mmcgrana Mar 19, 2024
cdb9266
Add wrapping errors (#517)
guettli Mar 21, 2024
a8332aa
Consolidate wrapping-and-sentinel-errors into errors
eliben Mar 21, 2024
61cf4c7
Update custom errors example
eliben Mar 21, 2024
ff35b38
Remove deleted example from public
eliben Mar 21, 2024
bee6d2d
Fix wording slightly
eliben Mar 21, 2024
f8d6019
Add the shell prompt/command to custom-errors.sh (#519)
johnd Mar 28, 2024
7958694
Add an example with automatic array size (#522)
adriancuadrado Apr 15, 2024
baadeda
Use seek enums and show other seek methods (#524)
kauppie Apr 29, 2024
665183d
Fix typo in a comment (#525)
hezhizhen May 1, 2024
e5e9031
fix ending extension (#526)
EmNudge May 1, 2024
e6b7ae8
Stop breaking browser navigation shortcuts. (#527)
HSPDev May 11, 2024
0d466e6
Clarify JS by attaching onkeydown explicitly on the window
eliben May 11, 2024
0b921ed
New example: enums
eliben May 14, 2024
d774175
Fix typo in the enums example (#530)
agorkem May 15, 2024
942f181
Adjust the index page intro text slightly
eliben May 24, 2024
308d4be
Fix typo in examples/enums/enums.go (#537)
jonathonwebb Jun 10, 2024
5cd7f20
Use console consistently for readme blocks
mmcgrana Jun 17, 2024
aa04b84
Remove unreachable code from enums example (#540)
BigJayToDaIzo Jul 3, 2024
9ee93ff
Add Burmese version translation (#541)
setkyar Jul 8, 2024
b832df6
fix: change "encoding.xml" to "encoding/xml" (#546)
havidtech Aug 5, 2024
1a76037
Fix example name in its .sh
bustosalex1 Aug 14, 2024
bf195bf
Clarify GC in structs example
eliben Aug 14, 2024
f7120b2
Suggest latest version on the home page
eliben Aug 17, 2024
d51709a
Rename range example to prepare for expansion
eliben Aug 21, 2024
6ed788f
Update generics example to prepare for iteration
eliben Aug 21, 2024
2f31c1f
Initial example of range over custom types
eliben Aug 21, 2024
4d27fb5
Small tweaks to the sample
eliben Aug 21, 2024
a0e4c07
Rename range over iterators sample and add a bit more sample
eliben Aug 21, 2024
9b3626a
Update GH action workflow to Go 1.23
eliben Aug 21, 2024
9f66cb2
Update generated HTML
eliben Aug 21, 2024
17b2ecf
Add type to const definitions for enum values
eliben Sep 11, 2024
78ecc3f
Correct inaccuracy in recursion description. (#554)
wwwstephen Sep 23, 2024
81ec07e
Correct inaccuracy in recursion description. (#555)
wwwstephen Sep 23, 2024
bc680ed
Fix http-server filename (#559)
mfurak Nov 4, 2024
7951241
Added json.NewDecoder example (#566)
bhaskar253 Dec 30, 2024
58894bd
Update text and output of json example slightly
eliben Dec 30, 2024
858d475
added type assertion example (#567)
kevin-kho Jan 6, 2025
ad9cea3
Tweak interface example type assertion + update link
eliben Jan 6, 2025
8621605
Add rel="next" to example.tmpl file for the next link (#569)
yodigi7 Jan 11, 2025
2a6ddb9
Fix Seek distances to work with sample data file
eliben Jan 21, 2025
9f92e06
Clean up the defer example a bit
eliben Jan 29, 2025
a0afe45
clarify a comment w.r.t. empty slice (#571)
hudem1 Feb 8, 2025
dc4d281
Update testing-and-benchmarking for new benchmark Loop of Go 1.24
eliben Feb 24, 2025
da87dfa
Update go version to 1.24
eliben Feb 24, 2025
6552c2c
Update public html of testin-and-benchmarking
eliben Feb 24, 2025
d393387
Fix link to http-server from context.go
eliben May 13, 2025
7f3bbda
Rename .hash file for consistency
eliben May 13, 2025
b783827
Remove broken translation links
eliben May 13, 2025
bd94e3e
Fix incorrect comment style in timers.sh (// → #) (#605)
asutosh2203 May 17, 2025
10aea2d
Replace for ;; with for range int in all examples (#609)
mattnathan Jun 2, 2025
cecfee7
Fix typo
eliben Jun 9, 2025
0d3b379
Update go to 1.25.0 in go.mod
eliben Aug 13, 2025
4035ec4
Update waitgroups sample to use the new WaitGroup.Go
eliben Aug 13, 2025
fdd9bb0
ignore navigation if command key held on MacOS (#621)
JacobThwaites Aug 29, 2025
7fe5e6e
Update atomic-counters to also use WaitGroup.Go
eliben Sep 8, 2025
d7a4061
(minor): update mutexes example to use wg.Go instead of manual creati…
Dawo9889 Sep 8, 2025
12661f5
Update Go version to 1.25.1 in CI and go.mod
eliben Sep 8, 2025
8590989
Add Italian translation link to README (#624)
andrearaponi Sep 26, 2025
d2eea41
Improve Spawning Processes, using errors.As (#625)
obaibula Sep 29, 2025
f6feee9
Slight tweaks to wording
eliben Sep 29, 2025
e263c6b
Slighly clarify channel-synchronization comment
eliben Oct 6, 2025
981a575
Update error example (#627)
antonlau-1 Oct 9, 2025
4f5f304
Fix #620 : Rewording constants statement (#628)
AqViolet Oct 14, 2025
90b98d2
Fix build
eliben Oct 14, 2025
409ebe6
fix file path on windows (#632)
dnp314 Nov 10, 2025
6d3cd80
Use os.TempDir in the panic() example as well
eliben Nov 10, 2025
980aff6
Fix missed /tmp mention from #632
eliben Nov 10, 2025
fc55ea2
Add Basic TCP Server Example (Fixes #619) (#636)
bhushan-mdn Nov 19, 2025
5ae02f4
Slight adjustments to wording in tcp-server example
eliben Nov 19, 2025
a28b91c
Remove Russian translation link from README
eliben Jan 2, 2026
01cb8cf
Fix Fibonacci sequence in range-over-iterators (#643)
jub0bs Jan 5, 2026
74ba850
Update stateful-goroutines.go (#647)
fatihkilic Feb 23, 2026
f5d0673
Increase code pane size to 65 chars and update CSS
eliben Feb 24, 2026
1edcdb5
Increase code width further to 68
eliben Feb 24, 2026
6f185a9
Update for go 1.26 and especially errors.AsType
eliben Feb 24, 2026
3299a8b
Update time-formatting-parsing to be less compressed
eliben Feb 24, 2026
12ad33d
Modernize signals.go sample
eliben Feb 24, 2026
bc5334f
Modernize range-over-iterators
eliben Feb 24, 2026
3fe16be
Update github CI default Go version
eliben Feb 24, 2026
e8ca216
Tweak mobile width for better presentation
eliben Feb 24, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
public/* linguist-generated=true
vendor/* linguist-vendored=true
public/** linguist-generated

30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: test

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

build:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
go-version: [1.26.0]

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Test
run: tools/build
env:
VERBOSE: 1
TESTING: 1
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

17 changes: 11 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

Thanks for your interest in contributing to Go by Example!

* When sending a PR that affects the displayed contents of the site, run
`tools/build` locally and include the generated HTML in the PR. If you
only want to submit a simple typo suggestion (for example, through the
Github website), feel free to send a PR anyway - we'll regenerate the
HTML and merge with your commit.
* When sending a PR that affects the displayed contents of the site,
updating the HTML in the `public` directory by itself is insufficient, since
the source of truth for the website is in the `examples` directory.

Instead, update the proper source file(s) in the `examples` directory and
run `tools/build` locally to regenerate the HTML; include both changes in
your PR.

If you don't want to deal with getting a proper PR in, feel free to just
open an issue and point out the change you suggest.

* We're open to adding more examples to the site. They should be on things
used by many programmers and only require the standard library. If you're
interested in adding an example, _please open an issue to discuss the topic
first_.

* We're not going to change the navigation of the site, in particular adding
a "previous section" link or an "index" link other than the on the title
a "previous section" link or an "index" link other than the one on the title
text.
64 changes: 49 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ a site that teaches Go via annotated example programs.

The Go by Example site is built by extracting code and
comments from source files in `examples` and rendering
them via the `templates` into a static `public`
them using `templates` into a static `public`
directory. The programs implementing this build process
are in `tools`, along with some vendor'd dependencies
in `vendor`.
are in `tools`, along with dependencies specified in
the `go.mod`file.

The built `public` directory can be served by any
static content system. The production site uses S3 and
CloudFront, for example.

### Building

[![Build Status](https://travis-ci.com/mmcgrana/gobyexample.svg "Travis CI status")](https://travis-ci.com/mmcgrana/gobyexample)
[![test](https://github.com/mmcgrana/gobyexample/actions/workflows/test.yml/badge.svg)](https://github.com/mmcgrana/gobyexample/actions/workflows/test.yml)

To build the site you'll need Go and Python installed. Run:
To build the site you'll need Go installed. Run:

```console
$ go get github.com/russross/blackfriday
$ tools/build
```

Expand All @@ -35,7 +34,7 @@ $ tools/build-loop

To see the site locally:

```
```console
$ tools/serve
```

Expand All @@ -46,7 +45,6 @@ and open `http://127.0.0.1:8000/` in your browser.
To upload the site:

```console
$ gem install aws-sdk
$ export AWS_ACCESS_KEY_ID=...
$ export AWS_SECRET_ACCESS_KEY=...
$ tools/upload
Expand All @@ -57,7 +55,7 @@ $ tools/upload
This work is copyright Mark McGranaghan and licensed under a
[Creative Commons Attribution 3.0 Unported License](http://creativecommons.org/licenses/by/3.0/).

The Go Gopher is copyright [Renée French](http://reneefrench.blogspot.com/) and licensed under a
The Go Gopher is copyright [Renée French](https://reneefrench.blogspot.com/) and licensed under a
[Creative Commons Attribution 3.0 Unported License](http://creativecommons.org/licenses/by/3.0/).


Expand All @@ -66,17 +64,53 @@ The Go Gopher is copyright [Renée French](http://reneefrench.blogspot.com/) and
Contributor translations of the Go by Example site are available in:

* [Chinese](https://gobyexample-cn.github.io/) by [gobyexample-cn](https://github.com/gobyexample-cn)
* [Czech](http://gobyexamples.sweb.cz/) by [martinkunc](https://github.com/martinkunc/gobyexample-cz)
* [French](http://le-go-par-l-exemple.keiruaprod.fr) by [keirua](https://github.com/keirua/gobyexample)
* [Italian](http://gobyexample.it) by the [Go Italian community](https://github.com/golangit/gobyexample-it)
* [Italian](https://gobyexampleit.andrearaponi.it/) by [andrearaponi](https://github.com/andrearaponi/gobyexample-it)
* [Japanese](http://spinute.org/go-by-example) by [spinute](https://github.com/spinute)
* [Korean](https://mingrammer.com/gobyexample/) by [mingrammer](https://github.com/mingrammer)
* [Russian](https://gobyexample.com.ru/) by [badkaktus](https://github.com/badkaktus)
* [Spanish](http://goconejemplos.com) by the [Go Mexico community](https://github.com/dabit/gobyexample)
* [Ukrainian](http://butuzov.github.io/gobyexample/) by [butuzov](https://github.com/butuzov/gobyexample)
* [Ukrainian](https://butuzov.github.io/gobyexample/) by [butuzov](https://github.com/butuzov/gobyexample)
* [Brazilian Portuguese](https://lcslitx.github.io/GoEmExemplos/) by [lcslitx](https://github.com/LCSLITX)
* [Burmese](https://setkyar.github.io/gobyexample) by [Set Kyar Wa Lar](https://github.com/setkyar/gobyexample)

### Thanks

Thanks to [Jeremy Ashkenas](https://github.com/jashkenas)
for [Docco](http://jashkenas.github.com/docco/), which
for [Docco](http://jashkenas.github.io/docco/), which
inspired this project.

### FAQ

#### I found a problem with the examples; what do I do?

We're very happy to fix problem reports and accept contributions! Please submit
[an issue](https://github.com/mmcgrana/gobyexample/issues) or send a Pull Request.
See `CONTRIBUTING.md` for more details.

#### What version of Go is required to run these examples?

Given Go's strong [backwards compatibility guarantees](https://go.dev/doc/go1compat),
we expect the vast majority of examples to work on the latest released version of Go
as well as many older releases going back years.

That said, some examples show off new features added in recent releases; therefore,
it's recommended to try running examples with the latest officially released Go version
(see Go's [release history](https://go.dev/doc/devel/release) for details).

#### I'm getting output in a different order from the example. Is the example wrong?

Some of the examples demonstrate concurrent code which has a non-deterministic
execution order. It depends on how the Go runtime schedules its goroutines and
may vary by operating system, CPU architecture, or even Go version.

Similarly, examples that iterate over maps may produce items in a different order
from what you're getting on your machine. This is because the order of iteration
over maps in Go is [not specified and is not guaranteed to be the same from one
iteration to the next](https://go.dev/ref/spec#RangeClause).

It doesn't mean anything is wrong with the example. Typically the code in these
examples will be insensitive to the actual order of the output; if the code is
sensitive to the order - that's probably a bug - so feel free to report it.




22 changes: 16 additions & 6 deletions examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ Switch
Arrays
Slices
Maps
Range
Functions
Multiple Return Values
Variadic Functions
Closures
Recursion
Range over Built-in Types
Pointers
Strings and Runes
Structs
Methods
Interfaces
Enums
Struct Embedding
Generics
Range over Iterators
Errors
Custom Errors
Goroutines
Channels
Channel Buffering
Expand All @@ -41,9 +47,10 @@ Sorting
Sorting by Functions
Panic
Defer
Collection Functions
Recover
String Functions
String Formatting
Text Templates
Regular Expressions
JSON
XML
Expand All @@ -53,21 +60,24 @@ Time Formatting / Parsing
Random Numbers
Number Parsing
URL Parsing
SHA1 Hashes
SHA256 Hashes
Base64 Encoding
Reading Files
Writing Files
Line Filters
File Paths
Directories
Temporary Files and Directories
Testing
Embed Directive
Testing and Benchmarking
Command-Line Arguments
Command-Line Flags
Command-Line Subcommands
Environment Variables
HTTP Clients
HTTP Servers
Logging
HTTP Client
HTTP Server
TCP Server
Context
Spawning Processes
Exec'ing Processes
Expand Down
26 changes: 23 additions & 3 deletions examples/arrays/arrays.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// In Go, an _array_ is a numbered sequence of elements of a
// specific length.
// specific length. In typical Go code, [slices](slices) are
// much more common; arrays are useful in some special
// scenarios.

package main

Expand Down Expand Up @@ -29,14 +31,32 @@ func main() {
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("dcl:", b)

// You can also have the compiler count the number of
// elements for you with `...`
b = [...]int{1, 2, 3, 4, 5}
fmt.Println("dcl:", b)

// If you specify the index with `:`, the elements in
// between will be zeroed.
b = [...]int{100, 3: 400, 500}
fmt.Println("idx:", b)

// Array types are one-dimensional, but you can
// compose types to build multi-dimensional data
// structures.
var twoD [2][3]int
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
for i := range 2 {
for j := range 3 {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)

// You can create and initialize multi-dimensional
// arrays at once too.
twoD = [2][3]int{
{1, 2, 3},
{1, 2, 3},
}
fmt.Println("2d: ", twoD)
}
4 changes: 2 additions & 2 deletions examples/arrays/arrays.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
e71f2d3763eb2950727faa39b90cf1bc037d85fa
TaahifSGSwU
96cbe484a18a0dd8c1839a92963447bed94cc997
-NFSggT7dFH
6 changes: 3 additions & 3 deletions examples/arrays/arrays.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set: [0 0 0 0 100]
get: 100
len: 5
dcl: [1 2 3 4 5]
dcl: [1 2 3 4 5]
idx: [100 0 0 400 500]
2d: [[0 1 2] [1 2 3]]

# You'll see _slices_ much more often than arrays in
# typical Go. We'll look at slices next.
2d: [[1 2 3] [1 2 3]]
32 changes: 12 additions & 20 deletions examples/atomic-counters/atomic-counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,30 @@ import (

func main() {

// We'll use an unsigned integer to represent our
// We'll use an atomic integer type to represent our
// (always-positive) counter.
var ops uint64
var ops atomic.Uint64

// A WaitGroup will help us wait for all goroutines
// to finish their work.
var wg sync.WaitGroup

// We'll start 50 goroutines that each increment the
// counter exactly 1000 times.
for i := 0; i < 50; i++ {
wg.Add(1)

go func() {
for c := 0; c < 1000; c++ {
// To atomically increment the counter we
// use `AddUint64`, giving it the memory
// address of our `ops` counter with the
// `&` syntax.
atomic.AddUint64(&ops, 1)
for range 50 {
wg.Go(func() {
for range 1000 {
// To atomically increment the counter we use `Add`.
ops.Add(1)
}
wg.Done()
}()
})
}

// Wait until all the goroutines are done.
wg.Wait()

// It's safe to access `ops` now because we know
// no other goroutine is writing to it. Reading
// atomics safely while they are being updated is
// also possible, using functions like
// `atomic.LoadUint64`.
fmt.Println("ops:", ops)
// Here no goroutines are writing to 'ops', but using
// `Load` it's safe to atomically read a value even while
// other goroutines are (atomically) updating it.
fmt.Println("ops:", ops.Load())
}
4 changes: 2 additions & 2 deletions examples/atomic-counters/atomic-counters.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
7b491b40d56a77b01d8e2bd08366de081a4e8d99
j-14agntvEO
d898b7b79368f70c6f6d1c181575f7b83c592718
yiGAVfTH49v
11 changes: 6 additions & 5 deletions examples/atomic-counters/atomic-counters.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# We expect to get exactly 50,000 operations. Had we
# used the non-atomic `ops++` to increment the counter,
# we'd likely get a different number, changing between
# runs, because the goroutines would interfere with
# each other. Moreover, we'd get data race failures
# when running with the `-race` flag.
# used a non-atomic integer and incremented it with
# `ops++`, we'd likely get a different number,
# changing between runs, because the goroutines
# would interfere with each other. Moreover, we'd
# get data race failures when running with the
# `-race` flag.
$ go run atomic-counters.go
ops: 50000

Expand Down
Loading