数组索引基本懂了,头痛,感觉切片好难,下次再学一下,我先睡一下了
This commit is contained in:
58
6.切片.go
Normal file
58
6.切片.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
slice_()
|
||||
}
|
||||
|
||||
func slice_() {
|
||||
var nameList []string
|
||||
nameList = append(nameList, "1")
|
||||
nameList = append(nameList, "2")
|
||||
|
||||
fmt.Println(nameList[0])
|
||||
|
||||
var nameList1 []string
|
||||
fmt.Println(nameList1 == nil)
|
||||
|
||||
// 初始化
|
||||
var nameList2 []string = []string{}
|
||||
/*
|
||||
其他初始化的方法
|
||||
var nameList2 = []string{}
|
||||
nameList2 := []string{}
|
||||
nameList2 = make([string], 0)
|
||||
*/
|
||||
nameList2 = append(nameList2, "1")
|
||||
nameList2 = append(nameList2, "2")
|
||||
|
||||
fmt.Println(nameList2 == nil)
|
||||
|
||||
// make 函数
|
||||
// 格式
|
||||
//make([]type, length, capacity)
|
||||
// 可以创建指定长度,指定容量的切片
|
||||
ageList := make([]int, 3)
|
||||
fmt.Println(ageList)
|
||||
|
||||
// 数组切片
|
||||
array :=[3]int{1, 2, 3}
|
||||
slices := array[:]
|
||||
fmt.Println(slices)
|
||||
fmt.Println(array[0:2])
|
||||
fmt.Println(array[2:])
|
||||
|
||||
// 切片排序
|
||||
var ints = []int{2, 4, 5, 2, 8, 0, 2, 5,}
|
||||
sort.Ints(ints)
|
||||
fmt.Println(ints) // 默认升序
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(ints))) // 降序
|
||||
fmt.Println(ints)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user