golang数组引用示例。例如,b[1:4]引用了元素1、2、3。

huangapple go评论99阅读模式
英文:

golang array referencing eg. b[1:4] references elements 1,2,3

问题

Golang博客中提到:

“切片也可以通过“切片”现有的切片或数组来形成。切片是通过用两个索引用冒号分隔的半开范围来完成的。例如,表达式b[1:4]创建一个包含b的第1到第3个元素的切片(结果切片的索引将是0到2)。”

有人可以解释一下上述逻辑吗?也就是说,为什么b[1:4]不引用第1到第4个元素?这与其他数组引用一致吗?

英文:

The golang blog states :

"A slice can also be formed by "slicing" an existing slice or array. Slicing is done by specifying a half-open range with two indices separated by a colon. For example, the expression b[1:4] creates a slice including elements 1 through 3 of b (the indices of the resulting slice will be 0 through 2)."

Can someone please explain to me the logic in the above. IE. Why doesn't b[1:4] reference elements 1 through 4? Is this consistent with other array referencing?

答案1

得分: 9

索引指向元素的“开始”。所有使用从零开始索引的语言都共享这一点:

       | 0 | 第一 | 1 | 第二 | 2 | 第三 | 3 | 第四 | 4 | 第五 | 5 |
[0]   =  ^
[0:1] =  ^ --------> ^
[1:4] =              ^-------------------------------------> ^
[0:5] =  ^ ----------------------------------------------------------> ^

支持负索引也很常见,尽管Go不允许这样做:

       |-6 |       |-5 |        |-4 |       |-3 |        |-2 |       |-1 |
       | 0 | 第一 | 1 | 第二 | 2 | 第三 | 3 | 第四 | 4 | 第五 | 5 |
英文:

Indexes point to the "start" of the element. This is shared by all languages using zero-based indexing:

       | 0 | first | 1 | second | 2 | third | 3 | fourth | 4 | fifth | 5 |
[0]   =  ^
[0:1] =  ^ --------> ^
[1:4] =              ^-------------------------------------> ^  
[0:5] =  ^ ----------------------------------------------------------> ^

It's also common to support negative indexing, although Go doesn't allow this:

       |-6 |       |-5 |        |-4 |       |-3 |        |-2 |       |-1 |
       | 0 | first | 1 | second | 2 | third | 3 | fourth | 4 | fifth | 5 |

答案2

得分: 4

半开区间在很多情况下都是有意义的,当你深入了解时会发现。例如,对于这样一个半开区间,元素的数量是:

n = end - start

这是一个相当简单和容易记忆的公式。对于一个闭区间,公式将变为:

n = (end - start) + 1

这个公式(虽然不复杂,但仍然)更加复杂。

这也意味着对于例如字符串,整个字符串是[1, len(s)],这也是直观的。如果区间是闭合的,要获取整个字符串,你需要[1, len(s) + 1]

英文:

Half-open intervals make sense for many reasons, when you get down to it. For instance, with a half-open interval like this, the number of elements is:

n = end - start

which is a pretty nice and easy formula. For a closed interval, it would be:

n = (end - start) + 1

which is (not a lot, but still) more complicated.

It also means that for e.g. a string, the entire string is [1, len(s)] which also seems intuitive. If the interval was closed, to get the entire string you would need [1, len(s) + 1].

答案3

得分: 4

原因在Go语言规范的Slices部分中给出。

> 对于字符串、数组或切片a,主要表达式

> a[low : high]

> 构造一个子字符串或切片。索引表达式low和high选择出现在结果中的元素。结果的索引从0开始,长度等于high - low。

> 为了方便起见,任何索引表达式都可以省略。缺少的低索引默认为零;缺少的高索引默认为切片操作数的长度。

计算切片的长度为high - low非常简单和高效。

英文:

The reason is given in the Go Language Specification section on Slices.

> For a string, array, or slice a, the
> primary expression
>
> a[low : high]
>
> constructs a substring or slice. The
> index expressions low and high select
> which elements appear in the result.
> The result has indexes starting at 0
> and length equal to high - low.
>
> For convenience, any of the index
> expressions may be omitted. A missing
> low index defaults to zero; a missing
> high index defaults to the length of
> the sliced operand.

It's easy and efficient to calculate the length of the slice as high - low.

答案4

得分: 2

Go使用半开区间(half-open intervals)来表示切片,就像许多其他语言一样。在更数学化的表示法中,切片b[1:4]表示的是区间[1,4),不包括上限点。

英文:

Go uses half-open intervals for slices like many other languages. In a more mathematical notation, the slice b[1:4] is the interval [1,4) which excludes the upper endpoint.

huangapple
  • 本文由 发表于 2011年5月3日 19:42:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/5868927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定