github.com/huandu/go@v0.0.0-20151114150818-04e615e41150/test/chan/perm.go (about)

     1  // errorcheck
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test various correct and incorrect permutations of send-only,
     8  // receive-only, and bidirectional channels.
     9  // Does not compile.
    10  
    11  package main
    12  
    13  var (
    14  	cr <-chan int
    15  	cs chan<- int
    16  	c  chan int
    17  )
    18  
    19  func main() {
    20  	cr = c  // ok
    21  	cs = c  // ok
    22  	c = cr  // ERROR "illegal types|incompatible|cannot"
    23  	c = cs  // ERROR "illegal types|incompatible|cannot"
    24  	cr = cs // ERROR "illegal types|incompatible|cannot"
    25  	cs = cr // ERROR "illegal types|incompatible|cannot"
    26  
    27  	c <- 0 // ok
    28  	<-c    // ok
    29  	x, ok := <-c	// ok
    30  	_, _ = x, ok
    31  
    32  	cr <- 0 // ERROR "send"
    33  	<-cr    // ok
    34  	x, ok = <-cr	// ok
    35  	_, _ = x, ok
    36  
    37  	cs <- 0 // ok
    38  	<-cs    // ERROR "receive"
    39  	x, ok = <-cs	// ERROR "receive"
    40  	_, _ = x, ok
    41  
    42  	select {
    43  	case c <- 0: // ok
    44  	case x := <-c: // ok
    45  		_ = x
    46  
    47  	case cr <- 0: // ERROR "send"
    48  	case x := <-cr: // ok
    49  		_ = x
    50  
    51  	case cs <- 0: // ok
    52  	case x := <-cs: // ERROR "receive"
    53  		_ = x
    54  	}
    55  
    56  	for _ = range cs {// ERROR "receive"
    57  	}
    58  
    59  	for range cs {// ERROR "receive"
    60  	}
    61  
    62  	close(c)
    63  	close(cs)
    64  	close(cr)  // ERROR "receive"
    65  }