package main

import "fmt"

func main() {
    c0, c1 := make(chan int, 10), make(chan int, 10)
    
    go func() {
        for {
            c0 <- 0
        }
    }()
    
    go func() {
        for {
            c1 <- 1
        }
    }()
    
    var n0, n1 int
    
    f0 := func() {
        n0++
    }
    
    f1 := func() {
        n1++
    }
    
    // pass one
    
    n0, n1 = 0, 0
    for range [300000]struct{}{} {
        select {
        case <- c0: f0()
        case <- c1: f1()
        }
    }
    
    fmt.Printf("pass one: %d/%d=%.2f \n", n0, n1, float64(n0) / float64(n1))
    
    // pass two
    
    n0, n1 = 0, 0
    for range [300000]struct{}{} {
        select {
        case <- c0: f0()
        case <- c1: f1()
        case <- c1: f1()
        }
    }
    
    fmt.Printf("pass two: %d/%d=%.2f \n", n0, n1, float64(n0) / float64(n1))
    
    // pass three
    
    n0, n1 = 0, 0
    for range [300000]struct{}{} {
        select {
        case <- c0: f0()
        case <- c0: f0()
        case <- c1: f1()
        }
    }
    
    fmt.Printf("pass three: %d/%d=%.2f \n", n0, n1, float64(n0) / 
float64(n1))
}

The output:

pass one: 154970/145030=1.07 
pass two: 177853/122147=1.46 
pass three: 127768/172232=0.74

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to