package main
import (
"fmt"
"time"
"github.com/gopherjs/gopherjs/js"
)
func main() {
for range time.Tick(time.Second) {
fmt.Println("Hello World")
return false
}
}
go 1.10 just print once
https://gopherjs.github.io/playground/#/-0ATHsy2Xk
I don't think this is an issue with GopherJS; the standard library documents exactly this "problem" with time.Tick. So if you care about the tidy up of the ticker, use time.NewTicker and then call Stop
To expand on @myitcv's answer: In regular Go the timer goes away in this case because the program exits. In Go, if you start some goroutines (like a timer) and then exit main, your program will exit and those goroutines will, perforce, stop -- or, more accurately, just cease to exist.
GopherJS doesn't really have the concept of the program exiting. In GopherJS if you start some goroutines and then exit main, those goroutines keep on going. To get really analogous behavior, when you exited main GopherJS would either have to close the browser page, or jump through a lot of hoops to make the compiler track all the goroutines you start and shut them down again (or something like that). That's been discussed and it was decided that that was a bad idea.
Also, a note: You want return not return false. And running your code in the GopherJS playground, Hello World only prints once, just as with regular Go. Was this a condensation of a larger test case?
At a guess, you meant something like this
func main() {
go func() {
for range time.Tick(time.Second) {
fmt.Println("Hello World")
}
}()
return
}
in which case Hello World does indeed keep printing after main exits.
If you run this code as-is in the Go playground, you get no output at all, since there's a race between the goroutine running and printing anything, and the program exiting, a race which the goroutine loses. If you add a 1s sleep before the return, then you get a line of output.
Most helpful comment
To expand on @myitcv's answer: In regular Go the timer goes away in this case because the program exits. In Go, if you start some goroutines (like a timer) and then exit
main, your program will exit and those goroutines will, perforce, stop -- or, more accurately, just cease to exist.GopherJS doesn't really have the concept of the program exiting. In GopherJS if you start some goroutines and then exit
main, those goroutines keep on going. To get really analogous behavior, when you exitedmainGopherJS would either have to close the browser page, or jump through a lot of hoops to make the compiler track all the goroutines you start and shut them down again (or something like that). That's been discussed and it was decided that that was a bad idea.Also, a note: You want
returnnotreturn false. And running your code in the GopherJS playground,Hello Worldonly prints once, just as with regular Go. Was this a condensation of a larger test case?At a guess, you meant something like this
in which case
Hello Worlddoes indeed keep printing aftermainexits.If you run this code as-is in the Go playground, you get no output at all, since there's a race between the goroutine running and printing anything, and the program exiting, a race which the goroutine loses. If you add a 1s sleep before the return, then you get a line of output.