Skip to content
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

encoding/json: marshal map[string]interface{} with error get empty object #26889

Closed
meilihao opened this issue Aug 9, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@meilihao
Copy link

commented Aug 9, 2018

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

go version go1.11beta3 linux/amd64

Does this issue reproduce with the latest release?

go version go1.10.3 linux/amd64

What did you do?

func main() {
	err := errors.New("abc")
	m := map[string]interface{}{
		"error": err,
	}
	data, _ := json.Marshal(m)
	fmt.Println(string(data))
}

What did you expect to see?

{"error":"abc"}

What did you see instead?

{"error":{}}
@mvdan

This comment has been minimized.

Copy link
Member

commented Aug 9, 2018

You're marshaling an interface. And it so happens that the errors package returns a struct with no exported fields, hence {}: https://golang.org/src/errors/errors.go?s=293:320#L1

If you want to print the error string, use "error": err.Error().

@mvdan mvdan closed this Aug 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.