1

If we need a channel dedicated to sending data and a channel dedicated to receiving data, we can write:
(We assume the data to be sent and received is 9):

 package main

import (
    "fmt"
    "time"
)

//只写信道定义
type Writter = chan<- int

//只读信道定义
type Reader = <-chan int

func main() {
    //声明一个信道
    var ch = make(chan int)

    go func() {
        var writter Writter = ch
        fmt.Println("写入数据:9")
        writter <- 9
    }()

    go func() {
        var reader Reader = ch
        data := <-reader
        fmt.Printf("读出数据:%d", data)
    }()

    time.Sleep(time.Second)
}


LiberHome
409 声望1.1k 粉丝

有问题 欢迎发邮件 📩 liberhome@163.com