for
package mainimport "fmt"func main(){// The most basic type, with a single conditioni := 1for i <= 3 {fmt.Println(i)i = i + 1}// A classic initial/condition/after for loopfor j := 7; j <= 9; j++ {fmt.Println(j)}// for without a condition will loop repeatedly until you break out of// the loop or return from the enclosing functionfor {fmt.Println("loop")break}// You can also continue to the next iteration of the loopfor n := 0; n <= 5; n++{if n%2 == 0{continue}fmt.Println(n)}}
123789loop135
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特性全

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