# Label Breaks In Go

DevFeed: [Label Breaks In Go](<https://devfeed.tech/articles/label-breaks-in-go-22082.md>)

Original publisher: [Read original article](<https://www.ardanlabs.com/blog/2013/11/label-breaks-in-go.html>)

Published: 2013-11-21T00:00:00Z

Content type: tutorial

Language: en

Sources: [William Kennedy](<https://devfeed.tech/sources/william-kennedy.md>)

Topics: [Go Language](<https://devfeed.tech/topics/go-language.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [ardan-labs](<https://devfeed.tech/tags/ardan-labs.md>), [blog](<https://devfeed.tech/tags/blog.md>), [channel](<https://devfeed.tech/tags/channel.md>), [code](<https://devfeed.tech/tags/code.md>), [coding](<https://devfeed.tech/tags/coding.md>), [go](<https://devfeed.tech/tags/go.md>), [go-programming](<https://devfeed.tech/tags/go-programming.md>), [golang](<https://devfeed.tech/tags/golang.md>), [interrupt](<https://devfeed.tech/tags/interrupt.md>), [os](<https://devfeed.tech/tags/os.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

A tutorial explaining how labeled break statements in Go can exit both a case within a select statement and the surrounding for loop. It contrasts ordinary break behavior with using a Loop label and break Loop.

## Source excerpt

Have you ever found yourself in this situation. You have a case statement inside of a for loop and you would like to break from both the case and for statements in a single call? var err error timeout := time.After(30 * time.Second) sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, os.Interrupt) complete := make(chan error) go launchProcessor(complete) for { select { case <-sigChan: atomic.StoreInt32(&shutdownFlag, 1) continue case <-timeout: os.Exit(1) case err = <-complete: break } // Break the loop break } return err Here I have an endless for loop waiting on three channels using a select statement. The first case is listening for an operating system Interrupt event. If the operating system requests the program to shutdown, this case will set a package level variable and continue back into the loop. The second case is listening for a timeout event. If the programs runs for 30 seconds, the timeout event will fire and the program will immediately terminate. The third case is listening for a complete event. If the Goroutine that is launched prior to entering the loop completes it work, it will notify the code on this channel. In this case we need to break out of both the case and the for loop. Fortunately there isn't any more logic to process outside of the select statement, so the second break statement works. If there were other cases that broke out of the select statement and did not require the loop to terminate, I would be in trouble. The code would require more logic and flags to determine when to break out of the loop and when to continue iterating. Go has an answer to this coding delima. You can define a label and break to that label. var err error timeout := time.After(30 * time.Second) sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, os.Interrupt) complete := make(chan error) go launchProcessor(complete) Loop: for { select { case <-sigChan: atomic.StoreInt32(&shutdownFlag, 1) continue case <-timeout: os.Exit(1) case err = <-complete: break