Go 语言提供内置支持,以通过strconv Package实现基本数据类型的字符串表示的转换。这个包提供了一个QuoteRuneToGraphic()函数,用于查找代表符文的单引号 Go字符字面量。如果符文不是 IsGraphic 定义的 Unicode 图形字符,则返回的字符串将使用 Go 转义序列(\t、\n、\xFF、\u0100)。要访问QuoteRuneToGraphic()函数,您需要借助 import 关键字在程序中导入 strconv 包。
句法:
func QuoteRuneToGraphic(rn rune) string
参数:该函数接受一个符文类型的参数,即rn。
返回值:此函数返回一个单引号 Go字符串字面量,它代表符文。
让我们在给定示例的帮助下讨论这个概念:
示例 1:
// Golang program to illustrate
// strconv.QuoteRuneToGraphic() Function
package main
import (
"fmt"
"strconv"
)
func main() {
// Finding a single-quoted Go
// string literal representing rune
// Using func QuoteRuneToGraphic() function
str := strconv.QuoteRuneToGraphic('♥')
fmt.Println (str)
}
输出:
'♥'
示例 2:
// Golang program to illustrate
// strconv.QuoteRuneToGraphic() Function
package main
import (
"fmt"
"strconv"
)
func main() {
// Finding a single-quoted Go
// string literal representing rune
// Using QuoteRuneToGraphic() function
val1 := strconv.QuoteRuneToGraphic('®')
fmt.Println("Result 1: ", val1)
val2 := strconv.QuoteRuneToGraphic('\u2666')
fmt.Println("Result 2: ", val2)
val3 := strconv.QuoteRuneToGraphic('\u000a')
fmt.Println("Result 3: ", val3)
}
输出:
Result 1: '®'
Result 2: '♦'
Result 3: '\n'