func changeLocal(num [5]int) {
num[0] = 55
fmt.Println("inside function ", num)
}
func main() {
num := [...]int{5, 6, 7, 8, 8}
fmt.Println("before passing to function ", num)
changeLocal(num) //num is passed by value
fmt.Println("after passing to function ", num)
}
for i, v := range list {
fmt.Println(i, v)
}
a := [5]int{1, 2, 67, 76, 88}
var b []int = a[1:4]
// 1, 2, 67
a := [5]int{76, 77, 78, 79, 80}
var b = a[1:4] //creates a slice from a[1] to a[3]
fmt.Println(b[1:4], b) // 注意此处slice的len为3,cap为4,不可以用[]访问,但是可以使用[:]格式进行访问,
func make([]T, len, cap int) []T
numa := [3]int{78, 79 ,80}
nums1 := numa[:] //creates a slice which contains all elements of the array
nums2 := numa[:]
fmt.Println("array before change 1",numa)
nums1[0] = 100
fmt.Println("array after modification to slice nums1", numa)
nums2[1] = 101
fmt.Println("array after modification to slice nums2", numa)
fruitarray := [...]string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
fruitslice := fruitarray[1:3]
fmt.Printf("length of slice %d capacity %d\n", len(fruitslice), cap(fruitslice)) //length of is 2 and capacity is 6
fruitslice = fruitslice[:cap(fruitslice)] //re-slicing furitslice till its capacity
fmt.Println("After re-slicing length is",len(fruitslice), "and capacity is",cap(fruitslice)) // 6, 6
type slice struct {
Length int
Capacity int
ZerothElement *byte // 头指针
}