Variables in Go Language

In GO, a variable is a piece of storage containing a value. You can give a variable name by using the variable declaration but before going all out on the variables, let’s look at how to name a variable in GO.

Naming conventions for variables In GO

  • A variable name must begin with a letter or an underscore( _ ) followed by any number of additional letters or numbers or an underscore( _ ).
  • If the name of the variable or function starts with an uppercase letter. In that case, it is considered exported and can be accessed outside the current package ( So, now you know why in the Println function, P is capital). And, if the variable or function name starts with a small case letter, it is considered unexported and can be used within the package only.

So, these were the significant rules that we want to run through you first and now let’s focus on how to declare a variable in GO.

How to declare a variable in Golang?

We can declare a variable in Golang using the var keyword and the variable’s type. Below is the syntax for the same.

var variableName type

Here variableName can be anything adhering to the naming conventions, and type is the kind of value that this variable will hold.

var height int

So, in the above statement, height is the variable name, and int is the data type.

Now, we can assign an int type value to the height variable.

package main

import (
	"fmt"
)

func main() {

	var height int // declaring the variable

	height = 167 // assigning value to the variable

	// to print the value of the variable
	fmt.Println("Value of height:", height)

}

Output –

Value of height: 167

Declaring a variable and assigning its value in a single line

We can declare a variable and set its value in a single line using the below syntax.

var variableName type = expression

Below is the code example for the same.

package main

import (
	"fmt"
)

func main() {

	var height int = 167 // declaring the variable

	// to print the value of the variable
	fmt.Println("Value of height:", height)

}

Output –

Value of height: 167

Do you know that you can omit the type of the variable? Go will assign a type using the variable’s value if you set the value while declaring the variable itself.

So, we could have easily written our declaration statement as shown below, and the program would work just fine.

var height = 167

Declaring multiple variables and assigning their values in a single line

We can also declare multiple variables and set their values in the same line. We have to write variable names on the left side of =, and then the same number of comma-separated values will go on to the right side.

var variable1, variable2, variable3 = value1, value2, value3

Let’s see this in action with some code.

package main

import (
	"fmt"
)

func main() {

	var age, height, booleanValue = 22, 169, false // declaring the variable

	// to print the value of the variable
	fmt.Println("Value of age: ", age)
	fmt.Println("Value of height: ", height)
	fmt.Println("Value of booleanValue: ", booleanValue)

}

Output –

Value of age:  22
Value of height:  169
Value of booleanValue:  false

This example also highlighted one crucial point: we can assign multiple values of different types in a single statement. Still, if we specifically tell Golang the type of value the variables can hold, then it will only be able to hold one kind of value.

var a, b, c, d int  // now a,b,c,d can only hold int values and not any other type

Shorthand variable declarations

We have already discussed how to declare and assign variable values on the same line. But if we know the variable’s value as soon as we declare it, it would be easier to declare and initialize it with the shorthand declaration. Instead of explicitly declaring the type of the variable and later assigning value to it using =, we can do both at once using the “:=

variableName := value
package main

import (
	"fmt"
)

func main() {

	height := 167 // declaring a variable and
	              //assigning value to it using shorthand declaration

	fmt.Println("Value of height: ", height)

}

Output –

Value of height:  167

We can assign multiple type values, too, using the shorthand declaration.

package main

import (
	"fmt"
)

func main() {

	age, height, booleanValue := 22, 169.47, false
	fmt.Println("Value of age: ", age)
	fmt.Println("Value of height: ", height)
	fmt.Println("Value of booleanValue: ", booleanValue)

}

Output –

Value of age:  22
Value of height:  169.47
Value of booleanValue:  false

Essential points about shorthand declaration

  • Shorthand declaration can only declare the local variables and not the global variables.
  • While declaring variables using shorthand declarations, there must be at least one new variable in that declaration. It will lead to an error if there is not, as shown in the program below.
package main

import (
	"fmt"
)

func main() {

	height := 167

	height := 170 // this line will give an error

	fmt.Println("Value of height: ", height)

}

Output –

 no new variables on left side of :=

As shown below, we have to declare one new variable to avoid this issue.

package main

import (
	"fmt"
)

func main() {

	height := 167

	age, height := 22, 170

	fmt.Println("Value of height: ", height)
	fmt.Println("Value of age: ", age)

}

Output –

Value of height:  170
Value of age:  22

Avoid shadowing variable names

What is shadowing mean here? When we declare a variable, we should ensure that it doesn’t have the same name as any functions, packages, data types, or other variables. Because if we declare a variable with the same name as that previously defined or declared, then our variable will overshadow it, which means our variable will take precedence over it.

Shadowing of variables doesn’t cause any troubles or errors by themselves. Look at the below program where we have declared a variable name int, same as the data type, and it is working just fine.

package main

import (
	"fmt"
)

func main() {

	var int int = 167
	fmt.Println("int variable value is: ", int)
}

Output –

int variable value is:  167

See, the program compiled just fine, But now if we try to declare any other variable of type int, it won’t work because int is no longer a data type but a variable.

package main

import (
	"fmt"
)

func main() {

	var int int = 167
	var age int = 22

	fmt.Println("int variable value is: ", int)
	fmt.Println("age: ", age)
}

Output –

.codekru.go:10:6: int is not a type

It gave an error telling us that int is not a type.

The What If scenarios

What if we try to use the shorthand declaration on a global variable in the Go language?

We can’t use the shorthand declaration on a global variable, as illustrated by the below program.

package main

import (
	"fmt"
)
age := 22   // will give error here

func main() {

	fmt.Println("Value of age: ", age)

}

Output –

syntax error: non-declaration statement outside function body

What if we just declared a variable but never assigned a value and used it somewhere in the program?

In this scenario, a default value will be assigned to the variable.

Data typeDefault value
int0
float 0.0
booleanfalse
string“”
package main

import (
	"fmt"
)

func main() {

	var intDefaultValue int
	var booleanDefaultValue bool

	fmt.Println("Value of intDefaultValue: ", intDefaultValue)
	fmt.Println("Value of booleanDefaultValue: ", booleanDefaultValue)

}

Output –

Value of intDefaultValue:  0
Value of booleanDefaultValue:  false

What if we declared a variable but never used it?

Well, there are two scenarios here –

  • We can leave the global variables without using them in the program.
  • But for local variables, we have to use them if we have declared it anywhere in the function.
package main

import (
	"fmt"
)

func main() {

	var height int = 136 // local variable

	fmt.Println("Hi codekru")

}

Output –

 height declared but not used

The error itself shows us that we have only declared the height variable but never used it. Now, let’s see the global variable.

package main

import (
	"fmt"
)

var height int = 136 // global variable

func main() {

	fmt.Println("Hi codekru")

}

Output –

Hi codekru

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *