GO基本数据结构练习:数组,切片,映射
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GO基本数据结构练习:数组,切片,映射相关的知识,希望对你有一定的参考价值。
按《GO IN ACTION》的书上进行。
应该是第二次了哦~~
package main import ( "fmt" ) func main() { array := [5]*int{0: new(int), 3: new(int)} *array[3] = 50 /* for k, v := range array { fmt.Println(*array[k], k, v) } */ fmt.Println(*array[3]) var array1 [3]*string array2 := [3]*string{new(string), new(string), new(string)} *array2[0] = "Red" *array2[1] = "Blue" *array2[2] = "Green" array1 = array2 *array1[1] = "Black" fmt.Println(array1) fmt.Println(array2) for k, v := range array1 { fmt.Println(*array1[k], k, *v) } for k, v := range array2 { fmt.Println(*array2[k], k, *v) } slice := []int{10, 20, 30, 40, 50} newSlice := slice[1:3:3] newSlice[1] = 35 newSlice = append(newSlice, 60) newSlice = append(newSlice, 70) newSlice = append(newSlice, 80) newSlice = append(newSlice, 90) slice = append(slice, 60) slice = append(slice, 70) slice = append(slice, 80) newSlice = append(newSlice, 100) slice1 := []int{10, 20, 30, 40} newSlice1 := append(slice1, 50) slice1[2] = 200 newSlice1[2] = 2000 fmt.Println(slice) fmt.Println(newSlice) fmt.Println(slice1) fmt.Println(newSlice1) for index, value := range slice { fmt.Printf("index: %d Value: %d\\n", index, value) } for index := 2; index < len(slice); index++ { fmt.Printf("Index: %d Value: %d\\n", index, slice[index]) } source := []string{"Apple", "Orange", "Plum", "Banana", "Grape"} newSource := source[2:3:4] fmt.Println(source) fmt.Println(newSource) s1 := []int{1, 2} s2 := []int{3, 4} fmt.Printf("%v\\n", append(s1, s2...)) colors := map[string]string{ "AliceBlue": "#f0f8ff", "Coral": "#ff7F50", "DarkGray": "#a9a9a9", "ForestGreen": "#228b22", } delete(colors, "Coral") for key, value := range colors { fmt.Printf("Key: %s Value: %s\\n", key, value) } }
以上是关于GO基本数据结构练习:数组,切片,映射的主要内容,如果未能解决你的问题,请参考以下文章