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
Regular expression to extract numbers from a string in GolangSimple function with return value in GolangGolang String ConcatenationRegular expression to extract DNS host-name or IP Address from stringHow to read names of all files and folders in current directory?How to set timeout for http.Get() requests in Golang?What is Rune? How to get ASCII value of any character in Go?How to use a mutex to define critical sections of code and fix race conditions?Add N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-timeRegular expression to extract domain from URL