我最近开始学习 Go,我写过一个案例,我无法理解为什么他会根据仅打印 [第 13 行] 的一行中所做的更改获得两种不同的行为,
在第一次运行中,我使用 [第 13 行] 运行程序,然后在主例程中,当我在 [第 21 行] 打印通道长度时打印 0,在下一行打印 2 之后(我'谈论主要程序制作的第一个印刷品)。
在第二次运行中,我删除了 [第 13 行],然后在第一次打印时,通道的长度为 2。
在图片中,您可以在控制台中看到两个不同的打印件,我不明白为什么它们不同 [只是因为添加/删除第 13 行]。
// Go behavior that I do not understand /:
package main
import "fmt"
func main() {
mychnl := make(chan string, 2)
go func(input chan string) {
input <- "Inp1"
// If we remove this line the length of the chan in the print will be equal in both prints
fmt.Println("Tell me why D:")
input <- "Inp2"
input <- "Inp3"
input <- "Inp4"
close(input)
}(mychnl)
for res := range mychnl {
fmt.Printf("Length of the channel is: %v The received value is: %s length need to be == ", len(mychnl), res)
fmt.Println(len(mychnl))
}
}
/*
Output ->
Line 13 = fmt.Println("Tell me why D:")
First run with line 13:
Tell me why D:
Length of the channel is: 0 The received value is: Inp1 length need to be == 2
Length of the channel is: 2 The received value is: Inp2 length need to be == 2
Length of the channel is: 1 The received value is: Inp3 length need to be == 1
Length of the channel is: 0 The received value is: Inp4 length need to be == 0
Second run without line 13:
Length of the channel is: 2 The received value is: Inp1 length need to be == 2
Length of the channel is: 2 The received value is: Inp2 length need to be == 2
Length of the channel is: 1 The received value is: Inp3 length need to be == 1
Length of the channel is: 0 The received value is: Inp4 length need to be == 0
*/
慕容708150
相关分类