在Go语言中如何编写超过0xFFFF的Unicode字面量?

huangapple go评论70阅读模式
英文:

Writing Unicode literal above 0xFFFF in Go?

问题

在Go语言中,你可以使用\u后跟四个十六进制数字来编码Unicode代码点。在下面的示例中,我将黑桃A的Unicode代码以十六进制表示为\u1F0A1,但是当我打印它时,它显示为Ἂ1。为什么会这样呢?如果我复制并粘贴黑桃A的图形,它会正确打印出来。

package main

import "fmt"

func main() {
    fmt.Println("🂡 \u1F0A1")
}

输出结果:

🂡 Ἂ1

以上示例在Go Playground中的链接:https://play.golang.org/p/ukK57CnVuE

英文:

How do you encode unicode code points in Go? In the example below I'm storing the hex representation of the unicode for ace of spades as \u1F0A1 but when I print it comes out as Ἂ1. Why is that? If I copy and paste the ace of spades glyph it prints fine.

package main

import "fmt"

func main() {
	fmt.Println("🂡 \u1F0A1")
}

Output

🂡 Ἂ1

Example above in the Go playground https://play.golang.org/p/ukK57CnVuE

答案1

得分: 7

小写的\u用于表示Unicode代码点范围在\u0000\uFFFF之间的字符。如果你想表示超过0xFFFF的代码点,可以使用\U

package main

import "fmt"

func main() {
    fmt.Println("🄱 = \U0001F0B1")
}

参考资料:playground规范中的字符串字面量部分

英文:

Lowercase \u is for Unicode code points from \u0000 to \uFFFF. Use \U if you want to have code points above 0xFFFF:

package main

import "fmt"

func main() {
	fmt.Println("🂡 = \U0001F0A1")
}

See also: playground and the string literals section of the specification.

huangapple
  • 本文由 发表于 2015年2月13日 00:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/28482562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定