Golang Strings And Runes Complete Guide

 Last Update:2025-06-23T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    6 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of GoLang Strings and Runes

Strings in GoLang: A Comprehensive Overview

GoLang's strings are immutable sequences of bytes typically representing UTF-8 encoded characters. This design ensures that once a string is created, it cannot be changed. Instead, you need to create a new one whenever modifications are necessary. The immutability aspect brings safety and simplicity because it eliminates the risk of modifying a string unintentionally or unexpectedly by different parts of your program.

Creating Strings

Strings can be defined using double quotes:

greeting := "Hello, Universe!"

You can also include special characters like \n, \t, \\, and others to denote new lines, tabs, and backslashes respectively:

multilineString := "First line\nSecond line"

String Length

To determine the length of a string, use the len() function:

lengthOfGreeting := len(greeting) // Returns the number of bytes in the string

Notably, len() provides the byte count, not the character count. This distinction is important for non-ASCII characters that may occupy more than one byte.

Indexing and Slicing

Accessing substrings (slices) in Go:

subString := greeting[7:] // Retrieves characters starting from index 7 ("Universe!")

Indexing into a string with greeting[i] gives you the byte at position i, not the character at position i. This is due to the UTF-8 encoding, where characters can span multiple bytes.

Runes in GoLang: The Essential Detail

Rune is essentially an integer type used to represent Unicode code points in Go, often referred to as 'int32'. It's a more suitable type for dealing with individual characters, especially when those characters include multi-byte Unicode sequences.

Understanding Runes

Rune can be declared explicitly:

char := 'A' // Single quotes denote a single rune, not a string
fmt.Println(char) // Outputs: 65 (the ASCII value of 'A')
fmt.Printf("%c", char) // Outputs: A (the character represented by 65)

Alternatively, you can convert from a byte to a rune implicitly if the byte represents an ASCII character:

var b byte = 'B'
var r rune = rune(b)
fmt.Println(r) // Outputs: 66
fmt.Printf("%c", r) // Outputs: B

Iterating Over Characters in a String

When you need to iterate over individual characters including non-ASCII ones, treat the string as a slice of runes:

str := "Hello, 世界!"
for _, r := range str {
    fmt.Printf("%c ", r)
}
// Output: H e l l o ,    世 界 !

In this loop, each iteration assigns the next unicode character to the variable r.

Counting Characters

Since len(str) gives byte count, for accurate character count (including non-ASCII ones), use:

import "unicode/utf8"

characterCount := utf8.RuneCountInString(str) // Returns 12 in this case

Key Points and Importance

  1. Immutability: Go's string immutability makes them safe to use across concurrent programs without additional synchronization.

  2. UTF-8 Encoding: Go's default encoding for strings is UTF-8, which supports every character in the Unicode standard. This ensures wide compatibility but requires careful handling when accessing individual components.

  3. Byte vs Rune Indexing: Be aware that byte indexing can lead to incorrect results if your strings contain multi-byte characters (e.g., non-English alphabets). Use runes for precise character operations.

  4. Memory Efficiency: String concatenation creates a new string; this can be inefficient with large strings or many concatenations. For building strings, prefer using the strings.Builder or bytes.Buffer types which offer efficient append operations.

  5. Standard Library Functions: Utilize the strings package for various useful string manipulation functions such as Split(), Join(), Contains(), etc.

  6. Rune Handling: Use the unicode package to perform operations on runes based on their unicode properties, such as checking for uppercase, lowercase, digits, punctuation, etc.

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement GoLang Strings and Runes

1. Understanding Strings

String in Go is a sequence of bytes (more precisely, UTF-8 encoded characters). Here’s how you can work with strings:

Example 1: Basic String Manipulation

package main

import (
	"fmt"
)

func main() {
	// Create a string
	s := "Hello, World!"

	// Print the string
	fmt.Println("String:", s)

	// Get the length of the string in bytes
	fmt.Println("Length of string in bytes:", len(s))

	// Access a character by index - This gives you the byte, not the rune
	fmt.Printf("Byte at index 0: %c\n", s[0])

	// Iterate over string as bytes
	for i := 0; i < len(s); i++ {
		fmt.Printf("%c", s[i])
	}
	fmt.Println()

	// Iterate over string as runes
	for _, r := range s {
		fmt.Printf("%c", r)
	}
	fmt.Println()
}

2. Understanding Runes

Rune is an alias for the int32 type and represents a Unicode code point. If you need to work with individual characters (including multi-byte characters), you work with runes.

Example 2: Basic Rune Manipulation

package main

import (
	"fmt"
)

func main() {
	// Create a string with multi-byte characters
	s := "你好,世界!"

	// Length of string in bytes
	fmt.Println("Length of string in bytes:", len(s))

	// Length of string in runes
	runeCount := 0
	for range s {
		runeCount++
	}
	fmt.Println("Length of string in runes:", runeCount)

	// Access a rune by index - This is more complex since string indices are byte based
	// Convert string to rune slice to manipulate by rune
	runes := []rune(s)
	fmt.Printf("Rune at index 0: %c\n", runes[0])

	// Iterate over string as runes
	for _, r := range s {
		fmt.Printf("%c", r)
	}
	fmt.Println()
}

3. Converting Between Strings and Runes

Example 3: Converting String to Rune Slice and Vice Versa

