课程名称:Go 语言语法进阶指南
课程章节:内建方法
课程讲师:Gavin
课程内容:
对变量进行操作
package main import "fmt" func main() { //test func } //向切片添加元素并打印出切片的长度和容量 func appendSlice() { mSlice := make([]string,2) mSlice[0] = "id-1" mSlice[1] = "id-2" fmt.Println("len=",len(mSlice)) fmt.Println("cap=",cap(mSlice)) mSlice= append(mSlice, elems:"id-3") fmt.Println(mSlice) fmt.Println("After len=",len(mSlice)) fmt.Println("After cap=",cap(mSlice)) } //拷贝切片 func copySlice() { //拷贝 mIDSliceDst := make([]string,3) mIDSliceDst[0] = "id-dst-1" mIDSliceDst[1] = "id-dst-2" mIDSliceDst[2] = "id-dst-3" //源 mSlice:= make([]string,2) mIDSliceSrc[0] = "id-src-1" mIDSliceSrc[1] = "id-src-2" mIDSliceSrc[2] = "id-src-3" copy(mIDSliceDst, mIDSliceSrc) fmt.Println(mIDSliceDst) } //删除map中的数据 func deleteMap() { mMap:=make(map[int]string) mMap[0] = "id-1" mMap[1] = "id-2" delete(mMap,key:0)//map[1:id-2] delete(mMap,key:1)//map[] fmt.Println(mMap) }
课程收获: