package main
import "fmt"
type MyClass struct{
data string
}
func (this MyClass) MyMethod() {
this.data = "Changed!"
}
func main() {
obj := MyClass{}
obj.MyMethod()
fmt.Println(obj)
}
I need that the data
property gets changed by MyMethod()
, but I cannot change the receiver type to pointer (func (this *MyClass)
) because it must satisfy an interface whose receiver is not a pointer, can this achieved some other way?
You need to use a pointer receiver, not a value receiver:
func (this *MyClass) MyMethod() {
this.data = "Changed!"
}
See your modified example in play.golang.org:
The output is:
{Changed!}