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 program to reading plain text files
Unveiling the Potential of Quantum Computing in AI: A Game Changer for Industries
Naming Conventions for Golang Functions
How to check specific field exist in struct?
Defining a type that satisfies an interface in Go Programming Language
Golang program for implementation of Tower of Hanoi Algorithm
How do you read headers from an HTTP response with an HTTP client in Go?
Golang program for implementation of Merge Sort
How to declare Interface Type in Go Programming Language
Cannot use <variable> as <type> value in return statement error in Golang
Most Helpful This Week
How to use array in Go Programming Language?Regular expression to validate the date format in "dd/mm/yyyy"How to kill execution of goroutine?Convert Int data type to Int16 Int32 Int64Convert Float32 to Float64 and Float64 to Float32Sierpinski triangle in Go Programming LanguageExample: Fields and FieldsFunc from BYTES PackageExample: Arrays of Arrays, Arrays of Slices, Slices of Arrays and Slices of SlicesPassing multiple string arguments to a variadic functionRegular expression to extract domain from URL