Created
March 28, 2025 09:02
-
-
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/52e1141bb0c8bdd599f992fcd4d74e6a0456b363
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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// 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