Consider the following gist linked here:
Code:
package main
import (
"fmt"
)
type StateTransition struct {
msg Message
}
type Message interface {
To() *string
}
type Transaction struct {
data txdata
}
type txdata struct {
Recipient *string
}
func (t Transaction) To() (*string) {
return t.data.Recipient
}
func UnMask(n **string, k string) {
*n = &k
}
func main() {
toField := "Bob"
toPtr := &toField
txd := txdata{toPtr}
tx := Transaction{txd}
st := StateTransition{tx}
n1 := st.msg.To()
fmt.Printf("Hello, %s
", *n1)
UnMask(&n1, "Joe")
fmt.Printf("Hello, %s
", *n1)
n2 := st.msg.To()
fmt.Printf("Hello, %s
", *n2)
}
Output
Hello, Bob
Hello, Joe
Hello, Bob
Expected Output
Hello, Bob
Hello, Joe
Hello, Joe
The result is the sequence "Bob, Joe, Bob" is printed whereas my intuition says that it should be "Bob, Joe, Joe" (this is also what i want it to print). Can someone experienced in go please explain to me enough about combining pointers, structs, and interfaces as they relate to this problem to give me some understanding about why I'm wrong, and how to fix it?