Go Variables

Go Variables

In this tutorial, we are going to discuss on variables in Go language. There are various syntax’s to declare variables in Go. Let’s look at them one by one.

A variable is a data name that can be used to store a data value. A variable must be defined with the type of data it is holding. For example, string is a data type provided by Go to store character or text data.

In technical terms, a variable is assigning a storage location to a value that is tied to a symbolic name or identifier. We use the variable name to reference that stored value within a computer program.

Declaring a variable

The keyword var is used for declaring variables. To declare a single variable, you can use the following syntax.

var variableName dataType

E.g

package main

import "fmt"

func main() {
     var name string // variable declaration
     fmt.Println("My name is", name)
}

Run in playground

The statement var name string declares a variable named name of type string. We have not assigned any value to this variable.

If a variable is not assigned any value, Go automatically initializes it with the zero value of the variable’s type. In this case, name is assigned the value empty string (“”), which is the zero value of string. If you run this program, you can see the below output.

My name is
Zero values

Variables declared without an explicit initial value are given their zero value.

The zero value is:

  • 0 for numeric types,
  • "" (the empty string) for strings and
  • false for the boolean type.

A variable can be assigned to any value of its type. In the above program, name can be assigned any string value.

package main

import "fmt"

func main() {
     var name string // variable declaration
     fmt.Println("My name is", name)
     name = "Ashok" //assignment
     fmt.Println("My name is", name)
     name = "Ashok Kumar" //assignment
     fmt.Println("My updated name is", name)
}

Output

My name is 
My name is Ashok
My updated name is Ashok Kumar

Run in playground

Declaring a variable with an initial value

A variable can also be provided an initial value when it is declared. The following is the syntax to declare a variable with an initial value.

var variableName dataType = initialValue

E.g

package main

import "fmt"

func main() {
     var name string = "Ashok Kumar"
     fmt.Println("My name is", name)
}

Output

My name is Ashok Kumar

Run in playground

Here are some important things to know about Golang variables.

1. Golang is statically typed language, this means that when Golang variables are declared, they either explicitly or implicitly assigned a type even before your program runs.

2. Golang requires that every variable you declare inside main() function get used somewhere in your program.

3. You can assign new value to an existing variable, but the value need to be of same type.

4. A variable declared within brace brackets {} may be accessed anywhere within the block. The opening curly brace { introduces a new scope that ends with a closing brace }. Inner blocks can access variables within outer blocks. Outer blocks cannot access variables within inner blocks.

Variable Declaration Omit Types

If a variable has an initial value, Go will automatically be able to infer the type of that variable using that initial value. This is called Type inference.

Hence if a variable has an initial value, the type in the variable declaration can be removed. If the variable is declared using the following syntax

var name = initialvalue

Go will automatically infer the type of that variable from the initial value.

E.g

package main

import "fmt"

func main() { 
     var name = "Ashok Kumar" 
     var age = 29 
     fmt.Println("My name is", name, "and age is" , age)
}

Output

My name is Ashok Kumar and age is 29

Run in playground

Short Variable Declaration in Golang

Go also provides another concise way to declare variables. This is known as short hand declaration and it uses := operator. There is no need to use the var keyword or declare the variable type.

E.g

package main

import "fmt"

func main() {
     name := "Ashok Kumar" 
     fmt.Println("My name is", name)
}

Output

My name is Ashok Kumar

Run in playground

Declare Multiple Variables

Golang allows you to assign values to multiple variables in one line.

E.g

package main

import "fmt"

func main() { 
     var fname, lname string = "Ashok Kumar", "Mariyala" 
     fmt.Println("My first name is", fname)
     fmt.Println("My last name is", lname)
}

Output

My first name is Ashok Kumar
My last name is Mariyala

Run in playground

The type can be removed if the variables have an initial value. Since the above program has initial values for variables, the string type can be removed.

E.g

package main
 
import "fmt"

func main() { 
     var fname, lname = "Ashok Kumar", "Mariyala" 
     fmt.Println("My first name is", fname)
     fmt.Println("My last name is", lname)
}

Output

My first name is Ashok Kumar
My last name is Mariyala

Run in playground

There might be cases where we would want to declare variables belonging to different types in a single statement. The syntax for doing that is

var (  
       name1 = initialvalue1
       name2 = initialvalue2
       name3 = initialvalue3
)

E.g

package main

import "fmt"

func main() {
     var (
         name   = "Ashok Kumar"
         age    = 29
         salary int
     )
     fmt.Println("My name is", name, ", age is", age, "and salary is", salary)
 }

Here we declare a variable name of type string, age and salary of type int.

Go Variables


Scroll to top