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
Various examples of printing and formatting in Golang
Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]
How to add and update elements in Map?
Golang HTTP GET request with parameters
Example: Fields and FieldsFunc from BYTES Package
How to replace emoji characters in string using regex in Golang?
Convert Int data type to Int16 Int32 Int64
Normal function parameter with variadic function parameter
Find length of Channel, Pointer, Slice, String and Map
What is Rune? How to get ASCII value of any character in Go?