Multiple variables of different types in one line in Go (without short variable declaration syntax)

huangapple go评论69阅读模式
英文:

Multiple variables of different types in one line in Go (without short variable declaration syntax)

问题

我想知道在Go语言中是否有一种方法可以在一行中声明和初始化多个不同类型的变量,而不使用短声明语法:=

例如,声明两个相同类型的变量是可能的:

var a, b string = "hello", "world"

使用:=语法声明不同类型的变量也是可能的:

c, d, e := 1, 2, "whatever"

但是,下面的语法会导致错误:

var f int, g string = 1, "test"

当然,我希望保留变量的类型,否则我可以直接使用:=语法。

不幸的是,我找不到任何示例,所以我认为这是不可能的。

如果不行,有人知道是否计划在未来的版本中引入这样的语法吗?

英文:

I was wondering if there's a way with Go to declare and initialise multiple variables of different types in one line without using the short declaration syntax :=.

Declaring for example two variables of the same type is possible:

<!-- language: go -->

var a, b string = &quot;hello&quot;, &quot;world&quot;

Declaring variables of different types with the := syntax is also possible:

<!-- language: go -->

c, d, e := 1, 2, &quot;whatever&quot;

This gives me an error instead:

<!-- language: go -->

var f int, g string = 1, &quot;test&quot;

Of course I'd like to keep the type otherwise I can just use the := syntax.

Unfortunately I couldn't find any examples so I'm assuming this is just not possible?

If not, anyone knows if there's a plan to introduce such syntax in future releases?

答案1

得分: 20

如果省略类型,是可以的:

var i, s = 2, "hi"
fmt.Println(i, s)

输出结果(在Go Playground上尝试):

2 hi

请注意,短变量声明实际上就是这个的简写形式:

一个 短变量声明 的语法如下:

ShortVarDecl = IdentifierList ":=" ExpressionList .

它是一个带有初始化表达式但没有类型的常规变量声明的简写形式:

"var" IdentifierList = ExpressionList .

如果不省略类型,是不可能的,因为变量声明的语法如下:

VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .

(对于带有表达式列表的标识符列表,只有一个可选类型。)

此外,我假设你不将以下代码视为一行(否则它是有效的语法,但 gofmt 会将其拆分为多行):

var (i int = 2; s string = "hi")

另外,如果你只想明确指定类型,可以在右侧提供它们:

var i, s = int(2), string("hi")

总的来说,对于两种不同类型,使用两行代码,没有什么损失,但可读性更好。

英文:

It's possible if you omit the type:

var i, s = 2, &quot;hi&quot;
fmt.Println(i, s)

Output (try it on the Go Playground):

2 hi

Note that the short variable declaration is exactly a shorthand for this:

> A short variable declaration uses the syntax:
>
> ShortVarDecl = IdentifierList ":=" ExpressionList .
>
> It is shorthand for a regular variable declaration with initializer expressions but no types:
>
> "var" IdentifierList = ExpressionList .

Without omitting the type it's not possible, because the syntax of the variable declaration is:

VarSpec = IdentifierList ( Type [ &quot;=&quot; ExpressionList ] | &quot;=&quot; ExpressionList ) .

(There is only one optional type for an identifier list with an expression list.)

Also I assume you don't count this as 1 line (which otherwise is valid syntax, but gofmt breaks it into multiple lines):

var (i int = 2; s string = &quot;hi&quot;)

Also if you only want to be able to explicitly state the types, you may provide them on the right side:

var i, s = int(2), string(&quot;hi&quot;)

But all in all, just use 2 lines for 2 different types, nothing to lose, readability to win.

答案2

得分: 5

这不完全针对OP的问题,但由于它会出现在搜索结果中,用于在一行中声明多个变量(目前不可能)。更简洁的方法是:

var (
    n []int
    m string
    v reflect.Value
)
英文:

This isn't exactly specific to the OP's question, but since it gets to appear in search results for declaring multiple vars in a single line (which isn't possible at the moment). A cleaner way for that is:

var (
    n []int
    m string
    v reflect.Value
)

huangapple
  • 本文由 发表于 2017年7月14日 00:13:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/45086082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定