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

proposal: encoding/json: ordered struct tag for maps #47177

Closed
dans-stuff opened this issue Jul 13, 2021 · 0 comments
Closed

proposal: encoding/json: ordered struct tag for maps #47177

dans-stuff opened this issue Jul 13, 2021 · 0 comments

Comments

@dans-stuff
Copy link

dans-stuff commented Jul 13, 2021

Description

Add a struct tag for maps to sort by key while marshalling.

type MyStruct struct {
	Results map[string]int `json:",ordered"`
}

This would collect the keys of the map (which always resolve to strings), sort them using sort.Strings, and then encode each key/value in that order. This proposal is in response to seeing 2 instances of people asking how to encode a map with consistent ordering.

Edit: This would also help with unit testing.

Edit2: It seems this is already implemented without a struct tag - disregard.

@gopherbot gopherbot added this to the Proposal milestone Jul 13, 2021
@golang golang locked and limited conversation to collaborators Jul 13, 2022
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

2 participants