![]() |
VOOZH | about |
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:
String 1: Welcome to GeeksforGeeks String 2: GeeksforGeeks
Note: String can be empty, but they are not nil.
In Go language, string literals are created in two different ways:
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 character | Description |
|---|---|
| \\ | Backslash(\) |
| \000 | Unicode 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 |
| \a | ASCII bell (BEL) |
| \b | ASCII backspace (BS) |
| \f | ASCII formfeed (FF) |
| \n | ASCII linefeed (LF |
| \r | ASCII carriage return (CR) |
| \t | ASCII tab (TAB) |
| \uhhhh | Unicode character with the given 4-digit 16-bit hex code point. |
| Unicode character with the given 8-digit 32-bit hex code point. | |
| \v | ASCII vertical tab (VT) |
| \xhh | Unicode character with the given 2-digit 8-bit hex code point. |
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:
String 1: Welcome to GeeksforGeeks String 2: Welcome! GeeksforGeeks String 3: Hello!GeeksforGeeks String 4: Hello!\nGeeksforGeeks
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:
String: Welcome to GeeksforGeeks
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
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
In Go language, you are allowed to create a string from the slice of bytes.
Example:
Output:
String 1: Geeks
String 2: Geeks
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