Last active
February 3, 2026 20:18
-
-
Save dacr/ed8d732bee97195cb9191bc1a27eaf7d to your computer and use it in GitHub Desktop.
go routines atomics / published by https://github.com/dacr/code-examples-manager #21f820ec-141e-4ca2-808d-3dfde9948b0c/8e93dbe6d1a03f38b56978664df44c11d68c8ac9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*?sr/bin/true; exec /usr/bin/env nix-shell -p go --run "go run $0" #*/ | |
| // summary : go routines atomics | |
| // keywords : go, goroutines, atomics, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 21f820ec-141e-4ca2-808d-3dfde9948b0c | |
| // created-on : 2025-03-27T15:36:24+01:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : nix-shell -p go --run "go run $file" | |
| package main | |
| import ( | |
| "sync" | |
| "sync/atomic" | |
| ) | |
| func main() { | |
| sg := sync.WaitGroup{} | |
| var counter atomic.Uint64 | |
| for i := range 100 { | |
| sg.Add(1) | |
| go func() { | |
| defer sg.Done() | |
| counter.Add(uint64(i)) | |
| }() | |
| } | |
| sg.Wait() | |
| println(counter.Load()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment