Gopher

Introduction to strings in Golang

Published on 10 May 2020
Last Updated on 10 May 2020

Strings in Golang are implemented differently than strings in any other programming language.

What is a String in Golang

In Golang a string is a slice of bytes of arbitrary length whose default encoding is UTF-8.

Because UTF-8 supports ASCII characters, one does not have to worry about underlying encoding of the characters in most cases.

Strings and immutability

A string is a slice of read only bytes.

Once a string is initialized, its individual bytes are immutable, which means these re-assigning new values to the individual bytes is not allowed in Golang.

Instead of re-assigning new values to individual bytes, entire string must be re-assigned a new value.

This is demonstrated in the program given below

package main

import "fmt"

func main() {
	var str = "hello"

	// valid
	str = "world"

	// not valid
	str[0]='a'
}

Initializing strings from slice of bytes

Strings are slice of bytes, so we can initialize a string from a slice of bytes as follows:

package main

import "fmt"

func main() {
	var sliceOfBytes = []uint8{97, 98, 99, 100} // abcd
	var str = string(sliceOfBytes)
	fmt.Println(str) // prints abcd to console
}

Program output

Above program produces following output:

abcd

Program Description

Above program makes use of type alias for defining a slice of bytes. Once the slice of bytes is defined, it is used for initializing string str. Once string is initialized it is printed to console using fmt.Println.

Type aliases

It is important to note that byte is an alias for uint8 and rune is an alias for int32. You might see these aliases being used interchangeably throughout various examples.

Program to demonstrate how to initialize strings in Golang

Program given below demonstrates all the possible ways by which we can initialize a string in Golang.

package main

import "fmt"

func main() {
	// initializing a string using the var keyword
	var str1 = "Hello World"

	// initializing a string using ":=" annotation
	str2 := "Hello World!"

	// initializing a string a back-tick also
	// known as the raw string literal
	var str3 = `\\`
	str4 := `\\`

	// creating multiline strings with raw string literal
	var str5 = `
this
is
a
raw
multiline 
string
`

	str6 := `
this is also a raw multiline string
`

	// printing values for all the initialized strings
	fmt.Println("str1 is:", str1)
	fmt.Println("str2 is:", str2)
	fmt.Println("str3 is:", str3)
	fmt.Println("str4 is:", str4)
	fmt.Println("str5 is:", str5)
	fmt.Println("str6 is:", str6)
}

Program output

Above program produces following output:

str1 is: Hello World
str2 is: Hello World!
str3 is: \\
str4 is: \\
str5 is: 
this
is
a
raw
multiline 
string

str6 is: 
this is also a raw multiline string

Program Description

Above program demonstrates various ways by which a string can be initialized in Golang.

Also note that back-tick (`), can be used for initializing raw strings as well as for initializing raw multi line strings.

Be careful while defining raw multiline strings

Program given below demonstrates how indentation and other raw characters are treated in raw multiline string.

// program to demonstrate raw strings in Golang
package main

import "fmt"

func main() {
	// no indentation
	var str1 = `
this
is
a
raw
multiline 
string
`

	// indentation
	var str2 = `
	this
	is
	a
	raw
	multiline 
	string
`

	// printing values for all the initialized strings
	fmt.Println("str1 is:", str1)
	fmt.Println("str2 is:", str2)
}

Program output

Above program produces following output:

str1 is: 
this
is
a
raw
multiline 
string

str2 is: 
        this
        is
        a
        raw
        multiline 
        string

Program Description

Notice that, in above program raw multiline strings preserves all indentation and it also preserves any special characters that are defined in it.

Because of this behaviour, one needs to be extra careful while defining raw multiline strings in Golang.

Also note that raw multiline strings do not give special treatment for special escape characters.

Commonly used escape characters in Golang strings

Given below is a table that lists various commonly used string escape characters in Golang.

Escape character Description
\xhh Unicode character with the given 8-bit hex code point.
\r ASCII carriage return “CR”
\’ Single quote “‘”
\000 Unicode character with the given 8-bit octal code point
\b ASCII backspace “BS”
\a ASCII bell “BEL”
\uxxxx Unicode character with the given 16-bit hex code point.
\f ASCII formfeed “FF”
\” Double quote ““”.
\v ASCII vertical tab “VT”
\t ASCII tab “TAB”
\ Backslash “"
\n ASCII linefeed “LF”

Program to demonstrate how to access individual bytes of a String

Program given below demonstrates how we can access individual bytes of a string using a for range loop.

// program to demonstrate how to access individual bytes of a string
package main

import "fmt"

func main() {
	var str = "abcd"
	for index, value := range str {
		fmt.Println("\nindex:", index)
		fmt.Println("value:", value)
	}
}

Program output

Above program produces following output:

index: 0
value: 97

index: 1
value: 98

index: 2
value: 99

index: 3
value: 100

Program to calculate length of a string in Golang

// program to demonstrate how to calculate length of string in Golang
package main

import "fmt"

func main() {
	var str = "abcd"
	fmt.Println("Length of string is:", len(str))
}

Program output

Above program produces following output:

Length of string is: 4

Program Description

Above program uses built in len function for calculating length of string. As shown in above program, len(str) returns the length of the string.

That’s it for this chapter. In other chapters we will discuss how to do string concatenation, string comparison and many other aspects of using strings in Golang.