Home »
Golang
Variables in Golang
By Anshuman Singh Last updated : October 05, 2024
Go Variables
In Golang, variables are the names given to a storage area or memory location for storing the data of a specific type. The variables are declared using the var keyword in Golang. There are some other ways to declare variables in Golang, those are explained below.
There are various ways to declare the variables in Golang.
Declare single variable at a time
To declare a single variable in Golang, you need to write the var keyword followed by the variable name and then specify the type of the variable.
Syntax
Here is the syntax to declare a single variable at a time in the Go language:
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).
Declare a variable with initial value
To declare a single variable with an initial value in Golang, you need to write the var keyword followed by the variable name, type of the variable, and assign the initial value by using the assignment operator (=).
Syntax
Here is the syntax to declare a variable with initial value in the Go language:
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
Multiple variable declarations
In go, we can declare multiple variables of the same type in a single statement.
Syntax
Here is the syntax to declare multiple variables in the Go language:
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.
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
Here is the syntax to declare multiple variables with initial values in the Go language:
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
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
Here is the syntax to declare a variable with initial value without mentioning type in the Go language:
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.
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".
Short hand declaration
In short hand declaration, we use := operator.
Syntax
Here is the syntax for the sort hand declaration of a variable in the Go language:
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.