Skip to content

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

@marjune163

Description

@marjune163

在用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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions