Go — 3

Himashi Karunathilake
7 min readJul 17, 2023
Link to Part 1: https://himashikarunathilake.medium.com/go-1-74940ce2556d
Link to Part 2: https://himashikarunathilake.medium.com/go-2-79dc2c04db26

Given below is the main.go file that will be used to run all the sub files in this section:

// The third program in Go.

package main

import "fmt"

func main() {
fmt.Println()

fmt.Println("*************** RUNNING THE INPUT_STATEMENT.GO FILE ***************")
InputStatement()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()

fmt.Println("*************** RUNNING THE CONDITIONS.GO FILE ***************")
Conditions()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()

fmt.Println("*************** RUNNING THE LOOPS.GO FILE ***************")
Loops()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()

fmt.Println("*************** RUNNING THE SPECIAL_STATEMENTS.GO FILE ***************")
SpecialStatements()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()
}

The Input Statement

To obtain a user input when necessary, the following steps can be followed:

package main

import "fmt"

func InputStatement() {
// Declare variable to assign the user input
var fname string

fmt.Print("Enter your first name: ")
// Obtain user input
fmt.Scan(&fname)

fmt.Printf("Hi %s!\n", fname)
}
Given below are the input statements that you can use:

fmt.Scan Used to read individual values from the input such as
integers or strings
fmt.Scanln Used to read a line of input containing multiple space-
separated values
Obtaining a user input

Condition Statements

if Statement

The following piece of code checks whether the user provides a last name. If the input is given as -1, the program will exit that particular code block.

package main

import "fmt"

func Conditions() {
// if statement
var lname string
fmt.Print("Please enter your last name (-1 to exit): ")
fmt.Scan(&lname)

if lname != "-1" {
fmt.Printf("%s's Conditions Program!\n", lname)
}
}
The program executes when the last name is provided
The program exits that code block if the input is given as -1

if-else Condition

The following piece of code checks whether the user is an adult based on their input.

package main

import "fmt"

func Conditions() {
// if statement
var lname string
fmt.Print("Please enter your last name (-1 to exit): ")
fmt.Scan(&lname)

if lname != "-1" {
fmt.Printf("%s's Conditions Program!\n", lname)
}

fmt.Println()

// if-else statement
var age int
fmt.Print("Please enter your age: ")
fmt.Scan(&age)

if age >= 18 {
fmt.Println("You are an adult!")
} else {
fmt.Println("You are a child!")
}
}
Output when the input is 15
Output when the input is 18
Output when the input is 23
If required,you can also follow the following format to have multiple 
conditions within an if-else statement:

if condition1 {
// Statements to be executed if the condition1 is met
} else if condition2 {
// Statements to be executed if the condition2 is met
} else if condition3 {
// Statements to be executed if the condition3 is met
} ...
...
...
} else {
// Statements to be executed if none of the above conditions are met
}

if Statement with Initialization

Go allows you to initialize a variable in the if statement itself and use it within the scope of the block.

package main

import "fmt"

func Conditions() {
// if statement
var lname string
fmt.Print("Please enter your last name (-1 to exit): ")
fmt.Scan(&lname)

if lname != "-1" {
fmt.Printf("%s's Conditions Program!\n", lname)
}

fmt.Println()

// if-else statement
var age int
fmt.Print("Please enter your age: ")
fmt.Scan(&age)

if age >= 18 {
fmt.Println("You are an adult!")
} else {
fmt.Println("You are a child!")
}

fmt.Println()

// if with initialization
if num1 := 123480; num1%20 == 0 {
fmt.Printf("The number %d is divisible by 20.\n", num1)
}
}
Output when a variable is initialized within the if statement
This can be followed for if-else statements as well:

if initialization; condition {
// Statements to be executed if the condition is met
} else {
// Statements to be executed if the condition is not met
}

switch Statement

switch statement allows you to select one of the many code blocks to execute based on the value for an expression. This can be considered as an alternative to long if-else statements.

package main

import "fmt"

func Conditions() {
// if statement
var lname string
fmt.Print("Please enter your last name (-1 to exit): ")
fmt.Scan(&lname)

if lname != "-1" {
fmt.Printf("%s's Conditions Program!\n", lname)
}

fmt.Println()

// if-else statement
var age int
fmt.Print("Please enter your age: ")
fmt.Scan(&age)

if age >= 18 {
fmt.Println("You are an adult!")
} else {
fmt.Println("You are a child!")
}

fmt.Println()

// if with initialization
if num1 := 123480; num1%20 == 0 {
fmt.Printf("The number %d is divisible by 20.\n", num1)
}

fmt.Println()

// switch statement
var day string
fmt.Print("Enter any day of the week: ")
fmt.Scan(&day)

switch day {
case "Monday":
fmt.Println("It is the first day of the week!")
case "Saturday", "Sunday":
fmt.Println("It is a day of the weekend!")
default:
fmt.Println("It is a regular weekday!")
}
}
Output when the input is Monday
Output when the input is a day of the weekend
Output when the input is any other day

Loops

