golang if作用域

package main

import (
    "fmt"
)

func main() {
    x := 0
    if x := test(); x < 0 {
        fmt.Println("first")
        fmt.Println(x)
    } else {
        fmt.Println("second")
        fmt.Println(x)
    }
    fmt.Println(x)
}

func test() int {
    return 1
}

结果:

second
1
0
posted @ 2025-01-09 10:48  rincloud  阅读(13)  评论(0)    收藏  举报