doumicheng6732 2013-06-05 17:03
浏览 697
已采纳

Go中[0]和[:1]有什么区别?

I split a string by spaces:

splstr = strings.Split(str, " ")

Then I iterate each word, and look at the first character like this:

splstr[i][0] == "#"

But I got these errors from that line:

... : cannot convert "#" to type uint8

... : invalid operation: splstr[i][0] == "#" (mismatched types uint8 and string)

But then I spliced it:

splstr[i][:1] == "#"

And that works. I get why [:1] is of type string, but why is [0] of type uint8? (I'm using Go 1.1.)

  • 写回答

2条回答 默认 最新

  • doubengman2072 2013-06-05 17:28
    关注

    Because the array notation on a string gives access to the string's bytes, as documented in the language spec:

    http://golang.org/ref/spec#String_types

    A string's bytes can be accessed by integer indices 0 through len(s)-1.

    (byte is an alias for uint8)

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料