Answer the question
In order to leave comments, you need to log in
How to correctly organize a cyclic event on a timer in Go?
I'm just getting started with Go. As a training, I decided to write a websocket server for a js game. It is necessary to organize a cyclic event every 50ms to process movements and send new coordinates to clients. I'm trying to start a goroutine that will wait for a timer event:
func main() {
go worker(time.NewTicker(50 * time.Millisecond))
for {
}
}
func worker(ticker *time.Ticker) {
var lastUpdate = time.Now()
var timeSince int64
for range ticker.C {
timeSince = time.Since(lastUpdate).Nanoseconds() / 1000000
fmt.Println("Time to work ", timeSince)
lastUpdate = time.Now()
}
}
Answer the question
In order to leave comments, you need to log in
You load the processor to the maximum with a useless cycle
for {
}
select {
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question