Go String Fundamentals

GolangGolangBeginner
Practice Now

Introduction

In the previous lesson, we learned that characters in Go are encoded using UTF-8 and stored as either the byte or rune types. Now, let's discuss strings, which are collections of characters. Let's explore this topic together.

Knowledge Points:

  • What is a string
  • Creating a string
  • Declaring a string
  • Common string functions
  • Accessing string elements

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Golang`")) -.-> go/BasicsGroup(["`Basics`"]) go(("`Golang`")) -.-> go/DataTypesandStructuresGroup(["`Data Types and Structures`"]) go(("`Golang`")) -.-> go/FunctionsandControlFlowGroup(["`Functions and Control Flow`"]) go(("`Golang`")) -.-> go/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) go(("`Golang`")) -.-> go/AdvancedTopicsGroup(["`Advanced Topics`"]) go/BasicsGroup -.-> go/values("`Values`") go/BasicsGroup -.-> go/variables("`Variables`") go/DataTypesandStructuresGroup -.-> go/strings("`Strings`") go/FunctionsandControlFlowGroup -.-> go/for("`For`") go/FunctionsandControlFlowGroup -.-> go/functions("`Functions`") go/ObjectOrientedProgrammingGroup -.-> go/struct_embedding("`Struct Embedding`") go/AdvancedTopicsGroup -.-> go/text_templates("`Text Templates`") go/AdvancedTopicsGroup -.-> go/number_parsing("`Number Parsing`") subgraph Lab Skills go/values -.-> lab-149069{{"`Go String Fundamentals`"}} go/variables -.-> lab-149069{{"`Go String Fundamentals`"}} go/strings -.-> lab-149069{{"`Go String Fundamentals`"}} go/for -.-> lab-149069{{"`Go String Fundamentals`"}} go/functions -.-> lab-149069{{"`Go String Fundamentals`"}} go/struct_embedding -.-> lab-149069{{"`Go String Fundamentals`"}} go/text_templates -.-> lab-149069{{"`Go String Fundamentals`"}} go/number_parsing -.-> lab-149069{{"`Go String Fundamentals`"}} end

What is a String

In the first program we learned in Go, we printed the string "hello, world".

A string is a basic data type in Go, also known as a string literal. It can be understood as a collection of characters and occupies a continuous block of memory. This block of memory can store any type of data, such as letters, text, emoji, and so on.

However, unlike other languages, strings in Go are immutable and cannot be modified. This means that once a string is created, you cannot change its individual characters. If you need a modified version of a string, you have to create a new one.

Creating a String

Strings can be declared in several ways. Let's take a look at the first method. Create a new file called string.go:

touch ~/project/string.go

Write the following code:

package main

import "fmt"

func main() {
    // Use the var keyword to create a string variable a
    var a string = "labex"
    a = "labex" // Assign "labex" to variable a

    // Declare variable b and assign its value
    var b string = "labs"

    // Type declaration can be omitted
    var c = "Monday"

    // Use := for quick declaration and assignment
    d := "Sunday"
    fmt.Println(a, b, c, d)
}

The above code demonstrates how to create strings using the var keyword and the := operator. If you assign a value when creating a variable with var, you can omit the type declaration, as shown in the creation of variable c. The := operator is a shorthand for declaring and initializing a variable in Go. It automatically infers the type of the variable from the value assigned to it. Using it will make your code more concise.

go run string.go

The expected output is as follows:

labex labs Monday Sunday

Declaring a String

In most cases, we use double quotes "" to declare strings. The advantage of double quotes is that they can be used as escape sequences. For example, in the program below, we use the \n escape sequence to create a new line:

package main

import "fmt"

func main() {
    x := "linux\nlabex"
    fmt.Println(x)
}
go run string.go

The expected output is as follows:

linux
labex

Here are some common escape sequences:

Symbol Description
\n New line
\r Carriage return
\t Tab
\b Backspace
\\ Backslash
\' Single quote
\" Double quote

If you want to preserve the original format of the text or need to use multiple lines, you can use backticks to represent them:

package main

import "fmt"

func main() {
    // Output the ASCII art of "labex"
    ascii := `
        ##        #####   #########  ########## ##    ##         ##       ##  ##  ##    ## ##       ##  ##         ##      ##    ## #########  #########    ###         ##      ########## ##    ## ##        ###         ##      ##    ## ##    ## ##       ##  ##         ########## ##    ## #########  ########## ##    #`
    fmt.Println(ascii)
}

