<分区>
我正在玩 Go,但在做一些测试时发现了这种奇怪的情况。
我正在使用结构中的方法将变量发送到另一个应该更改字段的方法,但是当我最后检查它时,该字段返回到第一个值,这让我感到困惑。
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)
}
我在模拟对象中使用的只是更改此结构字段的值。
manager.sendMessage("This is a message")
fmt.Println("1", connector)
assert.Equal(t, expected, connector.last_command, "Command should be equal")
但是当我检查它时,它又回到了内部。我设置了一些打印品来尝试跟踪这些值,它们按预期更改了值,但随后又恢复了。
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)
这只是一个小程序,我将通过它来学习一些 Go,所以我很感激任何人能给我的帮助。