How to sort struct fields in alphabetical order
问题 How could I get an output of struct, sorted by fields? type T struct { B int A int } t := &T{B: 2, A: 1} doSomething(t) fmt.Println(t) // &{1 2} --> Sorted by fields 回答1: A struct is an ordered collection of fields. The fmt package uses reflection to get the fields and values of a struct value, and generates output in the order in which they were defined. So the simplest solution would be to declare your type where you already have your fields arranged in alphabetical order: type T struct { A