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)
	}

}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值