GoExpertProgramming icon indicating copy to clipboard operation
GoExpertProgramming copied to clipboard

管道部分增加一个有趣的试题

Open RainbowMango opened this issue 4 years ago • 0 comments

func ReadChanReturnValue() {
	ch := make(chan int, 10)
	ch <- 1
	ch <- 2
	fmt.Printf("length of channel is: %d\n", len(ch))

	_, ok := <-ch
	fmt.Printf("second return value before channel closed is: %v\n", ok) // true

	close(ch)

	_, ok = <-ch
	fmt.Printf("second return value after channel(have data) closed is: %v\n", ok) // true

	_, ok = <-ch
	fmt.Printf("second return value after channel(no data) closed is: %v\n", ok) // false
}

RainbowMango avatar May 28 '20 03:05 RainbowMango