Golang Basic 5 - If statement

Golang Basic 5 - If statement

if

if statement

if boolVal{
    // if boolVal is true, run this code block.
}

Sample

root@go:l5/ # cat l5s1.go
package main
import (
    "fmt"
)

func main(){
    var flag bool = true

    if flag {
        fmt.Printf("%t\n", flag)
    }
}
root@go:l5/ # go run l5s1.go
true

if... else...

if... else... statement

if boolVal{
    // if boolVal is true, run this code block.    
}else{
    //if boolVal is false, run this code block.
}

Sample

root@go:l5/ # cat l5s2.go
package main
import (
    "fmt"
)

func main(){
    var flag bool = false

    if flag {
        fmt.Printf("%t\n", flag)

    }else{
        fmt.Printf("%t\n", flag)

    }
}
root@go:l5/ # go run l5s2.go
false

if... else if... else...

if... else if... else... statement

if boolVal1{
    // if boolVal1 is true, run this code block.
}else if boolVal2{
    // if boolVal2 is true, run this code block.
}else{
    //if boolVal1 and boolVal2 is false, run this code block.
}

Sample

root@go:l5/ # cat l5s3.go
package main
import (
    "fmt"
)

func main(){
    var flag1 bool = false
    var flag2 bool = true

    if flag1 {
        fmt.Printf("flag 1\n")
        fmt.Printf("%t\n", flag1)
        fmt.Printf("%t\n", flag2)

    }else if flag2 {
        fmt.Printf("flag 2\n")
        fmt.Printf("%t\n", flag1)
        fmt.Printf("%t\n", flag2)

    }else{
        fmt.Printf("else\n")
        fmt.Printf("%t\n", flag1)
        fmt.Printf("%t\n", flag2)

    }
}
root@go:l5/ # go run l5s3.go
flag 2
false
true

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据