Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What't the means about var () in golang

Tags:

go

go-swagger

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

like image 982
fisafoer Avatar asked Feb 16 '17 05:02

fisafoer


People also ask

What is variable declaration Golang?

Short Variable Declaration Operator(:=) in Golang is used to create the variables having a proper name and initial value. The main purpose of using this operator to declare and initialize the local variables inside the functions and to narrowing the scope of the variables.

How do I declare multiple variables in Golang?

Multiple variables can be declared using a single statement. var name1, name2 type = initialvalue1, initialvalue2 is the syntax for multiple variable declaration. The type can be removed if the variables have an initial value. Since the above program has initial values for variables, the int type can be removed.


1 Answers

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.

like image 119
Marco Avatar answered Nov 09 '22 21:11

Marco