Created
September 15, 2019 17:56
-
-
Save arindamroynitw/5e2b5dacf2bf48b75fbe8c0b46e0d625 to your computer and use it in GitHub Desktop.
Parallel Execution
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" | |
"math/rand" | |
"sync" | |
"time" | |
) | |
func main() { | |
list := []string{"a", "b", "c", "d", "e", "f", "g", "h"} | |
var wg sync.WaitGroup | |
wg.Add(len(list)) | |
for i := 0; i < len(list); i++ { | |
go func(val string) { | |
defer wg.Done() | |
d := time.Duration(rand.Intn(10)) * time.Microsecond | |
time.Sleep(d) | |
fmt.Printf("Current value is : %s \n", val) | |
}(list[i]) | |
} | |
wg.Wait() | |
fmt.Println("Program finished executing") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment