🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

github.com/sfomuseum/go-csvdict

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/sfomuseum/go-csvdict

v1.0.0
Source
Go
Version published
Created
Source

go-csvdict

Go package to implement a "dict reader" style CSV parser (on top of the default encoding/csv package) to return rows a key-value dictionaries rather than lists.

Documentation

Go Reference

Example

Reading files

import (
        "github.com/whosonfirst/go-csvdict"
	"os"
)

reader, reader_err := csvdict.NewReaderFromPath("example.csv")

// or maybe you might do
// reader, err := csvdict.NewReader(os.Stdin)

if err != nil {
	panic(err)
}

for {
	row, err := reader.Read()

	if err == io.EOF {
		break
	}

	if err != nil {
		return err
	}

	value, ok := row["some-key"]

	// and so on...
}

Writing files

import (
	"github.com/whosonfirst/go-csvdict"
	"os"
)

fieldnames := []string{"foo", "bar"}

writer, err := csvdict.NewWriter(os.Stdout, fieldnames)

// or maybe you might do
// writer, err := csvdict.NewWriterFromPath("new.csv", fieldnames)

if err != nil {
	panic(err)
}

writer.WriteHeader()

row := make(map[string]string)
row["foo"] = "hello"
row["bar"] = "world"

// See this? "baz" is not included in the list of fieldnames
// above so it will be silently ignored and excluded from your
// CSV file. Perhaps it should trigger an error. It doesn't, today...

row["baz"] = "wub wub wub"

writer.WriteRow(row)

See also

FAQs

Package last updated on 21 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts