Golang 演示转义序列字符
在Go编程语言中,有几种类型的特殊转义序列字符。每种字符的使用实现了不同类型的功能。在接下来的示例中,我们将逐一讨论它们。
示例1
在这个示例中,我们将编写一个Go语言程序来演示反斜杠(//)字符。它用于在程序输出中显示斜杠。
package main
import "fmt"
func main() {
fmt.Println("Program to display a backslash using in the program")
}
输出
Program to display a backslash using in the program\
示例2
在这个示例中,我们将编写一个Go语言程序来演示ASCII响铃字符。它由\a转义字符字面值显示。
package main
import "fmt"
func main() {
fmt.Println("Go landuage program to display an ASCII bell (BEL): \a")
}
输出
Go landuage program to display an ASCII bell (BEL):
示例3
在此示例中,我们将编写一个Go语言程序来演示换行符。它使用\n转义字符字面量显示,并用于开始新行。在此字符之后写入的字符串/数字将显示在新的一行中。
package main
import "fmt"
func main() {
fmt.Println("Go landuage program to show a line feed. \nThis string will be displayed in a new line.")
}
输出
Go landuage program to show a line feed.
This string will be displayed in a new line.
示例4
在这个示例中,我们将编写一个Go语言程序来演示回车换行。它通过\r转义字符字面量来显示,并用于开始新的一行。换行和回车的区别在于回车字符在新行的起始字符处留下一个小空格。
package main
import "fmt"
func main() {
fmt.Println("Go languaage program to display carriage return (CR): \r This is second statement and appears in the new line.")
}
输出
Go languaage program to display carriage return (CR):
This is second statement and appears in the new line.
示例5
在这个示例中,我们将编写一个Go语言程序来演示水平制表符。它由\t转义字符文字显示,并用于在显示的数据后面给出一个空格。
package main
import "fmt"
func main() {
fmt.Println("Go language program to display ASCII horizontal tab (TAB): \n This is second statement and appears after a space")
}
输出
Go language program to display ASCII horizontal tab (TAB):
This is second statement and appears after a space
示例6
在这个示例中,我们将编写一个 Go 语言程序来演示双引号。它是通过 \” 转义字符字面量来显示的,并且用于封装在其中输入的双引号中的数据。
package main
import "fmt"
func main() {
fmt.Println("Golang program to display Double quote string literal (∖"this will come in quotations ∖"): ")
}
输出
Golang program to display Double quote string literal ("this will come in quotations"):
示例7
在这个示例中,我们将编写一个Go语言程序来演示Unicode转义序列字符。它由\u显示,并用于在程序中包含十六进制数字。
package main
import "fmt"
func main() {
fmt.Printf("Go language program to display a Unicode code point (Heart): %c", '\u2665')
}
输出
Go language program to display a Unicode code point (Heart): ♥
示例8
在这个示例中,我们将编写一个Go语言程序来演示十六进制转义序列字符。 它由\x显示,用于使用其十六进制代码代表一个字符。
package main
import "fmt"
func main() {
fmt.Printf("Go language program to show Hexadecimal escape sequence character (Star): %c", '\x2a')
}
输出
Go language program to show Hexadecimal escape sequence character (Star): *
结论
我们已成功编译和执行了一个Go语言程序,以演示转义序列字符及其示例。本文展示了不同的转义序列文字字符以及它们的用法。