Go slice interception rules

Array subscripts start at 0

Slices can be created based on arrays and slices. The rule of interception is left closed and right open

package main
import "fmt"

func main() {
        n := [5]int{1, 2, 3, 4, 5}

        for i:=0; i < len(n); i++ {
                fmt.Println(n[i])
        }

        //左闭右开
        n1 := n[1:]     
        fmt.Println(n1) 

        n2 := n[:4]    
        fmt.Println(n2)

}

1
2
3
4
5
[2 3 4 5]
[1 2 3 4]

Guess you like

Origin blog.csdn.net/wangchao701123/article/details/123312528