The main go routine places a ball on a channel. The player goroutine gets the ball, manipulates it, then places the ball back on the channel and loops. At this point it stalls.
Why does the player goroutine stall? Shouldn't it be able to pick up the "ball" from itself? Sorry if this is staring me right in the face, but my understanding of golang concurrency leads me to believe the player go routine should be able to play ping pong all by itself.
type Ball struct{ hits int }
func main() {
table := make(chan *Ball)
go player("pong", table)
table <- new(Ball) // game on; toss the ball
time.Sleep(1 * time.Second)
<-table // game over; grab the ball
}
func player(name string, table chan *Ball) {
for {
ball := <-table
ball.hits++
fmt.Println(name, ball.hits)
time.Sleep(100 * time.Millisecond)
table <- ball
}
}