英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论