登录
首页 >  Golang >  Go问答

为何通道不使用最新的声明值?

来源:stackoverflow

时间:2024-02-03 12:13:29 356浏览 收藏

各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题《为何通道不使用最新的声明值?》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

问题内容

我正在尝试了解频道。在这段代码中,我声明了从 1 到 10 的值。最终声明的值为 10,但是当我打印它时,它总是返回 before(8) 之前两个声明的值。如果有人能解释一下,我会很高兴。

func main() {
    channel := make(chan int, 3)
    isOver := make(chan bool)

    go func() {
        for val := range channel {
            fmt.Println(val)
        }
        isOver <- true
    }()

    channel <- 1
    channel <- 2
    channel <- 3
    channel <- 4
    channel <- 5
    channel <- 6
    channel <- 7
    channel <- 8
    channel <- 9
    channel <- 10
    close(channel)

    fmt.Println("Channel Value is: ", <-channel)

    <-isOver

}

当我将声明更改为 8 时,它返回之前两个声明的值,即 6。


正确答案


因为你为通道设置了3个缓冲区大小!

注意:您的示例在不同的操作系统中具有不同的输出(例如我得到 Channel 值为:0

设置时间。在代码中休眠看看会发生什么。在您的代码中:

func main() {
    channel := make(chan int)
    isOver := make(chan bool)

    go func() {
        for val := range channel {
            fmt.Println(val)
            // sleep 1 second
            time.Sleep(1 * time.Second)
        }
        isOver <- true
    }()

    channel <- 1
    channel <- 2
    channel <- 3
    channel <- 4
    channel <- 5
    channel <- 6
    channel <- 7
    channel <- 8
    channel <- 9
    channel <- 10
    close(channel)

    fmt.Println("Channel Value is: ", <-channel)
    <-isOver
}

输出为:

1
2
3
4
5
6
7
Channel Value is:  8
9
10

注意:了解代码中发生的情况的更好方法是逐步跟踪代码。

发生了什么? 看到这个痕迹:

1 // print 1 and sleep 1s
2,3,4 // stop for get values

3,4 // print 2 and sleep 1
3,4,5 // stop for get value

4,5 // print 3 and sleep 1
4,5,6 // stop for get value

5,6 // print 4 and sleep 1
5,6,7 // stop for get value

6,7 // print 5 and sleep 1
6,7,8 // stop for get value

7,8 // print 6 and sleep 1
7,8,9 // stop for get value

8,9 // print 7 and sleep 1
8,9,10 // stop for get value
// close channel
// in this line get value : fmt.Println("Channel Value is: ", <-channel)

9,10 // before get value
10 // get 9 and sleep 1
// get 10 and sleep 1

// isdone send signal true

在跟踪代码中,当通道获取树值 8,9,10 时,通道关闭,在第 2 步中发生了事情:

1 - 如果 goroutine 快速获取所有值(通道值是:0)
2 - 如果 goroutine 繁忙(通道值为:7 或 8 或 9 或 10)

有关详细信息,请参阅此问题:何时使用缓冲通道
并查看此网站以更好地理解:链接

到这里,我们也就讲完了《为何通道不使用最新的声明值?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>