我用空格分割一个字符串:
splstr = strings.Split(str, " ")
然后我遍历每个单词,然后像这样查看第一个字符:
splstr[i][0] == "#"
但是我从那一行得到了这些错误:
... : cannot convert "#" to type uint8
... : invalid operation: splstr[i][0] == "#" (mismatched types uint8 and string)
但后来我拼接了它:
splstr[i][:1] == "#"
那行得通。我明白为什么 [:1]
是 string
类型,但为什么 [0]
是 uint8
类型? (我正在使用 Go 1.1。)
最佳答案
因为字符串上的数组符号可以访问字符串的字节,如语言规范中所述:
http://golang.org/ref/spec#String_types
A string's bytes can be accessed by integer indices 0 through len(s)-1.
(byte是uint8的别名)
关于go - [0] 和 [ :1] in Go? 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16945816/