csvx

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: MIT Imports: 10 Imported by: 1

README

CSVX

Go Coverage Go Report Card

Convert array struct to csv format and Parse csv format to array struct with Golang

Install

go get github.com/prongbang/csvx

Define struct for Convert

Add header for mapping in csv header and no start with 1 for sort header

type MyStruct struct {
    Name  	string 	`json:"name" header:"Name Space" no:"2"`
    ID    	int    	`json:"id" header:"ID" no:"1"`
    Address *string `json:"address" header:"Address" no:"4" default:"N/A"`
    Phone   *string `json:"phone" header:"Phone" no:"3" default:"NULL"`
    Email   *string `json:"email" header:"Email" no:"5"`
    Age     *string `json:"age" header:"Age" no:"6" default:"999"`
    Other string
}

Support type

  • int, int8, int16, int32, int64
  • string
  • float64

Using for Convert

m := []MyStruct{
    {ID: 1, Name: "N1"},
    {ID: 2, Name: "N2"}
}
csv := csvx.Convert[MyStruct](m)

Result

"ID","Name Space"
"1","N1"
"2","N2"

Define struct for Parse

Add header for mapping in csv header

type Struct struct {
	ID   string `header:"ID"`
	Name string `header:"Name Space"`
}

Using for Parse

rows := [][]string{
    {"ID", "Name Space"},
    {"1", "Name1"},
    {"2", "Name2"},
    {"3", "Name3"},
    {"4", "Name4"},
}
s := csvx.Parser[Struct](rows)

Result

[
  {"ID":"1","Name":"Name1"},
  {"ID":"2","Name":"Name2"},
  {"ID":"3","Name":"Name3"},
  {"ID":"4","Name":"Name4"}
]

Benchmark

goos: darwin
goarch: arm64
pkg: github.com/prongbang/csvx
cpu: Apple M1 Pro
BenchmarkConvert-10          	  430015	      5751 ns/op
BenchmarkManualConvert
BenchmarkManualConvert-10    	 2002738	       614.8 ns/op
BenchmarkTryConvert
BenchmarkTryConvert-10    	      760494	      1573 ns/op

Documentation

Index

Constants

View Source
const Utf8BOM = "\uFEFF"

Utf8BOM represents the Byte Order Mark (BOM) for UTF-8 encoding. It is used to signal UTF-8 encoding in text files for better compatibility with tools like Excel.

Variables

This section is empty.

Functions

func Append

func Append(filePath string, record []string) error

Append appends the given record to the end of the file specified by the filePath parameter. The record should be a slice of strings, where each string represents a field of the record. If the file does not exist, it will be created. If an error occurs during the operation, an error value will be returned.

func ByteReader added in v1.0.10

func ByteReader(data []byte, options ...func(r *csv.Reader)) [][]string

ByteReader creates an io.Reader from a byte slice. It allows the byte data to be read sequentially as a stream.

func ClearUnicode added in v1.0.6

func ClearUnicode(text string) string

ClearUnicode clear unicode from text

func Convert

func Convert[T any](data []T, ignoreDoubleQuote ...bool) string

Convert array struct to csv format Struct supported

type MyStruct struct {
	Name string `json:"name" header:"Name" no:"2"`
	ID   int    `json:"id" header:"ID" no:"1"`
}

m := []MyStruct{{ID: 1, Name: "N1"}, {ID: 2, Name: "N2"}} csv := csvx.Convert[MyStruct](m)

Result:

"ID","Name"
"1","N1"
"2","N2"

func F64ToString

func F64ToString(num float64) string

F64ToString converts the given float64 value to a string representation. The resulting string will be formatted as a decimal number with up to 10 decimal places. This function can be used to convert floating-point values to string values, which may be useful for printing or other output operations. If the given value is NaN or infinite, the resulting string will reflect this. If the given value is not representable as a finite decimal number, this function may return an inaccurate or nonsensical result.

func FileHeaderReader

