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 Array Reverse Sort Functions for integer and strings
How do you handle HTTP server health checks in Go?
How to convert Struct fields into Map String?
Program in Golang to print Pyramid of Numbers
How do you handle HTTP server HTTP/2 in Go?
Undefined reference to <variable/function> error in Golang
Most Helpful This Week
How can we reverse a simple string in Go?How to check UPPERCASE characters in a string in Golang?How to get first and last element of slice in Golang? How to kill execution of goroutine?Example: How to use ReadFull from IO Package in Golang?How to read/write from/to file in Golang?How to extract text from between html tag using Regular Expressions in Golang?How to check if a map contains a key in Go?How to delete or remove element from a Map?Regular expression to extract domain from URL