doufeng2877 2016-01-16 07:24
浏览 28
已采纳

从地图获取价值时出现意外价值

So I have a struct like this:

type Magni struct {
    ...
    Handlers map[string]func(*Message)
    ...
}

And I have a function to create a new instance of the struct:

func New(nick, user, real string) *Magni {
    return &Magni{
        ...
        Handlers: make(map[string]func(*Message)),
        ...
    }
}

But I can't get something from the Handlers map with the key "hey" when "hey" is in a variable, it only works if I type it myself. Here is a method of the struct Magni and m is a pointer to the struct Magni:

handler := m.Handlers[cmd[3][1:]] // cmd[3][1:] contains string "hey"
handler2 := m.Handlers["hey"]

For some reason, the value of handler is nil and the value of handler2 is 0x401310, basically I am not expecting handler to be nil.

Am I doing something wrong or is this the expected behavior?

  • 写回答

2条回答 默认 最新

  • dsmvqp3124 2016-01-17 11:24
    关注

    Trimming the string solves the problem.

    strings.TrimSpace(cmd[3])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?