douyao1856 2016-11-18 15:42
浏览 152
已采纳

Go中的地图是按值传递还是按引用传递?

Are maps passed by value or reference in Go ?

It is always possible to define a function as following, but is this an overkill ?

func foo(dat *map[string]interface{}) {...}

Same question for return value. Should I return a pointer to the map, or return the map as value ?

The intention is of course to avoid unnecessary data copy.

  • 写回答

3条回答 默认 最新

  • duanben1324 2016-11-18 15:44
    关注

    In this thread you will find your answer :

    Golang: Accessing a map using its reference

    You don't need to use a pointer with a map.

    Map types are reference types, like pointers or slices[1]

    If you needed to change the Session you could use a pointer:

    map[string]*Session
    

    https://blog.golang.org/go-maps-in-action

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 数学建模数学建模需要
  • ¥15 c语言数据结构实验单链表的删除
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决