Parse Custom Delimeter Seperated File In Golang With encode/csv Package

Encode/csv Package Golang
Encode/csv Package Golang


Package csv reads and writes comma-separated values (CSV) files. There are many kinds of CSV files; this package supports the format described in RFC 4180.
A csv file contains zero or more records of one or more fields per record. Each record is separated by the newline character. The final record may optionally be followed by a newline character.
CSV stands for the " comma seperated values ", But you can parse the file with any custom defined delimeter with the encode/csv package of golang.
For example ,you can use pipe ( | ) symbol or underscoer (_) as delimiter insted of comma.


Writing A Pipe Seperated File With encode/csv

Here is the example in which we are encoding the string of array to a csv

Here we are sending output to standard output. But you can also store it in file.

Output Is

First_name|last_name|username
Rob|Pike|rob
Ken|Thompson|ken
Robert|Griesemer|gri





You can also read a file with custom delimiter.The paramter that defines the delimiter is w.CommaHere Comma field defines the rune that has to be considerd as the delimiter.

For Note,
By Default value for .Comma is ',' but you can set it to any rune.


Comments

Popular Posts