golang 无缓冲channel

golang 无缓冲channel

package main

import "fmt"

func main() {

    // 1S =1000ms
    //1ms = 1000us
    //1us =1000ns
    /*
    定义channel:make(chan 类型,容量)
    ch :=make(chan string)
    写端 ch <-"hello"
    读端 str := <-ch

     */

    ch :=make(chan string) //无缓冲channel
    fmt.Println("len(ch)=",len(ch),"cap(ch)=",cap(ch))

    go func() {
        for i:=0;i<2;i++{
            fmt.Println("i=",i)
        }
        ch <-"子go打印完毕"
    }()

    str := <-ch
    fmt.Println("str=",str)


}

 

posted @ 2019-07-16 10:54  landv  阅读(275)  评论(0编辑  收藏  举报