csvutils

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2026 License: GPL-3.0 Imports: 3 Imported by: 0

README

CSV Utils

Tests Status Go Report Card PkgGoDev

Simple Go utilities for reading CSV files into arrays or maps.

Installation

go get github.com/dracory/csvutils

Usage

ToArrays

Reads a CSV file and returns an array with each line as an array of strings.

package main

import (
    "fmt"
    "log"
    "github.com/dracory/csvutils"
)

func main() {
    records, err := csvutils.ToArrays("data.csv")
    if err != nil {
        log.Fatal(err)
    }

    // records[0] contains headers
    // records[1:] contains data rows
    for _, row := range records {
        fmt.Println(row)
    }
}
ToMaps

Reads a CSV file and returns an array of maps using the header row as keys.

package main

import (
    "fmt"
    "log"
    "github.com/dracory/csvutils"
)

func main() {
    // Optional: replace header names
    replacements := map[string]string{
        "First Name": "first_name",
        "Last Name":  "last_name",
    }

    records, err := csvutils.ToMaps("data.csv", replacements)
    if err != nil {
        log.Fatal(err)
    }

    for _, row := range records {
        fmt.Printf("Name: %s %s\n", row["first_name"], row["last_name"])
    }
}

Features

  • ToArrays - Returns CSV as [][]string for raw access
  • ToMaps - Returns CSV as []map[string]string with header-based keys
  • Header trimming and name replacement support
  • Standard Go encoding/csv compliance (handles quoted fields)

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToArrays

func ToArrays(filePath string) ([][]string, error)

ToArrays reads a CSV file and returns an array with each line of the file as array of strings

Parameters - filePath string - The path to the CSV file Returns - [][]string - The lines as array of string

func ToMaps

func ToMaps(filePath string, replacements map[string]string) (returnMap []map[string]string, err error)

ToMaps reads a CSV file and returns an array with each line of the file as a key-value map

Parameters - filePath string - The path to the CSV file Returns - []map[string]string - The lines each one as a map

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL