Here is how I have done it and it takes few seconds to do it. Below is code extract. "rowMeta.ColsInfo" has information on what is to be processed, i.e. which keys to extract unique values for as there are many other columns that should be ignored. For each column to be looked for unique values I am starting a routine. That routine will find the unique values using a temp map and appends it to the allocated internal slice inside rowsTowrite.
func ProcessArrayForUniqueValues(repeatData []interface{}, rowMeta *excel.RowInfo, csvfile *csvfile.CSVReport) {
var rowsTowrite = make([][]string, len(rowMeta.ColsInfo), len(rowMeta.ColsInfo))
var wg sync.WaitGroup
for cn, col := range rowMeta.ColsInfo {
if col.Key != "" {
wg.Add(1)
go func(index int, colInfo excel.ColInfo, data []interface{}) {
defer wg.Done()
kmap := make(map[string]bool)
uvalues := make([]string, 0, 500)
for _, rowRefTmp := range data {
rowref := rowRefTmp.(map[string]interface{})
uvalue := fmt.Sprint(rowref[colInfo.Key])
_, ok := kmap[uvalue]
if !ok {
kmap[uvalue] = true
uvalues = append(uvalues, uvalue)
}
}
rowsTowrite[index] = uvalues
}(cn, *col, repeatData)
}
}
wg.Wait()
//write to file
maxColLength := 0
for cn, _ := range rowsTowrite {
slicelen := len(rowsTowrite[cn])
if slicelen > maxColLength {
maxColLength = slicelen
}
}
for i := 0; i < maxColLength; i++ {
var record []string
for j := 0; j < len(rowsTowrite); j++ {
tmpslice := rowsTowrite[j]
if len(tmpslice) > i {
record = append(record, tmpslice[i])
} else {
record = append(record, "")
}
}
csvfile.Write(&record)//todo: send in batches of [][]string to WriteAll
}
}