AppDividend
Latest Code Tutorials

Golang Short Declaration Operator: Complete Overview

The primary purpose of using the short declaration operator is to declare and initialize the local variables inside the functions and narrow the scope of the variables. The type of the expression defines the type of the variable.

Golang Short Declaration Operator

Golang Short Declaration Operator is used to create the variables having a proper name and initial value.  A short variable declaration uses the following syntax.

ShortVarDecl = IdentifierList ":=" ExpressionList

Here, you must initialize a variable just after declaration. But using the var keyword, you can avoid the initialization at the time of declaration.

There is no need to mention the type of the variable. Expression or value on the right-hand side is used to evaluate a type of variable.

It is shorthand for the regular variable declaration with initializer expressions but no types:

"var" IdentifierList = ExpressionList

The:= is for both declaration, assignment, and also for redeclaration, and it guesses (infer) the variable’s type automatically.

Example

// hello.go

package main

import "fmt"

func main() {

	kiernanAge := 19
	show := "Sabrina"

	fmt.Println("Kiernan Shipka's age is: ", kiernanAge)
	fmt.Println("The show is: ", show)
}

Output

go run hello.go
Kiernan Shipka's age is:  19
The show is:  Sabrina

Declaring Multiple Variables Using Short Declaration Operator (:=)

Short Declaration operators can also be used to declare the multiple variables of the same type or different types in the single declaration. The type of these variables is evaluated by the expression on the right-hand side of := operator.

See the following code.

// hello.go

package main

import "fmt"

func main() {

	kirnan, ross, gavin := 13, "Lynch", false

	fmt.Printf("The value of kirnan is : %d\n", kirnan)
	fmt.Printf("The type of kirnan is : %T\n", kirnan)

	fmt.Printf("The value of ross is : %s\n", ross)
	fmt.Printf("The type of ross is : %T\n", ross)

	fmt.Printf("The value of gavin is : %t\n", gavin)
	fmt.Printf("The type of gavin is : %T\n", gavin)
}

Output

go run hello.go
The value of kirnan is : 13
The type of kirnan is : int
The value of ross is : Lynch
The type of ross is : string
The value of gavin is : false
The type of gavin is : bool

A short declaration operator can be used when at least one of the variables on the left-hand side of := operator is newly declared.

A short variable declaration operator behaves like the assignment for those variables which are already declared in the same lexical block.

See the following code example to get a better idea about this concept.

// hello.go

package main

import "fmt"

func main() {

	// taking two variables
	x, y := 21, 19

	fmt.Println("Value of x ", x, "Value of y ", y)

	// we have defined again two variables
	// there are no new variable
	// on the left-hand side of :=
	x, y := 11, 46

	fmt.Println("Value of x ", x, "Value of y ", y)
}

Output

go run hello.go
# command-line-arguments
./hello.go:15:7: no new variables on left side of :=

We got the error because we did not define any new variable a second time.

We have defined x and y on the left side of := and then again reassign x and y on the left side of :=, and that is why it gives us the error. Let’s see that scenario where we define a new variable.

// hello.go

package main

import "fmt"

func main() {

	// taking two variables
	x, y := 21, 19

	fmt.Println("Value of x ", x, "Value of y ", y)

	// we have defined again two variables
	// there are one new variable
	// on the left-hand side of :=
	x, z := 11, 46

	fmt.Println("Value of x ", x, "Value of z ", z)
}

Output

go run hello.go
Value of x  21 Value of y  19
Value of x  11 Value of z  46

In this code, first, we have defined x and y, and then we have defined x and z. So, it won’t give us an error.

Go is the strongly typed language as you cannot assign a value of another type to the declared variable.

See the following code.

// hello.go

package main

import "fmt"

func main() {

	// taking two variables
	x := 19

	fmt.Println("Value of x ", x)

	// we have defined again two variables
	// there are one new variable
	// on the left-hand side of :=
	x := "Kiernan Shipka"

	fmt.Println("Value of x ")
}

Output

go run hello.go
# command-line-arguments
./hello.go:15:10: cannot use "Gavin Leatherwood" (type string) as type int in assignment

In the above code, we define x as an int, and then we change its type to string, which is not correct because golang is a strongly typed language.

Conclusion

Golang short declaration operator creates the variables having a proper name and initial value.

That’s it for this tutorial.

See also

Golang Variables

Golang Constants

Rune in Golang

Operators in Golang

Scope of Variables

Leave A Reply

Your email address will not be published.

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