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