Go String
In Go, Strings are immutable, meaning once you create a string, the content of the string cannot be modified again. Therefore, the initial value of the string is empty (" ") by default.
What is a string?
A string is a sequence of characters (letters, numbers, symbols, emojis, etc.). Here are some examples of strings:
"Hello, World!", "Hello, δΈη", "Hello, π", etc.
They are enclosed in double quotes (") and have the type string.
How to declare a string and access its characters?
There are two ways to declare a string in Go.
- Using double quotes
- Using backticks
Declaring a string in Go using double quotes
Like for all other types, you can declare a string variable using the var
keyword:
var s string = "Hello, World!"
Or you can use the short variable declaration operator :=
:
s := "Hello, World!"
Accessing characters of a string in Go
To access a character in a string, you can use the square brackets ([]
) operator:
package main
import "fmt"
func main() {
s := "Hello, World!"
// Print the Unicode Code points
fmt.Println(s[0]) // 72
fmt.Println(s[7]) // 87
}
Output
72
87
Note: 72
and 87
are the Unicode code points of the characters H
and W
, respectively.
To print the characters themselves, you can use fmt.Printf
:
package main
import "fmt"
func main() {
s := "Hello, World!"
fmt.Printf("%c", s[0]) // H
fmt.Printf("%c", s[7]) // W
}
Output
H
W
Note: The characters are accessed using their index number, and the index starts from 0, not from 1.
But you can't use the square brackets operator to update a character in a string:
s := "Hello, World!"
s[0] = 'h' // error: cannot assign to s[0]
This is because strings are immutable in Go. So once it's created, we cannot modify the contents of the string.
Declare and access strings using backtick
In Go, String literals are created using backticks (``). Hence, it is also called raw literals.
The string literals do not support escape characters, but they can span over multiple lines. Therefore, it is mainly used to declare longer strings spanning multiple lines.
package main
import "fmt"
func main() {
s := `Hello, World!
Welcome to Go Programming`
fmt.Println(s)
}
Output
Hello, World!
Welcome to Go Programming
Find the length of a string
We can find the length of a string using the len()
method. The len()
function returns the total count of characters in a given string.
package main
import "fmt"
func main() {
s := "Hello, World!"
fmt.Println("The length of the string is:", len(s))
}
Output
The length of the string is: 13
Useful string methods
Go provides several useful methods for strings:
Compare
to compare two stringsToUpper
andToLower
to convert a string to upper or lower case,Contains
to check if a string contains a substring,Replace
to replace a substring with another substring,Split
to split a string into a slice of strings based on a separator,Trim
to remove leading and trailing characters from a string (for example, when dealing with files and user input).
Here is how to use them:
package main
import (
"fmt"
"strings"
)
func main() {
s := "Hello, World!"
t := "Hello, World!"
fmt.Println(strings.Compare(s, t)) // 0
fmt.Println(strings.ToUpper(s)) // HELLO, WORLD!
fmt.Println(strings.ToLower(s)) // hello, world!
fmt.Println(strings.Contains(s, "Hello")) // true
fmt.Println(strings.Replace(s, "World", "δΈη", 1)) // Hello, δΈη!
fmt.Println(strings.Split(s, " ")) // [Hello World!]
fmt.Println(strings.Trim(" Hello, World! ", " ")) // Hello, World!
}
Output
0
HELLO, WORLD!
hello, world!
true
Hello, δΈη!
[Hello, World!]
Hello, World!
The strings
package of the standard library contains many more useful methods for strings that you can find in the documentation.