Can you declare multiple variables at once in Go?

前端 未结 6 1272
鱼传尺愫
鱼传尺愫 2020-12-01 15:36

Is it possible to declare multiple variables at once using Golang?

For example in Python you can type this:

a = b = c = 80

and all

6条回答
  •  青春惊慌失措
    2020-12-01 15:57

    In terms of language specification, this is because the variables are defined with:

    VarDecl     = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
    VarSpec     = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
    

    (From "Variable declaration")

    A list of identifiers for one type, assigned to one expression or 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
    

提交回复
热议问题