Golang conditionals: Difference between revisions
From wikinotes
(Created page with "= If statements = <blockquote> <syntaxhighlight lang="go"> if 1 > 0 { fmt.Println("true") } </syntaxhighlight> You can also assign and test a variable in one step. <synta...") |
No edit summary |
||
(11 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= If | = If Not = | ||
<blockquote> | |||
<syntaxhighlight lang="go"> | |||
if !(1 == 0) { | |||
fmt.Println("hi") | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- If Not --> | |||
= If Statements = | |||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
if 1 > 0 { | if 1 > 0 { | ||
// ... | |||
} else if 2 > 0 { | |||
// ... | |||
} else { | |||
// ... | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Similar to for loops, go's if statements can use an '''initializer'''. | |||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
if err := file.Chmod(0664); err != nil { | if err := file.Chmod(0664); err != nil { | ||
log.Print(err) | log.Print(err) | ||
} | |||
// common in hash tests | |||
if _, ok := myMap["someKey"]; ok { | |||
// eval if key present | |||
} | |||
</syntaxhighlight> | |||
Short Circuiting | |||
<syntaxhighlight lang="go"> | |||
// the final 'false' is never evaluated, because the second is true | |||
if false || true || false { ... } | |||
// same deal with elses | |||
if false { | |||
// ... | |||
} else if true { | |||
// ... | |||
} else if false { // <-- expression never evaluated | |||
// ... | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- If statements --> | </blockquote><!-- If statements --> | ||
= Switch Statements = | |||
<blockquote> | |||
Control flow | |||
<syntaxhighlight lang="go"> | |||
break // exit switch statement early | |||
fallthrough // continue evaluating switch, when true | |||
</syntaxhighlight> | |||
Regular switch statements that test specific variable | |||
<syntaxhighlight lang="go"> | |||
switch var { | |||
case 'a', 'b', 'c': | |||
fmt.Println("is a, b, or c") | |||
case 'd', 'e', 'f': | |||
fmt.Println("is d, e, or f") | |||
default: | |||
fmt.Println("default behaviour") | |||
} | |||
</syntaxhighlight> | |||
Switch statements can also test different variables | |||
<syntaxhighlight lang="go"> | |||
switch { | |||
case '0' <= number: | |||
fmt.Println("is zero") | |||
case 'a' <= letter: | |||
fmt.Println("is a") | |||
} | |||
</syntaxhighlight> | |||
You may also force evaluate sections even if one case is matched with '''falthrough'''. | |||
<syntaxhighlight lang="go"> | |||
require "strings" | |||
var arch string | |||
var wordsize string | |||
cpuarch := "x86-64" | |||
switch { | |||
case strings.HasPrefix(cpuarch, "x86"): | |||
arch = "x86" | |||
fallthrough // <-- continue to next case statement, even when evaluates to true | |||
case strings.Split(cpuarch, "-")[1] == "64": | |||
wordsize = 64 | |||
} | |||
</syntaxhighlight> | |||
You can also swith on variable type. | |||
<syntaxhighlight lang="go"> | |||
wordsize := 64 | |||
switch wordsize.(type) { | |||
int: | |||
// ... | |||
string: | |||
// ... | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- Switch Statements --> |
Latest revision as of 14:32, 26 June 2022
If Not
if !(1 == 0) { fmt.Println("hi") }
If Statements
if 1 > 0 { // ... } else if 2 > 0 { // ... } else { // ... }Similar to for loops, go's if statements can use an initializer.
if err := file.Chmod(0664); err != nil { log.Print(err) } // common in hash tests if _, ok := myMap["someKey"]; ok { // eval if key present }Short Circuiting
// the final 'false' is never evaluated, because the second is true if false || true || false { ... } // same deal with elses if false { // ... } else if true { // ... } else if false { // <-- expression never evaluated // ... }
Switch Statements
Control flow
break // exit switch statement early fallthrough // continue evaluating switch, when trueRegular switch statements that test specific variable
switch var { case 'a', 'b', 'c': fmt.Println("is a, b, or c") case 'd', 'e', 'f': fmt.Println("is d, e, or f") default: fmt.Println("default behaviour") }Switch statements can also test different variables
switch { case '0' <= number: fmt.Println("is zero") case 'a' <= letter: fmt.Println("is a") }You may also force evaluate sections even if one case is matched with falthrough.
require "strings" var arch string var wordsize string cpuarch := "x86-64" switch { case strings.HasPrefix(cpuarch, "x86"): arch = "x86" fallthrough // <-- continue to next case statement, even when evaluates to true case strings.Split(cpuarch, "-")[1] == "64": wordsize = 64 }You can also swith on variable type.
wordsize := 64 switch wordsize.(type) { int: // ... string: // ... }