先上代码……

package main

import "fmt"

func testMap() {
//两种声明map方式,切记,必须初始化才能用,否则panic //var a map[string]string = map[string]string{
// "key": "value",
//}
a := make(map[string]string, )
a["abc"] = "efg"
//map的key是唯一的,修改值可以直接改
a["abc"] = "efg2"
a["abc1"] = "efg"
fmt.Println(a)
} //map嵌套map
//map是无序排序
func testMap2() {
a := make(map[string]map[string]string, )
a["key1"] = make(map[string]string)
a["key1"]["key2"] = "abc"
a["key1"]["key3"] = "abc"
a["key1"]["key4"] = "abc"
a["key1"]["key5"] = "abc"
fmt.Println(a)
} func modify(a map[string]map[string]string) {
_, ok := a["zhangsan"]
if !ok {
a["zhangsan"] = make(map[string]string)
}
//与_,ok写法一样
//if a["zhangsan"] == nil {}
//
a["zhangsan"]["passwd"] = ""
a["zhangsan"]["nickname"] = "pangpang"
return
} func testMap3() {
a := make(map[string]map[string]string, )
modify(a)
fmt.Println(a)
}
func trans(a map[string]map[string]string) {
for k, v := range a {
fmt.Println(k)
for k1, v1 := range v {
fmt.Println("\t", k1, v1)
}
}
}
func testMap4() {
a := make(map[string]map[string]string, )
a["key1"] = make(map[string]string)
a["key1"]["key2"] = "abc"
a["key1"]["key3"] = "abc"
a["key1"]["key4"] = "abc"
a["key1"]["key5"] = "abc"
//删除map键的内置函数delete
//delete(a,"key1")
trans(a) fmt.Println(len(a)) } func testMap5() {
var a []map[int]int
a = make([]map[int]int, ) //for i:=0;i<5;i++{} //map,slice判断空是nil
if a[] == nil {
a[] = make(map[int]int)
}
a[][] =
fmt.Println(a)
} func main() {
testMap()
testMap2()
testMap3()
testMap4()
testMap5()
}

map反转

package main

import (
"fmt"
"sort"
) func testMapSort() {
var a map[int]int
a = make(map[int]int, )
a[] =
a[] =
a[] =
a[] =
a[] =
var keys []int
for k, _ := range a {
keys = append(keys, k) }
sort.Ints(keys)
for _, v := range keys {
fmt.Println(v, a[v])
}
}
//map反转
func testMapSort1() {
var a map[string]int
var b map[int]string
a = make(map[string]int, )
b = make(map[int]string, )
a["abc"] =
a["efg"] =
for k, v := range a {
b[v] = k
}
fmt.Println(b)
}
func main() {
//testMapSort()
testMapSort1()
}

map 键值反转

golang之map数据类型的更多相关文章

  1. golang之map的使用声明

    1.map的基本介绍 map是key-value数据结构,又称为字段或者关联数组.类似其它编程语言的集合,在编程中是经常使用到的 2.map的声明 1)基本语法 var map 变量名 map[key ...

  2. Golang中map的三种声明方式和简单实现增删改查

    package main import ( "fmt" ) func main() { test3 := map[string]string{ "one": & ...

  3. 总结golang之map

    总结golang之map 2017年04月13日 23:35:53 趁年轻造起来 阅读数:18637 标签: golangmapgo 更多 个人分类: golang   版权声明:本文为博主原创文章, ...

  4. golang自己定义数据类型查询与插入postgresql中point数据

    golang自己定义数据类型查询与插入postgresql中point数据 详细代码例如以下: package main import ( "bytes" "databa ...

  5. golang 中 map 转 struct

    golang 中 map 转 struct package main import ( "fmt" "github.com/goinggo/mapstructure&qu ...

  6. golang之基本数据类型

    目录 一.golang之基本数据类型 1. 整型 (1)有符号(范围是负数.0和正数) (2)无符号(范围是0和正数) (3)特殊整型 (4)数字字面量语法 2. 浮点型 3. 复数类型 4. 布尔类 ...

  7. Golang的基础数据类型-字符串型

    Golang的基础数据类型-字符串型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字符型概述 上一篇博客学习了使用单引号括起来的单个字符就是字符类型,在Golang中有两种表 ...

  8. Golang的基础数据类型-字符型

    Golang的基础数据类型-字符型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字符型概述 Go语言中的字符有两种,即uint8类型和rune类型. uint8类型: 我们也 ...

  9. Golang的基础数据类型-浮点型

    Golang的基础数据类型-浮点型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.浮点型概述 Go语言提供两种精度的浮点数,即float32和float64,其中float32 ...

随机推荐

  1. tf.cast()数据类型转换

    tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换,比如读入的图片如果是int8类型的,一般在要在训练前把图像的数据格式转换为float32. cast定义: cast(x, ...

  2. busybox microcom Segmentation fault

    /********************************************************************************* * busybox microco ...

  3. InputStream,InputStreamReader和Reader的关系

    InputStream:得到的是字节输入流,InputStream.read("filename")之后,得到字节流 Reader:读取的是字符流 InputStreamReade ...

  4. gqlgen golang graphql server 基本试用

    gqlgen golang 的graphql server 具体代码参考https://github.com/rongfengliang/gqlgen-demo 特点 模型优先 类型安全 代码生成 安 ...

  5. web应用中的Filter过滤器之基础概述

    1 过滤器概述 当web容器接收到对一个资源的请求时,它将判断是否有过滤器与这个资源相关联,如果有,那么容器将把这个请求交给过滤器进行处理.在过滤器中,你可以改变请求的内容或者重新设置请求的报头信息, ...

  6. CentOS 6.5 下HeartBeat的安装与配置

    CentOS 6.5 下HeartBeat的安装与配置 参考网站: http://wenku.baidu.com/link?url=BvqJatdx1m12PLil-7YA1zkM0yUOEO8OnN ...

  7. maven库 mvn依赖

    http://maven.outofmemory.cn/ http://mvnrepository.com/ 先执行 mvn clean  然后执行  mvn 命令 如:mvn  compile  . ...

  8. nginix.conf 中的gzip模块设置

    gizp模块配置 gzip  on;    gzip_min_length  1k;    gzip_buffers     4 16k;    gzip_http_version 1.0;    g ...

  9. PAT 甲级 1011 World Cup Betting (20)(20 分)(水题,不用特别在乎精度)

    1011 World Cup Betting (20)(20 分) With the 2010 FIFA World Cup running, football fans the world over ...

  10. python的with用法

    转自http://blog.kissdata.com/2014/05/23/python-with.html With语句是什么? 有一些任务,可能事先需要设置,事后做清理工作.对于这种场景,Pyth ...