Home
Map
if, else Examples (If Map)Use if, else if and else statements to test values. Use if with a map.
Go
This page was last reviewed on Sep 16, 2021.
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 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.
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.
Detail The variable is scoped to the if-statement and its contents. Here we cannot reference "t" outside the if.
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.
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).
Detail If we use the variable outside its scope, an "undefined" error will occur. This is a compile-time error.
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.
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.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 16, 2021 (image).
Home
Changes
© 2007-2024 Sam Allen.