Golang關鍵字—— if/else


  Golang中,if/else 關鍵字用於條件判斷,如果滿足條件就做某事,否則做另一件事:

    if age >= 18 {
        fmt.Println("成年人")
    } else {
        fmt.Println("未成年")
    }

  多重判斷:

    if score >= 90 {
        fmt.Println("優秀")
    } else if score >= 70 {
        fmt.Println("良好")
    } else if score >= 60 {
        fmt.Println("一般")
    } else {
        fmt.Println("")
    }

  Golang允許在條件判斷語句里聲明一個變量,該變量的作用域只在該條件邏輯塊內:

    if score := 40; score >= 90 {
        fmt.Println("優秀:", score)
    } else if score >= 70 {
        fmt.Println("良好")
    } else if score >= 60 {
        fmt.Println("一般")
    } else {
        fmt.Println("差:", score)
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM