Created
February 21, 2025 10:20
-
-
Save thebirk/9582f566362a95f359e191299b232a7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TabWriteSliceOfStructs[E any](w io.Writer, d []E) error { | |
t := reflect.TypeOf(d).Elem() | |
for i := 0; i < t.NumField(); i++ { | |
if i > 0 { | |
if _, err := w.Write([]byte{'\t'}); err != nil { | |
return err | |
} | |
} | |
if _, err := w.Write([]byte(t.Field(i).Name)); err != nil { | |
return err | |
} | |
} | |
if _, err := w.Write([]byte{'\n'}); err != nil { | |
return err | |
} | |
for _, dd := range d { | |
v := reflect.ValueOf(dd) | |
for i := 0; i < v.NumField(); i++ { | |
if i > 0 { | |
if _, err := w.Write([]byte{'\t'}); err != nil { | |
return err | |
} | |
} | |
if _, err := w.Write([]byte(fmt.Sprint(v.Field(i).Interface()))); err != nil { | |
return err | |
} | |
} | |
if _, err := w.Write([]byte(b.String())); err != nil { | |
return err | |
} | |
if _, err := w.Write([]byte{'\n'}); err != nil { | |
return err | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment