慕课网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)
	}
}
相关推荐
故事和你911 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
techdashen2 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
星空椰2 小时前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript
忒可君2 小时前
C# winform 自制分页功能
android·开发语言·c#
Rust研习社2 小时前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
leaves falling3 小时前
C++模板进阶
开发语言·c++
坐吃山猪3 小时前
Python27_协程游戏理解
开发语言·python·游戏
gCode Teacher 格码致知3 小时前
Javascript提高:小数精度和随机数-由Deepseek产生
开发语言·javascript·ecmascript
椰猫子4 小时前
Javaweb(Filter、Listener、AJAX、JSON)
java·开发语言
盛世宏博北京4 小时前
以太网温湿度传感器运维技巧,提升设备稳定性与使用寿命
开发语言·php·以太网温湿度传感器