gocarina / gocsv

The GoCSV package aims to provide easy CSV serialization and deserialization to the golang programming language
MIT License
1.95k stars 243 forks source link

Support []interface{} as json package does #267

Open CaledoniaProject opened 9 months ago

CaledoniaProject commented 9 months ago

Hi there,

I'm wondering why encoding/json could work with []interface{} but not gocsv, can you explain why won't you implement that?

I had to do a lot a reflect job to fix this, it looks very much unnecessary to me, e.g

func DoEncode(tx *gorm.DB, resultModel interface{}, query string) {
  var (
    result = reflect.New(resultType).Interface()
  )

  // use gorm to fill result repeatly
  ...
  tx.ScanRows(rows, result)
  ...

  resultSlice := reflect.MakeSlice(reflect.SliceOf(resultType), 1, 1)
  resultSlice = reflect.Append(resultSlice, reflect.ValueOf(result).Elem())

  data, err := gocsv.MarshalString(resultSlice.Interface())
}