你能在Go中一次声明多个变量吗?

Kev*_*rke 53 variable-assignment go

是否可以使用Golang一次声明多个变量?

例如,在Python中,您可以键入:

a = b = c = 80
Run Code Online (Sandbox Code Playgroud)

并且所有值都是80.

Kev*_*rke 74

是的你可以:

var a, b, c string
a = "foo"
fmt.Println(a)
Run Code Online (Sandbox Code Playgroud)

您可以为内联分配执行类似的操作,但不太方便:

a, b, c := 80, 80, 80
Run Code Online (Sandbox Code Playgroud)

  • @CharlieParker它有效.你只需要将类型声明移到`=`的另一边:http://play.golang.org/p/cj0nZ-RbpI (9认同)
  • 在 Python 中执行 `a = b = c = 80` 与在 Go 中执行 `a, b, c := 80, 80, 80` 不同,因为在 Python 中,您使用一个值初始化 3 个变量,而在 Go 中,您可以使用一个值来初始化 3 个变量。需要提供3个值。所以我认为这并不能回答问题。 (4认同)
  • 这个答案是错误的,他的问题的真正答案是:不,你不能。我正在寻找这个,但是 a, b = 80, 80 与 a, b = 80 不同 (2认同)

Von*_*onC 11

在语言规范方面,这是因为变量定义为:

VarDecl     = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
VarSpec     = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
Run Code Online (Sandbox Code Playgroud)

(摘自" 变量声明 ")

一种类型的标识符列表,分配给一个表达式ExpressionList.

const a, b, c = 3, 4, "foo"  // a = 3, b = 4, c = "foo", untyped integer and string constants
const u, v float32 = 0, 3    // u = 0.0, v = 3.0
Run Code Online (Sandbox Code Playgroud)


Mat*_*att 9

另一种方法是这样的

var (
   a = 12
   b = 3
   enableFeatureA = false

   foo = "bar"
   myvar float64
   anothervar float64 = 2.4
)
Run Code Online (Sandbox Code Playgroud)

也适用于 const

const (
  xconst    = 5
  boolconst = false
)
Run Code Online (Sandbox Code Playgroud)


Cpp*_*oob 7

是的,您可以,它比看起来要细微得多。

首先,您可以做一些简单的事情:

var a, b, x, y int  // declares four variables all of type int
Run Code Online (Sandbox Code Playgroud)

您可以在函数参数声明中使用相同的语法:

func foo(a, b string) {  // takes two string parameters a and b
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后是用于同时声明和分配变量的简写语法。

x, y := "Hello", 10   // x is an instance of `string`, y is of type `int`
Run Code Online (Sandbox Code Playgroud)

Golang中经常遇到的模式是:

result, err := some_api(...)  // declares and sets `result` and `err`
if err != nil {
    // ...
    return err
}

result1, err := some_other_api(...)   // declares and sets `result1`, reassigns `err`
if err != nil {
    return err
}
Run Code Online (Sandbox Code Playgroud)

因此,只要分配给变量的:=至少一个是新变量,就可以在运算符的左侧分配给已定义的变量。否则格式不正确。这很不错,因为它允许我们为多个API调用重用相同的错误变量,而不必为每个API调用定义一个新的错误变量。但请注意不要误用以下内容:

result, err := some_api(...)  // declares and sets `result` and `err`
if err != nil {
    // ...
    return err
}

if result1, err := some_other_api(...); err != nil {   // result1, err are both created afresh, 
                                                       // visible only in the scope of this block.
                                                       // this err shadows err from outer block
    return err
}
Run Code Online (Sandbox Code Playgroud)


Oli*_*ver 7

有几个答案是不正确的:它们忽略了OP询问是否可以一次性将多个变量设置为相同值的事实(抱歉双关语)。

在 go 中,如果 a、b、c 是变量,则似乎不能,即您必须单独设置每个变量:

a, b, c := 80, 80, 80
Run Code Online (Sandbox Code Playgroud)

但如果 a、b、c 是常数,您可以:

const (
        a = 80
        b
        c
    )
Run Code Online (Sandbox Code Playgroud)