Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于书中for...range <string>语法第二个返回值的说明似乎有误 #6

Open
marjune163 opened this issue Aug 16, 2020 · 0 comments

Comments

@marjune163
Copy link

marjune163 commented Aug 16, 2020

在用for...range迭代string时,range返回的第二个值是rune类型,其内容为对应字符的(完整的)Unicode码点。不过书中一直说是Unicode编码的首字节,而不是全部。

Go语言规范中的说法也是Unicode码点:https://golang.org/ref/spec#For_range

For a string value, the "range" clause iterates over the Unicode code points in the string starting at byte index 0. On successive iterations, the index value will be the index of the first byte of successive UTF-8-encoded code points in the string, and the second value, of type rune, will be the value of the corresponding code point.

书中涉及之处有:

2.2.2节-3

第78页

range作用于string时……元素值是该字符对应的Unicode编码的首个字节的值

image

第79页

range的第二个返回值的类型为rune类型,它仅代表Unicode编码的1个字节

image

2.2.3节-3

第83页

返回的元素值为rune类型的单个字节值……该字节值仅代表Unicode编码的首个字节

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant