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
Line 1: Line 1:
= If statements =
= If Statements =
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
Line 14: Line 14:
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- If statements -->
</blockquote><!-- If statements -->
= Switch Statements =
<blockquote>
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")
}
</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>
</blockquote><!-- Switch Statements -->

Revision as of 20:46, 23 May 2022

If Statements

if 1 > 0 {
    fmt.Println("true")
}

You can also assign and test a variable in one step.

if err := file.Chmod(0664); err != nil {
    log.Print(err)
}

Switch Statements

Regular 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")
}

Switch statements can also test different variables

switch {
case '0' <= number:
    fmt.Println("is zero")
case 'a' <= letter:
    fmt.Println("is a")
}