Created
September 30, 2020 05:41
-
-
Save kaihendry/dff2442de20d73f900026d13bf7a11d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"log" | |
"os" | |
) | |
func main() { | |
txtfile, err := os.Open("data.txt") | |
if err != nil { | |
log.Fatalln("Couldn't open the csv file", err) | |
} | |
r := csv.NewReader(txtfile) | |
r.Comma = ':' | |
records, err := r.ReadAll() | |
if err != nil { | |
log.Fatal(err) | |
} | |
w := csv.NewWriter(os.Stdout) | |
for _, record := range records { | |
if err := w.Write(record); err != nil { | |
log.Fatalln("error writing record to csv:", err) | |
} | |
} | |
// Write any buffered data to the underlying writer (standard output). | |
w.Flush() | |
if err := w.Error(); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment