首页 > 文章列表 > 如何将unicode代码点转换为Go 中的文字字符?

如何将unicode代码点转换为Go 中的文字字符?

golang
317 2023-05-31

问题内容

如何将将 unicode 代码点转换为 Go 中的文字字符?

正确答案

可以使用rune类型来表示Unicode代码点,并将其转换为对应的文字字符。

以下是一个示例代码,展示了如何将Unicode代码点转换为Go中的文字字符:

package main

import (
	"fmt"
)

func main() {
	codePoint := 65 // Unicode代码点

	character := rune(codePoint) // 将代码点转换为rune类型

	fmt.Printf("Unicode代码点: %dn", codePoint)
	fmt.Printf("对应的文字字符: %cn", character)
}

在上述示例中,我们将Unicode代码点65(对应大写字母'A')转换为rune类型,然后使用%c格式化符打印出对应的文字字符。

运行上述代码将输出:

Unicode代码点: 65
对应的文字字符: A