for

  1. package main
  2. import "fmt"
  3. func main(){
  4. // The most basic type, with a single condition
  5. i := 1
  6. for i <= 3 {
  7. fmt.Println(i)
  8. i = i + 1
  9. }
  10. // A classic initial/condition/after for loop
  11. for j := 7; j <= 9; j++ {
  12. fmt.Println(j)
  13. }
  14. // for without a condition will loop repeatedly until you break out of
  15. // the loop or return from the enclosing function
  16. for {
  17. fmt.Println("loop")
  18. break
  19. }
  20. // You can also continue to the next iteration of the loop
  21. for n := 0; n <= 5; n++{
  22. if n%2 == 0{
  23. continue
  24. }
  25. fmt.Println(n)
  26. }
  27. }
  1. 1
  2. 2
  3. 3
  4. 7
  5. 8
  6. 9
  7. loop
  8. 1
  9. 3
  10. 5

if/else

package main

import "fmt"

func main() {
    // Here’s a basic example
    if 7%2 == 0 {
        fmt.Println("7 is even")
    } else {
        fmt.Println("7 is odd")
    }


    // You can have an if statement without an else
    if 8%4 == 0 {
        fmt.Println("8 is divisible by 4")
    }

    // A statement can precede conditionals; 
    // any variables declared in this statement are available in all branches.
    if num := 9; num < 0 {
        fmt.Println(num, "is negative")
    } else if num < 10 {
        fmt.Println(num, "has 1 digit")
    } else {
        fmt.Println(num, "has multiple digits")
    }
}
7 is odd
8 is divisible by 4
9 has 1 digit

C++ 版

  • 目前只有MSVC支持C++20特性全

image.png

import <format>;
import <iostream>;

int main() {
    if (auto num = 9; num < 0) {
        std::cout << std::format("{} is negative", num);
    }
    else if (num < 10) {
        std::cout << std::format("{} has 1 digit", num);
    } 
    else {
        std::cout << std::format("{} has multiple digits", num);
    }

    return 0;
}

Switch

package main

import (
    "fmt"
    "time"
)

func main() {
    // Here’s a basic switch
    i := 2
    fmt.Print("Write ", i, " as ")
    switch i {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    }

    // You can use commas to separate multiple expressions in the same case statement.
    // We use the optional default case in this example as well.
    switch time.Now().Weekday() {
    case time.Saturday, time.Sunday:
        fmt.Println("It's the weekend")
    default:
        fmt.Println("It's a weekday")
    }

    // switch without an expression is an alternate way to express if/else logic. 
    // Here we also show how the case expressions can be non-constants
    t := time.Now()
    switch {
    case t.Hour() < 12:
        fmt.Println("It's before noon")
    default:
        fmt.Println("It's after noon")
    }

    // A type switch compares types instead of values. You can use this to discover the type of an interface value. 
    // In this example, the variable t will have the type corresponding to its clause
    whatAmI := func(i interface{}) {
        switch t := i.(type) {
        case bool:
            fmt.Println("I'm a bool")
        case int:
            fmt.Println("I'm a int")
        default:
            fmt.Printf("Don't know type %T\n", t)
        }

    }

    whatAmI(true)
    whatAmI(1)
    whatAmI("hey")
}