Hi, Today we will learn about the variable and how to declare variables on Go/Golang.

Variables are the name assigned to the memory location where your data will be stored.

Variables help us to store and retrieve the data.

There are four types of Data types in which a variable can store data Int, Float32,Strings and boolean.

Int: Int or Integer store whole numbers like 245,11,09,etc. It can’t store floating point numbers like 1.2,45.6 etc.

Float32: Float32 store numbers with points such as 3.6,567.6,9.1 etc. It store only numbers with floating point.

String: Strings store characters and special characters such as “apple”,”cat”,”*’,”&”,”Hello” etc.

Boolean: It stores simply true and false. It doesn’t store anything it’s just boolean which means true and false.

First, Let’s see how we declare variables. We use “Var” and “Const” to declare variables.

Var is used to declare variables which value can be changed later. You can see in example below.

package main

import "fmt"
func main() {
    var score int = 32
    var name string = "Golang"
    var Temp float32 = 34.6
    fmt.Printf("Score value before changing:%v",score)
    score=68
    fmt.Printf("Score value after changing:%v",score)
   
    fmt.Println(name)
    fmt.Printf("Todays temprature is : %v", Temp)
}

Output:

C:\Users\HP\Desktop\Go lang articles\data types>go run "c:\Users\HP\Desktop\Go lang articles\data types\main.go"
Score value before changing:32
Score value after changing:68
Golang
Todays temprature is : 34.6

Here you can see in starting I declare ‘score’ as Int whose value is 32. And later I changed the value with 68. And in output you can see it changed. With that I hope you also leaned how to declare a variable.

Now let’s see how const work.

package main
 
import "fmt"
func main() {
    const marks int = 100
    var fruit string = "apple"
    var interest float32 = 34.6
    fmt.Printf("Marks value before changing:%v\n", marks)
    marks = 68
    fmt.Printf("Marks value after changing:%v\n", marks)
    fmt.Println(fruit)
    fmt.Printf("Interest rate : %v", interest)
}

Output:

C:\Users\HP\Desktop\Go lang articles\data types>go run "c:\Users\HP\Desktop\Go lang articles\data types\main.go"
# command-line-arguments
.\main.go:11:2: cannot assign to marks (constant 100 of type int)

Now as you can see in output it gave a warning that “marks” variable’s value can’t be changes because it’s a constant.

We use it to store the value which we won’t need to change in future.

We saw how declaration work. Let see how boolean work as well.

package main
 
import "fmt"
func main() {
    var x bool
    var y bool
    fmt.Println(x)
    fmt.Println(y)
}

Output:

C:\Users\HP\Desktop\Go lang articles\data types>go run "c:\Users\HP\Desktop\Go lang articles\data types\main.go"
false
false

In output we got “False”. Why? Because the variables X and Y have not stored any data. And when a variable don’t have stored any data in it, it’s value is 0 which is false in boolean. Boolean give two values True and False. Here ‘0’ is false and ‘1’ is true.

If a variables has data  0 means false and any non-zero data is considered as True in Boolean.

One more thing, In Go/Golang we also have short hand operator to assign a variable ‘:=’. See in example to know in detail.

package main                                                            
 
import "fmt"
func main() {
    fruit := "apple"
    Score := 80
    Temp := 35.6
    fmt.Println(fruit)
    fmt.Println(Score)
    fmt.Println(Temp)
}

Output:

C:\Users\HP\Desktop\Go lang articles\data types>go run "c:\Users\HP\Desktop\Go lang articles\data types\main.go"
apple
80
35.6
  

As you see by using a short hand operator ‘:=’ we can assign value without declaring it’s data types.

Compiler automatically assign the data type of variable according to the data stored in that.

So that’s it for this chapter. In next chapter we will study about data types in details.