测试指数超出范围golang

时间:2021-09-15 16:39:32

Sorry for this noob question, but I'm not sure how I test to see if an element I'm accessing is valid for an array, consider the following contrived code:

很抱歉这个noob问题,但我不确定我如何测试我正在访问的元素是否对数组有效,请考虑以下设计代码:

func main() {
    strings := []string{"abc", "def", "ghi", "jkl"}
    for i := 0; i<5; i++ {
        if strings[i] {
            fmt.Println(strings[i])
        }
    }
}

https://play.golang.org/p/8QjGadu6Fu

https://play.golang.org/p/8QjGadu6Fu

I'm obviously going outside of the bounds, but I'm not sure how I test to prevent the error. I'm used to PHP where I would use an isset or !empty test, does go have such a thing?

我显然超出了界限,但我不确定如何测试以防止错误。我已经习惯了PHP,我会使用isset或!empty测试,确实会有这样的事情吗?

I've browsed other questions and seen the len function used, but that doesn't appear to work.

我已经浏览了其他问题并看到了使用的len函数,但这似乎不起作用。

2 个解决方案

#1


19  

In Go, this is not quite as easy as in PHP - but keep in mind that in PHP it's only easy because "indexed" arrays are actually associative arrays under the hood. Otherwise you wouldn't be able to "punch holes" into arrays by unsetting individual elements.

在Go中,这并不像在PHP中那么容易 - 但请记住,在PHP中它很容易,因为“索引”数组实际上是关联数组。否则,您将无法通过取消设置单个元素来“打孔”。

With Go arrays/slices, you have to actually check against the length of the array, as you wrote:

使用Go数组/切片,您必须实际检查数组的长度,如您所写:

if i>=0 && i<len(strings) {...}

Personally, I've yet to come across a real life situation that requires doing this - in your example, you could use range(strings) and stop worrying about indexes.

就个人而言,我还没有遇到需要这样做的现实生活情况 - 在你的例子中,你可以使用范围(字符串)并且不再担心索引。

With maps, which are the Go equivalent of PHP arrays, you can do "isset" by writing:

使用映射,它是Go等效的PHP数组,您可以通过编写“isset”:

value, isset := map[index]

if index is present in the map, value will be set appropriately and isset will be true; if not, value will be set to the zero value of the map's type and isset will be false.

如果地图中存在索引,则将适当设置值并且isset将为true;如果不是,则将值设置为地图类型的零值,并且isset将为false。

#2


1  

len() should return the number of elements of the slice.

len()应返回切片的元素数。

The length of a nil slice, map or channel is 0. (https://golang.org/ref/spec#Length_and_capacity)

零片,地图或通道的长度为0.(https://golang.org/ref/spec#Length_and_capacity)

#1


19  

In Go, this is not quite as easy as in PHP - but keep in mind that in PHP it's only easy because "indexed" arrays are actually associative arrays under the hood. Otherwise you wouldn't be able to "punch holes" into arrays by unsetting individual elements.

在Go中,这并不像在PHP中那么容易 - 但请记住,在PHP中它很容易,因为“索引”数组实际上是关联数组。否则,您将无法通过取消设置单个元素来“打孔”。

With Go arrays/slices, you have to actually check against the length of the array, as you wrote:

使用Go数组/切片,您必须实际检查数组的长度,如您所写:

if i>=0 && i<len(strings) {...}

Personally, I've yet to come across a real life situation that requires doing this - in your example, you could use range(strings) and stop worrying about indexes.

就个人而言,我还没有遇到需要这样做的现实生活情况 - 在你的例子中,你可以使用范围(字符串)并且不再担心索引。

With maps, which are the Go equivalent of PHP arrays, you can do "isset" by writing:

使用映射,它是Go等效的PHP数组,您可以通过编写“isset”:

value, isset := map[index]

if index is present in the map, value will be set appropriately and isset will be true; if not, value will be set to the zero value of the map's type and isset will be false.

如果地图中存在索引,则将适当设置值并且isset将为true;如果不是,则将值设置为地图类型的零值,并且isset将为false。

#2


1  

len() should return the number of elements of the slice.

len()应返回切片的元素数。

The length of a nil slice, map or channel is 0. (https://golang.org/ref/spec#Length_and_capacity)

零片,地图或通道的长度为0.(https://golang.org/ref/spec#Length_and_capacity)