Explain Strings in GoLang | Go Programming Tutorial
Explain Strings in GoLang | Go Programming Tutorial
In this article you will learn:
- What are Strings in Go Programming Language?
- Types of String Literals in Go Programming Language
- Essential Points about the Strings
What are Strings in Go Programming Language?
In Go Programming Language, Strings are different from other programming languages. In GoLang, it is a sequence of variable-width characters where every character is indicative by one or more bytes using UTF-8 encoding. In simple words, strings are the immutable chain of arbitrary bytes which also includes bytes with zero values. The string has a read-only slice of bytes and the bytes of the strings can be represented in Unicode only with the help of UTF-8 encoding. Because of UTF-8 encoding GoLang string can have a text which is a mixture of any language present worldwide. The string is written in double quotes (“”).
Note: The string can be nil, or it can be empty.
Types of String Literals in Go Programming Language:
String literals can be created in two ways in the Go Programming Language:
- Using double quotes(“”): The string literals are created using the double quotes(“”). These types of string support escape characters but it does not span multiple lines. It is the most used string literal in GoLang.
- Using backticks(”): The string literals created with the help of backticks(”) are also known as raw literals. They do not support the escape characters, can span multiple lines, and can have any character except backtick. It is used in writing multiple-line messages in regular expression.
Essential Points about the Strings
- Strings are immutable: In GoLang, the string value can not be changed once created. In other words, strings are read-only. If you try to change the value of the string, the compiler will throw an error.
- Iterate over a string: The user can iterate over the string using the range loop. The loop can be iterated over the Unicode point for a string.
- The characters in the string are of a byte so we can access each byte of the given string.
- The user can create a string from the slice of bytes in GoLang.
- The two functions that are used in finding the length of a string are len() and RuneCountInString().