Open urbim opened 6 months ago
sample.go
package main import ( "encoding/json" "fmt" ) type Foo interface { } type Foo1 struct{ Bar1 string } func (f Foo1) MarshalJSON() ([]byte, error) { return []byte(`{"test": "foo"}`), nil } func main() { f1 := Foo1{ Bar1: "bar1", } printJson(f1) } func printJson(f Foo) { marshal, err := json.Marshal(f) if err != nil { panic(err) } fmt.Println(string(marshal)) }
{"test":"foo"}
null
v0.15.1
Another sample with different result:
package main import ( "encoding/json" "fmt" ) type Foo interface { } type Foo1 struct{ Bar1 string } func (f Foo1) MarshalJSON() ([]byte, error) { return []byte(`{"test": "foo"}`), nil } type Foo2 struct{ Bar2 string Foo1 } func main() { f1 := Foo1{ Bar1: "bar1", } f2 := Foo2{ Bar2: "bar2", Foo1: f1, } printJson(f2) } func printJson(f Foo) { marshal, err := json.Marshal(f) if err != nil { panic(err) } fmt.Println(string(marshal)) }
{"Bar2":"bar2","Bar1":"bar1"}
The following program
sample.go
triggers an unexpected resultExpected result
Got
Yaegi Version
v0.15.1
Additional Notes
Another sample with different result:
Expected result
Got