Sling Academy
Home/Golang/Escape Sequences in Go Strings: What You Should Know

Escape Sequences in Go Strings: What You Should Know

Last updated: November 24, 2024

Strings are an integral part of any programming language. In Go, strings bring powerful capabilities, but there is one aspect that sometimes confuses developers, especially newcomers — escape sequences. This article dives into escape sequences in Go strings, illustrating their usage with examples ranging from basic to advanced.

Introduction to Escape Sequences

Escape sequences are special characters used within strings to allow for the inclusion of complex or otherwise non-printable text. They start with a backslash (\) followed by a specific character or sequence of characters.

The Basic Escape Sequences

Let's start with the basics. Here are some commonly used escape sequences in Go:

  • \n: Newline
  • \t: Horizontal tab
  • \": Double quote
  • \\: Backslash
  • \': Single quote
package main

import "fmt"

func main() {
    fmt.Println("Hello,\nWorld!") // Newline
    fmt.Println("\tIndented text") // Tabs
    fmt.Println("Quote:\"This is in quotes\"") // Double quotes
}

Intermediate Usage of Escape Sequences

Once you are comfortable with the basics, it is time to explore more advanced usages of escape sequences in Go.

Using Raw Strings

Go also supports raw string literals, which can span multiple lines and you do not need to worry about escape sequences within them.

package main

import "fmt"

func main() {
    rawString := `Hello,
World!
This text is "raw" and spans multiple lines.`
    fmt.Println(rawString)
}

Unicode Characters

To include Unicode characters within a string, use the \u followed by the Unicode code.

package main

import "fmt"

func main() {
    fmt.Println("Snowman: \u2603") // Unicode for Snowman
}

Advanced Escape Sequences

For those looking to fully leverage escape sequences, you can write and work with more complex situations, especially when dealing with raw data and file I/O operations.

Hexadecimal and Octal Escapes

Go allows the use of hexadecimal and octal values to represent characters in strings:

package main

import "fmt"

func main() {
    // Hexadecimal escape sequence
    fmt.Println("Hello\x20World") // \x20 is Space in hexadecimal

    // Octal escape sequence
    fmt.Println("A\101B\102C") // 101 is 'A', 102 is 'B'
}

Larger Unicode Adoptations

Beyond basic Unicode, you can represent characters using longer Unicode codes.

package main

import "fmt"

func main() {
    fmt.Println("Face: \U0001F600") // Unicode Emoji: Grinning Face
}

Escape sequences are a key part of working with strings in Go. Mastering them provides greater flexibility and power when manipulating string outputs in your programs.

Next Article: Handling Multiline Strings in Go with Raw String Literals

Previous Article: Trimming and Replacing Characters in Strings Using Go

Series: Working with Strings in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant