Open In App

How to write backslash in Golang string?

Last Updated : 04 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In most of the programming languages, Backslash (\) works as an escape sequence character and the same is the case with Golang. Thus, if a user needs to write backslash in a string, he can make use of the two common methods.

1. A user can use another backslash just before the backslash (\) he/she wants to display.

Example:




// Printing backslash in Golang string
package main
  
import "fmt"
  
func main() {
  
    // using another backslash
    fmt.Println("\\Hello, Welcome to GeeksforGeeks")
}


Output:

\Hello, Welcome to GeeksforGeeks

Explanation: Suppose, the user wants to write backslash at the starting of the string. This is done by using an escape operator “\” just before the desired backslash as shown in the above example. In this manner, the user can insert the backslash at any desired position in the string.

2. Another approach is to use a raw string literal (`).




// Printing backslash in Golang string
package main
  
import "fmt"
  
func main() {
  
    // using raw string lateral (`)
    fmt.Println(`\Happy Learning\`)
}


Output:

\Happy Learning\

Explanation: In this example, we used a raw string lateral (`) to write a backslash in the string. If a user wants to write a string in between the backslashes, he can do so by writing the desired string within raw string literals (`) as shown in this example.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads