Go — 4

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

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

// The fourth program in Go.

package main

import "fmt"

func main() {
fmt.Println()

fmt.Println("*************** RUNNING THE MULTIPLICATION.GO FILE ***************")
Multiplication()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()

fmt.Println("*************** RUNNING THE STRUCT.GO FILE ***************")
Struct()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()

fmt.Println("*************** RUNNING THE MAPS.GO FILE ***************")
Maps()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()
}

Functions in Go

Functions can be used to reuse code segments at various times by simply calling the function name when and where necessary. These functions prevent repetition of code blocks.

package main

import "fmt"

func multiply(num1, num2 int) int {
return num1 * num2
}

func Multiplication() {
fmt.Println("Performing multiplications..........")
var num1, num2 int
fmt.Print("Please provide the first number for the multiplication: ")
fmt.Scan(&num1)
fmt.Print("Please provide the second number for the multiplication: ")
fmt.Scan(&num2)

// Call the multiply() function
fmt.Println("The multiplication of the two numbers is: ", multiply(num1, num2))
}
A function that calculated the multiplication of two values

Structs

Structs in Go are composite data types that allow you to define your own custom types by grouping together related fields. They provide a way to create complex data structures that can hold different types of values.

Defining a Struct

package main

type Person struct { // Define a struct
Name string
Age int
}

Instances / Struct Literals

package main

type Person struct { // Define a struct
Name string
Age int
}

func Struct() {
person := Person{
Name: "Rachael",
Age: 23,
}
}

Accessing Struct Fields

package main

import "fmt"

type Person struct { // Define a struct
Name string
Age int
}

func Struct() {
// Create a new instance of the Person struct
person := Person{
Name: "Rachael",
Age: 23,
}

// Access and modify the fields of the Person struct instance
fmt.Println("Name of the individual: ", person.Name)
fmt.Println("Age of the individual: ", person.Age)

person.Age = 29
fmt.Println("Updated age of the individual: ", person.Age)
}
Accessing the fields of the struct instance

Struct Methods

Go allows you to define methods associated with structs that are functions that operate on instances of the struct.

package main

import "fmt"

type Person struct { // Define a struct
Name string
Age int
}

func (person Person) SayHello() { // Define a method associated with the Person struct
fmt.Printf("Hi, %s!\n", person.Name)
}

func Struct() {
// Create a new instance of the Person struct
person := Person{
Name: "Rachael",
Age: 23,
}

// Access and modify the fields of the Person struct
fmt.Println("Name of the individual: ", person.Name)
fmt.Println("Age of the individual: ", person.Age)

person.Age = 29
fmt.Println("Updated age of the individual: ", person.Age)

// Call the SayHello() method on the Person struct
person.SayHello()
}
Define and call a method associated with the Person struct

Struct Embedding

Struct embedding allows one struct to include another struct as a field, effectively inheriting its fields and methods.

package main

import "fmt"

type Person struct { // Define a struct
Name string
Age int
}

type Employee struct { // Define another struct embedding the Person struct
Person
EmployeeID int
}

func (person Person) SayHello() { // Define a method associated with the Person struct
fmt.Printf("Hi, %s!\n", person.Name)
}

func Struct() {
fmt.Println("==================== PERSON STRUCT ====================")
fmt.Println()

// Create a new instance of the Person struct
person := Person{
Name: "Rachael",
Age: 23,
}

// Access and modify the fields of the Person struct
fmt.Println("Name of the individual: ", person.Name)
fmt.Println("Age of the individual: ", person.Age)

person.Age = 29
fmt.Println("Updated age of the individual: ", person.Age)

// Call the SayHello() method on the Person struct
person.SayHello()

fmt.Println()
fmt.Println("==================== EMPLOYEE STRUCT ====================")
fmt.Println()

employee := Employee{
Person: Person{
Name: "Ross",
Age: 35,
},
EmployeeID: 003452,
}

employee.SayHello()
fmt.Println("Here are your details.")
fmt.Println("Name: ", employee.Name)
fmt.Println("Age: ", employee.Age)
fmt.Println("Employee ID: ", employee.EmployeeID)
}
Embedding the Person struct into the Employee struct

Anonymous Structs

Anonymous structs are structs without a defined name that are useful for temporary data structures or one-time use cases.

package main

import "fmt"

type Person struct { // Define a struct
Name string
Age int
}

type Employee struct { // Define another struct embedding the Person struct
Person
EmployeeID int
}

func (person Person) SayHello() { // Define a method associated with the Person struct
fmt.Printf("Hi, %s!\n", person.Name)
}

