title: Golang: How can I stop the execution of a for loop from outside the loop?
body: <p>I am using an infinite for loop with a label. Outside the scope of the for loop, I have a scheduled function running as a go routine. I want to break the for loop from the scheduled function when a certain condition is met. How can I accomplish the same ? This is what I am trying, which obviously won't work due to scope issue.</p>
  
  <pre><code>package main
  
  import (
    "fmt"
    "time"
    "sync"
  )
  
  func main() {
    count := 0
    var wg sync.WaitGroup
    wg.Add(1)
    t := time.NewTicker(time.Second*1)
  
    go func (){
      for {
          fmt.Println("I will print every second", count)
          count++ 
          if count &gt; 5 {
            break myLoop;
            wg.Done()
          }
          &lt;-t.C
      }  
    }()
  
    i := 1
  
    myLoop:
    for {
      fmt.Println("iteration", i)
      i++
    }
  
    wg.Wait()
    fmt.Println("I will execute at the end")
  
  }
  </code></pre>