[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mYga7Cz2-1656118501140)(/images/2022-06-21-10-39-25.png)]
package main
import "fmt"
func main() {
var my_array [100]int
my_slice := my_array[0:50]
fmt.Printf("Slice len = %d\n", len(my_slice))
fmt.Printf("Slice cap = %d\n", cap(my_slice))
}
输出结果
Slice len = 50
Slice cap = 100
slice := make([]int, 10)
在《Effective Go》中的解释为
The built-in function make takes a type T, which must be a slice, map or channel type, optionally followed by a type-specific list of expressions.
It returns a value of type T (not *T).
The memory is initialized as described in the section on initial values.
对于上述描述的需要理解的是:
package main
import "fmt"
func main() {
a := [...]int{1, 2, 3, 4, 5}
s1 := a[2:4]
fmt.Printf("s1 = %d\n", s1)
fmt.Printf("s1 = %v\n", s1)
s2 := a[1:5]
fmt.Printf("s2 = %v\n", s2)
s3 := a[:]
fmt.Printf("s3 = %v\n", s3)
s4 := a[:4]
fmt.Printf("s4 = %v\n", s4)
s5 := s2[:]
fmt.Printf("s5 = %v\n", s5)
s6 := a[2:4:5]
fmt.Printf("s6 = %v\n", s6)
}
输出结果
s1 = [3 4]
s1 = [3 4]
s2 = [2 3 4 5]
s3 = [1 2 3 4 5]
s4 = [1 2 3 4]
s5 = [2 3 4 5]
s6 = [3 4]
package main
func main() {
var array [100]int
slice := array[0:99]
slice[98] = 1 3
// ERROR: index out of range
slice[99] = 2 4
}
package main
import "fmt"
func main() {
s0 := []int{0, 0}
s1 := append(s0, 2)
s2 := append(s1, 3, 5, 7)
// Append slice instead of value, add ...
s3 := append(s2, s0...)
fmt.Println("s0 = %v", s0)
fmt.Println("s1 = %v", s1)
fmt.Println("s2 = %v", s2)
fmt.Println("s3 = %v", s3)
}
如果追加的是slice,在后面加三个点,普通值直接追加即可
s0 = %v [0 0]
s1 = %v [0 0 2]
s2 = %v [0 0 2 3 5 7]
s3 = %v [0 0 2 3 5 7 0 0]
copy( destSlice, srcSlice []T) int
package main
import "fmt"
func main() {
var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
var s = make([]int, 6)
fmt.Printf("initial a = %v\n", a)
fmt.Printf("initial s = %v\n", s)
n1 := copy(s, a[0:])
fmt.Printf("n1 = %v\n", n1)
fmt.Printf("a = %v\n", a)
fmt.Printf("s = %v\n", s)
fmt.Printf("s[2:] = %v\n", s[2:])
n2 := copy(s, s[2:])
fmt.Printf("n2 = %v\n", n2)
fmt.Printf("a = %v\n", a)
fmt.Printf("s = %v\n", s)
}
输出结果
initial a = [0 1 2 3 4 5 6 7]
initial s = [0 0 0 0 0 0]
n1 = 6
a = [0 1 2 3 4 5 6 7]
s = [0 1 2 3 4 5]
s[2:] = [2 3 4 5]
n2 = 4
a = [0 1 2 3 4 5 6 7]
s = [2 3 4 5 4 5]