go channel

channel

  1. 管道,類似unix/linux中的pipe
  2. 多個goroute之間通過channel進行通信
  3. 支持任何類型
func main() {
  pipe := make(chan int,3)
pipe <- 1
pipe <- 2
}

example-01

package main

import(
	"fmt"
)

func main()  {
	test_pipe()
}

func test_pipe(){
	pipe := make(chan int, 3)
	pipe <- 11
	pipe <- 7
	pipe <- 73
	var t1 int
	t1 =<- pipe
	//pipe <- 33

	fmt.Println(len(pipe)) // 2
	fmt.Println(t1) // 11
} 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章