Introduction to Strings
Understand what strings are and how they differ from arrays and characters in Go. Explore string properties, storage, encoding standards, and practical distinctions essential for working with text data.
We'll cover the following...
By this point, you are familiar with arrays and slices as ordered collections of elements stored by position. They provide a general way to store sequences of values such as integers, floating-point numbers, and structs. Each element can be accessed efficiently using its index.
However, many important computational problems involve textual and symbolic data rather than only numeric values. Names, passwords, file paths, search queries, source code, and biological sequences are all examples of data that must be handled as sequences of characters. Although slices can store characters, viewing such data only as a general slice is often not enough for algorithmic study.
Character-based data gives rise to a different kind of problem. In numerical arrays, common tasks include searching for values, computing sums, or rearranging elements. In character sequences, the focus shifts to questions such as whether two words are equal, whether a pattern appears inside a larger text, how often a character occurs, whether a string is a palindrome, and how two strings compare.
Strings provide a natural way to represent this type of data. A string is an ordered sequence of characters, and it forms the basis of many important computational tasks such as text processing, pattern matching, and validation.
What is a character?
Before defining a string precisely, we need to be clear about what a string is made of. The basic building block of every string is a character.
A character is a single symbol. It can be a letter like 'a' or 'Z', a digit like '3' or '9', a space, a punctuation mark like '?' or '.', or a special symbol like '@' or '#'. Anything you can type on a keyboard is a character. In Go, a Unicode character is commonly represented as a rune; for simple ASCII examples, each character corresponds to one byte in a string.
It is important to distinguish a character from a string early on because they are two different things, even though they can look similar when written in code.
ch := 'h' // a single character (rune)s := "hello" // a string made up of 5 ASCII characters