Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Shadowing promoted methods with a struct field is not working #1228

Open
@flimzy

Description

@flimzy

It's unclear yet whether this is a bug in the GopherJS compiler, or some customizations we've made either to reflect or encoding/json. I'll try to investigate when I have time. But consider the following code:

package main

import (
	"encoding/json"
	"fmt"
)

type Foo struct {
	Foo string `json:"foo"`
}

func (f *Foo) UnmarshalJSON(p []byte) error {
	t := struct {
		Foo
		UnmarshalJSON struct{}
	}{}
	return json.Unmarshal(p, &t)
}

func main() {
	input := []byte(`{"foo":"a string"}`)
	var f Foo
	_ = json.Unmarshal(input, &f)
	fmt.Println(f)
}

In the Go playground, it works as expected, with the UnmarshalJSON struct field shadowing the promoted UnmarshalJSON method on the embedded Foo type, and thus producing the output {}.

However, in GopherJS, it appears we attempt to execute the non-function, and produce a runtime panic: h.UnmarshalJSON is not a function.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions