github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2015/go-for-java-programmers/pingselect.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  	in, out := make(chan *Ball), make(chan *Ball)
    15  	go player("ping", in, out)
    16  	go player("pong", in, out)
    17  
    18  	for i := 0; i < 8; {
    19  		select { // HL
    20  		case in <- new(Ball): // feed the pipeline // HL
    21  		case <-out: // drain the pipeline // HL
    22  			i++ // HL
    23  		} // HL
    24  	}
    25  }
    26  
    27  func player(name string, in <-chan *Ball, out chan<- *Ball) {
    28  	for i := 0; ; i++ {
    29  		ball := <-in
    30  		ball.hits++
    31  		fmt.Println(name, i, "hit", ball.hits)
    32  		time.Sleep(100 * time.Millisecond)
    33  		out <- ball
    34  	}
    35  }
    36  
    37  // STOPMAIN1 OMIT