Grokking Go
Vote

0% completed

​

Strings in Go

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

Go
Go

. . . .

Explanation

  • greeting is explicitly declared as a string.
  • name uses type inference, meaning Go automatically sets it as a string.

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

Go
Go

. . . .

Explanation

  • The + operator joins firstName, a space (" "), and lastName to form "John Doe".

Getting String Length

Use the built-in len() function to get the length of a string.

Example: Finding String Length

Go
Go

. . . .

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

Go
Go

. . . .

Explanation

  • text[0] gets the first character (G).
  • text[len(text)-1] gets the last character (g).
  • The string() function is used because text[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

Go
Go

. . . .

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 CharacterDescriptionExample Output
\nNew Line"Hello\nGo" → Hello (new line) Go
\tTab 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

Go
Go

. . . .

Explanation

  • \n moves 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%


Vote for new content

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