초기화되지 않은 채널을 보유하는 구조체가 있습니다.초기화되지 않은 채널은 어떻게 작동합니까?
내가 쓸 때 루틴이 예상대로 차단되지만 독자에게 파이프에 무엇이 있는지 알려주지 않습니다.
나는 오류가 없으며 무엇을하고 있는지 궁금합니다. Go.
다음 예에서는 pushed
또는 got it
메시지가 인쇄되지 않습니다. 여기
type MyStruct struct {
over chan bool
}
func main() {
nonInitialized := &MyStruct{}
// nonInitialized.over = make(chan bool)
go func() {
for i := 0; i < 10; i++ {
select {
case <-nonInitialized.over:
fmt.Println("got it")
default:
// proceed
}
fmt.Println("do some stuff")
time.Sleep(10 * time.Millisecond)
}
panic("took too long")
}()
// push on the non initialized channel
fmt.Println("pushing")
nonInitialized.over <- true
fmt.Println("pushed")
}
는
https://play.golang.org/p/76zrCuoeoh는이 질문의 목적 (내가 채널을 초기화해야 알 수없는
놀이터입니다합니다 (초기화 데이터 주석을 제거하고 매력처럼 작동합니다), 내가 알고 싶은 Go에서 초기화되지 않은 채널로 어떤 일이 일어나는지)