csvparser

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2025 License: MIT Imports: 9 Imported by: 0

README

csv-parser

Description

This is a simple CSV parser that reads a CSV file and populates a given struct pointer with the contents of the CSV file. This is a small personal project that I created to learn more about the Go programming language.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConcuctStruct

func ConcuctStruct[T any](structPtr *T, filePath string, settings *AppSettings) error

ConcuctStruct reads a CSV file and populates a struct with the data.

The struct must be a pointer to a slice of a struct. It must either have fields with the "csv:x" tag set to the column name in the CSV file, or no tags at all. Also its fields must have fields of type slice of string, int, float32 or float64.

An AppSettings struct must be provided with the WithHeader field set to true if the CSV file has a header row.

Example:

type MyStruct struct {
    Id []Int `csv:"id"`
    Name []strings `csv:"name"`
}

func main() {
    myStruct := MyStruct{}
    err := ConcuctStruct(&myStruct, "data.csv", &AppSettings{WithHeader: false})
    if err != nil {
        log.Fatalln(err)
    }

    fmt.Println(myStruct)
}

Types

type AppSettings

type AppSettings struct {
	WithHeader bool
}

Jump to

Keyboard shortcuts

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