O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter02/recipe10.
  2. Navigate to the directory.
  3. Create a file named data.csv with the following content:
        "Name","Surname","Age"        # this is comment in data        "John","Mnemonic",20        Maria,Tone,21
  1. Create the data.go file with the following content:
        package main        import (          "encoding/csv"          "fmt"          "os"        )        func main() {          file, err := os.Open("data.csv")          if err != nil {            panic(err)          }          defer file.Close()          reader := csv.NewReader(file)          reader.FieldsPerRecord = 3          reader.Comment = '#'          for {            record, e := reader.Read()            if e != nil {              fmt.Println(e)              break            }            fmt.Println(record)          }        }
  1. Run the code by executing go run data.go.
  2. See the output in the Terminal:
  1. Create a file named data_uncommon.csv with the following content: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required