golang中,map作为函数参数是如何传递的
当你声明一个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
mapwithout writing\*map.
意思是,一开始也是写作 *map[key]value,后来发现所有的map都是当作指针来用的,于是就省略简写了。
golang中,map作为函数参数是如何传递的的更多相关文章
- golang 中 map 转 struct
golang 中 map 转 struct package main import ( "fmt" "github.com/goinggo/mapstructure&qu ...
- 关于 Go 中 Map 类型和 Slice 类型的传递
关于 Go 中 Map 类型和 Slice 类型的传递 Map 类型 先看例子 m1: func main() { m := make(map[int]int) mdMap(m) fmt.Printl ...
- 【go】golang中置new()函数和make()函数的区别
Go语言中的内建函数new和make是两个用于内存分配的原语(allocation primitives),其功能相似,却有本质区别. 1.new 官方文档 // The new built-in f ...
- golang中的init函数以及main函数
首先我们看一个例子:init函数: init 函数可在package main中,可在其他package中,可在同一个package中出现多次. main函数 main 函数只能在package ma ...
- C++中,引用作为函数参数
引用作为函数参数 C++之所以增加引用类型, 主要是把它作为函数参数,以扩充函数传递数据的功能. ———————————————————— c++,函数传参:(1)将变量名作为实参和形参.这时传给形参 ...
- Java中能否利用函数参数来返回值
转自https://blog.csdn.net/da_da_xiong/article/details/70039532 我们在写代码时通常会遇到一种情况,就是我们可能希望在一个函数操作完成后返回两个 ...
- Golang中map的三种声明方式和简单实现增删改查
package main import ( "fmt" ) func main() { test3 := map[string]string{ "one": & ...
- JavaScript中函数参数的值传递和引用传递
结论: 对于数字.字符串等基本类型变量,是将它们的值传递给了函数参数,函数参数的改变不会影响函数外部的变量. 对于数组和对象等是将对象(数组)的变量的值传递给了函数参数,这个变量保存的指向对象(数组) ...
- [GO]map做函数参数
package main import "fmt" func test(m map[int]string) { delete(m, ) } func main() { m := m ...
随机推荐
- 部署django到服务器
部署 服务器环境配置 在本地的虚拟环境中,项目根目录下,执行命令收集所有的包 pip freeze > plist.txt 安装并创建虚拟环境,如已创建则跳过此步 sudo apt-get in ...
- linux命令之------touch命令
touch命令 1) 作用:用于修改文件或目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 2) -a:改变档案的读取时间记录: 3) -m:改变档案的 ...
- R语言中查询帮助
可以尝试下面的几种方式 help(lapply,package=,....)?lapply??lapplyhelp.search('lapply')apropos('norm') #函数名记不全时用? ...
- Fluent Meshing生成空心球体网格(空心部分不生成网格)
原视频下载地址: https://pan.baidu.com/s/1OJdHuIJd7pAr7cHgVCD_0g 密码: puiq
- 启动uiautomatorview 提示无法初始化主类
启动uiautomatorview 提示无法初始化主类, 重新安装jdk到1.8版本就好了,就是这么神奇.
- Spring Transaction 使用入门
一.开篇陈述 1.1 写文缘由 最近在系统学习spring框架IoC.AOP.Transaction相关的知识点,准备写三篇随笔记录学习过程中的感悟.这是第一篇,记录spring Transactio ...
- java基础之 clone
参考文档:深拷贝&浅拷贝:http://blog.csdn.net/cws1214/article/details/52193341 克隆的分类: (1)浅克隆(shallow clone) ...
- 经典算法(四) 数组相关 & 螺旋矩阵 & 数字大小写转换 & 字符串相关
一.求所有子数组的和的最大值 public static void main(String[] args) { int[] a = { 1, -2, 3, 10, -4, 7, 2, -5 }; Fi ...
- mysql将多条结果拼接成一条结果
1,实际数据 SELECT resource_id, resource_type FROM res_resource_mount 2,拼接之后数据 SELECT c.resource_id, GROU ...
- Server Tomcat v8.5 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
Server Tomcat v9.0 Server at localhost was unable to start within 45 seconds. If the server requires ...