当你声明一个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. Vue中的native修饰符解析

    native修饰符 一般来说,vue本身提供了v-on:eventName这个语法来提供vue的时事件绑定,通常使用@eventName这个语法糖代替上述语法. 使用过程中没有考虑@eventName ...

  2. uni-app下拉刷新加载刷新数据

    onPullDownRefresh监听该页面用户下拉刷新事件需要在 pages.json 里开启 enablePullDownRefresh "globalStyle": { } ...

  3. MyBatis框架,增删改查

    一.recourses中核心配置文件mybatis-config.xml 二. recourse中jdbc.properties 三.entity实体类 四.Dao层 五.ISmbmsUserDao. ...

  4. dubbo简单示例

    dubbo简单示例 2019-09-06 1 Zookeeper注册中心的搭建(windows单机) 下载zookeeper压缩包并解压到 D:\zookeeper\apache-zookeeper- ...

  5. SpringBoot——配置文件加载位置及外部配置加载顺序

    声明 本文部分转自:SpringBoot配置文件加载位置与优先级 正文 1. 项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者applic ...

  6. [清华集训2015]灯泡(浙江大学ZOJ 3203 Light Bulb)

    Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildleopard's wealthiness, his brother ...

  7. arcpy地理处理工具案例教程-景观形状指数计算

    arcpy地理处理工具案例教程-景观形状指数计算 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 使用方法:输入要素类即可,其余参数均默认. 商务 ...

  8. ROC曲线 VS PR曲线

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&ut ...

  9. x3d

    目录 3d format introduction x3d resources open source C++ implementations file formats 1. Feature matr ...

  10. Springmvc 异步处理

    package com.lookcoder.haircutmember.controller.login.page.async; import org.slf4j.Logger; import org ...