慕课网Go-2.数组、slice、map、list

数组

go 复制代码
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

go 复制代码
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

go 复制代码
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

go 复制代码
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)
	}
}
相关推荐
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
凯子坚持 c2 天前
精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
c++·redis·list
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号2 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_2 天前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty2 天前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序