英文:
what's wrong with the golang code by using ticker
问题
以下是您提供的代码的翻译:
package main
import (
"fmt"
"time"
)
func main() {
intChan := make(chan int, 1)
ticker := time.NewTicker(time.Second)
go func() {
for _ = range ticker.C {
select {
case intChan <- 1:
case intChan <- 2:
case intChan <- 3:
}
}
/*defer */
fmt.Println("结束。[发送者]")
}()
var sum int
for e := range intChan {
fmt.Printf("接收到: %v\n", e)
sum += e
if sum > 10 {
fmt.Printf("得到: %v\n", sum)
break
//ticker.Stop()
}
}
fmt.Println("结束。[接收者]")
//time.Sleep(10)
}
在这段代码中,您想要在 goroutine 结束时打印一次 "结束。[发送者]"。我尝试使用 ticker.Stop()
、time.Sleep()
和 defer
,但没有效果。请问有什么问题,给我一些想法。谢谢。
在这段代码中,问题可能出在 ticker.Stop()
的位置上。您可以尝试将 ticker.Stop()
放在 break
语句之前,如下所示:
if sum > 10 {
fmt.Printf("得到: %v\n", sum)
ticker.Stop()
break
}
这样做可以确保在跳出循环之前停止 ticker。另外,您还可以使用 defer
关键字来确保在函数结束时调用 ticker.Stop()
,如下所示:
defer ticker.Stop()
这样无论函数是正常结束还是发生异常,都会调用 ticker.Stop()
。希望这些想法对您有帮助!
英文:
package main
import (
"fmt"
"time"
)
func main() {
intChan := make(chan int, 1)
ticker := time.NewTicker(time.Second)
go func() {
for _ = range ticker.C {
select {
case intChan <- 1:
case intChan <- 2:
case intChan <- 3:
}
}
/*defer */
fmt.Println("End. [sender]")
}()
var sum int
for e := range intChan {
fmt.Printf("Received: %v\n", e)
sum += e
if sum > 10 {
fmt.Printf("Got: %v\n", sum)
break
//ticker.Stop()
}
}
fmt.Println("End. [receiver]")
//time.Sleep(10)
}
I'm new in golang. In this code, I want print "End. [sender]" once when the goroutine is over.
I try to use ticker.stop(), or even time.sleep(), defer , but no effect.
What's wrong with it, please give me some idea. thanks
答案1
得分: 2
根据文档所说,ticker.Stop不会关闭通道,所以你不能期望它会中断循环。你可以添加一个新的通道来退出。
package main
import (
"fmt"
"time"
)
func main() {
intChan := make(chan int, 2)
ticker := time.NewTicker(time.Second)
quit := make(chan bool)
go func() {
loop:
for {
select {
case <-ticker.C:
select {
case intChan <- 1:
case intChan <- 2:
case intChan <- 3:
}
case <-quit:
break loop
}
}
/*defer */
fmt.Println("End. [sender]")
close(intChan)
}()
var sum int
for e := range intChan {
fmt.Printf("Received: %v\n", e)
sum += e
if sum > 10 {
fmt.Printf("Got: %v\n", sum)
quit <- true
//break
//ticker.Stop()
}
}
fmt.Println("End. [receiver]")
//time.Sleep(10)
}
英文:
As documentation says, ticker.Stop doesn't close channel. So you must not expect break-loop. You can add new channel for quit.
package main
import (
"fmt"
"time"
)
func main() {
intChan := make(chan int, 2)
ticker := time.NewTicker(time.Second)
quit := make(chan bool)
go func() {
loop:
for {
select {
case <-ticker.C:
select {
case intChan <- 1:
case intChan <- 2:
case intChan <- 3:
}
case <-quit:
break loop
}
}
/*defer */
fmt.Println("End. [sender]")
close(intChan)
}()
var sum int
for e := range intChan {
fmt.Printf("Received: %v\n", e)
sum += e
if sum > 10 {
fmt.Printf("Got: %v\n", sum)
quit <- true
//break
//ticker.Stop()
}
}
fmt.Println("End. [receiver]")
//time.Sleep(10)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论