func Struct() {
fmt.Println("==================== PERSON STRUCT ====================")
fmt.Println()

// Create a new instance of the Person struct
person := Person{
Name: "Rachael",
Age: 23,
}

// Access and modify the fields of the Person struct
fmt.Println("Name of the individual: ", person.Name)
fmt.Println("Age of the individual: ", person.Age)

person.Age = 29
fmt.Println("Updated age of the individual: ", person.Age)

// Call the SayHello() method on the Person struct
person.SayHello()

fmt.Println()
fmt.Println("==================== EMPLOYEE STRUCT ====================")
fmt.Println()

employee := Employee{
Person: Person{
Name: "Ross",
Age: 35,
},
EmployeeID: 003452,
}

employee.SayHello()
fmt.Println("Here are your details.")
fmt.Println("Name: ", employee.Name)
fmt.Println("Age: ", employee.Age)
fmt.Println("Employee ID: ", employee.EmployeeID)

fmt.Println()
fmt.Println("==================== PETDOG STRUCT ====================")
fmt.Println()

petDog := struct { // Create an anonymous struct
Name string
Breed string
Age int
}{
Name: "Allie",
Breed: "Labrador",
Age: 2,
}

fmt.Println("Here are the details of your pet dog.")
fmt.Println("Name of pet dog: ", petDog.Name)
fmt.Println("Breed of pet dog: ", petDog.Breed)
fmt.Println("Age of pet dog: ", petDog.Age)
}
An anonymous struct that provides the details of the employee’s pet dog

Maps

Maps in Go are unordered collections of key-value pairs, where each key must be unique within the map.

Declare and Initialize a Map

package main

func Maps() {
// Declare and initialize a map
employeeIDs := map[string]int{
"Chandler": 1000000,
"Joey": 1000001,
"Monica": 1000002,
"Phoebe": 1000003,
"Rachael": 1000004,
"Ross": 1000005,
}
}

Access Values by Key

package main

import "fmt"

func Maps() {
// Declare and initialize a map
employeeIDs := map[string]int{
"Chandler": 1000000,
"Joey": 1000001,
"Monica": 1000002,
"Phoebe": 1000003,
"Rachael": 1000004,
"Ross": 1000005,
}

// Access values by key
fmt.Println("Phoebe's Employee ID: ", employeeIDs["Phoebe"])
}
Accessing a map value by its key

Add or Update Values

package main

import "fmt"

func Maps() {
// Declare and initialize a map
employeeIDs := map[string]int{
"Chandler": 1000000,
"Joey": 1000001,
"Monica": 1000002,
"Phoebe": 1000003,
"Rachael": 1000004,
"Ross": 1000005,
}

// Access values by key
fmt.Println("Phoebe's Employee ID: ", employeeIDs["Phoebe"])

// Add or update values
employeeIDs["Gunther"] = 1000007
employeeIDs["Chandler"] = 1000006
}

Delete a Key-Value Pair

package main

import "fmt"

func Maps() {
// Declare and initialize a map
employeeIDs := map[string]int{
"Chandler": 1000000,
"Joey": 1000001,
"Monica": 1000002,
"Phoebe": 1000003,
"Rachael": 1000004,
"Ross": 1000005,
}

// Access values by key
fmt.Println("Phoebe's Employee ID: ", employeeIDs["Phoebe"])

// Add or update values
employeeIDs["Gunther"] = 1000007
employeeIDs["Chandler"] = 1000006

// Delete a key-value pair
delete(employeeIDs, "Gunther")
}

Check if a Key Exists in the Map

package main

import "fmt"

func Maps() {
// Declare and initialize a map
employeeIDs := map[string]int{
"Chandler": 1000000,
"Joey": 1000001,
"Monica": 1000002,
"Phoebe": 1000003,
"Rachael": 1000004,
"Ross": 1000005,
}

// Access values by key
fmt.Println("Phoebe's Employee ID: ", employeeIDs["Phoebe"])

// Add or update values
employeeIDs["Gunther"] = 1000007
employeeIDs["Chandler"] = 1000006

// Delete a key-value pair
delete(employeeIDs, "Gunther")

fmt.Println()

// Check if a key exists in the map
employeeID, exists := employeeIDs["Monica"]
if exists {
fmt.Println("Monica's Employee ID: ", employeeID)
}
}
Checking if a key exists in a map

Iterate Over a Map

package main

import "fmt"

func Maps() {
// Declare and initialize a map
employeeIDs := map[string]int{
"Chandler": 1000000,
"Joey": 1000001,
"Monica": 1000002,
"Phoebe": 1000003,
"Rachael": 1000004,
"Ross": 1000005,
}

// Access values by key
fmt.Println("Phoebe's Employee ID: ", employeeIDs["Phoebe"])

// Add or update values
employeeIDs["Gunther"] = 1000007
employeeIDs["Chandler"] = 1000006

// Delete a key-value pair
delete(employeeIDs, "Gunther")

fmt.Println()

// Check if a key exists in the map
employeeID, exists := employeeIDs["Monica"]
if exists {
fmt.Println("Monica's Employee ID: ", employeeID)
}

fmt.Println()

// Iterate over the map
for name, empID := range employeeIDs {
fmt.Printf("%s's Employee ID: %d\n", name, empID)
}
}

Sign up to discover human stories that deepen your understanding of the world.

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