结构字段恢复 [英] Struct field reverts

查看:109
本文介绍了结构字段恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在结构中使用方法将变量发送给另一个方法应该改变一个字段,但是当我在最后检查它时,字段会返回到第一个值,这让我感到困惑。

  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)
}

这是我测试的方法,它调用连接器的sendCommand。

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

在我使用的模拟对象中,只需更改此struct字段的值即可。

  manager .sendMessage(This is a message)

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

但是当我检查它时,它会回到内部。

  1 {我设置了一些打印以尝试d跟踪值,并且它们按预期更改了值,但是它会恢复。 false map []} 
5 {false map []}
0 {false map []}
0 {true map [uri:ssap://system.notifications/createToast payload:{ 'message':'这是一条消息'} id:0 type:request]}
4 {false map []}
1 {false map []}
--- FAIL: TestTVManagerSendsNotificationDownToConnector(0.00s)

这只是我要学习一些Go的一个小程序,所以我很感激任何人都可以给我的帮助。 你正在通过价值传递结构。只要你不修改结构,这工作正常,但如果你修改它,你实际上只是修改一个副本。要做到这一点,您需要使用指针来指定需要修改的结构。



而不是:

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

使用:

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

另外,使用 this (或 self )作为Go中的接收者名称,因为接收者与 this

另一个最佳实践是,如果给定类型的一个方法需要指针接收器,则该类型的所有方法都应该有指针接收器。这样,无论该值是否为指针,方法集都保持一致。

请参阅方法集,以及这些 常见问题 回答了解更多信息。


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.

解决方案

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.

这篇关于结构字段恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