Created
November 23, 2023 10:09
-
-
Save yumed15/8f15daa7ee5eb28fd71308346cb8c5ce to your computer and use it in GitHub Desktop.
This file contains 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
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