dsgrs26202 2012-10-19 00:08
浏览 68
已采纳

前往:使用自动返回值初始化地图

If I declare a map[string]string return value in a function definition, do I have to make it before using it, just like if I had instead declared it in the function body? http://play.golang.org/p/iafZbG2ZbY

package main

import "fmt"

func fill() (a_cool_map map[string]string) {
    // This fixes it: a_cool_map = make(map[string]string)
    a_cool_map["key"] = "value"
    return
}
func main() {
    a_cool_map := fill()
    fmt.Println(a_cool_map)
}

panic: runtime error: assignment to entry in nil map

  • 写回答

1条回答 默认 最新

  • drgd73844 2012-10-19 00:48
    关注

    Map types

    The value of an uninitialized map is nil.

    A new, empty map value is made using the built-in function make.

    A nil map is equivalent to an empty map except that no elements may be added.

    Yes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用