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
GO language program with example of Sort Functions for integer, strings and float64 data type
Golang Program to print Floyd's triangle
Go program to find Forward(A) record of a domain
Golang program to print all Permutations of a given string
Launches 10 Goroutines and each goroutine adding 10 values to a Channel
Golang program for implementation of Shell Sort
Most Helpful This Week
How to append text to a file in Golang?How to convert Boolean Type to String in Go?Various examples of Carbon date-time package in GolangHow to convert Go struct to JSON?Example: ReadAll, ReadDir, and ReadFile from IO PackageCreating a Function in GolangReplace any non-alphanumeric character sequences with a dash using RegexHow to write backslash in Golang string?How to Draw a rectangle in Golang?How to trim leading and trailing white spaces of a string in Golang?