I have a following example https://play.golang.org/p/xgvFRVIrOZr
package main
import (
"fmt"
)
// Getter sets a string
type Getter interface {
Get() string
}
// Setter sets a string
type Setter interface {
Set(string)
}
// A is a demo struct
type A struct {
s string
}
func (a A) Get() string {
return a.s
}
func (a *A) Set(s string) {
a.s = s
}
// NewGetter returns new Getter interface
func NewGetter() Getter {
return A{s: "Hello World!"}
}
func main() {
a := NewGetter()
fmt.Println(a.Get()) // Prints Hello World!
// Following does not work and it is ok.
// a.(Setter).Set("Hello Earth!") // panic: interface conversion: main.A is not main.Setter: missing method Set
// Following does not work and it is ok, but weird.
// (&a).(Setter).Set("Hello Earth!") // build failed: invalid type assertion: (&a).(Setter) (non-interface type *Getter on left)
// Following does not work, but wait, what works than?
// var i interface{} = &a
// i.(Setter).Set("Hello Earth!") // panic: interface conversion: *main.Getter is not main.Setter: missing method Set
// Following works, but how is it different from the previous example?
var j interface{} = &A{
s: "Hello World!",
}
j.(Setter).Set("Hello Earth!")
}
My task is really simple, I want to convert a variable from one interface to another. Is there a way to do so?
P.S.
I know how to do that using a reflection, but I am looking for a more simpler solution, any ideas?