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 fix race condition using Atomic Functions in Golang?Various examples of Carbon date-time package in GolangHigher Order Functions in GolangHow to update content of a text file?How to create a photo gallery in Go?Golang HTTP GET request with parametersRegular expression to validate phone numberConstructors in GolangHow to compare equality of struct, slice and map?Convert Int data type to Int16 Int32 Int64