Example of Switch Case with Break in For Loop
Below is a short example in which Break Statement breaks a loop inside Switch Case. This is a very rear scenario but good to learn.
Example
package main
import "fmt"
func main() {
testLoop:for val := 1; val < 7; val++ {
fmt.Printf("%d", val)
switch {
case val == 1:
fmt.Println("->Start")
case val == 5:
fmt.Println("->Break")
break testLoop
case val > 2:
fmt.Println("->Running")
break
default:
fmt.Println("->Progress")
}
}
}
Output
1->Start
2->Progress
3->Running
4->Running
5->Break
Most Helpful This Week
How do you write multi-line strings in Go?
Add N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time
Regular expression to extract filename from given path in Golang
How to get the current date and time with timestamp in local and other timezones ?
Different ways to convert Byte Array into String
How to set timeout for http.Get() requests in Golang?