Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Break statement in Select in Go (Golang)

Posted on August 14, 2023August 14, 2023 by admin

Table of Contents

  • Overview
  • Code

Overview

Break keyword can be used in select to terminate the execution of innermost statement similar to switch and for look. Below is the break keyword example in select.

Code

package main
import "fmt"
func main() {
ch := make(chan string, 1)
ch <- "Before break"
select {
case msg := <-ch:
fmt.Println(msg)
break
fmt.Println("After break")
default:
fmt.Println("Default case")
}
}

Output

Before break

break statement will terminate the execution of innermost statement and below line below will never be executed

fmt.Println("After break")
  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP