Go from Beginner to Expert: A Complete Guide to Learn Golang PART-3 Step-by-Step Guide to understand Variables, constant and types in Golang

Go from Beginner to Expert: A Complete Guide to Learn Golang PART-3

Step-by-Step Guide to understand Variables, constant and types in Golang

Md. Faiyaj Zaman
4 min readJan 7, 2023

--

Hello and welcome to this tutorial on variables, constants, and basic types in Go.

If you want to to build your First Application in Golang, please check this Step-by-Step Guide.

In this tutorial, we will be discussing how to declare and use variables, constants, and basic types in Go. We will be using a simple program as an example to demonstrate these concepts.

So let’s get started.

Create a file named main.go and write the code:

package main

import "fmt"

func main() {
// Declare a variable with the var keyword and give it a type
var age int32 = 37

// Declare a constant with the const keyword
const pi = 3.14159265358979323846

// Declare a variable without specifying a type (inferred type is float64)
var salary = 78000.0

// Declare multiple variables at once
var width, height int = 100, 50

// Declare a variable and initialize it to a default value
var name string
name = "Faiyaj"

// Declare a variable using the shorthand syntax
surname := "Zaman"

fmt.Println("Age:", age)
fmt.Println("Pi:", pi)
fmt.Println("Salary:", salary)
fmt.Println("Width:", width)
fmt.Println("Height:", height)
fmt.Println("Name:", name)
fmt.Println("Surname:", surname)
}

The first line of our program is package main. In Go, every program must start with a package declaration. The package name specifies which package the code belongs to. The main package is used for programs that can be compiled and run.

The next line is import “fmt”. This line includes the fmt package, which provides functions for formatting input and output. We will be using the fmt package later in our program to output variables to the console.

Next, we have the func main() line, which declares the main function. The main function is the entry point of every Go program. It is called when the program is run.

Inside the main function, we have several lines of code that declare and initialize variables and constants.

The first line is var age int32 = 37. This line declares a variable named age of type int32 and initializes it to the value 37.

The next line is const pi = 3.14159265358979323846. This line declares a constant named pi and initializes it to the value 3.14159265358979323846. Constants are used to store values that will not change throughout the program.

The third line is var salary = 78000.0. This line declares a variable named salary and initializes it to the value 78000.0. The type of the variable is inferred to be float64.

The fourth line is var width, height int = 100, 50. This line declares two variables, width and height, both of type int and initializes them to the values 100 and 50, respectively.

The fifth line is var name string. This line declares a variable named name of type string.

The sixth line is name = “Faiyaj”. This line assigns the value “Faiyaj” to the name variable.

The seventh line is surname := “Zaman”. This line declares a variable named surname using the shorthand syntax and initializes it to the value “Zaman”.

Finally, we have several lines that use the fmt package to output the values of the variables and constants to the console.

The fmt.Println function is used to output a line of text to the console. It takes any number of arguments and separates them with a space.

So, for example, the line fmt.Println(“Age:”, age) outputs the string “Age: “ followed by the value of the age variable.

We have similar lines for the other variables and constants, such as fmt.Println(“Pi:”, pi), fmt.Println(“Salary:”, salary), and so on.

That’s it for this tutorial. I hope you now have a better understanding of how to use variables, constants, and basic types in Go.

--

--