Go if语句中的多个初始值设定项

Edd*_*die 8 initialization go

刚刚发现了Go,到目前为止我非常好奇.我知道我只是懒惰,但我想知道是否可以在if语句中初始化多个变量.我知道以下是可能的:

if x := 5; x == 5 {
    fmt.Printf("Whee!\n")
}
Run Code Online (Sandbox Code Playgroud)

我尝试过以下方法:

if x := 5, y := 38; x == 5 {
    fmt.Printf("Whee! %d\n", y)
}

if x := 5 && y := 38; x == 5 {
    fmt.Printf("Whee! %d\n", y)
}
Run Code Online (Sandbox Code Playgroud)

但都没有奏效.我查看了Go网站上的文档,所以我有什么遗漏或者这根本不可能吗?

小智 15

这是怎么做的:

package main

import (
    "fmt"
)

func main() {
    if x, y := 5, 38; x == 5 {
        fmt.Printf("Whee! %d\n", y)
    }
}
Run Code Online (Sandbox Code Playgroud)

测试版本:

changeset:   3975:b51fd2d6c160
tag:         tip
user:        Kevin Ballard <xxxxxxxxxxxxxxxxxxxxx>
date:        Tue Nov 10 20:05:24 2009 -0800
summary:     Implement new emacs command M-x gofmt
Run Code Online (Sandbox Code Playgroud)