AppDividend
Latest Code Tutorials

var Keyword In Golang | Go var Example

0

The var keyword in Golang is to create the variables. The variables should be proper data type and initial value. Go programming language also allows us to define various other types of variables, such as Enumeration, Pointer, Array, Structure, and Union.

var Keyword In Golang

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 the list of one or more variables of that type.

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 declared the data type the value has.

Declare and initialize the values of different types in Golang

While using the type during declaration, you are only allowed to declare multiple variables of the same type. But removing type during declarations, you are allowed 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, and so on), and 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.

  1. 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.
  2. rvalue: The term rvalue refers to the data value that is stored at some address in memory. 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, and so it may look on a left-hand side of the assignment. 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.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.