Documentation
¶
Overview ¶
Package csv reads and writes comma-separated values (CSV) files.
You can use flat struct data type to recieve the csv data.
This package are an extended version of encoding/csv package. See encoding/csv package : http://golang.org/pkg/encoding/csv/
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
A Reader reads records from a CSV-encoded file.
Example ¶
package main
import (
"fmt"
"strings"
"time"
"github.com/atotto/encoding/csv"
)
func main() {
csvtext := "Gopher,2,12.3,34.5,2020/01/01 12:20:02\nGhost,4,12.3,3.4e-05,2020/02/03 15:20:02"
type Person struct {
Name string
Age int
Height float32
Weight float64
CreateTime time.Time
}
var person = []Person{}
r := csv.NewReader(strings.NewReader(csvtext))
r.SetTimeFormat("2006/01/02 15:04:05")
r.SetTimeLocation(time.UTC)
err := r.ReadStructAll(&person)
if err != nil {
return
}
fmt.Printf("%v", person)
}
Output: [{Gopher 2 12.3 34.5 2020-01-01 12:20:02 +0000 UTC} {Ghost 4 12.3 3.4e-05 2020-02-03 15:20:02 +0000 UTC}]
func (*Reader) ReadStruct ¶
func (*Reader) ReadStructAll ¶
func (*Reader) SetTimeFormat ¶
func (*Reader) SetTimeLocation ¶ added in v0.1.3
type UnsupportedTypeError ¶
An UnsupportedTypeError is returned by Writer when attempting to encode an unsupported value type.
func (*UnsupportedTypeError) Error ¶
func (e *UnsupportedTypeError) Error() string
type Writer ¶
A Writer writes records to a CSV encoded file.
see encoding/csv package.
Example ¶
package main
import (
"os"
"time"
"github.com/atotto/encoding/csv"
)
func main() {
type Person struct {
Name string
Age int
Height float32
Weight float64
CreateTime time.Time
}
member := []Person{
{"Gopher", 2, 12.3, 34.5, time.Date(2020, 1, 1, 12, 20, 2, 0, time.UTC)},
{"Ghost", 4, 12.3, 0.000034, time.Date(2020, 2, 3, 15, 20, 2, 0, time.UTC)},
}
w := csv.NewWriter(os.Stdout)
w.SetTimeLocation(time.UTC)
w.WriteStructAll(member)
}
Output: Gopher,2,12.3,34.5,2020-01-01 12:20:02 Ghost,4,12.3,3.4e-05,2020-02-03 15:20:02
func (*Writer) SetTimeFormat ¶
func (*Writer) SetTimeLocation ¶ added in v0.1.3
func (*Writer) WriteStruct ¶
WriteStruct writes a single CSV record to w along with any necessary quoting. A record is a struct of flat structure.
func (*Writer) WriteStructAll ¶
WriteStructAll write
func (*Writer) WriteStructHeader ¶
Click to show internal directories.
Click to hide internal directories.