title: Game Loop simulation in Golang
body: <p>I want to create game loop in go(lang), so i tried this:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      // "runtime"
      "sync"
      "time"
  )
  
  var v = 0
  var wg sync.WaitGroup
  var sec = 5
  
  func main() {
      wg.Add(1)
      gameLoop()
      wg.Wait()
  }
  
  func gameLoop() {
      time.AfterFunc(16*time.Millisecond, gameLoop)
      v++
      fmt.Println(v)
      if v == sec*60 {
          // fmt.Println("Goroutines: ", runtime.NumGoroutine())
          panic("err")
          wg.Done()
      }
  }
  </code></pre>
  
  <p>This program running at 62.5Hz <code>(16*time.Millisecond)</code>, var <code>sec</code> is used for calling <code>wg.Done()</code> after 5 second and caused var <code>v</code> printed 300 times.</p>
  
  <p>calling <code>panic("err")</code> making the result like this:</p>
  
  <pre><code>panic: err
  
  goroutine 314 [running]:
  panic(0x493c60, 0xc420094370)
      /usr/local/go/src/runtime/panic.go:500 +0x1a1
  main.gameLoop()
      /home/billyzaelani/Desktop/main.go:26 +0x11f
  created by time.goFunc
      /usr/local/go/src/time/sleep.go:154 +0x44
  exit status 2
  </code></pre>
  
  <p>Well what is the meaning of <code>goroutine 314 [running]</code> ? did i use 314 goroutine for 5 second game loop? how if this run for hours?</p>
  
  <p>But, if the program use runtime package and print <code>runtime.NumGoroutine</code> which is return number of goroutine, the result is <code>Goroutines:  2</code></p>
  
  <p>So, back again what is the meaning of <code>goroutine 314 [running]</code>? while runtime package say different things.</p>
  
  <p>Last one, if anyone can show me the better way to create game loop in golang, i really appreciate that, thankyou</p>