Strings in Go
By Flavio
An introduction to strings in Go, which are immutable sequences of bytes. Learn indexing, slicing, concatenation with +, and the handy strings package.
A string in Go is a sequence of byte values.
You define a string using this syntax:
var name = "test"
It’s important to note that unlike other languages, strings are defined only using double quotes, not single quotes.
To get the length of a string, use the built-in len() function:
len(name) //4
len() counts bytes, not characters. That matters as soon as the text is not plain ASCII:
word := "café"
len(word) //5
é is two bytes in UTF-8, so the length is 5, not 4.
You can access individual bytes using square brackets, passing the index of the byte you want to get:
name[0] //116, the byte for 't' (indexes start at 0)
name[1] //101, the byte for 'e'
string(name[0]) //"t"
You can get a portion of the string using this syntax:
name[0:2] //"te"
name[:2] //"te"
name[2:] //"st"
Using this you can create a copy of the string using
var newstring = name[:]
You can assigning a string to a new variable:
var first = "test"
var second = first
Strings are immutable, so you cannot update the value of a string.
Even if you assign a new value to first using an assignment operator, the value second is still going to be "test":
var first = "test"
var second = first
first = "another test"
first //"another test"
second //"test"
A string value is a small header: a pointer plus a length. That header is copied when you pass the string. The bytes stay shared. Since strings are immutable, this is cheap and safe.
You can concatenate two strings using the + operator:
var first = "first"
var second = "second"
var word = first + " " + second //"first second"
To walk Unicode text, range over the string. Each step gives you a rune:
word := "café"
for i, r := range word {
fmt.Printf("%d: %c\n", i, r)
}
//0: c
//1: a
//2: f
//3: é
i is the byte index. It jumps from 3 to the end because é takes two bytes. r is the character.
Go provides several string utilities in the strings package.
We already saw how to import a package in the “Hello, World!” example.
Here’s how you can import strings:
package main
import (
"strings"
)
And then you can use it.
For example we can use the HasPrefix() function to see if a string starts with a specific substring:
package main
import (
"strings"
)
func main() {
strings.HasPrefix("test", "te") // true
}
The full list of methods can be found here: https://pkg.go.dev/strings
Here’s a list of methods you might use frequently:
strings.ToUpper()returns a new string, uppercasestrings.ToLower()returns a new string, lowercasestrings.HasSuffix()checks if a string ends with a substringstrings.HasPrefix()checks if a string starts with a substringstrings.Contains()checks if a string contains a substringstrings.Cut()(Go 1.18) splits a string at the first match of a separatorstrings.Count()counts how many times a substring appears in a stringstrings.Join()used to join multiple strings and create a new onestrings.Split()used to create an array of strings from a string, dividing the original one on a specific character, like a comma or a spacestrings.ReplaceAll()used to replace a portion in a string and replace it with a new one
Want me to talk about your product? You can sponsor this site.