VOOZH about

URL: https://www.geeksforgeeks.org/go-language/strings-in-golang/

⇱ Strings in Golang - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Strings in Golang

Last Updated : 10 Mar, 2026

In Go language, strings are immutable sequence of bytes that typically represents UTF-8 encoded text. Each character may be represented by one or more bytes. Since Go uses UTF-8 encoding, strings can easily represent text from many different languages.

Example:


Output
String 1: Welcome to GeeksforGeeks
String 2: GeeksforGeeks

Note: String can be empty, but they are not nil.

String Literals

In Go language, string literals are created in two different ways:

Using double quotes("")

Here, the string literals are created using double-quotes(""). This type of string support escape character as shown in the below table, but does not span multiple lines. This type of string literals is widely used in Golang programs. 

Escape characterDescription
\\Backslash(\)
\000Unicode character with the given 3-digit 8-bit octal code point
\'Single quote ('). It is only allowed inside character literals
\"Double quote ("). It is only allowed inside interpreted string literals
\aASCII bell (BEL)
\bASCII backspace (BS)
\fASCII formfeed (FF)
\nASCII linefeed (LF
\rASCII carriage return (CR)
\tASCII tab (TAB)
\uhhhhUnicode character with the given 4-digit 16-bit hex code point.
Unicode character with the given 8-digit 32-bit hex code point.
\vASCII vertical tab (VT)
\xhhUnicode character with the given 2-digit 8-bit hex code point.

Using backticks(``)

Here, the string literals are created using backticks(``) and also known as raw literals. Raw literals do not support escape characters, can span multiple lines, and may contain any character except backtick. It is, generally, used for writing multiple line message, in the regular expressions, and in HTML.

Example:


Output
String 1: Welcome to GeeksforGeeks
String 2: Welcome!
GeeksforGeeks
String 3: Hello!GeeksforGeeks
String 4: Hello!\nGeeksforGeeks

Important Points About String

Strings are immutable

In Go language, strings are immutable once a string is created the value of the string cannot be changed. Or in other words, strings are read-only. If you try to change, then the compiler will throw an error.

Example:


Output
String: Welcome to GeeksforGeeks

How to iterate over a string?

You can iterate over string using for range loop. This loop can iterate over the Unicode code point for a string.

Syntax:

for index, chr:= range str{
// Statement..
}

Here, the index is the variable which store the first byte of UTF-8 encoded code point and chr store the characters of the given string and str is a string.

Example:


Output:

The index number of G is 0
The index number of e is 1
The index number of e is 2
The index number of k is 3
The index number of s is 4
The index number of F is 5
The index number of o is 6
The index number of r is 7
The index number of G is 8
The index number of e is 9
The index number of e is 10
The index number of K is 11
The index number of s is 12

How to access the individual byte of the string?

The string is of a byte so, we can access each byte of the given string.

Example:


Output:

Character = W Bytes = 57
Character = e Bytes = 65
Character = l Bytes = 6c
Character = c Bytes = 63
Character = o Bytes = 6f
Character = m Bytes = 6d
Character = e Bytes = 65
Character = Bytes = 20
Character = t Bytes = 74
Character = o Bytes = 6f
Character = Bytes = 20
Character = G Bytes = 47
Character = e Bytes = 65
Character = e Bytes = 65
Character = k Bytes = 6b
Character = s Bytes = 73
Character = f Bytes = 66
Character = o Bytes = 6f
Character = r Bytes = 72
Character = G Bytes = 47
Character = e Bytes = 65
Character = e Bytes = 65
Character = k Bytes = 6b
Character = s Bytes = 73

How to create a string form the slice?

In Go language, you are allowed to create a string from the slice of bytes.

Example:


Output:

String 1: Geeks
String 2: Geeks

How to find the length of the string?

In Golang string, you can find the length of the string using two functions one is len() and another one is RuneCountInString(). The RuneCountInString() function is provided by UTF-8 package, this function returns the total number of rune presents in the string. And the len() function returns the number of bytes of the string.

Example:


Output:

string: Welcome to GeeksforGeeks ??????
Length 1: 31
Length 2: 31
Comment
Article Tags:

Explore