Strings in Go

Alissa LozhkinAlissa Lozhkin
3 min read

Strings are sequences of characters used to represent text, and they can contain letters, digits, and special characters. In Go, strings are immutable. This post will go over the steps on how to declare a string, and it will also go over basic string functions.

Just like any other datatype, a string can be defined using either the var keyword or the shorthand method. Both ways can be seen in the code block below:

var string1 string = "hello"  // using 'var' keyword
string2 := "hi"  // using shorthand method

In order to use basic string functions in a code block, we have to import the strings package:

import "strings"

We can access many different functions from this package. The following section will present example usage of some of these functions.

The Contains() function returns whether a substring is found in another string. The first parameter is the string we want to search, and the second parameter is the substring we want to search for.

import (
"strings"
"fmt"
)

string3 := "Hello, how are you?"
contains_o := strings.Contains(string3, "o")
contains_q := strings.Contains(string3, "q")

fmt.Println(contains_o)  // true
fmt.Println(contains_q)  // false

The Index() function returns the index of a specific character in a string. The first parameter is the string we want to search, and the second parameter is the character whose index we want.

import (
"strings"
"fmt"
)

string4 := "Good morning"
index_o := strings.Index(string4, "o")
index_od := strings.Index(string4, "od")
index_q := strings.Index(string4, "q")

fmt.Println(index_o)  // 1
fmt.Println(index_od)  // 2
fmt.Println(index_q)  // -1: because q is not in the string

Index_o stores the index of the first instance of the letter o. Index_od stores the index of where the substring od starts. Since q is not in string4, -1 is returned when trying to get its index.

The Replace() function can be used to replace substring a in a string with substring b a specified number of times (n). The first parameter is the original string from which a copy is returned, the second parameter is a , the third parameter is b , and the fourth parameter is n. The code block below demonstrates how this function works:

import (
"strings"
"fmt"
)

string4 := "ababababa"
string4 = strings.Replace(string4, "b", "a", 2)
string5 := strings.Replace(string4, "b", "a", -1)

fmt.Println(string4)  // aaaaababa
fmt.Println(string5)  // aaaaaaaaa

The first example shows the first two instances of b replaced by a The second example - string5 - uses -1 as the last parameter, which indicates that all instances of ‘b’ should be replaced by a.

The Split() function slices a string into all substrings that are separated by a specified character. The function returns a slice with the substrings as its elements. The code block below demonstrates how this function works:

import (
"strings"
"fmt"
)

string6 := "1-2-3-4"
slice1 := []string{}
slice1 = strings.Split(string4, "-")

fmt.Println(slice1)  // [1 2 3 4]
0
Subscribe to my newsletter

Read articles from Alissa Lozhkin directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Alissa Lozhkin
Alissa Lozhkin

My name is Alissa and I am a fourth-year computer science and math student at the University of Toronto! I am very interested in computer science and software development, and I love learning about new technologies! Get in touch with me on LinkedIn: https://www.linkedin.com/in/alissa-lozhkin