Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalString ¶
MarshalString is encodes a structure of slice of a structure into CSV string.
Example ¶
package main import ( "fmt" "go.nanasi880.dev/x/encoding/csvutil" ) func main() { type csvData struct { Name string Age int Memo string `csv:"Comment"` Ignore string `csv:"-"` } d := []csvData{ {Name: "Bob", Age: 18, Memo: "my name is Bob", Ignore: "Hi"}, {Name: "Alice", Age: 18, Memo: "my name is Alice", Ignore: "Hi"}, } encoded, err := csvutil.MarshalString(d) if err != nil { panic(err) } fmt.Println(encoded) }
Output: Name,Age,Comment Bob,18,my name is Bob Alice,18,my name is Alice
func UnmarshalString ¶
UnmarshalString is decodes a slice of a structure from CSV string.
Example ¶
package main import ( "fmt" "go.nanasi880.dev/x/encoding/csvutil" ) func main() { const csvString = `Name,Age,Comment Bob,18,my name is Bob Alice,18,my name is Alice` type csvData struct { Name string Age int Memo string `csv:"Comment"` Ignore int `csv:"-"` } var d []csvData if err := csvutil.UnmarshalString(csvString, &d); err != nil { panic(err) } fmt.Println(len(d)) fmt.Println(d[0]) fmt.Println(d[1]) }
Output: 2 {Bob 18 my name is Bob 0} {Alice 18 my name is Alice 0}
Types ¶
type Decoder ¶
type Decoder struct { Comma rune Comment rune FieldsPerRecord int LazyQuotes bool TrimLeadingSpace bool ReuseRecord bool UseHeader bool Nil string // contains filtered or unexported fields }
Decoder reads CSV values to an input stream.
type Encoder ¶
type Encoder struct { Comma rune UseCRLF bool UseHeader bool Nil string // contains filtered or unexported fields }
An Encoder writes CSV values to an output stream.
func (*Encoder) Encode ¶
Encode is encodes a structure or slice of a structure into CSV data and output to the io.Writer specified by NewEncoder.
Example ¶
package main import ( "os" "go.nanasi880.dev/x/encoding/csvutil" ) func main() { type csvData struct { Name string Age int Memo string `csv:"Comment"` Ignore string `csv:"-"` } d := []csvData{ {Name: "Bob", Age: 18, Memo: "my name is Bob", Ignore: "Hi"}, {Name: "Alice", Age: 18, Memo: "my name is Alice", Ignore: "Hi"}, } err := csvutil.NewEncoder(os.Stdout).Encode(d) if err != nil { panic(err) } }
Output: Name,Age,Comment Bob,18,my name is Bob Alice,18,my name is Alice
type Marshaler ¶
Marshaler is the interface implemented by types that can marshal themselves into valid CSV.
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal a CSV description of themselves. The input can be assumed to be a valid encoding of a CSV value. UnmarshalCSV must copy the CSV data if it wishes to retain the data after returning.