dsk88199 2014-08-28 20:18
浏览 342
已采纳

无效的Unicode代码点0xd83f

I'm trying to port some Java to Go. The Java code has a character variable with the value '\ud83f'. When I try to use this value in Go, it doesn't compile:

package main
func main() {
    c := '\ud83f'
    println(c)
}
$ go run a.go
# command-line-arguments
./a.go:3: invalid Unicode code point in escape sequence: 0xd83f

Why? I also tried making a string with that value in Python and it worked too. It's just not working in Go for some reason.

  • 写回答

2条回答 默认 最新

  • dsm13698679318 2014-08-29 00:59
    关注

    That rune literal you tried to use is invalid because it denotes a surrogate code point. The spec says rune literals cannot denote a surrogate code point ("as well as others" (which?)):

    Rune Literals

    [...]

    The escapes \u and \U represent Unicode code points so within them some values are illegal, in particular those above 0x10FFFF and surrogate halves.

    Further below in the examples, you can see another case which is deemed illegal:

    '\U00110000' // illegal: invalid Unicode code point

    Which seems to imply that invalid code points (such as those above 10ffff) are also illegal in rune literals.

    Note that since rune is merely an alias for int32, you can simply do:

    var r rune = 0xd8f3
    

    instead of

    var r rune = '\ud8f3'
    

    And if you wanted to get a number above 10FFFF you could do

    var r rune = 0x11ffff
    

    instead of

    var r rune = '\U0011ffff'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?