Go — 2

Himashi Karunathilake
6 min readJul 16, 2023

--

Link to Part 1: https://himashikarunathilake.medium.com/go-1-74940ce2556d

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

// The second program in Go.

package main

import "fmt"

func main() {
fmt.Println()

fmt.Println("*************** RUNNING THE SLICES.GO FILE ***************")
Slices()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()

fmt.Println("*************** RUNNING THE OPERATORS.GO FILE ***************")
Operators()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()

fmt.Println("*************** RUNNING THE FORMAT_SPECIFIERS.GO FILE ***************")
FormatSpecifiers()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()

fmt.Println("*************** RUNNING THE STRING_MANIPULATION.GO FILE ***************")
StringManipulation()
fmt.Println("__________________________________________________________________________________________")
fmt.Println()
}

Go Slices

Go Slices are a powerful and flexible data type that can be used to store sequences of data. They are similar to arrays in some languages (e.g., JavaScript, Ruby, PHP), lists in Python, ArrayLists in Java, and Vectors in C++.

Following are some key features of Go slices:

  1. Slices are dynamic: This means that the length of a slice can be grown or shrunk at runtime.
  2. Slices are references: This means that when you create a slice, you are referencing to the data in an underlying array as opposed to creating a new copy of the data. Therefore, changes made to the slice will be reflected in the underlying array.

Creating a Slice

A string slice in go can be declared as follows:

package main

import "fmt"

//In Go, only identifiers that start with an uppercase letter are exported and accessible from other packages. Hence, "Slices".
func Slices() {
// Create a slice
names := []string{"Anne", "John", "Miley"}
}

Accessing Elements in a Slice

To access a particular element n in a slice, you should provide the (n-1) as the index value. This is because, indexing starts from 0 (i.e., the first element in a slice will have an index of (1–1) → 0, second element will have an index of (2–1) → 1, etc.).

package main

import "fmt"

//In Go, only identifiers that start with an uppercase letter are exported and accessible from other packages. Hence, "Slices".
func Slices() {
// Create a slice
names := []string{"Anne", "John", "Miley"}

// Print the second element - "John"
fmt.Println(names[1])
}
Accessing a particular element in Go

Printing a Slice

To print a complete slice, simply provide the name of the slice without an index.

package main

import "fmt"

//In Go, only identifiers that start with an uppercase letter are exported and accessible from other packages. Hence, "Slices".
func Slices() {
// Create a slice
names := []string{"Anne", "John", "Miley"}

// Print the second element - "John"
fmt.Println(names[1])

// Print new line
fmt.Println()

// Print the complete slice
fmt.Println(names)
}
Printing the complete slice in Go

Operations in Go

Arithmetic Operators

Arithmetic operations in Go can be performed by utilizing the following arithmetic operators.

Addition          +
Subtraction -
Multiplication *
Division /
Remainder %
package main

import "fmt"

func Operators() {
// Arithmetic operators
var1 := 5
var2 := 2

fmt.Println("var1 = ", var1, "\tvar2 = ", var2)
fmt.Println("Addition of the two variables: ", (var1 + var2))
fmt.Println("Subtraction of the two variables: ", (var1 - var2))
fmt.Println("Multiplication of the two variables: ", (var1 * var2))
fmt.Println("Division of var1 by var2: ", (var1 / var2))
fmt.Println("Remainder when var1 is divided by var2: ", (var1 % var2))
}
Arithmetic operations in Go

Go has a package called “math” in its standard library that can be used to perform mathematical operations.

package main

import (
"fmt"
"math"
)

func Operators() {
// Arithmetic Operators
var1 := 5
var2 := 2

fmt.Println("var1 = ", var1, "\tvar2 = ", var2)
fmt.Println("Addition of the two variables: ", (var1 + var2))
fmt.Println("Subtraction of the two variables: ", (var1 - var2))
fmt.Println("Multiplication of the two variables: ", (var1 * var2))
fmt.Println("Division of var1 by var2: ", (var1 / var2))
fmt.Println("Remainder when var1 is divided by var2: ", (var1 % var2))

fmt.Println()

// Power operator
fmt.Println("var1 to the power of var2: ", math.Pow(float64(var1), float64(var2))) // Convert the two integers to float64
}
Finding the power of a base value

String Formatting

Given below is a list of some format specifiers available in Go’s “fmt” standard library package.

%s or %v  Formats the value using the default format for its type
%d Formats the value as a decimal integer
%f Formats the value as a floating-point number
%c Formats the value as a character
%b Formats the value as a binary representation
%x or %X Formats the value as a hexadecimal representation
%o Formats the value as an octal respresentation
%t Formats the value as a boolean
package main

import "fmt"

func FormatSpecifiers() {
name := "Rachael"
age := 24
height := 1.5

// Printf formats according to format specifiers
fmt.Printf("Name: %s, Age: %d, Height: %.2f\n", name, age, height)
}
Format specifiers

String Manipulation

Using the len() function, we can find the length of a string function as follows:

package main

import "fmt"

func StringManipulation() {
// Finding the length of a string
str1 := "Hello World!"
fmt.Printf("Length of the string \"%s\": %d\n", str1, len(str1))
}
Finding the length of a string

In Go, the standard library package “strings” can be used to perform string manipulation.

package main

import (
"fmt"
"strings"
)

func StringManipulation() {
// Finding the length of a string
str1 := "Hello World!"
fmt.Printf("Length of the string \"%s\": %d\n", str1, len(str1))

fmt.Println()

// Counting the occurrences of a string
str2 := "Hello, Hello, Hello"
fmt.Printf("Number of occurrences of the word \"Hello\" in the string \"%s\": %d\n", str2, strings.Count(str2, "Hello"))
}
Counting the occurrences of a string
package main

import (
"fmt"
"strings"
)

func StringManipulation() {
// Finding the length of a string
str1 := "Hello World!"
fmt.Printf("Length of the string \"%s\": %d\n", str1, len(str1))

fmt.Println()

// Counting the occurrences of a string
str2 := "Hello, Hello, Hello"
fmt.Printf("Number of occurrences of the word \"Hello\" in the string \"%s\": %d\n", str2, strings.Count(str2, "Hello"))

fmt.Println()

// Checking the presence of a string
fmt.Printf("The word \"World\" exists in the string \"%s\": %t\n", str1, strings.Contains(str1, "World"))
}
package main

import (
"fmt"
"strings"
)

func StringManipulation() {
// Finding the length of a string
str1 := "Hello World!"
fmt.Printf("Length of the string \"%s\": %d\n", str1, len(str1))

fmt.Println()

// Counting the occurrences of a string
str2 := "Hello, Hello, Hello"
fmt.Printf("Number of occurrences of the word \"Hello\" in the string \"%s\": %d\n", str2, strings.Count(str2, "Hello"))

fmt.Println()

// Checking the presence of a string
fmt.Printf("The word \"World\" exists in the string \"%s\": %t\n", str1, strings.Contains(str1, "World"))

fmt.Println()

// Checking the index location of a string
fmt.Printf("The index location of the word \"World\" in the string \"%s\": %d\n", str1, strings.Index(str1, "World"))
}
Checking the index location of a string

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