• 【原创】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
    ", 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
    ", 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
    ", key, value)
    		keys = append(keys, key)
    	}
    
    	sort.Strings(keys)
    	for _, value := range keys {
    		fmt.Printf("key:%s val:%d
    ", 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
    ", 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
    ", 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
    ", s)
    }
    
    func main(){
    	//sliceMap()
    }
    

      

  • 相关阅读:
    thinkphp中的验证码的实现
    js深入研究之牛逼的类封装设计
    js深入研究之函数内的函数
    js深入研究之初始化验证
    js深入研究之Person类案例
    js深入研究之匿名函数
    js深入研究之类定义与使用
    sublime text3 自动编译php 适合用于简单的php文件执行
    PHP实现四种基本排序算法 得多消化消化
    thinkphp中的类库与引用import引入机制
  • 原文地址:https://www.cnblogs.com/wangshuyang/p/11801572.html
Copyright © 2020-2023  润新知