Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active July 26, 2018 22:44
Show Gist options
  • Save YumaInaura/e480986140ef9319ab0d71a87e94bae9 to your computer and use it in GitHub Desktop.
Save YumaInaura/e480986140ef9319ab0d71a87e94bae9 to your computer and use it in GitHub Desktop.
Run goroutines include not calling "go" ( From "Tour of Go " )
package main
import (
"fmt"
"time"
)
func say(s string, round int) {
for i := 0; i < round; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("Alice", 5) // Run in A goroutine
go say("Bob", 5) // Run in B goroutine
go say("Carol", 5) // Run in C goroutine
say("David <=", 3) // Run in D groutine
say("Diana <=", 3) // Run in D goroutine // Run after say("David")
}
Carol
Alice
Bob
David <=
Alice
David <=
Carol
Bob
Alice
Bob
David <=
Carol
Diana <=
Bob
Carol
Alice
Alice
Carol
Bob
Diana <=
Diana <=

cf

https://tour.golang.org/concurrency/1 The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.

About script

Using 4 kinds of goroutine.

The evaluation say David and say Diana happens in same goroutine.

A-D are fake names. Groutine has no names and IDs .

Why is there no goroutine ID?

About outputs

See "David" "Alice" "Bob" and "Carol" appear randomly. But "Diana" appears after "David".

@YumaInaura
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment