Go — 3

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

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)
}
}


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!")
}
}



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)
}
}

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!")
}
}



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()
}

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()
}

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)
}
}

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()
}

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))
}

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()
}

You can access the source code at: Go/Part-3 at master · Himashi-Karunathilake/Go (github.com)
Link to Part 4: Go — 4. Given below is the main.go file that… | by Himashi Karunathilake | Jul, 2023 | Medium