英文:
How to store different structs in a interface for json
问题
我只能直接让vA工作,如果我想根据我期望的JSON将A或B存储在vI中,我会得到以下错误:
json: 无法将对象解组为类型为main.TA的Go值
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()
}
}
请帮我翻译一下。
英文:
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()
}
}
答案1
得分: 2
由于您希望解码器填充结构体的字段,您需要使用指针。像这样在类型的指针上定义接口的方法:http://play.golang.org/p/WUMt9Ok9Xp
package main
import (
"encoding/json"
"fmt"
"strings"
)
type T interface {
Printer()
}
type A struct {
JA string
}
func (a *A) Printer() {
fmt.Printf("A: %v\n", a.JA)
}
type B struct {
JB string
}
func (b *B) Printer() {
fmt.Printf("B: %v\n", b.JB)
}
func main() {
vI := []T{&A{}, &B{}}
dec := json.NewDecoder(strings.NewReader("[{\"JA\":\"OKA\"}, {\"JB\":\"OKB\"}]"))
if err := dec.Decode(&vI); err != nil {
fmt.Print(err)
}
for _, v := range vI {
v.Printer()
}
}
希望对您有所帮助!
英文:
Since you expect the decoder to fill the fields of a struct, you have to use pointers. Define methods of the interface on the pointer of the type like this: http://play.golang.org/p/WUMt9Ok9Xp
package main
import (
"encoding/json"
"fmt"
"strings"
)
type T interface {
Printer()
}
type A struct {
JA string
}
func (a *A) Printer() {
fmt.Printf("A: %v\n", a.JA)
}
type B struct {
JB string
}
func (b *B) Printer() {
fmt.Printf("B: %v\n", b.JB)
}
func main() {
vI := []T{&A{}, &B{}}
dec := json.NewDecoder(strings.NewReader("[{\"JA\":\"OKA\"}, {\"JB\":\"OKB\"}]"))
if err := dec.Decode(&vI); err != nil {
fmt.Print(err)
}
for _, v := range vI {
v.Printer()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论