- var name []T
- name:表示变量名
- T:表示切片中的元素类型
package main
import "fmt"
func main() {
//切片是引用类型,不支持直接比较,只能和 nil 比较
var a []string //声明一个字符串切片
fmt.PrintIn(a) //[]
fmt.PrintIn(a == nil) //true
var b = []int{} //声明一个整型切片并初始化
fmt.PrintIn(b) //[]
fmt.PrintIn(b==nil) //false
var c = []bool{false,true}
fmt.PrintIn(c) //[false true]
fmt.PrintIn(c == nil) //false
}
bool->false
numbers->0
string->""
pointers->nil
slices->nul
maps->nil
channels->nil
functions->nil
interfaces->nil
package main
import "fmt"
func main(){
s:=[]int{2,3,5,7,11,13}
fmt.Printf("长度:%v 容量%v\n",len(s),cap(s)) //长度:6 容量 6
c := s[:2]
fmt.PrintIn(c) //[2 3]
fmt.Printf("长度:%v 容量 %v\n",len(c),cap(c)) //长度 2 容量 6
d :=s[1:3]
fmt.PrintIn(d) //[3 5]
fmt.Printf("长度:%v 容量%v",len(d),cap(d)) //长度:2 容量 5
}
package main
import "fmt"
func main(){
var a = []string{"北京","上海","深圳"}
for i:=0;i
package main
import "fmt"
func main() {
var a = []string{"北京", "上海", "深圳"}
for index, value := range a {
fmt.Println(index, value)
}
}
/*
0 北京
1 上海
2 深圳
*/
package main
import "fmt"
func main() {
a := [5]int{55,56,57,58,59} //基于数组定义切片
b := a[1:4]
fmt.PrintIn(b) // [56 57 58]
fmt.Printf("type of b:%T\n",b) //type of b:[]int
c := b[0:2]
fmt.PrintIn(c) //[56 57]
}
package main
import "fmt"
func main() {
a := make([]int,2,10)
fmt.PrintIn(a) //[0 0]
fmt.PrintIn(len(a)) // 2
fmt.PrintIn(cap(a)) //10
}
package main
import "fmt"
func main() {
//append()添加元素和切片扩容
var numSlice []int
for i :=0;i<10;i++ {
numSlice =append(numSlice,i)
fmt.Printf("%v len:%d cap:%d ptr:%p\n",numSlice,len(numSlice),cap(numSlice),numSlice)
}
}
package main
import "fmt"
func main() {
var citySlice []string
citySlice = append(citySlice,"北京") //追加一个元素
citySlice = append(citySlice,"上海","杭州","深圳") //追加多个元素
a := []string{"成都","重庆"}
citySlice = append(citySlice,a...) //追加切片
fmt.PrintIn(citySlice) //[北京 上海 杭州 深圳 成都 重庆]
}
package main
import "fmt"
func main() {
a := int{30,31,32,33,34,35,36,37}
a = append(a[:2],a[3:]...) //要删除索引为2的元素
fmt.PrintIn(a) //[30 31 33 34 35 36 37]
}
package main
import "fmt"
func main() {
arr1 :=[]int{2,7,1}
arr2 :=[]int{5,9,3}
fmt.PrintIn(arr2,arr1)
arr1 = append(arr1,arr2...)
fmt.PrintIn(arr1) //[2 7 1 5 9 3]
}
package main
import "fmt"
func main() {
a := []int{1,2,3,4,5}
b :=a
fmt.PrintIn(a) //[1 2 3 4 5]
fmt.PrintIn(b) //[1 2 3 4 5]
b[0] = 1000
fmt.PrintIn(a) //[1000 2 3 4 5]
fmt.Println(b) //[1000 2 3 4 5]
}
package main
import "fmt"
func main() {
a := []int{1,2,3,4,5}
c := make([]int,5,5) //[0 0 0 0 0]
copy(c,a) //使用copy函数将切片a中的元素复制到切片c
fmt.Println(a) //[1 2 3 4 5]
fmt.Println(c) //[1 2 3 4 5]
c[0] = 1000
fmt.Println(a) //[1 2 3 4 5]
fmt.Println(c) //[1000 2 3 4 5]
}
pacakge main
import {
"fmt"
"sort"
}
func main() {
intList := []int{2, 4, 3, 5, 7, 6, 9, 8, 1, 0}
sort.Ints(intList)
fmt.Println(intList) // [0 1 2 3 4 5 6 7 8 9]
stringList := []string{"a", "c", "b", "z", "x", "w", "y", "d", "f", "i"}
sort.Strings(stringList)
fmt.Println(stringList) // [a b c d f i w x y z]
}
2、sort降序排序
package main
import (
"fmt"
"sort"
)
func main() {
intList := []int{2, 4, 3, 5, 7, 6, 9, 8, 1, 0}
sort.Sort(sort.Reverse(sort.IntSlice(intList)))
fmt.Println(intList) // [9 8 7 6 5 4 3 2 1 0]
stringList := []string{"a", "c", "b", "z", "x", "w", "y", "d", "f", "i"}
sort.Sort(sort.Reverse(sort.StringSlice(stringList)))
fmt.Println(stringList) // [z y x w i f d c b a]
}