Strings in Go

I am a self-taught Fullstack Web and Mobile App developer with over 4 years of experience. I have built a few apps using React and React Native with a backend in Nest.JS and Express.JS
Search for a command to run...

I am a self-taught Fullstack Web and Mobile App developer with over 4 years of experience. I have built a few apps using React and React Native with a backend in Nest.JS and Express.JS
No comments yet. Be the first to comment.
In this series, I will write articles that help you understand go! I am learning go so I am writing this series as a note for myself and hope you also love this series and learn along with me!
In this guide, we'll explore how to work with strings and the strconv package in Go, covering useful functions that help manipulate and convert strings. Common String Operations Go provides several built-in functions in the strings package that allow...
In this lesson, we’ll explore how pointers work in Go, which is different from languages like Java and .NET. Go gives programmers control over memory allocation and layout, though it doesn’t support pointer arithmetic (e.g., adding to or subtracting ...

The time package offers the Time datatype, which represents an instant in time. It also includes several functions for extracting and formatting different components of a timestamp. For example, you can get the current time using time.Now(), and then...

In this guide, we'll explore how to work with strings and the strconv package in Go, covering useful functions that help manipulate and convert strings. Common String Operations Go provides several built-in functions in the strings package that allow...

In Go, operators are symbols used to perform operations, such as arithmetic, logical, or bitwise operations. This lesson will cover different types of operators and their behaviour, along with examples and ASCII diagrams where needed. Arithmetic Ope...

In this lesson, we'll dive deep into the string datatype in Go. Strings are a sequence of UTF-8 characters. Go efficiently handles string memory by reserving only 1 byte for ASCII characters when possible, and between 2-4 bytes for UTF-8 characters when necessary. UTF-8 is the most common text encoding, and it is used widely across formats like text files, XML, and JSON.
Unlike other languages like C++, Java, or Python that use fixed-width characters (e.g., Java always uses 2 bytes for characters), Go strings are made up of variable-width characters. Each character in a Go string can be between 1 and 4 bytes long.
Memory Efficiency: Go strings take up less memory and disk space due to the use of variable-width characters.
UTF-8 Standard: Go uses UTF-8 as the default encoding, avoiding the need to convert between different string encodings.
Immutability: Strings in Go are immutable, meaning once created, their contents cannot be changed. They are essentially immutable arrays of bytes.
Go supports two types of string literals:
Interpreted Strings: These are enclosed in double quotes ("") and interpret escape sequences like \n, \r, \t, and Unicode characters (\u, \U). For example:
"\n" // Newline
"\t" // Tab
Raw Strings: Enclosed in backticks (``````), raw strings do not interpret any escape sequences. For example:
`This is a raw string \n`
In this case, \n is treated literally and not as a newline. Raw strings can also span multiple lines.
Unlike C/C++, Go strings do not end with a special terminating character. The default value of a string is an empty string "". Go allows standard comparison operators (==, !=, <, <=, >=, >) for string comparisons, which are done byte by byte.
You can find the length (in bytes) of a string using the len() function:
len(str)
String indexing starts at 0, meaning the first character can be accessed with str[0]. For example, with the string "Hello", you can access characters like this:
H = str[0]
e = str[1]
The last character can be accessed with str[len(str)-1]. However, note that Go doesn't allow taking the address of a character in a string.
Go allows the concatenation of two or more strings using the + operator. For example:
s1 := "Hello"
s2 := "World"
s := s1 + s2 // Result: "HelloWorld"
To create multi-line strings, you can split them across lines, but ensure that the + operator stays at the end of the first line:
str := "This is the first part " +
"and this is the second part"
Alternatively, you can use the shorthand += to append strings:
s1 += s2 // s1 becomes "HelloWorld"
This covers the basics of strings in Go, including their structure, types, and operations.
In the next lesson, you'll practice by writing a function to solve a problem using Go strings.