Skip to content

Instantly share code, notes, and snippets.

@qbig
Created August 20, 2018 02:27
Show Gist options
  • Select an option

  • Save qbig/f742cb8d886df8ca3b9b38eb5259a54d to your computer and use it in GitHub Desktop.

Select an option

Save qbig/f742cb8d886df8ca3b9b38eb5259a54d to your computer and use it in GitHub Desktop.
Golang Weird; break keyword

A "break" statement terminates execution of the innermost "for", "switch", or "select" statement within the same function.

Infinite Loop!!!!

package main

import (
	"fmt"
	"time"
)

func main() {
	done := time.After(1 * time.Millisecond)
	numbers := make(chan int)
	go func() {
		for n := 0; ; {
			numbers <- n
			n++
		}
	}()
	for {
		select {
		case <-done:
			break
		case num := <-numbers:
			fmt.Println(num)
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment