Add Goroutines
This commit is contained in:
26
goroutines/goroutines.go
Normal file
26
goroutines/goroutines.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package goroutines
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func print1To20(label string) {
|
||||
for i := range 20 {
|
||||
fmt.Printf("%s:%d\n", label, i+1)
|
||||
}
|
||||
}
|
||||
|
||||
func Goroutines() {
|
||||
print1To20("Synchronous")
|
||||
|
||||
go print1To20("Goroutine 1")
|
||||
|
||||
go func(label string) {
|
||||
print1To20(label)
|
||||
}("Goroutine 2")
|
||||
|
||||
fmt.Println("Crudely waiting for 2 seconds")
|
||||
time.Sleep(2 * time.Second)
|
||||
fmt.Println("Done")
|
||||
}
|
||||
Reference in New Issue
Block a user