Home » Golang

Variables in Golang

In this tutorial, we are going to learn about the variables in Golang, variable declarations, declarations with initializations, etc with the syntaxes and examples.
Submitted by Anshuman Singh, on June 28, 2019

Variable in Golang

Variables in go are used to give the name to a memory location for storing the data of specific type.

Variable Declarations

There are various ways to declare the variables in Golang.

1) Declare single variable at a time

Syntax:

    var variable_name type

Example:

package main
import "fmt"

func main() {  
    var student_weight int // variable declaration
    fmt.Println("Student weight is: ", student_weight)
}

In the above example, the statement var student_weight int declares a variable named student_weight of type int.

Output

Student weight is:  0

Here, Output is zero (0). Reason being, when a variable is not assigned with any value. go compiler automatically initializes default value of variable type. Here, the variable type is int so the default value of int is zero (0).

2) Declare a variable with initial value

Syntax:

    var variable_name type = initial_value

Example:

package main

import "fmt"

func main() {  
	// variable declaration with 20 initial value
	var student_weight int = 20

	fmt.Println("Student weight is: ", student_weight)
}

In the above example, The statement var student_weight int = 20 declares a variable named student_weight of type int with initial value 20.

Output

Student weight is: 20

3) Multiple variable declarations

In go, we can declare multiple variables of the same type in a single statement.

Syntax:

    var variable_name_1, variable_name_2, variable_name_3 type

Example:

package main

import "fmt"

func main() {  
	// three variable declaration
	var age, height, length int  
	fmt.Printf("age: %d, height: %d, length: %d", age, height, length)
}

In the above example, the statement var age, height, length int declares three variables named age, height, and length of type int.

Output

age: 0, height: 0, length: 0

Here, Go compiler assigns zero value (default value of int) to these variables.

4) Multiple variable declarations with initial value

In go, we can declare multiple variables of the same type with an initial value in a single statement.

Syntax:

     var variable_name_1, variable_name_2, 
     variable_name_3 type = value_1, value_2, value_3

Example:

package main

import "fmt"

func main() {  
	// three variable declaration with initial value
	var age, height, length int  = 10,  20, 30 

	fmt.Printf("age: %d, height: %d, length: %d", age, height, length)
}

In the above example, the statement var age, height, length int declares three variables named age, height, and length of type int with initial values 10, 20, 30 respectively.

Output

age: 10, height: 20, length: 30

5) Type inference

In go, you can declare a variable with initial value without mentioning type. Go will automatically be able to infer the type of that variable using initial value.

Syntax:

    var variable_name = initial_value

Example:

package main

import "fmt"

func main() {  
	// variable declaration with 20 initial value
	var student_weight = 20

	fmt.Println("Student weight is: ", student_weight)
}

Output

Student weight is: 20

In the above example, go can infer it of type int.

6) Declare a variable and assign value later

In go, you can declare variables and assign values to variables later.

Example:

package main

import "fmt"

func main() {  

    var height, weight int

    fmt.Printf("height: %d, weight: %d\n",  height, weight)

    height = 8

    weight = 50

    fmt.Printf("height: %d, weight: %d",  height, weight)
}

Output

height: 0, weight: 0
height: 8, weight: 50

In the above example, 1st we declare the height and weight variables. Here, go compiler assigns a default value of int. that's why 1st output has 0 value of height and weight.

Later we assign value to these variables as 8 to height and 50 to weight. So the second output is " height: 8, weight: 50".

7) Short hand declaration

In short hand declaration, we use := operator.

Syntax:

    variable_name := initial_value

Example:

package main
      
import "fmt"

func main() {  

    height, weight := 10, 20

    fmt.Printf("height: %d, weight: %d\n",  height, weight)

    name := "includehelp"

    fmt.Printf("name: %s", name)
}

Output

height: 10, weight: 20
name: includehelp

Note: Short hand declaration requires initial value of all the variables.





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.