-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Labels
Milestone
Description
package main
import (
"fmt"
"encoding/json"
)
type Interface interface {
Inter()
}
type A struct {
AValue string
}
func(a *A)Inter() {}
type B struct {
BValue string
}
func(b *B)Inter() {}
func main() {
var i Interface
err := json.Unmarshal([]byte("{\"BValue\":\"B\"}"), &i)
if err != nil {
fmt.Println(err)
}
}error:json: cannot unmarshal object into Go value of type main.Interface.
A and B implemented Unmarshaler :
package main
import (
"fmt"
"encoding/json"
)
type Interface interface {
Inter()
UnmarshalJSON([]byte) error
}
type A struct {
AValue string
}
func(a *A)Inter() {}
func(a *A)UnmarshalJSON([]byte) error{
return nil
}
type B struct {
BValue string
}
func(b *B)Inter() {}
func(b *B)UnmarshalJSON([]byte) error{
return nil
}
func main() {
var i Interface
err := json.Unmarshal([]byte("{\"BValue\":\"B\"}"), &i)
if err != nil {
fmt.Println(err)
}
}The same error,also not ok.
But Marshaler could marshaled.
OneOfOne