What't the means about var () in golang

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

What't the means about var () in golang

问题

我浏览了由go-swagger生成的代码,发现以下代码:

// NewReceiveLearningLabActsParams创建一个新的ReceiveLearningLabActsParams对象
// 并初始化默认值。
func NewReceiveLearningLabActsParams() ReceiveLearningLabActsParams {
	var ()
	return ReceiveLearningLabActsParams{}
}

我注意到这里:

var ()

我完全不理解这是什么意思,有人可以帮我理解这段代码吗?谢谢。

英文:

I go though the code generated by go-swagger, found follow code:

// NewReceiveLearningLabActsParams creates a new ReceiveLearningLabActsParams object
// with the default values initialized.
func NewReceiveLearningLabActsParams() ReceiveLearningLabActsParams {
	var ()
	return ReceiveLearningLabActsParams{}
}

I noticed here:

var ()

I totally not understand what's the means, could anyone help me understand this code? thanks

答案1

得分: 47

在Go语言中,这是一种批量定义变量的简写方式。
不需要在每个变量声明前写var关键字,可以使用var声明块。

例如:

var (
	a,b,c string = "this ", "is ","it"
    e,f,g int = 1, 2, 3
)

等同于

var a,b,c string = "this ", "is ","it"
var d,e,f int = 1, 2, 3

你代码示例中的var ()表示没有声明任何变量。

请参考官方Go文档获取更多信息。

英文:

In Go this is a shorthand for defining variables in bulk.
Instead of having to write var in front of every variable declaration, you can use a var declaration block.

For example:

var (
	a,b,c string = "this ", "is ","it "
    e,f,g int = 1, 2, 3
)

is the same as

var a,b,c string = "this ", "is ","it "
var d,e,f int = 1, 2, 3

The var () in your code example simply states that no variables were declared.

Refer to the official Go documentation for more information.

huangapple
  • 本文由 发表于 2017年2月16日 13:56:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/42266041.html
匿名

发表评论

匿名网友

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

确定