Strings in Golang

In Go, a string is a sequence of characters enclosed within double quotes. It’s a fundamental data type used to represent textual data. Strings in Go are immutable, meaning once they are created, their values cannot be changed. Instead, operations on strings usually create new strings.

Here are some key points about strings in Go:

1. String Declaration and Initialization: You can declare and initialize a string using double quotes.

var message string = "Hello, World!"

2. String Literals: You can create string literals using raw string literals (enclosed in backticks) or interpreted string literals (enclosed in double quotes).

rawString := `This is a raw string literal.`
interpretedString := "This is an interpreted string literal."

3. String Concatenation: Strings can be concatenated using the + operator.

firstName := "John"
lastName := "Doe"
fullName := firstName + " " + lastName

4. String Length: The length of a string (number of characters) can be obtained using the len() function.

message := "Hello, Go!"
length := len(message) // This will be 10

5. Accessing Characters: In Go, strings are a sequence of bytes, and you can access individual characters using indexing.

message := "Hello"
firstChar := message[0] // This will be 'H'

6. String Manipulation: Since strings are immutable, operations like changing a character at a specific index aren’t possible directly. However, you can convert a string to a []byte, modify the byte slice, and then convert it back to a string.

text := "apple"
bytes := []byte(text)
bytes[0] = 'b'
newText := string(bytes) // This will be "bpple"

7. Unicode Support: Go supports Unicode characters, allowing you to work with a wide range of languages and symbols.

8. String Functions: The strings package in Go provides a range of functions for string manipulation, such as strings.ToUpper(), strings.ToLower(), strings.HasPrefix(), strings.HasSuffix(), strings.Contains(), and more.

Strings are fundamental to working with textual data in Go, and their immutability makes them safe for concurrent operations. If you need mutable sequences of bytes, you can use byte slices instead.

String Literals

String literals are representations of text enclosed within either double quotes (interpreted string literals) or backticks (raw string literals) in Go. They are a fundamental way to express textual data in the programming language.

Here’s a brief explanation of interpreted and raw string literals:

1. Interpreted String Literals (Double Quotes): Interpreted string literals are enclosed within double quotes. They can contain escape sequences like \n (newline), \t (tab), and more.

message := "Hello, Go!\nWelcome to the world of programming."

2. Raw String Literals (Backticks): Raw string literals are enclosed within backticks. They are used when you want to include line breaks or escape sequences without any special interpretation. This is useful, for example, for regular expressions or multi-line text.

pattern := `^\d{3}-\d{2}-\d{4}$`

Escaped Characters in Interpreted Strings: Interpreted strings support escape sequences that allow you to include special characters within the string. For instance:

  • \n: Newline
  • \t: Tab
  • \": Double quote
  • \\: Backslash
  • and more…
specialCharacters := "Escape sequences: \\n (newline), \\t (tab), \\\" (double quote)"

Backslashes in Raw Strings: In raw strings, backslashes are treated as literal characters, so you don’t need to escape them. This makes raw strings useful for regular expressions and paths.

path := `C:\Users\username\documents`

Both types of string literals have their uses. Interpreted strings are more common for everyday text, while raw strings are often used for patterns, regular expressions, file paths, and other situations where you want to include special characters or line breaks without escaping them.

Important Points About String

