slice

package main

import (
    "fmt"
    "unsafe"
)

type Slice struct {
    ptr unsafe.Pointer
    len int
    cap int
}

func printSlice(name string, s []int) {
    myS := (*Slice)(unsafe.Pointer(&s))
    fmt.Printf("name:%+v\n", myS)
}

func main() {
    s1 := make([]int, 6)
    fmt.Printf("s1=%+v len=%d cap=%d\n", s1, len(s1), cap(s1))
    printSlice("s1", s1)

    s2 := s1[2:3]
    printSlice("s2", s2)
    
    s3 := s1[3:6:15]
    printSlice("s3", s3)

    fmt.Println("vim-go")
}

s3:=s1[2:3:4] s3的ptr指向s1底层array的2号位置,长度为3-2=1,cap的结尾指向s1底层array的4号位置,即cap=4-2=2

你可能感兴趣的:(slice)