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 it works...

Instead of simply scanning the input line by line and using strings.Split and other methods to parse the CSV format, Go offers a better way. The NewReader function in the encoding/csv package returns the Reader structure, which provides the API to read the CSV file. The Reader struct keeps variables to configure the read parameters, according to your needs.

The FieldsPerRecord parameter of Reader is a significant setting. This way the cell count per row could be validated. By default, when set to 0, it is set to the number of records in a first line. If a positive value is set, the number of records must match. If a negative value is set, there is no cell count validation.

Another interesting configuration is the Comment

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