func FileHeaderReader(fileHeader *multipart.FileHeader) ([][]string, error)

FileHeaderReader extracts the header of a multipart file specified by the given *multipart.FileHeader parameter and returns a slice of slices of strings representing the parsed header. Each slice in the result represents a single header field, where the first element is the header field name and the second element is the header field value. If the header is empty or cannot be parsed, an empty slice will be returned. If an error occurs during the operation, an error value will be returned. Ex: file, _ := c.FormFile("file") rows, err := csvx.FileHeaderReader(file)

func Format

func Format(cell []string) string

Format formats a slice of strings as a single, comma-separated string. Each element in the slice will be separated by a comma and a space. This function can be used to generate formatted output for CSV files or other data formats that use comma-separated values. If the input slice is empty, this function will return an empty string.

func IsFloat

func IsFloat(t reflect.Type) bool

IsFloat returns true if the given reflect.Type is a float32 or float64 type, and false otherwise. This function can be used to check whether a given type is a floating-point type, which may be useful for type assertions and other operations that require type checking. If the given type is not a valid float type, this function will return false.

func IsPointer added in v1.0.8

func IsPointer(t reflect.Type) bool

IsPointer checks whether the given interface is a pointer. Returns true if the input is a pointer type, otherwise false.

func ManualConvert added in v1.1.2

func ManualConvert[T any](data []T, headers []string, onRecord func(data T) []string) string

ManualConvert performs a manual conversion of the input data. It applies the specified rules or transformations to achieve the desired output.

func Parser

func Parser[T any](rows [][]string) []T

Parser parses the provided input data and returns the result. It handles different formats based on the input type.

func ParserByReader added in v1.0.12

func ParserByReader[T any](ir *csv.Reader, delimiter ...rune) []T

ParserByReader parses data from an io.Reader and returns the result. This is useful for streaming data or reading from large files.

func ParserFunc added in v1.0.8

func ParserFunc(excludeHeader bool, rows [][]string, onRecord func([]string) error) error

ParserFunc processes the input data using a custom parsing function. This allows for flexible and reusable parsing logic.

err := csvx.ParserFunc(true, rows, func (record []string) {
	return nil
})

func ParserString added in v1.1.0

func ParserString[T any](rows [][]string) []T

ParserString is a generic function that takes a slice of slices of strings as input and returns a slice of values of type T, where T is a type parameter that represents the desired output type. The input slice should represent a CSV file or other tabular data in which each inner slice represents a single row of data, and each element in the inner slice represents a single field value. This function will attempt to parse each field value into the corresponding type T using the built-in strconv package. If parsing fails or the input slice is empty, an empty slice of type T will be returned.

type Struct struct {
	  ID   string `header:"ID"`
	  Name string `header:"Name Space"`
}

rows := [][]string{
   {"ID", "Name Space"},
   {"1", "Name1"},
}

s := csvx.ParserString[Struct](rows)

func ReadByte

func ReadByte(filename string) []byte

ReadByte reads the entire contents of the file with the specified filename and returns them as a slice of bytes. If an error occurs during the operation, a nil slice will be returned. This function can be used to read the contents of text files or other files that are encoded as byte streams. Note that this function may not be suitable for reading large files, as it reads the entire file into memory at once. For large files, consider using the os package or a buffered reader to read the file in smaller chunks.

func Reader added in v1.0.11

func Reader(r *csv.Reader, options ...func(r *csv.Reader)) [][]string

Reader wraps an existing io.Reader to provide additional functionality. It may include features like buffering or line-by-line reading.

func RemoveDoubleQuote added in v1.0.6

func RemoveDoubleQuote(text string) string

RemoveDoubleQuote remove double quote (") and clear unicode from text

func TryConvert added in v1.1.2

func TryConvert[T any](data []T, ignoreDoubleQuote ...bool) string

TryConvert attempts to convert the input data to the specified format. It handles errors gracefully and returns the converted result along with an error (if any).

Types

This section is empty.

Jump to

Keyboard shortcuts

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