var in Golang: What is Golang var Keyword
A variable definition tells a compiler where and how much storage to create for a variable. A variable definition specifies the data type and contains one or more variables.
var in Golang
The var in Golang is a keyword to create the variables. The variables should be proper data type and initial value. Go programming language also defines other variables, such as Enumeration, Pointer, Array, Structure, and Union.
Syntax
See the following syntax.
var variablelist identifier type = expression
The variable list can be single or multiple variable names.
The identifier type is the data type.
The expression is value.
See the following code example.
// hello.go package main import "fmt" func main() { var x int = 10 fmt.Printf("The value of x is: %d", x) }
Output
go run hello.go The value of x is: 10
Multiple variable declarations using var Keyword
We can declare multiple variables using the var keyword. See the following syntax.
var sabrina, harvey, nick, hilda string
Okay, now, let’s declare with initial values.
var sabrina, harvey, theo, roz int = 19, 21, 30, 46
Type inference in Go
The type inference will let the compiler know about the type, i.e., and there is an option to remove a type while declaring multiple variables.
By assigning direct values to the variable, the variable automatically declares the value’s data type.
Declare and initialize the values of different types in Golang
While using the type during declaration, you can only declare multiple variables of the same type. But removing type during declarations allows you to declare multiple variables of different types.
See the following code.
var( sabrina = 11 nick = 21 zelda bool hilda string = "Ambrose" )
See the following complete example.
// hello.go package main import "fmt" func main() { var a, b, c = 107, "Billion Dollar", "Jeff" fmt.Printf("The value of a is : %d\n", a) fmt.Printf("The value of b is : %s\n", b) fmt.Printf("The value of c is : %s\n", c) }
Output
go run hello.go The value of a is : 107 The value of b is : Billion Dollar The value of c is : Jeff
Short variable declarations
Inside a function, the:= short assignment statement can be used in place of a var declaration with implicit type.
Outside a function, every statement begins with a keyword (var, func, etc.), so the:= construct is not available.
See the following code.
// hello.go package main import "fmt" func main() { var x, y int = 11, 21 z := 30 w, python, javascript := true, false, "no!" fmt.Println(x, y, z, w, python, javascript) }
Output
go run hello.go 11 21 30 true false no!
lvalues and rvalues in Golang
There are two kinds of expressions in Go.
- lvalue: Expressions that refer to the memory location is called “lvalue” expression. The lvalue may appear as either the left-hand or right-hand side of the assignment.
- rvalue: The term rvalue refers to the data value stored at some memory address. The rvalue is an expression that cannot have a value assigned to it, which means an rvalue may appear on the right but not a left-hand side of the assignment.
Variables are lvalues to look at on the left-hand side of the assignment. However, numeric literals are rvalues and so may not be assigned and can not appear on a left-hand side.
Conclusion
The name of a variable can be formed of letters, digits, and the underscore character. It must start with either a letter or an underscore.
Upper and lowercase letters are distinct because Go is a case-sensitive language.
That’s it for var in Go.