gocsv.go Simple CSV parsing with GO

gocsv.go Simple CSV parsing with GO

gocsv go simple csv parsing with go


Recently I was working with CSV files in Ruby. Parsing CSV files in Ruby code is easy, thanks to Ruby/csv.

Let’s try it with golang.

Go seems to be the pretty power-packed language for developers. Go, also commonly referred to as golang, is a programming language initially developed at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is a statically-typed language with a syntax loosely derived from that of C, adding garbage collection, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library. And since it’s from Google, the big giant, Go has built-in support for concurrency with go-routines, channels and select.

Let’s get to work now.


//Simple CSV reader
package main
import (
encoding/csv //Package csv reads and writes comma-separated values (CSV) files.
fmt //Package fmt implements formatted I/O with functions analogous to C’s printf and scanf.
io //Package io provides basic interfaces to I/O primitives.
os //Package os provides a platform-independent interface to operating system functionality.
// Ref http://golang.org/pkg/ for more on packages and links to documentation
func main() {
//Check for command-line argument filename.
//Ignore additional arguments.
if len(os.Args) < 2 {
fmt.Printf(Error: Source file name is required\n)
fmt.Println(Usage:, os.Args[0], <filename> \n)
file, err := os.Open(os.Args[1])
if err != nil {
fmt.Println(Error:, err)
// deferred call to Close() at the end of current method
defer file.Close()
//get a new cvsReader for reading file
reader := csv.NewReader(file)
//Configure reader options Ref http://golang.org/src/pkg/encoding/csv/reader.go?s=#L81
reader.Comma = ; //field delimiter
reader.Comment = # //Comment character
reader.FieldsPerRecord = -1 //Number of records per record. Set to Negative value for variable
reader.TrimLeadingSpace = true
lineCount := 1
for {
// read just one record, but we could ReadAll() as well
record, err := reader.Read()
// end-of-file is fitted into err
if err == io.EOF {
} else if err != nil {
fmt.Println(Error:, err)
lineCount += 1
// record is array of strings Ref http://golang.org/src/pkg/encoding/csv/reader.go?s=#L134
fmt.Printf(Record %d: %s\n, lineCount, record)
lineCount += 1
view raw gocsv.go hosted with ❤ by GitHub


Here is a sample CSV file for tests.






About RemotePanda

RemotePanda is a personalized platform for companies to hire remote talent and get the quality work delivered from the city Pune. The resources in our talent pool are our close network connections. While connecting them with you, we make sure to manage the quality, growth, legalities, and the delivery of their work. The idea is to make remote work successful for you. Get in touch with us to learn why RemotePanda is the best fit solution for your business requirements.

Comments are closed.