douhua1890 2017-08-25 04:08
浏览 10
已采纳

结构字段还原[重复]

This question already has an answer here:

I'm playing with Go a bit but found this weird situation while doing some tests.

I'm using method in a struct to send a variable to another method that should change a field, but when I check it at the end, the field goes back to the first value, which has me confused.

func (this TVManager) sendMessage(message string) {
    fmt.Println("5", this.connector)
    payload := map[string]string {
       "id": "0",
       "type": "request",
       "uri": "ssap://system.notifications/createToast",
       "payload": "{'message': 'This is a message'}"}
    this.connector.sendCommand(payload)
    fmt.Println("4", this.connector)
}

This is the method I'm testing, it calls sendCommand of connector.

func (this MockConnector) sendCommand(payload map[string]string) {
    fmt.Println("0", this)
    this.last_command = payload
    this.value = true
    fmt.Println("0", this)
}

Which in the mock object I'm using is simply changing the value of this struct fields.

    manager.sendMessage("This is a message")

fmt.Println("1", connector)
assert.Equal(t, expected, connector.last_command, "Command should be equal")

But when I check it, it goes back to internal. I set some prints to try an d track the values and they change the values as expected, but then it reverts.

 1 {false map[]}
 5 {false map[]}
 0 {false map[]}
 0 {true map[uri:ssap://system.notifications/createToast payload:{'message': 'This is a message'} id:0 type:request]}
 4 {false map[]}
 1 {false map[]}
 --- FAIL: TestTVManagerSendsNotificationDownToConnector (0.00s)

This is just a small program I'm going over to learn some Go, so I appreciate any help anybody could give me.

</div>
  • 写回答

1条回答 默认 最新

  • dongzaobei0942 2017-08-25 04:32
    关注

    You are passing the structures by value. This works fine so long as you are not modifying the structure, but if you do modify it you are actually only modifying a copy. To make this work you need to use pointers to the structures you need to modify.

    Instead of:

    func (this MockConnector) sendCommand(payload map[string]string)
    

    Use:

    func (this *MockConnector) sendCommand(payload map[string]string)
    

    Also, it is considered a bad idea to use this (or self) as a receiver name in Go, as a receiver is not the same thing as a this pointer/reference in other languages.

    Another best practice, is if one method for a given type needs a pointer receiver, all methods for that type should have pointer receivers. This is so that the method set remains consistent no matter if the value is a pointer or not.

    See method sets, and these FAQ answers for more information.

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题