Backticks are commonly used in prompts, HTML templates, and other cases where you need to preserve the original format of the output. The text inside the backticks is treated as a raw string literal, meaning that escape sequences are not interpreted. This makes it convenient to include multiline text and special characters without having to escape them.

Getting the Length of a String

In the previous lesson, we learned that English characters and general punctuation marks occupy one byte.

Therefore, in Go, we can use the len() function to get the byte length of a string. If there are no characters that occupy multiple bytes, the len() function can be used to accurately measure the length of the string.

If a string contains characters that occupy multiple bytes, you can use the utf8.RuneCountInString function to get the actual number of characters in the string.

Let's see an example. Write the following code to the string.go file:

package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {
    // Declare two empty strings using var and :=
    var a string
    b := ""

    c := "labex"
    d := "abc"


    // Output byte length
    fmt.Printf("The value of a is %s, the byte length of a is: %d\n", a, len(a))
    fmt.Printf("The value of b is %s, the byte length of b is: %d\n", b, len(b))
    fmt.Printf("The value of c is %s, the byte length of c is: %d\n", c, len(c))
	fmt.Printf("The value of d is %s, the byte length of d is: %d\n", d, len(d))


    // Output string length
    fmt.Printf("The length of c is: %d\n", utf8.RuneCountInString(c))
	fmt.Printf("The length of d is: %d\n", utf8.RuneCountInString(d))
}
go run string.go

The expected output is as follows:

The value of a is , the byte length of a is: 0
The value of b is , the byte length of b is: 0
The value of c is labex, the byte length of c is: 5
The value of d is abc, the byte length of d is: 3
The length of c is: 5
The length of d is: 3

In the program, we first declared two empty strings and the strings labex and abc. You can see that their byte lengths and actual lengths are the same since they only contain single-byte characters.

Accessing String Elements

Since strings are essentially sequences of bytes, we can access individual bytes or characters in a string using their index. In Go, string indexing starts from 0, like arrays.

However, it's important to remember that accessing string elements using index returns a byte, not a character (rune). If the string contains multi-byte characters, you need to be careful when using indices because you might not get the complete character. To get individual characters (rune) safely, you can iterate through the string using a for...range loop, which handles UTF-8 encoding properly.

Let's add the following code to the string.go file:

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	str := "Hello, world!"

	// Accessing byte by index
	fmt.Printf("Byte at index 0: %c\n", str[0]) // Output: H
	fmt.Printf("Byte at index 7: %c\n", str[7]) // Output: w

    // Iterate using for...range to safely get runes
    fmt.Println("Iterating through runes:")
    for index, char := range str {
        fmt.Printf("Index: %d, Char: %c\n", index, char)
    }

	// Getting the number of runes
	fmt.Printf("Number of runes (characters) in the string: %d\n", utf8.RuneCountInString(str))

}

Now run the program:

go run string.go

The output is:

Byte at index 0: H
Byte at index 7: w
Iterating through runes:
Index: 0, Char: H
Index: 1, Char: e
Index: 2, Char: l
Index: 3, Char: l
Index: 4, Char: o
Index: 5, Char: ,
Index: 6, Char:
Index: 7, Char: w
Index: 8, Char: o
Index: 9, Char: r
Index: 10, Char: l
Index: 11, Char: d
Index: 12, Char: !
Number of runes (characters) in the string: 13

In this output, you can see that:

  1. Accessing a string by a specific index returns a byte.
  2. Using a for...range loop allows you to iterate through runes correctly.

This example highlights the key difference between byte and rune and the importance of using the appropriate methods when handling characters in Go strings.

Converting Strings and Integers

We can use functions from the strconv package to convert between strings and integers:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    // Declare a string a and an integer b
    a, b := "233", 223

    // Use Atoi to convert a string to an integer
    c, _ := strconv.Atoi(a)

    // Use Sprintf and Itoa functions respectively
    // to convert an integer to a string
    d1 := fmt.Sprintf("%d", b)
    d2 := strconv.Itoa(b)

    fmt.Printf("The type of a: %T\n", a)   // string
    fmt.Printf("The type of b: %T\n", b)   // int
    fmt.Printf("The type of c: %T\n", c)   // int
    fmt.Printf("The type of d1: %T\n", d1) // string
    fmt.Printf("The type of d2: %T\n", d2) // string
}
go run string.go

The expected output is as follows:

The type of a: string
The type of b: int
The type of c: int
The type of d1: string
The type of d2: string

In the program, we use the Sprintf() function from the fmt package, which has the following format:

func Sprintf(format string, a ...interface{}) string

format is a string with escape sequences, a is a constant or variable that provides values for the escape sequences, and ... means that there can be multiple variables of the same type as a. The string after the function represents that Sprintf returns a string. Here's an example of using this function:

a = fmt.Sprintf("%d+%d=%d", 1, 2, 3)
fmt.Println(a) // 1+2=3

In this code snippet, the format is passed with three integer variables 1, 2, and 3. The %d integer escape character in format is replaced by the integer values, and the Sprintf function returns the result after replacement, 1+2=3.

Also, note that when using strconv.Atoi() to convert a string to an integer, the function returns two values, the converted integer val and the error code err. Because in Go, if you declare a variable, you must use it, we can use an underscore _ to ignore the err variable.

When strconv.Atoi() converts correctly, err returns nil. When an error occurs during conversion, err returns the error message, and the value of val will be 0. You can change the value of string a and replace the underscore with a normal variable to try it yourself. This is good practice for error handling, which is a crucial part of Go programming.

Concatenating Strings

The simplest way to concatenate two or more strings is to use the + operator. We can also use the fmt.Sprintf() function to concatenate strings. Let's take a look at an example:

package main

import (
    "fmt"
)

func main() {
    a, b := "lab", "ex"
    // Concatenate using the simplest method, +
    c1 := a + b
    // Concatenate using the Sprintf function
    c2 := fmt.Sprintf("%s%s", a, b)
    fmt.Println(a, b, c1, c2) // lab ex labex labex
}
go run string.go

The expected output is as follows:

lab ex labex labex

In the program, we also used the Sprintf() function from the fmt package to concatenate strings and print the results. Both methods are common ways to concatenate strings, and the choice between them often depends on readability and personal preference.

Removing Leading and Trailing Spaces from a String

We can use the strings.TrimSpace function to remove leading and trailing spaces from a string. The function takes a string as input and returns the string with leading and trailing spaces removed. The format is as follows:

func TrimSpace(s string) string

Here is an example:

package main

import (
    "fmt"
    "strings"
)

func main() {
    a := " \t \n  labex \n \t labs"
    fmt.Println(strings.TrimSpace(a))
}
go run string.go

The expected output is as follows:

labex
         labs

Notice that strings.TrimSpace() only removes spaces at the beginning and the end of the string, spaces inside the string remains as it is.

Summary

To summarize what we have learned in this lesson:

  • The relationship between strings and characters
  • Two ways to declare strings, using double quotes and backticks.
  • Accessing elements of a string using index (byte access) and for...range (rune access).
  • Getting the length of a string using len() (byte length) and utf8.RuneCountInString (character/rune length).
  • Converting strings and integers using strconv.Atoi() and strconv.Itoa().
  • Concatenating strings using the + operator and fmt.Sprintf().
  • Removing leading and trailing whitespace from a string using strings.TrimSpace().

In this lesson, we have explained the strings we use in daily life. We have learned about the relationship between strings and characters, mastered string creation and declaration, and gained some knowledge of common string functions. We also learned how to safely access individual characters in a string, especially when dealing with multi-byte characters, and now understand some crucial string manipulation methods. This gives you a strong foundation to work with string data in Go.

Other Golang Tutorials you may like