I want set the “time” to the first key。
{"level":"info","msg":"","time":"2017-11-11T14:38:52-05:00"}
As far as I can tell this is not supported my default because the Field type is a map and map iteration in go is random. If you really need it you can create a custom formater using an array of string to set the order.
And another obstacle: the json marshaler outputs the keys of maps in alphabetical order:
Map values encode as JSON objects. The map's key type must either be a string, an integer type, or implement encoding.TextMarshaler. The map keys are sorted and used as JSON object keys by applying the following rules, subject to the UTF-8 coercion described for string values above:
- string keys are used directly
- encoding.TextMarshalers are marshaled
- integer keys are converted to strings
Struct fields are marshalled in declaration order.
@zhangdaoling basically you cannot. The JSONFormatter implementation is based in the encoding/json package from the standard library. This package does not provide any control on the order of json dictionnary key.
no activity since 2018, closing
Most helpful comment
And another obstacle: the json marshaler outputs the keys of maps in alphabetical order:
Struct fields are marshalled in declaration order.