Skip to content

encoding/json: marshal struct with interface generates a panic #64057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Chise1 opened this issue Nov 10, 2023 · 1 comment
Closed

encoding/json: marshal struct with interface generates a panic #64057

Chise1 opened this issue Nov 10, 2023 · 1 comment

Comments

@Chise1
Copy link

Chise1 commented Nov 10, 2023

What version of Go are you using (go version)?

go version go1.21rc3 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

GOHOSTARCH='amd64'
GOHOSTOS='linux'

What did you do?

Marshal my struct error,code like this:

package main

import (
	"encoding/json"
	"fmt"
)

type W2 interface {
	json.Marshaler
	json.Unmarshaler
}

type Data struct {
	ID string `json:"id"`
	W2 `json:"-"`
}

func main() {
	var d Data
	marshal, err := json.Marshal(d)
	if err != nil {
		return
	}
	fmt.Println(string(marshal))
}

and I got error:


panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x49471b]



What did you expect to see?

Struct W2's tag is ``json:"-"` ,may not be check in marshal?

What did you see instead?

And I got a panic.

@seankhliao
Copy link
Member

Because of embedding, the methods get promoted.
closing as while unfortunate, this is how the language works.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Nov 10, 2023
@golang golang locked and limited conversation to collaborators Nov 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants