Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 打印/访问超出范围的切片索引时无死机_Go_Slice - Fatal编程技术网

Go 打印/访问超出范围的切片索引时无死机

Go 打印/访问超出范围的切片索引时无死机,go,slice,Go,Slice,在上面提到的程序中,即使我们从is[2]访问元素到on wards,并且切片只有2个元素,is[2:]也不会惊慌失措。为什么会这样?详细说明了切片中使用的索引要求: 如果0“为什么是这样?”切片不是索引,则索引在范围内是[2:][/code>不是“访问”是[2]@mkopriva是的,但是不允许从索引2进行切片,因为它不存在。让我重新表述一下,is[2:][/code>表示从is[2]到is[]结尾的元素。 package main import "fmt" func main() {

在上面提到的程序中,即使我们从is[2]访问元素到on wards,并且切片只有2个元素,is[2:]也不会惊慌失措。为什么会这样?

详细说明了切片中使用的索引要求:


如果0“为什么是这样?”切片不是索引,则索引在范围内<代码>是[2:][/code>不是“访问”
是[2]
@mkopriva是的,但是不允许从索引2进行切片,因为它不存在。让我重新表述一下,
is[2:][/code>表示从
is[2]
is[]
结尾的元素。
package main

import "fmt"

func main() {
    is := []int{1, 2}

    fmt.Println(is[2:]) // no panic here - this includes is[2] which is out of bound still no panic
    fmt.Println(is[3:]) // but panic here
    fmt.Println(is[2]) // panic here which is acceptable
}