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
Closed

Comments

@meilihao
Copy link

meilihao 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
Copy link
Member

mvdan 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 as completed Aug 9, 2018
@golang golang locked and limited conversation to collaborators Aug 9, 2019
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