Skip to content

Instantly share code, notes, and snippets.

@pmn
Last active August 7, 2021 19:12
Show Gist options
  • Save pmn/5374494 to your computer and use it in GitHub Desktop.
Save pmn/5374494 to your computer and use it in GitHub Desktop.
Convert an interface{} containing a slice of structs to [][]string (used in CSV serialization) [golang reflection example] Runnable example here: http://play.golang.org/p/ZSJRxkpFs9
// Convert an interface{} containing a slice of structs into [][]string.
func recordize(input interface{}) [][]string {
var records [][]string
var header []string // The first record in records will contain the names of the fields
object := reflect.ValueOf(input)
// The first record in the records slice should contain headers / field names
if object.Len() > 0 {
first := object.Index(0)
typ := first.Type()
for i := 0; i < first.NumField(); i++ {
header = append(header, typ.Field(i).Name)
}
records = append(records, header)
}
// Make a slice of objects to iterate through & populate the string slice
var items []interface{}
for i := 0; i < object.Len(); i++ {
items = append(items, object.Index(i).Interface())
}
// Populate the rest of the items into <records>
for _, v := range items {
item := reflect.ValueOf(v)
var record []string
for i := 0; i < item.NumField(); i++ {
itm := item.Field(i).Interface()
record = append(record, fmt.Sprintf("%v", itm))
}
records = append(records, record)
}
return records
}
Copy link

ghost commented Dec 11, 2019

if it can help other people like it was for me

package convert

import (
	"reflect"
)

type PairKeyValue struct {
	Id    int
	Key   string
	Value int
}

// Convert an interface{} containing a slice of structs to []PairKeyValue

func ConvertInterface_A(input interface{}) []PairKeyValue {

	var records []PairKeyValue
	object := reflect.ValueOf(input)

	// Make a slice of objects to iterate through and populate the string slice
	var items []interface{}
	for i := 0; i < object.Len(); i++ {
		items = append(items, object.Index(i).Interface())
	}

	// Populate the rest of the items into <records>
	for _, v := range items {
		item := reflect.ValueOf(v)
		var record PairKeyValue

		itm0 := item.Field(0).Interface()
		itm1 := item.Field(1).Interface()
		itm2 := item.Field(2).Interface()

		record.Id = itm0.(int)
		record.Key = itm1.(string)
		record.Value = itm2.(int)

		records = append(records, record)
	}
	return records
}

// Convert an interface{} containing a struct to PairKeyValue

func ConvertInterface_B(input interface{}) PairKeyValue {

	var record PairKeyValue
	item := reflect.ValueOf(input)

	// Populate the rest of the items into <records>
	itm0 := item.Field(0).Interface()
	itm1 := item.Field(1).Interface()
	itm2 := item.Field(2).Interface()

	record.Id = itm0.(int)
	record.Key = itm1.(string)
	record.Value = itm2.(int)

	return record
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment