数组
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)
allCourse3[0] = "c"
fmt.Println(allCourse3)
var allCourse4 []string
allCourse4 = append(allCourse4, "go")
allCourse4 = append(allCourse4, "grpc", "gin")
fmt.Println(allCourse4)
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]
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",
}
courseMap["sql"] = "mysql"
fmt.Println(courseMap["sql"])
for key, value := range courseMap {
fmt.Println(key, value)
}
d, ok := courseMap["java"]
fmt.Println(d, ok)
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)
}
}