How to write backslash in Golang string?
Backslash \ works as an escape sequence character in Golang. There are two different methods to write backslash in a Golang string.
Double Backslash
To print backslash, just need to type the backslash twice. Hence, Go interpreter treat it as a single backslash character instead of escape sequence character.
Example
package main
import "fmt"
func main() {
fmt.Println("Golang\\Java")
fmt.Println("Golang\\\\Java")
}
Output
Golang\Java
Golang\\Java
Raw String Lateral
In below example, a raw string lateral ` used to write a backslash in the string. If you want to write a single or multiple backslashes in the string, you can do so by writing the desired string within raw string literals ` as shown in this example.
Example
package main
import "fmt"
func main() {
fmt.Println(`\Golang\Java\`)
fmt.Println(`Golang\\Java`)
}
Output
\Golang\Java\
Golang\\Java
Most Helpful This Week
How to declare Interface Type in Go Programming Language
Convert Float32 to Float64 and Float64 to Float32
Program in Go language to print Pascal's Triangle
Golang program for implementation of Linked List
Missing return at end of function error in Golang
How do you handle HTTP Client server load balancing in Go?
Most Helpful This Week
Various examples of Carbon date-time package in GolangHow to check pointer or interface is nil?Example: Stack and Caller from RUNTIME packageSample program to create csv and write dataDifferent ways to convert Byte Array into StringExample of Sscan vs Sscanf vs Sscanln from FMT PackageVarious examples of printing and formatting in GolangGolang Read Write and Process data in CSVHow to rotate an image?Example of Fscan, Fscanf, and Fscanln from FMT Package