Working with strings in Go

11.06.2021

Intro

In Go, strings are a read-only sequences of bytes. If you know other programming languages, you are probably used to managing a sequence of characters. Go uses bytes to support many characters in UTF-8. In this article, we will learn how to use Go Strings.

Creating Strings

To create a string, we can type a list of characters in between two quotes.

package main

import (
    "fmt"
)

func main() {
    example := "Hello, World"

    fmt.Println(example)
}

Indexing Strings

Strings are like arrays, and we can index them. However, we access each byte instead of a character. A character can be 1-3 bytes.

package main

import (
    "fmt"
)

func main() {
    example := "Hello 🐘"

    fmt.Printf("%c\n", example[6])
}

Instead of printing the character, we printed the byte. If we would like to index a string by characters, we have to first convert the string to a rune array.

package main

import (
	"fmt"
)

func main() {
	example := "Hello 🐘"

	exampleRune := []rune(example)

	fmt.Printf("%c\n", example[6])
	fmt.Printf("%c\n", exampleRune[6])
}

Comparing Strings

We can compare strings using the normal compare operators. For example, we can use '==' and '<'

package main

import (
	"fmt"
)

func main() {
	first := "Hello"
	second := "World"

	fmt.Println(first == second)
	fmt.Println(first < second)
}

Concatenate Strings

We can create new strings from existing strings by using the + operator.

package main

import (
	"fmt"
)

func main() {
	first := "Hello"
	second := "World"

	fmt.Println(first + " " + second)
}

Escape Characters

In our text, we can use escape characters with the \. For example, we can use the tab \t and new line \n. Characters.

package main

import (
	"fmt"
)

func main() {
	fmt.Print("Hello\tWorld\n)
}

Multiline

If we would like to use multiline strings, we can use the backtick "`" character.

package main

import (
	"fmt"
)

func main() {
	multiString := `
		This is a string
		over multiple lines.
	`
	fmt.Print(multiString)
}

Looping Over Strings

We can loop over strings just like an array.

package main

import (
	"fmt"
)

func main() {
	example := "Hello, World"
	for idx, s := range example {
		fmt.Printf("Index: %d, Character %c \n", idx, s)
	}
}

Counting Strings

We can use len to get the number of bytes in a string. This is unlike other languages that return the character count. If you would like to get the character count, you can use utf8.RuneCountInString.

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	example2 := "Hello 🐘"
	fmt.Println(len(example2))
	fmt.Println(utf8.RuneCountInString(example2))
}

Joining Strings

We can use the strings.Join function to join a list of strings. This is a common task when working with arrays. We can also revert the process with strings.Split.

package main

import (
    "fmt"
    "strings"
)

func main() {
    helloString := []string{"Hello", "🐘"}

    joined := strings.Join(helloString, ", ")
    fmt.Println(joined)

    split := strings.Split(joined, ",")
    fmt.Println(split)
}