for Loop

Repeatedly executes a block of code until a given condition is met.

package main

import "fmt"

func Loops() {
// for loop
fmt.Println("Printing numbers less than 5..........")
for i := 0; i < 5; i++ {
fmt.Printf("%d\t", i)
}

fmt.Println()
}
Using a for loop to print numbers less than 5

while Loop Implemented Using a for Loop

Since Go does not have a separate while loop construct, a while loop behavior can be created using a for loop with only a condition.

package main

import "fmt"

func Loops() {
// for loop
fmt.Println("Printing numbers less than 5..........")
for i := 0; i < 5; i++ {
fmt.Printf("%d\t", i)
}

fmt.Println()
fmt.Println()
fmt.Println()

// while loop implemented using a for loop
var num int
fmt.Print("Please enter a number less than or equal to 10: ")
fmt.Scan(&num)

if num <= 10 {
fmt.Printf("Printing numbers less than %d..........\n", num)

j := 0

for j < num {
fmt.Printf("%d\t", j)
j++
}
} else {
fmt.Printf("ERROR! The number %d is greater than 10.\n", num)
}

fmt.Println()
}
Printing numbers less than a user provided value using a while loop implemented using a for loop

range Loop

The range loop in Go can be used to iterate over elements in an array, slice, string, map, or channel. It also provides both the index and the corresponding value of each element in the iteration.

package main

import "fmt"

func Loops() {
// for loop
fmt.Println("Printing numbers less than 5..........")
for i := 0; i < 5; i++ {
fmt.Printf("%d\t", i)
}

fmt.Println()
fmt.Println()
fmt.Println()

// while loop implemented using a for loop
var num int
fmt.Print("Please enter a number less than or equal to 10: ")
fmt.Scan(&num)

if num <= 10 {
fmt.Printf("Printing numbers less than %d..........\n", num)

j := 0

for j < num {
fmt.Printf("%d\t", j)
j++
}
} else {
fmt.Printf("ERROR! The number %d is greater than 10.\n", num)
}

fmt.Println()
fmt.Println()

// range loop
names := []string{"Chandler", "Joey", "Monica", "Phoebe", "Rachael", "Ross"}
fmt.Println("Printing the index number and the name of the student..........")
for index, value := range names {
fmt.Println(index, value)
}
}
Printing the index number and the name of the students in a slice using a range loop

Special Statements

break Statement

The break statement in Go is used to break out / terminate the current loop or switch statement.

package main

import "fmt"

func SpecialStatements() {
// break statement
fmt.Println("Printing numbers till 13 is met..........")
for i := 0; i <= 20; i++ {
if i == 13 {
break
}

fmt.Printf("%d\t", i)
}

fmt.Println()
}
Printing numbers less than or equal to 20, until 13 is met (the loop breaks at 13)

return Statement

The return statement in Go is used to terminate the execution of a function and return a value if expected.

package main

import "fmt"

func add(num1, num2 int) int {
return num1 + num2
}

func SpecialStatements() {
// break statement
fmt.Println("Printing numbers till 13 is met..........")
for i := 0; i <= 20; i++ {
if i == 13 {
break
}

fmt.Printf("%d\t", i)
}

fmt.Println()
fmt.Println()
fmt.Println()

// return statement
fmt.Println("Proceeding to add two numbers..........")

var num1, num2 int
fmt.Print("Please provide the first number: ")
fmt.Scan(&num1)
fmt.Print("Please provide the second number: ")
fmt.Scan(&num2)

fmt.Println("The sum of the two numbers are: ", add(num1, num2))
}
Printing the sum of two user provided values using a function that returns the sum of the two values

continue Statement

The continue statement in Go is used to skip the remaining code within the current iteration of a loop and move to the next iteration.

package main

import "fmt"

func add(num1, num2 int) int {
return num1 + num2
}

func SpecialStatements() {
// break statement
fmt.Println("Printing numbers till 13 is met..........")
for i := 0; i <= 20; i++ {
if i == 13 {
break
}

fmt.Printf("%d\t", i)
}

fmt.Println()
fmt.Println()
fmt.Println()

// return statement
fmt.Println("Proceeding to add two numbers..........")

var num1, num2 int
fmt.Print("Please provide the first number: ")
fmt.Scan(&num1)
fmt.Print("Please provide the second number: ")
fmt.Scan(&num2)

fmt.Println("The sum of the two numbers are: ", add(num1, num2))

fmt.Println()
fmt.Println()

// continue statement
fmt.Println("Proceeding to print odd numbers less than 20..........")

for j := 0; j <= 20; j++ {
if (j % 2) == 0 {
continue
}

fmt.Printf("%d\t", j)
}

fmt.Println()
}
Printing odd numbers less than 20 using a continue statement that skips over the even numbers

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Himashi Karunathilake
Himashi Karunathilake

Written by Himashi Karunathilake

I am a cybersecurity enthusiast and writer with a passion for demystifying complex topics. Join me as I explore the ever-evolving world of cybersecurity!

No responses yet

Write a response