当你声明一个map的时候:

m := make(map[int]int)

编译器会调用 runtime.makemap

// makemap implements a Go map creation make(map[k]v, hint)
// If the compiler has determined that the map or the first bucket
// can be created on the stack, h and/or bucket may be non-nil.
// If h != nil, the map can be created directly in h.
// If bucket != nil, bucket can be used as the first bucket.
func makemap(t *maptype, hint int64, h *hmap, bucket unsafe.Pointer) *hmap

所以实际上是返回一个hmap的指针。

如何验证呢?

func main(){
m := make(map[int]int)
m[1] = 1
fmt.Printf("原始map的内存地址是:%p\n", m)
modify(m)
fmt.Println("map值被修改了,新值为:", m)
} func modify(m interface{}){
fmt.Printf("函数里接收到map的内存地址是:%p\n", p)
m := p.(map[int]int)
m[1] = 2
}

输出结果:

原始map的内存地址是:0xc00009e030
函数里接收到map的内存地址是:0xc00009e030
map值被修改了,新值为: map[1:2]

在main函数中,m是个指针变量,它保存的值是:0xc00009e030。

在modify函数中,m也是个指针变量,保存的值也是:0xc00009e030。

说明初始化map后,返回的是指针变量,在函数之间,传递的是map的地址。

map和channel是相似的。

那么为什么不是 *map[key]value呢,这样不是更直观?

Ian Taylor answered this recently in a golang-nuts 原话是:

In the very early days what we call maps now were written as pointers, so you wrote *map[int]int. We moved away from that when we realized that no one ever wrote map without writing \*map.

意思是,一开始也是写作 *map[key]value,后来发现所有的map都是当作指针来用的,于是就省略简写了。

golang中,map作为函数参数是如何传递的的更多相关文章

  1. golang 中 map 转 struct

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

  2. 关于 Go 中 Map 类型和 Slice 类型的传递

    关于 Go 中 Map 类型和 Slice 类型的传递 Map 类型 先看例子 m1: func main() { m := make(map[int]int) mdMap(m) fmt.Printl ...

  3. 【go】golang中置new()函数和make()函数的区别

    Go语言中的内建函数new和make是两个用于内存分配的原语(allocation primitives),其功能相似,却有本质区别. 1.new 官方文档 // The new built-in f ...

  4. golang中的init函数以及main函数

    首先我们看一个例子:init函数: init 函数可在package main中,可在其他package中,可在同一个package中出现多次. main函数 main 函数只能在package ma ...

  5. C++中,引用作为函数参数

    引用作为函数参数 C++之所以增加引用类型, 主要是把它作为函数参数,以扩充函数传递数据的功能. ———————————————————— c++,函数传参:(1)将变量名作为实参和形参.这时传给形参 ...

  6. Java中能否利用函数参数来返回值

    转自https://blog.csdn.net/da_da_xiong/article/details/70039532 我们在写代码时通常会遇到一种情况,就是我们可能希望在一个函数操作完成后返回两个 ...

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

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

  8. JavaScript中函数参数的值传递和引用传递

    结论: 对于数字.字符串等基本类型变量,是将它们的值传递给了函数参数,函数参数的改变不会影响函数外部的变量. 对于数组和对象等是将对象(数组)的变量的值传递给了函数参数,这个变量保存的指向对象(数组) ...

  9. [GO]map做函数参数

    package main import "fmt" func test(m map[int]string) { delete(m, ) } func main() { m := m ...

随机推荐

  1. stack的简单用法总结

    stack中常见方法 top():返回一个栈顶元素的引用,类型为 T&.如果栈为空,返回值未定义. push(const T& obj):可以将对象副本压入栈顶.这是通过调用底层容器的 ...

  2. 关于C++中extern的简单笔记

    extern可以实现多文件共享同一个变量.const常量.函数. 下面结合几个例子来讲一下extern的相关性质(下述皆为多文件编译): 例1: //file1.cpp #include<ios ...

  3. flag&to do list&note

    没错,今天我要立几个看起来可能会倒的 flag 今天白天 早上除非有特殊情况,不许再看我的博客.不许再看我的qq空间.不许再跟别人聊闲话!!!☑已达成 今天早上一定要坚持做题,把昨天问老师的问题搞懂, ...

  4. 《jmeter接口自动化与性能实战-飞天小子.pdf》

    Jmeter作为开源测试工具,以其轻便,功能齐全的优点,正越来越受到企业的重视.作为纯java的工具,它的扩展性无比强大,既可以做功能测试,也可以做性能测试:既支持接口层面的测试,也支持webdriv ...

  5. 苹果手机微信浏览器select标签选择完成之后页面不会自动回到原位

    说明: html默认select选择框控件在IOS的浏览器中 是底部弹出下拉选择. 这样到时页面位置错位,选择结束后对应不少元素的点击事件不响应. 这样看起来问题不大,但是选择完成之后点击确定提交弹出 ...

  6. wordpress 本地环境安装

    1. 下载xmapp 2. 安装mysql 3. 启动xmapp的数据库与Apache,通常无法启动原因. mac的先关闭自己的数据库,系统偏好设置-MySQL Apache的端口默认是80,一般会被 ...

  7. git rebase命令

    使用git rebase合并多次commit. 当年提交代码后,管理员发现,你的代码不能提交到服务器上,注意原因在于,你的commit中的commit和服务器中的有些commit不在同一时间轴上,即: ...

  8. 基于webpack4的react开发环境配置

    一.基础配置 1.init项目 mkdir react-webpack4-cook cd react-webpack4-cook mkdir src mkdir dist npm init -y 复制 ...

  9. mysql5.7的主从切换

    mysql5.7的主从切换 主库: master 10.11.0.211 从库: slave 10.11.0.210 目标:主从切换,将slave切换为master,master切换为slave 一. ...

  10. Better ultra_simple for Slamtec RPLIDAR on Linux

    Improved the ultra_simple program to visualize the samples with GLUT on Linux, tested with Slamtec R ...