Golang中的'range str'和'range [] rune(str)'之间有什么区别 [英] Is there any difference between 'range str' and 'range []rune(str)' in golang

查看:94
本文介绍了Golang中的'range str'和'range [] rune(str)'之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

范围str https://play.golang.org/p/I1JCUJnN41h

范围[] rune(str) https://play.golang.org/p/rJvyHH6lkl_t

range []rune(str) https://play.golang.org/p/rJvyHH6lkl_t

我得到了相同的结果,是否相同?哪个更好?

I got the same results, are they the same? which one is better?

推荐答案

是的.给定

for i, c := range v {

无论 v 是字符串还是符文切片,

c 都相同,但是如果字符串包含多字节字符,则 i 会有所不同.

c will be the same whether v is a string or a rune slice, but i will vary if the string contains multibyte characters.

字符串是字节序列,索引适用于字节片.除非您有意读取或操作字节而不是代码点或字符,或者确保您的输入不包含多字节字符,否则无论您打算索引字符串的哪个位置,都应改用符文切片.

Strings are sequences of bytes and indexing is appropriate to a slice of bytes. Unless you are intentionally reading or manipulating bytes instead of code points or characters, or are sure your input contains no multibyte characters, wherever you are inclined to index a string you should use a rune slice instead.

for i, c := range str {

字符串的范围循环是特殊的. range 不会将字符串简单地视为字节的一部分,而是将字符串部分地视为字节的一部分,而部分地将其视为符文的一部分.

Range loops over strings are special. Instead of treating the string simply as a slice of bytes, range treats the string partly like a slice of bytes and partly like a slice of runes.

i 将是代码点开头的字节索引. c 将是一个可包含多个字节的符文.这意味着 i 可以在迭代中增加一个以上,因为先前的代码点是一个多字节字符.

The i will be the byte index of the beginning of the code point. The c will be a rune that can contain more than one byte. This means i can increase by more than one in an iteration because the prior code point was a multibyte character.

除了Go的源代码是UTF-8的公理细节之外,Go确实只有一种特别对待UTF-8的方式,那就是在字符串上使用for range循环时.我们已经看到了常规的for循环会发生什么.相比之下,for范围循环在每次迭代时解码一个UTF-8编码的符文.每次循环时,循环索引都是当前符文的起始位置(以字节为单位),代码点是其值.

Besides the axiomatic detail that Go source code is UTF-8, there's really only one way that Go treats UTF-8 specially, and that is when using a for range loop on a string. We've seen what happens with a regular for loop. A for range loop, by contrast, decodes one UTF-8-encoded rune on each iteration. Each time around the loop, the index of the loop is the starting position of the current rune, measured in bytes, and the code point is its value.

在Go官方博客中查看更多内容,以上摘录自: Go中的字符串,字节,符文和字符

See more in the official Go Blog post the above is excerpted from: Strings, bytes, runes and characters in Go

这篇关于Golang中的'range str'和'range [] rune(str)'之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