github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2015/go-for-java-programmers/pingpong.go (about)

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"time"
     8  )
     9  
    10  // STARTMAIN1 OMIT
    11  type Ball struct{ hits int }
    12  
    13  func main() {
    14  	table := make(chan *Ball)
    15  	go player("ping", table)
    16  	go player("pong", table) // HL
    17  
    18  	table <- new(Ball) // game on; toss the ball
    19  	time.Sleep(1 * time.Second)
    20  	<-table // game over; grab the ball
    21  }
    22  
    23  func player(name string, table chan *Ball) {
    24  	for i := 0; ; i++ {
    25  		ball := <-table
    26  		ball.hits++
    27  		fmt.Println(name, i, "hit", ball.hits)
    28  		time.Sleep(100 * time.Millisecond)
    29  		table <- ball
    30  	}
    31  }
    32  
    33  // STOPMAIN1 OMIT