top of page

Second day at Go Programming School - Variables

Updated: May 26, 2023

This article is in continuation to the first article of 'Go Programming Series' and today we will discuss what exactly I learnt on the second day.


After learning how to print data at output console, I wanted to explore, how to store data. When we talk about storing data, we need to store it somewhere and that is 'MEMORY'.


But computer memory is too complex to deal with, because if we want to store data in memory directly, we need to know memory addresses and they look like this 0xF2F1AC07.


Of course, nobody on earth can remember million of memory addresses, so there is a building block in programming languages that help in this situation and it's name is 'VARIABLE'.


So, what is VARIABLE ? People have different definitions, some say "It's a container that is used to store data" other says "It's a name mapped to memory addresses".


All definition are true, the only thing you need to understand is that variable can help you to store data in memory and you can give any familiar name to variable instead of remembering the complex addresses.


But the point is how to create a variable ? Before discussing how to create a variable I hope you have an understanding about the different type of data we can store in memory. Yes, you are right we are talking about 'Integers, String, Floating Point Numbers, etc. etc.


In Go Programming Language, there are two different methods to declare a variable:


a) First Method (Using var keyword) -> When we create a variable using var keyword, then it can be created without assigning any value to it.


Syntax:

var variable-name data-type;

For example :

var num1 int 
var num2 int = 10
var name string
var firstname string = "Techie"

Code:

package main

import "fmt"

func main() {
	var num1 int
	var num2 int = 10
	var name string
	var firstname string = "Techie"

	fmt.Println(num1)
	fmt.Println(num2)
	fmt.Println(name)
	fmt.Println(firstname)
}

Output:



b) Second Method (Using := sign) -> When we create a variable using this method, then we have to assign a value to it but we don't need to specify the data type of it. Go compiler implicitly take care of recognizing the data type.


Syntax:

num3 := 10
lastname := "Globus"
salary := 10000.999

Code:

package main

import "fmt"

func main() {
	num3 := 10
	lastname := "Globus"
	salary := 10000.999

	fmt.Println(num3)
	fmt.Println(lastname)
	fmt.Println(salary)
}

Output:


c) Third Method (Group of Variables) -> We can define group of variables using the var keyword and parenthesis () as shown below:


Syntax:

var ( 
  playerOne = "Sachin Tendulkar" 
  playerOneAge = 50
)

Code:

package main

import "fmt"

func main() {
	
	var (
       playerOne = "Sachin Tendulkar"
       playerOneAge = 50	
	)

	fmt.Println(playerOne)
	fmt.Println(playerOneAge)
}

Output:



I hope you now understood how to tackle with storing data in variables, You can explore about different data types supported by Go Programming Language and write about them in comments.

That's all for this article. Have a good day !



14 views0 comments

Read More

Never miss an update

Thanks for submitting!

bottom of page