Go遍历uint8切片?

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

Go range over uint8 slice?

问题

这是由于我的一个愚蠢错误,但是这个问题值得保留,以防其他人也犯同样的错误。

我希望这样能够工作:

var xs []uint8
var x uint8
for x = range xs {

}

但是我得到了错误:

cannot assign type int to x (type uint8) in range

也就是说(据我理解),即使它在一个 uint8 的切片上操作,range 仍然试图使用 int 作为迭代的值。

我已经查阅了语言规范,相关部分如下:

Range expression                          1st value          2nd value (if 2nd variable is present)

array or slice  a  [n]E, *[n]E, or []E    index    i  int    a[i]       E

所以我期望它应该是 E 这个“参数化类型”,在这种情况下是 uint8 而不是 int

我是否理解错了?是否有我错过的文档部分?或者我真的不能用 uint8 变量迭代遍历一个 uint8 切片吗?

*我知道这不是真正的泛型参数化类型。

英文:

EDIT This was due to a stupid error in my part, but the question is worth keeping around in case someone else does this.

I would hope that this works:

var xs []uint8
var x uint8
for x = range xs {

}

But I get the error:

cannot assign type int to x (type uint8) in range

i.e. (as I understand it) the range, even though it is operating on a slice of uint8, is trying to use int as the iterated value.

I have looked through the language spec, the relevant bit:

Range expression                          1st value          2nd value (if 2nd variable is present)

array or slice  a  [n]E, *[n]E, or []E    index    i  int    a[i]       E

so I would expect it to be E the 'parameterised type'*, in this case uint8 not int.

Have I grasped onto the wrong end of the stick? Is there a bit of documentation that I missed? Or can I really not iterate over a uint8 slice with a uint8 variable?

*I know it's not really a generic parameterised type.

答案1

得分: 6

range返回一个集合中的索引,并可能返回该位置上的值:

> range关键字 range关键字允许您遍历类似数组或映射的列表项。为了理解它,您可以将range关键字翻译为对于每个索引。当与数组和切片一起使用时,它返回该项的整数索引。
(教程)

示例代码:

package main

import "fmt"

func main() {
    var xs []uint8 = []uint8{255, 254, 253}
    var idx int
    var ui8 uint8
    for idx, ui8 = range xs {
        fmt.Println(idx, ui8)
    }
}

输出:

0 255
1 254
2 253

附言:

也许看一下JavaScript会让Go的方法变得不那么奇怪:

% a = [255, 254, 253]
255,254,253
% for (x in a) { print(x, " ", a[x]); }
0 255
1 254
2 253
英文:

range returns an index into a collection and possibly the value at that position:

> range keyword The range keyword allows you to iterate over items of a
> list like an array or a map. For understanding it, you could translate
> the range keyword to for each index of. When used with arrays and
> slices, it returns the integer index of the item.
(tutorial)

sample code:

package main

import "fmt"

func main() {
	var xs []uint8 = []uint8{255, 254, 253}
	var idx int
	var ui8 uint8
	for idx, ui8 = range xs {
		fmt.Println(idx, ui8)
	}
}

output:

0 255
1 254
2 253

P.S.:

Perhaps a look at Javascript will make Go's approach less strange:

% a = [255, 254, 253]
255,254,253
% for (x in a) { print(x, " ", a[x]); }
0 255
1 254
2 253

huangapple
  • 本文由 发表于 2013年1月6日 21:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/14182756.html
匿名

发表评论

匿名网友

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

确定