package main import ( "encoding/csv" "log" "os" ) type Record struct { Name string Surname string Number string LastAccess string } var myData = []Record{} func readCSVFile(filepath string) ([][]string, error) { _, err := os.Stat(filepath) if err != nil { return nil, err } f, err := os.Open(filepath) if err != nil { return nil, err } defer f.Close() // CSV file read all at once // lines data type is [][]string lines, err := csv.NewReader(f).ReadAll() if err != nil { return [][]string{}, err } return lines, nil } func saveCSVFile(filepath string) error { csvFile, err := os.Create(filepath) if err != nil { return err } defer csvFile.Close() csvWriter := csv.NewWriter(csvFile) // Change the default field delimiter to tab csvWriter.Comma = '\t' for _, row := range myData { record := []string{row.Name, row.Surname, row.Number, row.LastAccess} err = csvWriter.Write(record) if err != nil { return err } } csvWriter.Flush() return nil } func main() { if len(os.Args) != 3 { log.Println("csv_data input output!") os.Exit(1) } input := os.Args[1] output := os.Args[2] lines, err := readCSVFile(input) if err != nil { log.Println(err) os.Exit(1) } for _, line := range lines { record := Record{ Name: line[0], Surname: line[1], Number: line[2], LastAccess: line[3], } myData = append(myData, record) log.Println(record) } err = saveCSVFile(output) if err != nil { log.Println(err) os.Exit(1) } }