I want to replace an interface's value like this:
package main
import "fmt"
type Fooer interface {Foo(string)}
type Foo struct {foo string}
func (f *Foo) Foo(bar string) {f.foo = bar}
var z = &Foo{foo : "new"}
func swap(fooer Fooer) {fooer = z}
func main() {
f := &Foo{foo: "old"}
fmt.Printf("%s (want &{old})
", f)
swap(f)
fmt.Printf("%s (want &{new})", f)
}
But i get:
&{old}
&{old}
I tried around with various calls (fooer *= z
, *fooer = *z
, ..) but I cant seem to get it right.
You can try this example at play.golang: http://play.golang.org/p/EZEh3X8yHC
Okay, I think it works like this:
func swap(fooer Fooer) {
foo, _ := fooer.(*Foo)
*foo = *z
}