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 Make an HTTP Server in Golang?
How do you handle HTTP client server compression in Go?
Multiple-value <function> in single-value context error in Golang
How do you handle HTTP Client server load balancing in Go?
React JS Count number of checkboxes are checked
How do you set headers in an HTTP response in Go?
GO language program with example of String Compare function
Type assertion in Go Programming Language
GO Program to Generate Fibonacci Sequence Up to a Certain Number
How to set timeout for http.Get() requests in Golang?
Most Helpful This Week
Data encryption with AES-GCMConvert specific UTC date time to PST, HST, MST and SGTHow to print string with double quote in Go?Catch values from GoroutinesHow to read/write from/to file in Golang?How to collect information about garbage collection?How to replace emoji characters in string using regex in Golang?How to Draw a rectangle in Golang?Convert Float32 to Float64 and Float64 to Float32How to get Dimensions of an image type jpg jpeg png or gif ?