here are some important points to understand about strings in Go:

  1. Strings are Immutable: Once a string is created, you cannot modify its contents. Any operation that seems to modify a string actually creates a new string.
  2. UTF-8 Encoding: Strings in Go are UTF-8 encoded by default, which means they can represent characters from various languages and symbol sets.
  3. Indexing: You can access individual bytes of a string using indexing. However, keep in mind that indexing gives you bytes, not characters. Some characters might be represented by more than one byte in UTF-8.
  4. Length vs. Bytes: The length of a string (obtained using len()) is the number of bytes in the string, not the number of characters. To count characters, you might need to use the unicode/utf8 package.
  5. String Literals: Strings can be enclosed in double quotes ("like this") or backticks (`like this`). Interpreted string literals allow escape sequences, while raw string literals treat characters as-is.
  6. Concatenation: Strings can be concatenated using the + operator. However, if you’re doing a lot of concatenation, consider using the strings.Join() function for better performance.
  7. Comparison: You can compare strings using operators like ==, !=, <, >, <=, and >=. Comparisons are case-sensitive.
  8. String Conversion: You can convert between strings and other types using strconv.Itoa() (integer to string) and strconv.Atoi() (string to integer) from the strconv package.
  9. String Package: The strings package provides many useful functions for manipulating and working with strings, such as strings.Contains(), strings.HasPrefix(), strings.Join(), strings.Split(), and more.
  10. UTF-8 and Unicode: Go’s string handling is UTF-8 based, making it well-suited for handling Unicode characters from various languages.
  11. Slicing: You can create a substring of a string using slicing, just like with arrays and slices of other types.
  12. Conversion to Byte Slice: You can convert a string to a byte slice using []byte(s), but remember that modifying the byte slice won’t affect the original string.
  13. String as Read-Only Data: Strings are read-only, making them safe for concurrent operations without locking.

Understanding these points will help you work effectively with strings in Go.

How to access the individual byte of the string?

To access the individual bytes of a string in Go, you can treat the string like a slice of bytes and use indexing. However, it’s important to note that the index you use represents bytes, not characters. In UTF-8 encoded strings, characters might span multiple bytes.

Here’s an example of how to access individual bytes of a string:

package main

import (

func main() {
	message := "Hello, Go!"
	// Accessing individual bytes using indexing
	firstByte := message[0] // 'H'
	secondByte := message[1] // 'e'

	// Printing the individual bytes
	fmt.Println("First Byte:", firstByte)
	fmt.Println("Second Byte:", secondByte)

In this example, the string "Hello, Go!" is assigned to the message variable. We then use indexing to access the individual bytes at positions 0 and 1. Keep in mind that using indexing directly like this can be risky when dealing with characters that require multiple bytes in UTF-8 encoding. If you’re working with Unicode characters, it’s better to use the for range loop along with the utf8 package to safely iterate over the characters.

package main

import (

func main() {
	message := "Hello, Go!"
	// Iterating over individual characters using the for range loop
	for _, char := range message {
		fmt.Printf("Character: %c\n", char)

	// Getting the length of the string (number of bytes)
	length := len(message)
	fmt.Println("Length of the string (bytes):", length)

	// Getting the number of characters using utf8 package
	numChars := utf8.RuneCountInString(message)
	fmt.Println("Number of characters:", numChars)

The utf8.RuneCountInString() function from the unicode/utf8 package gives you the actual number of characters, not just bytes.

How to create a string form the slice?

To create a string from a slice of bytes in Go, you can use the string() type conversion. This allows you to convert a byte slice to a string. Here’s how you can do it:

package main

import "fmt"

func main() {
    // Create a byte slice
    byteSlice := []byte{'H', 'e', 'l', 'l', 'o'}

    // Convert the byte slice to a string
    str := string(byteSlice)

    // Print the resulting string

Output :


In this example, we create a byte slice containing individual characters 'H', 'e', 'l', 'l', and 'o'. Then, using the string() conversion, we convert the byte slice to a string. The resulting string is "Hello".

Keep in mind that while this approach works for converting byte slices to strings, modifying the byte slice won’t affect the original string, since strings are immutable in Go.

How to find the length of the string?

To find the length of a string in Go, you can use the built-in len() function. The len() function returns the number of bytes in the string, which is useful for various operations. However, remember that for strings containing multi-byte characters (such as UTF-8 encoded strings), the length returned by len() might not directly correspond to the number of visible characters.

Here’s how you can find the length of a string using the len() function:

package main

import "fmt"

func main() {
    message := "Hello, Go!"
    length := len(message)
    fmt.Printf("Length of the string: %d\n", length)

Output :

Length of the string: 11

In this example, the string "Hello, Go!" has a length of 11 bytes. Keep in mind that the length is based on the bytes in the string, not the characters. If you need the number of visible characters, especially when dealing with Unicode characters, you should use the unicode/utf8 package to calculate the number of runes (characters) in the string.