Skip to content

q007常见语法题 第8题 #75

@ImNotGeek

Description

@ImNotGeek
func main() {
	abc := make(chan int, 1000)
	for i := 0; i < 10; i++ {
		abc <- i
	}
	go func() {
		for  a := range abc  {
			fmt.Println("a: ", a)
		}
	}()
	close(abc)
	fmt.Println("close")
	time.Sleep(time.Second * 100)
}

range channel 会卡住,直到 channel 关闭了才会遍历,所以 “协程可能还未启动,管道就关闭了” 是没问题的。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions