1. 数组和切片
数组是具有相同唯一类型的一组已编号且长度固定的数据项序列
切片是内置类型切片("动态数组"),与数组相比切片的长度是不固定的,可以追加元素,在追加时可能使切片的容量增大
package main
import "fmt"
/*
append函数会改变slice所引用的数组的内容,从而影响到引用同一数组的其它slice。
但当slice中没有剩 余空间(即(cap-len) == 0)时,此时将动态分配新的数组空间。
返回的slice数组指针将指向这个空间,而原数组的内容将保持不变;其它引用此数组的slice则不受影响
*/
func main() {
var numbers []int
Slice(numbers)
// append()函数 向slice里面追加一个或者多个元素,然后返回一个和slice一样类型的slice
numbers = append(numbers, 0)
Slice(numbers)
numbers = append(numbers, 1)
Slice(numbers)
numbers = append(numbers, 2,3,4)
Slice(numbers)
// len()返回切片长度,cap()返回切片容量
numbers1 := make([]int, len(numbers),(cap(numbers))*2)
// copy()函数 从源slice的src中复制元素到目标dst,并且返回复制的元素的个数
copy(numbers1,numbers)
Slice(numbers1)
// 使用for 和 for...range遍历数组
for i,v := range numbers1{
fmt.Printf("index %d: the element of numbers1 is %v\n",i,v)
}
}
func Slice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}
运行结果:
len=0 cap=0 slice=[]
len=1 cap=1 slice=[0]
len=2 cap=2 slice=[0 1]
len=5 cap=6 slice=[0 1 2 3 4]
len=5 cap=12 slice=[0 1 2 3 4]
index 0: the element of numbers1 is 0
index 1: the element of numbers1 is 1
index 2: the element of numbers1 is 2
index 3: the element of numbers1 is 3
index 4: the element of numbers1 is 4
2. 对切片元素进行排序
package main
import (
"fmt"
"sort"
)
// sort.Slice包用于对切片进行排序
func main() {
// 升序排序
nums1 := []int{3, 4, 5, 2}
SortAsc(nums1)
fmt.Println(nums1)
// 降序排序
nums2 := []int{3, 4, 5, 2}
SortDesc(nums2)
fmt.Println(nums2)
// 部分元素升序排序
nums3 := []int{3, 4, 5, 2}
SortPartAsc(nums3, 1, 4)
fmt.Println(nums3)
}
func SortAsc(source []int) {
sort.Slice(source, func(i, j int) bool {
return source[i] < source[j]
})
}
func SortDesc(source []int) {
sort.Slice(source, func(i, j int) bool {
return source[i] > source[j]
})
}
func SortPartAsc(source []int, start int, end int) {
sort.Slice(source[start:end], func(i, j int) bool {
return source[start+i] < source[start+j]
})
}
运行结果:
[2 3 4 5]
[5 4 3 2]
[3 2 4 5]
3. 在切片中查找某个元素
package main
import "fmt"
func main() {
source := []string{"san","man","tan"}
result := find(source,"san")
fmt.Printf("Item san found: %t\n",result)
result = find(source, "can")
fmt.Printf("Item can found: %t\n",result)
}
// 查找切片中某个元素是否存在
func find(source []string, value string) bool {
for _, item := range source {
if item == value {
return true
}
}
return false
}
运行结果:
Item san found: true
Item can found: false
4. 查找并删除某个元素
package main
import "fmt"
func main() {
before1 := []int{'a', 'b', 'c', 'k', 'a'}
fmt.Printf("before1的操作地址是:%p, before1的元素是:%v\n", &before1, before1)
after1 := findAndDelete1(before1, 'a')
fmt.Printf("after1的操作地址是:%p, after1的元素是:%v\n", &after1, after1)
fmt.Printf("before1的操作地址是:%p, before1的元素是:%v\n", &before1, before1)
fmt.Println()
before2 := []int{'a','b','c','e','a'}
fmt.Printf("before2的操作地址是:%p, before2的元素是:%v\n",&before2,before2)
after2 := findAndDelete2(before2,'a')
fmt.Printf("after2的操作地址是:%p, after2的元素是:%v\n",&after2,after2)
fmt.Printf("before2的操作地址是:%p, before2的元素是:%v\n",&before2,before2)
}
// 查找并删除切片中某个元素是否存在,以下这种方式会改变原来的切片
func findAndDelete1(source []int, value int) []int {
index := 0
for _, item := range source {
if item != value {
source[index] = item
index++
}
}
return source[:index]
}
// 查找并删除切片中某个元素是否存在,以下这种方式不会改变原来的切片
func findAndDelete2(source []int, value int) []int {
var new = make([]int, len(source))
index := 0
for _, item := range source {
if item != value {
new[index] = item
index++
}
}
return new
}
运行结果:
before1的操作地址是:0xc000004078, before1的元素是:[97 98 99 107 97]
after1的操作地址是:0xc0000040a8, after1的元素是:[98 99 107]
before1的操作地址是:0xc000004078, before1的元素是:[98 99 107 107 97]
before2的操作地址是:0xc0000040f0, before2的元素是:[97 98 99 101 97]
after2的操作地址是:0xc000004120, after2的元素是:[98 99 101 0 0]
before2的操作地址是:0xc0000040f0, before2的元素是:[97 98 99 101 97]