package main

import (
	"fmt"
)

func main() {
	// String with multi-byte characters
	s := "Привет, Мир!"

	// Convert string to rune slice
	runes := []rune(s)

	// Print runes
	for i, r := range runes {
		fmt.Printf("Index: %d, Rune: %c\n", i, r)
	}

	// Convert rune slice back to string
	backToString := string(runes)

	// Print original string and converted string
	fmt.Println("Original String:", s)
	fmt.Println("Converted String:", backToString)
}

4. Modifying Strings

Since strings in Go are immutable, you often need to convert them to a rune slice, make modifications, and then convert them back to a string.

Example 4: Modifying a String

Top 10 Interview Questions & Answers on GoLang Strings and Runes

1. What are Strings in GoLang?

Answer: In GoLang, strings are sequences of bytes and are immutable, meaning that once a string is created, it cannot be changed. Typically, strings in Go are UTF-8 encoded, so they can represent any Unicode character.

s := "Hello, World!"
fmt.Println(s) // Output: Hello, World!

2. How are Strings different from Runes in GoLang?

Answer: While strings are sequences of bytes, runes are numeric values representing Unicode code points. A rune is just an alias for int32, and it represents a single Unicode character.

r := '👋' // This is a rune
fmt.Printf("%U\n", r) // Output: U+1F44B

Strings in Go are built out of these runes, especially when dealing with text that contains characters outside the ASCII range.

3. How do you convert a String to a Slice of runes in GoLang?

Answer: To convert a string to a slice of runes, you should use a type conversion:

s := "👋🌍"
runes := []rune(s)
fmt.Println(runes)
// Output will be something like: [128075 127759]

This helps in accurately processing multi-byte characters, such as emojis in this example.

4. How do you concatenate two or more strings in GoLang?

Answer: Concatenating strings can be done in multiple ways in Go. The simplest way is using the + operator. For more efficient concatenation, especially in loops, consider using strings.Builder.

Using + Operator:

s1 := "Hello"
s2 := ", World!"
result := s1 + s2
fmt.Println(result) // Output: Hello, World!

Using strings.Builder:

var builder strings.Builder
builder.WriteString("Hello")
builder.WriteString(", World!")
result := builder.String()
fmt.Println(result) // Output: Hello, World!

5. How do you get the length of a string in GoLang?

Answer: The len() function gives you the number of bytes in a string, not the number of characters. To get the real character count, convert the string to a slice of runes and then take its length.

s := "👋🌍"
byteLen := len(s)
runeLen := len([]rune(s))
fmt.Println(byteLen) // Output: 10
fmt.Println(runeLen) // Output: 2

Note the difference between byte length (10) and rune length (2).

6. What are some common methods available on the strings package in GoLang?

Answer: The strings package provides many functions for manipulating and inspecting strings:

  • Contains(s, substr string) bool: Checks if s contains substr.
  • Fields(s string) []string: Splits the string into fields separated by whitespace.
  • Join(elems []string, sep string) string: Joins a slice of strings with the specified separator.
  • ToUpper/ToLower(s string) string: Converts the whole string to uppercase/lowercase.
  • TrimSpace(s string) string: Removes leading and trailing whitespace from s.
  • Split(s, sep string) []string: Splits the string into substrings separated by sep.

Example:

s := "Hello,World!"
parts := strings.Split(s, ",")
fmt.Println(parts[0]) // Output: Hello
fmt.Println(parts[1]) // Output: World!

7. How do you check if a string has a specific prefix/suffix in GoLang?

Answer: Use the strings.HasPrefix() and strings.HasSuffix() functions.

s := "Hello, World!"
if strings.HasPrefix(s, "Hello") {
    fmt.Println("Starts with Hello")
}
if strings.HasSuffix(s, "World!") {
    fmt.Println("Ends with World!")
}

8. Can you show how to iterate over the characters in a string properly in GoLang?

Answer: To correctly iterate over characters (runes) in a multi-byte string:

s := "👋🌍"
for _, r := range s {
    fmt.Printf("%c\n", r) 
}
// Output:
// 👋
// 🌍

Here, range iterates over each rune in the string s.

9. How do you compare two strings in GoLang for equality?

Answer: Simply use the == operator for case-sensitive comparison or the Unicode normalization functions from golang.org/x/text/unicode/norm for more advanced comparison.

s1 := "Hello"
s2 := "Hello"
s3 := "hello"

fmt.Println(s1 == s2) // Output: true
fmt.Println(s1 == s3) // Output: false (case matters)

For case-insensitive comparison:

import (
    "strings"
)

func isEqualIgnoreCase(s1, s2 string) bool {
    return strings.EqualFold(s1, s2)
}

fmt.Println(isEqualIgnoreCase("Hello", "hello")) // Output: true

10. How do you find all matches of a regular expression in a string using GoLang?

Answer: For finding all matches of a regular expression within a string, you can use the regexp package and its FindAllString() method.

Example:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    s := "Go provides powerful regular expressions."
    re := regexp.MustCompile(`o`)
    all := re.FindAllString(s, -1)
    fmt.Println(all) // Output: [o o o]
}

Using FindAllString(s, -1) returns every match of the pattern within the string s. If you wish to limit the number of results, you can specify a non-negative integer instead of -1.


You May Like This Related .NET Topic

Login to post a comment.