Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Write to write or print backslash in a string in Go (Golang)

Posted on April 7, 2023April 7, 2023 by admin

Table of Contents

  • Overview
  • Program for double quotes
  • Program for back quotes

Overview

Backlash is an escaping character. To print a backslash we need to first escape is with another backslash character when using double-quotes. However, a backslash can also be printed using backquotes. It is also used to define a string. A string encoded in backquotes is a raw literal string and doesn’t honor any kind of escaping.

Program for double quotes

package main
import "fmt"
func main() {
fmt.Println("\\test")
}

Output

\test

A string defined within double quotes will honour escaping characters. That is why we need to escape backlash. Let’s see a program for back quotes

Program for back quotes

package main
import "fmt"
func main() {
fmt.Println(`\test`)
}

Output

\test

In this case, we don’t need any escaping as with back quotes a string is a raw literal string

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP