I am very new go Golang and my question is not cleared also, but this is what I am trying to achieve. I have a csv file as follow, as I am mainly trying to re-arrange/sort last column(status=passed,failed/skipped)
test,test-cat,skipped
test,test-cat,failed
test,test-cat,passed
test,test-cat,skipped
test,test-cat,passed
test,test-cat,failed
Expecting last column to be grouped them together if it has same status.
test,test-cat,skipped
test,test-cat,skipped
test,test-cat,failed
test,test-cat,failed
test,test-cat,passed
test,test-cat,passed
With this codes I did, it does not look good:-) but it works as I wanted.
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
var FailStat, SkipStat,PassStat []string
file, err := os.Open("test.csv")
if err != nil {
fmt.Println(err)
} else {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "failed") {
FailStat = append(FailStat, line)
}
if strings.Contains(line, "skipped") {
SkipStat = append(SkipStat, line)
}
if strings.Contains(line, "passed") {
PassStat = append(PassStat, line)
}
}
}
file.Close()
var finalstat []string
finalstat = append(SkipStat, FailStat...)
finalstat = append(finalstat, PassStat...)
for _, line := range finalstat {
fmt.Println(line)
}
}
Test-Run:
$ ./readfile
test,test-cat,skipped
test,test-cat,skipped
test,test-cat,failed
test,test-cat,failed
test,test-cat,passed
test,test-cat,passed
There must be a many better ways, please advice. Sorry for newbie question!