Go語言程式演示跳脫字元


在 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 語言程式來演示跳脫字元以及示例。在本文中,我們展示了不同的跳脫字元字面量及其用法。

更新於: 2023年2月13日

1K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.