jjzjj

go - 为什么当我在发件人端关闭时仍然会出现 "send on closed channel" panic ?

我有一个stopChan来通知发送者关闭channel,还有一个sync.Once来确保只有一个发送者可以关闭channel,但我仍然收到“发送在关闭的channel上”panic,为什么?funcmuitiSenderClose(){constSenderNum=3wg:=sync.WaitGroup{}wg.Add(SenderNum)intChan:=make(chanint)stopChan:=make(chanstruct{})once:=sync.Once{}fori:=0;i10{close(stopChan)fmt.Printf("Got%d\n",sum)break}

go - 使用 ticker 的 golang 代码有什么问题

packagemainimport("fmt""time")funcmain(){intChan:=make(chanint,1)ticker:=time.NewTicker(time.Second)gofunc(){for_=rangeticker.C{select{caseintChan10{fmt.Printf("Got:%v\n",sum)break//ticker.Stop()}}fmt.Println("End.[receiver]")//time.Sleep(10)}我是golang的新手。在这段代码中,我想在goroutine结束时打印一次“End.[sender]”。