Home
Search
Golang If, Else Examples (If Map)Use if, else if and else statements to test values. Use if with a map.
If. An if-statement tests a variable in some way. In Go we can build complex if constructs with else-if and else parts. Statements are evaluated until a condition is true.
Statement info. With initialization statements, we can declare variables in the same line as an if-statement. This syntax leads to clean, understandable code.
Switch
map
An example. We see an if-statement with else-if and else parts. The variable named "value" is initialized to 5. The first if-statement test, >= 5, evaluates to true.
Note The condition of an if-statement has no surrounding parentheses. The body statements are surrounded by curly braces.
Note 2 In Go the starting curly brace must be placed at the end of the same line. The Gofmt command can help with formatting issues.
Golang program that uses if, else statements
package main import "fmt" func main() { value := 10 // Test the value with an if-else construct. if value >= 5 { fmt.Println(5) } else if value >= 15 { fmt.Println(15) // Not reached. } else { fmt.Println("?") // Not reached. } }
5
Initialization statements. A variable can be declared and initialized before the condition of an if-statement (on the same line). This syntax makes sense.
Scope The variable is scoped to the if-statement and its contents. Here we cannot reference "t" outside the if.
Golang program that uses if with initialization statement
package main import "fmt" func test() int { return 100 } func main() { // Initialize a variable in an if-statement. // ... Then check it against a constant. if t := test(); t == 100 { fmt.Println(t) } }
100
Map syntax. The if-statement in Golang is often used when testing a map. We assign 2 values to a map lookup expression, and then test the boolean "exists" value.
Tip We do not need to do a separate lookup on the map to get the value again. It already was retrieved.
map
Golang program that uses if, map syntax
package main import "fmt" func main() { test := map[string]bool{} test["bird"] = true // We can assign to a map, then test the bool ok in a single if-statement. if value, ok := test["bird"]; ok { fmt.Println("Value:", value) } }
Value: true
Initialization scope. When we declare a variable at the start of an if-statement, it is scoped to that if-statement (and any else-if and elses).
Undefined If we use the variable outside its scope, an "undefined" error will occur. This is a compile-time error.
Golang program that uses causes error, undefined variable
package main import "fmt" func main() { mult := 10 // The variable "t" can be accessed only in the if-statement. if t := 100 * mult; t >= 200 { fmt.Println(t) } // This causes an error. fmt.Println(t) }
C:\programs\file.go:16: undefined: t
Unexpected newline error. An "else" in a Go program must be preceded by the closing brace. We cannot put "else" on a separate line. A compile-time error will occur.
Golang program that has unexpected newline error
package main import "fmt" func main() { // This program won't compile. value := 10 if value == 20 { fmt.Println(20) } else { fmt.Println(0) } }
# command-line-arguments syntax error: unexpected semicolon or newline before else syntax error: unexpected }
If, switch performance. In a Go benchmark we can show an integer switch has better performance from an integer if-else chain. This must be carefully tested.
Switch
Also A lookup table can offer better performance than switch or if statements. Some initialization is needed.
Lookup Table
A summary. Ifs are powerful in Go. In most ways they are the same as those in C-like languages. But the initialization statement feature provides a new, scoped variable for the block.
Home
© 2007-2021 sam allen. see site info on the changelog