Can an instance of a go type change its underlying value? Thanks to a previous question I know I can make my function's receiver a pointer and change struct fields if my type is a struct.
However, I'm interesting in changing the underlying type's value. Consider a small program like this
type Foo string
func (f *Foo) ChangeMe() {
val := Foo("Hello World")
f = &val
//fmt.Println(*f)
}
func main() {
f := Foo("Nope")
f.ChangeMe()
fmt.Println(f)
}
My naive assumption was "OK, I've created a new Foo("Hello World")
and then pointed f
at it. Since receiver is a pointer (*Foo
), this should change the value in main()
.
However, this does not work. The above program still prints out "Nope"
Interested both in what my incorrect assumption about go are, and any techniques for achieving what I want to do.