【原创】go语言学习(十)Map类型

目录

  • 声明和初始化
  • 基本操作
  • map排序
  • map类型的切⽚片
  • 课后作业

声明和初始化

1、map类型是⼀一个key-value的数据结构。

//var a map[key的类型]value类型
var a map[string]int
var b map[int]string
var c map[float32]string

  注意:map必须初始化才能使⽤用,否则panic

2、map类型的变量量默认初始化为nil,需要使⽤用make分配map内存

package main
import (
    "fmt"
)
func main() {
    var a map[string]int
    if a == nil {
        fmt.Println("map is nil. Going to make one.")
        A = make(map[string]int)
    }
}

  

 

基本操作

1、map插入操作

package main
import (
    "fmt"
)
func main() {
    a := make(map[string]int)
    a["steve"] = 12000
    a["jamie"] = 15000
    a["mike"] = 9000
    fmt.Println(“a map contents:", a)
}

  

2、声明时进⾏行行初始化

package main
import (
    "fmt"
)
func main() {
    var a map[string]int
    if a == nil {
        fmt.Println("map is nil. Going to make one.")
        A = make(map[string]int)
    }
}        

  

3、map插入操作

package main
import (
    "fmt"
)
func main() {
    a := make(map[string]int)
    a["steve"] = 12000
    a["jamie"] = 15000
    a["mike"] = 9000
    fmt.Println(“a map contents:", a)
}

  

4、声明时进⾏行行初始化

package main
import (
    "fmt"
)
func main() {
    a := map[string]int {
        "steve": 12000,
        "jamie": 15000,
    }
    a[“mike"] = 9000
    fmt.Println(“a map contents:", a)
}

  

5、 通过key访问map中的元素

package main
import (
    "fmt"
)
func main() {
    a := map[string]int{
        "steve": 12000,
        "jamie": 15000,
    }
    a["mike"] = 9000
    b := "jamie"
    fmt.Println("Salary of", b, "is", a[b])
}

  

6、 通过key访问map中的元素

package main
import (
    "fmt"
)
func main() {
    a := map[string]int{
        "steve": 12000,
        "jamie": 15000,
    }
    a["mike"] = 9000
    b := “123”
    fmt.Println("Salary of", b, "is", a[b])
}

  

7、如何判断map指定的key是否存在? value, ok := map[key]

package main
import (
    "fmt"
)
func main() {
    a := map[string]int{
        "steve": 12000,
        "jamie": 15000,
    }
    a["mike"] = 9000
    b := "joe"
    value, ok := a[b]
    if ok == true {
        fmt.Println("Salary of", b, "is", value)
    } else {
        fmt.Println(b,”not found")
    }
}        

  

8、map遍历操作

package main
import (
    "fmt"
)
func main() {
    a := map[string]int{
        "steve": 12000,
        "jamie": 15000,
    }
    a["mike"] = 9000
    fmt.Println("All items of a map")
    for key, value := range a {
        fmt.Printf("personSalary[%s] = %d\n", key, value)
    }
}        

  

9、map删除元素

package main
import (
    "fmt"
)
func main() {
    a := map[string]int{
        "steve": 12000,
        "jamie": 15000,
    }
    a["mike"] = 9000
    fmt.Println("map before deletion", a)
    delete(a, "steve")
    fmt.Println("map after deletion", a)
}

  

10、map的长度

package main
import (
    "fmt"
)
func main() {
    a := map[string]int{
        "steve": 12000,
        "jamie": 15000,
    }
    a["mike"] = 9000
    fmt.Println("length is", len(a))
}

  

11、map是引用类型

package main
import (
    "fmt"
)
func main() {
    a := map[string]int{
        "steve": 12000,
        "jamie": 15000,
    }
    a["mike"] = 9000
    fmt.Println(“origin map", a)
    b := a
    b["mike"] = 18000
    fmt.Println(“a map changed", a)
}

  

12、map按照key进行行排序,遍历

package main
import (
    "fmt"
    "sort"
)
func main() {
    var a map[string]int = make(map[string]int, 10)
    for i := 0; i < 10; i++ {
        key := fmt.Sprintf("key%d", i)
        a[key] = i
    }
    var keys []string
    for key, _ := range a {
        keys = append(keys, key)
    }
    sort.Strings(keys)
    for _, key := range keys {
        fmt.Printf("key:%s=%d\n", key, a[key])
    }
}    

 

map排序

package main

import (
	"fmt"
	"math/rand"
	"sort"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano())
	var a map[string]int = make(map[string]int, 1024)

	for i := 0; i < 128; i++ {
		key := fmt.Sprintf("stu%d", i)
		value := rand.Intn(1000)
		a[key] = value
	}

	var keys []string = make([]string, 0, 128)
	for key, value := range a {
		fmt.Printf("map[%s]=%d\n", key, value)
		keys = append(keys, key)
	}

	sort.Strings(keys)
	for _, value := range keys {
		fmt.Printf("key:%s val:%d\n", value, a[value])
	}
}

  

map类型的切片

package main

import (
	"fmt"
	"math/rand"
	"time"
)

func sliceMap(){
	rand.Seed(time.Now().UnixNano())
	var s[]map[string] int
	s = make([]map[string]int, 5, 16)
	for index,val := range s {
		fmt.Printf("slice[%d]=%v\n", index, val)
	}

	fmt.Println()
	s[0] = make(map[string]int, 16)
	s[0]["stu01"] = 1000
	s[0]["stu02"] = 1000
	s[0]["stu03"] = 1000

	for index, val := range s {
		fmt.Printf("slice[%d]=%v\n", index, val)
	}
}

func mapSlice() {
	rand.Seed(time.Now().UnixNano())
	var s map[string][]int
	s = make(map[string[]int, 16)
	key := "stu01"
	value, ok := s[key]
	if !ok {
		s[key] = make([]int, 0, 16)

		value  = s[key]
	}


	value = append(value, 100)
	value = append(value, 200)
	value = append(value, 300)

	s[key] = value
	fmt.Printf("map:%v\n", s)
}

func main(){
	//sliceMap()
}

  

posted @ 2019-11-05 20:55  shuyang  阅读(249)  评论(0编辑  收藏  举报