Conditional - Switch Statement Documentation

Part of Golang Mastery course

~15 min read
Interactive
Hands-on
Beginner-friendly

It's important as a programmer to be comfortable with the documentation, to know what the language specification is, to know what effective Go is, to be able to make sense of the way that the people who wrote these documents wrote them.

Have a look at the keywords

break default func interface select case defer go map struct chan else goto package switch const fallthrough if range type continue for import return var

So far we have looked at case, break, switch, fallthrough, continue, else, for, if, switch, var

Looking more broadly at the spec, we have already covered several of the items, including ...

The spec is about 50 or 60 pages describing in detail how the Go programming language works. So far, we have covered a fair amount of it already. We are doing great.

Let's look at the documentation for switch statements.

Switch statements#

"Switch" statements provide multi-way execution. An expression or type specifier is compared to the "cases" inside the "switch" to determine which branch to execute.

SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .

There are two forms: expression switches and type switches. In an expression switch, the cases contain expressions that are compared against the value of the switch expression. In a type switch, the cases contain types that are compared against the type of a specially annotated switch expression. The switch expression is evaluated exactly once in a switch statement.

Expression switches#

In an expression switch, the switch expression is evaluated and the case expressions, which need not be constants, are evaluated left-to-right and top-to-bottom; the first one that equals the switch expression triggers execution of the statements of the associated case; the other cases are skipped. If no case matches and there is a "default" case, its statements are executed. There can be at most one default case and it may appear anywhere in the "switch" statement. A missing switch expression is equivalent to the boolean value true.

ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" . ExprCaseClause = ExprSwitchCase ":" StatementList . ExprSwitchCase = "case" ExpressionList | "default" .

Effective Go has this to say about switch

Go's switch is more general than C's. The expressions need not be constants or even integers, the cases are evaluated top to bottom until a match is found, and if the switch has no expression it switches on true. It's therefore possible—and idiomatic—to write an if-else-if-else chain as a switch.

example.go
package main
 
import (
string">"fmt"
)
 
func main() {
fmt.Println(unhex(string">'E'))
}
 
func unhex(c byte) byte {
switch {
case string">'0' <= c && c <= string">'9':
return c - string">'0'
case string">'a' <= c && c <= string">'f':
return c - string">'a' + 10
case string">'A' <= c && c <= string">'F':
return c - string">'A' + 10
}
return 0
}
 

There is no automatic fall through, but cases can be presented in comma-separated lists.

example.go
func shouldEscape(c byte) bool {
switch c {
case string">' ', string">'?', string">'&', string">'=', string">'#', string">'+', string">'%':
return true
}
return false
}
 

Effective Go and the Go Spec are useful tools to make use of while learning Go.

Your Progress

24 of 103 modules
23%
Started23% Complete
Previous
SpaceComplete
Next