Documentation
¶
Index ¶
- Constants
- func Append(filePath string, record []string) error
- func ByteReader(data []byte, options ...func(r *csv.Reader)) [][]string
- func ClearUnicode(text string) string
- func Convert[T any](data []T, ignoreDoubleQuote ...bool) string
- func F64ToString(num float64) string
- func FileHeaderReader(fileHeader *multipart.FileHeader) ([][]string, error)
- func Format(cell []string) string
- func IsFloat(t reflect.Type) bool
- func IsPointer(t reflect.Type) bool
- func ManualConvert[T any](data []T, headers []string, onRecord func(data T) []string) string
- func Parser[T any](rows [][]string) []T
- func ParserByReader[T any](ir *csv.Reader, delimiter ...rune) []T
- func ParserFunc(excludeHeader bool, rows [][]string, onRecord func([]string) error) error
- func ParserString[T any](rows [][]string) []T
- func ReadByte(filename string) []byte
- func Reader(r *csv.Reader, options ...func(r *csv.Reader)) [][]string
- func RemoveDoubleQuote(text string) string
- func TryConvert[T any](data []T, ignoreDoubleQuote ...bool) string
Constants ¶
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 ¶
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
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
ClearUnicode clear unicode from text
func Convert ¶
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 ¶
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 ¶
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 ¶
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
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
ManualConvert performs a manual conversion of the input data. It applies the specified rules or transformations to achieve the desired output.
func Parser ¶
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
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
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
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 ¶
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
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
RemoveDoubleQuote remove double quote (") and clear unicode from text
func TryConvert ¶ added in v1.1.2
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.
