点击(此处)折叠或打开

  1. // IfElse
  2. package main

  3. import (
  4.     "fmt"
  5. )

  6. func main() {
  7.     if 7%2 == 0 {
  8.         fmt.Println("7 is even")
  9.     } else {
  10.         fmt.Println("7 is odd")
  11.     }
  12.     //You can have an if statement without an else.
  13.     if 8%4 == 0 {
  14.         fmt.Println("8 is divisible by 4")
  15.     }
  16.     //A statement can precede conditionals; any variables declared in this statement are available in all branches.
  17.     if num := 9; num < 0 {
  18.         fmt.Println(num, "is negative")
  19.     } else if num < 10 {
  20.         fmt.Println(num, "has 1 digit")
  21.     } else {
  22.         fmt.Println(num, "has multiple digits")
  23.     }
  24. }
转自https://gobyexample.com
09-06 17:20