go select and switch statement
tags: learning go programming diff-between
content
syntax
select:
go func() {
select {
case i := <-chInt
fmt.Println(i)
case c := <-chString
fmt.Println(c)
}
}()switch:
func main() {
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("macOS.")
case "linux":
fmt.Println("Linux.")
default:
fmt.Printf("%s.\n", os)
}
}function
switchis just another way to doif-elseselectlets go routine to wait on multiple communication operation- communication operation: channel is one of them
selectis blocked when none of the case is matchedselectpicks a random one when multiple communication is ready