Advertisement
cydside

CSV line by line reading

Apr 19th, 2023 (edited)
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.51 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/csv"
  5.     "fmt"
  6.     "io"
  7.     "log"
  8.     "os"
  9. )
  10.  
  11. func main() {
  12.     // open file
  13.     f, err := os.Open("data.csv")
  14.     if err != nil {
  15.         log.Fatal(err)
  16.     }
  17.  
  18.     // remember to close the file at the end of the program
  19.     defer f.Close()
  20.  
  21.     // read csv values using csv.Reader
  22.     csvReader := csv.NewReader(f)
  23.     for {
  24.         rec, err := csvReader.Read()
  25.         if err == io.EOF {
  26.             break
  27.         }
  28.         if err != nil {
  29.             log.Fatal(err)
  30.         }
  31.         // do something with read line
  32.         fmt.Printf("%+v\n", rec)
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement