Open In App

var keyword in Go

var keyword in Golang is used to create the variables of a particular type having a proper name and initial value. Initialization is optional at the time of declaration of variables using var keyword that we will discuss later in this article.

Syntax:



var identifier type = expression

Example:

// here geek1 is the identifier 
// or variable name, int is the
// type and 200 is assigned value
var geek1 int = 200

As you know that Go is a statically typed language but it still provides a facility to remove the declaration of data type while declaring a variable as shown in below syntax. This is generally termed as the Type Inference.



Syntax:

var identifier = initialValue

Example:

var geek1 = 200

Multiple variable declarations using var Keyword

var keyword is also used to declare multiple variables in a single line. You can also provide the initial value to the variables as shown below:

Note:

Important Points about var keyword:


Article Tags :