Lesson 2
Mastering String Operations in Go
Lesson Introduction

Welcome! Today, we're going to delve into the crucial operations associated with Strings in Go. Strings are fundamental in most programming languages as they are used for displaying and manipulating textual data. In this lesson, you'll learn about the basic string operations in Go, such as concatenation, comparison, and the use of common functions from the strings package.

Revising String Concatenation

Even though we already know what concatenation is and how it works, revisiting it strengthens our understanding! Concatenation — the process of joining items together — is a principal string operation. In Go, we achieve string concatenation by using the + operator.

Go
1package main 2 3import "fmt" 4 5func main() { 6 var hello = "Hello, " 7 var world = "World!" 8 var greeting = hello + world 9 10 fmt.Println(greeting) // "Hello, World!" 11}

In this case, "Hello, " and "World!" were combined to form the string "Hello, World!".

Comparing Strings

There are often times when we need to compare strings. Fortunately, in Go, the simple comparison operators == and < work perfectly fine.

Go
1package main 2 3import "fmt" 4 5func main() { 6 var firstWord = "Hello" 7 var secondWord = "Hello" 8 var areEqual = firstWord == secondWord 9 10 fmt.Println(areEqual) // Outputs: true 11}

Here, as firstWord and secondWord are equal, areEqual is true.

The < operator is used to determine if one string is alphabetically before the other.

Go
1package main 2 3import "fmt" 4 5func main() { 6 var firstWord = "Apple" 7 var secondWord = "Banana" 8 var isLess = firstWord < secondWord 9 10 fmt.Println(firstWord, "is less than", secondWord, "?", isLess) // Outputs: Apple is less than Banana? true 11}

As you can see, the comparison result is true, which means that alphabetically, "Apple" comes before "Banana", because A comes before B. A string that would come before another string in the dictionary is considered less than the other. In case of a tie, Go will compare the following letter. For example, "Apple" will be less than "Application", because e is less than i. As the first four letters in the words are equal, Go compares the fifth one.

Important String Functions: len

In Go, unlike some other languages, strings do not have built-in methods. However, the Go Standard Library provides a package named strings which contains many useful string-related functions. Among them, some functions are pretty common:

  • len(): This function returns the number of characters in a string.
Go
1package main 2 3import "fmt" 4 5func main() { 6 var word = "Hello" 7 var length = len(word) 8 9 fmt.Println(length) // 5 10}
Important String Functions: ToLower and ToUpper
  • strings.ToLower() and strings.ToUpper(): These functions return the string either in lowercase or in uppercase, respectively.
Go
1package main 2 3import ( 4 "fmt" 5 "strings" 6) 7 8func main() { 9 var word = "Hello" 10 var lowerCaseWord = strings.ToLower(word) 11 var upperCaseWord = strings.ToUpper(word) 12 13 fmt.Println(lowerCaseWord) // "hello" 14 fmt.Println(upperCaseWord) // "HELLO" 15}
Important String Functions: TrimSpace
  • strings.TrimSpace(): This function removes all white spaces at the beginning and the end of a string.
Go
1package main 2 3import ( 4 "fmt" 5 "strings" 6) 7 8func main() { 9 var sentence = " Hello, World! " 10 var trimmedSentence = strings.TrimSpace(sentence) 11 12 fmt.Println(trimmedSentence) // "Hello, World!" 13}
Lesson Summary and Next Steps

Excellent job! You've deepened your understanding of string operations and functions in Go. We've explored how to concatenate and compare strings. Additionally, we covered some of the most frequently used functions in the strings package.

The upcoming practice exercises will allow you to apply what you've learned about string operations in Go. With each step, you're getting closer to mastering Go programming! Keep going and good luck!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.