Skip to content

Instantly share code, notes, and snippets.

@yumed15
Created November 23, 2023 10:09
Show Gist options
  • Save yumed15/8f15daa7ee5eb28fd71308346cb8c5ce to your computer and use it in GitHub Desktop.
Save yumed15/8f15daa7ee5eb28fd71308346cb8c5ce to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
"time"
)
func printVal(n *int, wg *sync.WaitGroup) {
defer wg.Done()
fmt.Println(*n)
}
func main() {
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
// Create a new variable inside the loop
val := i
go printVal(&val, &wg)
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment