During the programming process, it is often necessary to escape some special characters to achieve the desired effect. For Golang developers, escaping strings is also one of the essential skills.
Golang provides some built-in escape functions, such as fmt.Printf() and strconv.Quote(), etc. These functions allow us to easily escape strings. Next, let’s take a closer look at Golang’s escape function.
fmt.Printf() function is one of the most common character escape functions in Golang. By using escape characters, we can output some special characters in the console. For example, we can use \n to print out the newline character in the console for line breaks.
package main import "fmt" func main() { fmt.Printf("Hello\nWorld") }
The above sample code will output the following content:
Hello World
fmt.Printf() and some other commonly used escape characters, such as \t represents tab character, \r represents return Che Fu etc. We can view all escape characters in [Official Documentation](https://golang.org/pkg/fmt/).
Another common string escape function is strconv.Quote(). This function can escape a string into a string enclosed in double quotes.
package main import ( "fmt" "strconv" ) func main() { fmt.Println(strconv.Quote(`"Go is awesome!"`)) }
The output result of the above code is:
"\"Go is awesome!\""
This function converts the string "Go is awesome!"
into "\"Go is awesome!\ ""
, with double quotes escaped.
Similar to the previous function, strconv.QuoteToASCII() can also escape a string into a string enclosed in double quotes. The difference is that this function can convert non-ASCII characters into their corresponding escape sequences.
package main import ( "fmt" "strconv" ) func main() { fmt.Println(strconv.QuoteToASCII("Golang是一个牛逼的编程语言!")) }
The output result of the above code is:
"Golang\u662f\u4e00\u4e2a\u725b\u903c\u7684\u7f16\u7a0b\u8bed\u8a00\uff01"
This function converts a string into a stringGolang is an awesome programming language!
Converted to "Golang\u662f\u4e00\u4e2a\u725b\u903c\u7684\u7f16\u7a0b\u8bed\u8a00\uff01"
, where each non-ASCII character is converted to its corresponding Escape sequences, such as Chinese characters, are converted into Unicode codes starting with \u
.
Similar to the strconv.QuoteToASCII() function, strconv.QuoteToGraphic() can also escape a string into a string enclosed in double quotes. The difference is that this function converts non-printable ASCII characters to their corresponding escape sequences.
package main import ( "fmt" "strconv" ) func main() { fmt.Println(strconv.QuoteToGraphic("这个字符串中有不可打印字符\x1f")) }
The output result of the above code is:
"这个字符串中有不可打印字符\u001f"
The unprintable characters \x1f
in the string are converted to \u001f
.
In Golang, the escape function is very practical, it can help us deal with different types of special characters. In this article, we took a detailed look at the built-in escaping functions fmt.Printf(), strconv.Quote(), strconv.QuoteToASCII(), and strconv.QuoteToGraphic(). These functions allow us to operate strings more conveniently during the programming process and improve programming efficiency.
The above is the detailed content of Learn more about several escape functions in Golang. For more information, please follow other related articles on the PHP Chinese website!