19.channel的基本定义和使用

1.示例

1.1 具体代码

package chapter15

import "fmt"

func RunChannel01() {
    // 定义一个 channel
    c := make(chan int)

    go func() {
        defer fmt.Println("goroutine结束")

        fmt.Println("goroutine 正在运行...")

        c <- 666 // 将 666 发送给 c
    }()

    num := <-c
    fmt.Println("num = ", num)
    fmt.Println("main goroutine 结束...")
}
// 执行结果
=== RUN   TestRunChannel01
goroutine 正在运行...
goroutine结束
num =  666
main goroutine 结束...
--- PASS: TestRunChannel01 (0.00s)
PASS
ok      

1.2.代码解析





2.总结


posted @ 2021-10-09 18:36  可可逗豆  阅读(436)  评论(0)    收藏  举报