http://play.golang.org/p/JJnU5ag234
I can only make vA work directly, if I want to use my vI to store A or B in it depending on the json I expect, I get
json: cannot unmarshal object into Go value of type main.TA
package main
import (
"encoding/json"
"fmt"
"strings"
)
type T interface {
Printer()
}
type A struct{ JA string }
func (t A) Printer() { fmt.Print("A") }
type B struct{ JB string }
func (t B) Printer() { fmt.Print("B") }
var vA []A
var vB []B
var vI []T
func main() {
// vA = []A{A{}}
// vI = []T{B{}}
vI = []T{A{}}
get()
}
func get() {
dec := json.NewDecoder(strings.NewReader("[{\"JA\":\"OK\"}]"))
if err := dec.Decode(&vI); err != nil {
fmt.Print(err)
}
for _, v := range vI {
v.Printer()
}
}