数组
package main
import "fmt"
func main() {
var course1 [3]string
course1[0] = "go"
course1[1] = "grpc"
course1[2] = "gin"
for _, value := range course1 {
fmt.Println(value)
}
course2 := [3]string{2: "grpc"}
fmt.Println(course2)
course3 := [...]string{"go", "grpc"}
fmt.Println(course3)
//多维数组
var courseinfo [3][4]string
courseinfo[0] = [4]string{"go1", "1h", "neo1", "基础1"}
courseinfo[1] = [4]string{"go2", "2h", "neo2", "基础2"}
courseinfo[2] = [4]string{"go3", "3h", "neo3", "基础3"}
for i := 0; i < len(courseinfo); i++ {
for j := 0; j < len(courseinfo[0]); j++ {
fmt.Print(courseinfo[i][j] + " ")
}
fmt.Println()
}
}
slice
package main
import (
"fmt"
"strconv"
)
func changeSlice(data []string) {
data[0] = "change!" //生效
for i := 0; i < 5; i++ {
data = append(data, strconv.Itoa(i)) //不生效,扩容导致地址改变
}
}
func main() {
//初始化
allCourse1 := [5]string{"go", "grpc", "gin", "mysql", "search"}
fmt.Println(allCourse1)
allCourse2 := [5]string{"go", "grpc", "gin", "mysql", "search"}
courseLine := allCourse2[0:4] //左闭右开
fmt.Println(courseLine)
allCourse3 := make([]string, 3, 5) //len、cap
allCourse3[0] = "c"
fmt.Println(allCourse3)
var allCourse4 []string
allCourse4 = append(allCourse4, "go")
allCourse4 = append(allCourse4, "grpc", "gin")
fmt.Println(allCourse4)
//访问,同python
fmt.Println(allCourse1[:])
fmt.Println(allCourse1[0:4])
//拼接
courseSlice1 := []string{"go", "grpc"}
courseSlice2 := []string{"gin", "mysql"}
courseSlice1 = append(courseSlice1, courseSlice2[:]...)
fmt.Println(courseSlice1)
//删除
courseSlice3 := []string{"go", "grpc", "gin", "sql"}
mySlice := append(courseSlice3[:2], courseSlice3[3:]...)
fmt.Println(mySlice)
//复制
fmt.Println("复制")
courseCopy := courseSlice1 //不独立
courseSlice1[0] = "java1"
fmt.Println(courseSlice1)
fmt.Println(courseCopy)
courseCopy2 := courseSlice1[:] //不独立
courseSlice1[0] = "java2"
fmt.Println(courseSlice1)
fmt.Println(courseCopy2)
courseCopy3 := make([]string, len(courseSlice1))
copy(courseCopy3, courseSlice1) //独立
courseSlice1[0] = "java3"
fmt.Println(courseSlice1)
fmt.Println(courseCopy3)
//值传递原理
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
s1 := data[1:6]
s2 := data[2:7]
//cap在512以内为2的幂,略大于等于len
//扩容导致指向data副本,s1和s2不再指向同一个slice
s2 = append(s2, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1, 1, 1, 1, 1, 1, 1, 1, 1)
fmt.Println(s1)
fmt.Println(s2)
s2[2] = 22
fmt.Println(s1) //不再改变
fmt.Println(s2)
courseChange := []string{"go", "grpc", "gin", "sql"}
changeSlice(courseChange)
fmt.Println(courseChange)
}
map
package main
import "fmt"
func main() {
var courseMap = map[string]string{
"go": "go1",
"grpc": "grpc1",
"gin": "gin1",
}
//var courseMap1=make(map[string]string,3)
courseMap["sql"] = "mysql"
fmt.Println(courseMap["sql"])
for key, value := range courseMap {
fmt.Println(key, value)
}
d, ok := courseMap["java"]//若不存在,则值为""
fmt.Println(d, ok) //"",false
delete(courseMap, "gin")
delete(courseMap, "cpp")//不存在也不会报错
}
list
package main
import (
"container/list"
"fmt"
)
func main() {
var myList list.List
myList.PushBack("go")
myList.PushBack("grpc")
myList.PushBack("gin")
fmt.Println(myList) //指针
myList.PushFront("sql")
myList.InsertBefore("last", myList.Back())
for it := myList.Front(); it != nil; it = it.Next() {
fmt.Println(it.Value)
}
for it := myList.Back(); it != nil; it = it.Prev() {
fmt.Println(it.Value)
}
}
文章来源地址https://www.toymoban.com/news/detail-614796.html
文章来源:https://www.toymoban.com/news/detail-614796.html
到了这里,关于慕课网Go-2.数组、slice、map、list的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!