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 \n", *n1)
UnMask(&n1, "Joe")
fmt.Printf("Hello, %s \n", *n1)
n2 := st.msg.To()
fmt.Printf("Hello, %s \n", *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?
Unmask takes a pointer to a pointer, let's say pointer X to pointer Y, pointer Y points to the string value. Unmask then changes the pointer to which X is pointing to, Y is unchanged and points still to the same old string.
You can do this:
func UnMask(n **string, k string) {
**n = k
}
or
func UnMask(n *string, k string) {
*n = k
}
// ....
UnMask(n1, "Joe") // drop the '&'