0% completed
Strings in Go
On This Page
What is a String?
Declaring and Initializing Strings in Go
Syntax
Example: Declaring Strings
String Operations in Go
Concatenation (Joining Strings)
Getting String Length
Accessing Individual Characters
Multi-line Strings
Escape Characters in Strings
Strings are one of the most commonly used data types in programming. They allow you to store and manipulate text data efficiently.
What is a String?
A string is a sequence of characters enclosed in double quotes (" "). Strings in Go are immutable, meaning their values cannot be changed after they are created. Instead, any modifications result in a new string.
Declaring and Initializing Strings in Go
Syntax
var variableName string = "value"
or using type inference:
var variableName = "value"
Example: Declaring Strings
Explanation
greetingis explicitly declared as astring.nameuses type inference, meaning Go automatically sets it as astring.
String Operations in Go
In this section, we have covered multiple string operations with code examples.
Concatenation (Joining Strings)
You can join strings using the + operator.
Example: Concatenating Strings
Explanation
- The
+operator joinsfirstName, a space (" "), andlastNameto form"John Doe".
Getting String Length
Use the built-in len() function to get the length of a string.
Example: Finding String Length
Explanation
len(message)returns the number of bytes in the string, including spaces and punctuation. For plain English (ASCII) text like this, each character is one byte, so the result is also the number of characters. A character such asétakes more than one byte.
Accessing Individual Characters
Strings in Go are indexed like arrays, meaning each byte has a position starting from 0. In ASCII text like "Golang", each byte is one character.
Example: Accessing Characters
Explanation
text[0]gets the first character (G).text[len(text)-1]gets the last character (g).- The
string()function is used becausetext[index]returns a byte, not a string.
Multi-line Strings
Use backticks (`) instead of double quotes to define multi-line strings.
Example: Multi-line String
Explanation
- The backticks (
`) allow you to write strings across multiple lines without needing escape characters (\n).
Escape Characters in Strings
Escape characters help you format strings with special characters.
| Escape Character | Description | Example Output |
|---|---|---|
\n | New Line | "Hello\nGo" → Hello (new line) Go |
\t | Tab Space | "Go\tLang" → Go Lang |
\" | Double Quote | "He said \"Go is great\"" → He said "Go is great" |
\\ | Backslash | "C:\\Program Files" → C:\Program Files |
Example: Using Escape Characters
Explanation
\nmoves text to a new line.\\prints a backslash.
In the next lesson, we will cover Declaring Multiple Variables in Go, where we explore different ways to declare multiple variables in a single statement.
Reading Progress
0%
On This Page
What is a String?
Declaring and Initializing Strings in Go
Syntax
Example: Declaring Strings
String Operations in Go
Concatenation (Joining Strings)
Getting String Length
Accessing Individual Characters
Multi-line Strings
Escape Characters in Strings