Updated April 6, 2023
Definition of Go Variables
Variables in go language is an important attribute; they used to hold the value for any numeric, string, or boolean value. Basically, if we have some number, then we can assign that number to any variable and later we can access that variable with the name of the variables, it happens because of every variable in go language assigned with a memory address, and when we try to access that value with variable name actually we are accessing that value with its memory address where it stored because we can not remember the memory address of any value we give them some user-friendly name and we call them variables.
How to Declare Variables in Go?
Below is a very simple syntax for declaring any variable. We use the keyword variable before the name of the variable.
var name_variable ;
var studentName;
How to Initialize Variables in Go?
Below is a simple syntax to initialize any variable; we just took an example with a student name and assigned it a name. We have used the keyword variable before the name of the variable and assign the value to the variable.
var name_variable =initialValue
var sudentName =”Ranjan”
Types of Variables in Go
Broadly we can define three types of variables they are:
- Numeric Variables: These types of variables contain numeric values; these numeric values can be either integer or decimal numbers. They can be negative numbers also. So all the number related variables come under it
- String Variables: These are the variables that hold the address for the string-like name, address, etc.
- Boolean Variables: These are the variables that hold true or false, and they are used to check some flag like yes or no.
First, we are going to discuss the numeric variables(integer and floating)
1. Integer Variables
- Integer variables are used to hold the integer non-decimal value. It holds the memory address of any integer value by which we can access it.
- Once we define the variable as the integer, we inform the compiler about how the compiler will need much memory space to hold the variable.
- We can perform the all basic operation of comparison and addition on the integer type variables.
- We can use the + operator for addition, and we can use the == operator for comparing two variable values.
Note: Always comparison means comparing the value of the variables, not the memory address held by the variable. Because memory address is always different for every variable.
Example
Here in the below example, we perform addition and comparisons and print the output of the two variables price1 and price2. The comparison will print the output as the boolean value true and false.
Code:
package main
import "fmt"
func main() {
var price1 = 56
var price2 = 4
var price = price1+price2
fmt.Println(price)
fmt.Println(price1 == price2)
fmt.Println(price1 > price2)
}
Output:
2. Floating Type Variables
- The floating type variables are the variable that holds the variable of value with decimal numbers.
- Once we define the variable with a decimal number, the go compiler will reserve the required space for any floating value.
- Examples of floating-point values are like 5.5,4554.7 etc.
Example
Here in the below example, we perform addition and comparisons and print the output of the two floating variables price1 and price2. A comparison of two floating variables will print the output as the boolean value true and false.
Code:
package main
import "fmt"
func main() {
var price1 = 5.6
var price2 = 4.8
var price = price1-price2
fmt.Println(price)
fmt.Println(price1 == price2)
fmt.Println(price1 > price2)
}
Output:
3. String Variables
- String type variables are the variables that hold the string only.
- We can create string variables in the language in two ways, either by using a single quote(‘’) or by using the double quote(“).
- Mostly string variables are used for holding string type value like name, address, etc.
Example
In the below example, we are using two string variables, student1 and student2, and these two variables are holding the name of two students. We are performing two operations here one, we are adding these two variables, and second, we are comparing these two variables.
Code:
package main
import "fmt"
func main() {
var student1= "Sujit"
var student2 = "Ajay"
var combinedStudent = student1+ " " + student2
fmt.Println(combinedStudent)
fmt.Println(student1 == student2)
}
Output:
4. Boolean Variables
- Boolean variables are the variables that hold only two things, true and false.
- Once we define a variable as the boolean, our variable as the boolean, the go compiler will allocate the memory location for true and false.
- We use the boolean variables where we wanted to make some flags for true and false only.
Example
In the below example, we have taken two boolean variables with holding value true and another one holding the false. Here we have used the if the condition for comparing these two variables and printing their outputs.
Code:
package main
import "fmt"
func main() {
var boolVar = true
var simpleBool =false
if(simpleBool==boolVar){
fmt.Println("Two boolean flags contains the same value")
}else{
fmt.Println("Two boolean flags does not contains the same value")
}
}
Output:
Rules and Regulation for Declaring Variables in Go
There are a few important rules for declaring any variable in go language; they are given below.
- We can not declare any variable of two types, like in the below.
var X
var X.
This will give an error.
- We can not declare or change the type of variable; if we declare a variable as the integer and again if we declared it as a string, then it will throw an error.
- We can not initialize any value before declaring the variable in go language.
Conclusion
From this tutorial, we learned some of the important concepts about the variables in go language; we learned declaration and initialization, focused on the types of the variables with some examples and learned about the various important rules used for variable declaration.
Recommended Articles
This is a guide to Go Variables. Here we also discuss the definition and how to declare variables in go along with different examples and its code implementation. You may also have a look at the following articles to learn more –