@@ -178,24 +178,35 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
178178 writeByte (p , '\n' )
179179 pp = p .indent ()
180180 }
181+ type field struct {
182+ name string
183+ t reflect.Type
184+ value reflect.Value
185+ }
186+ fields := make ([]field , 0 , v .NumField ())
187+ // Collect fields, filtering out zero fields if needed
181188 for i := 0 ; i < v .NumField (); i ++ {
182- fValue := getField (v , i )
183- if p .skipZeroFields && ! nonzero (fValue ) {
189+ value := getField (v , i )
190+ if p .skipZeroFields && ! nonzero (value ) {
184191 continue
185192 }
193+ f := t .Field (i )
194+ fields = append (fields , field {f .Name , f .Type , value })
195+ }
196+ for i , field := range fields {
186197 showTypeInStruct := true
187- if f := t . Field ( i ); f . Name != "" {
188- io .WriteString (pp , f . Name )
198+ if field . name != "" {
199+ io .WriteString (pp , field . name )
189200 writeByte (pp , ':' )
190201 if expand {
191202 writeByte (pp , '\t' )
192203 }
193- showTypeInStruct = labelType (f . Type )
204+ showTypeInStruct = labelType (field . t )
194205 }
195- pp .printValue (fValue , showTypeInStruct , true )
206+ pp .printValue (field . value , showTypeInStruct , true )
196207 if expand {
197208 io .WriteString (pp , ",\n " )
198- } else if i < v . NumField ( )- 1 {
209+ } else if i < len ( fields )- 1 {
199210 io .WriteString (pp , ", " )
200211 }
201212 }
0 commit comments