Documentation
¶
Overview ¶
*********************************************************
- Author : Michael
- Email : dolotech@163.com
- Last modified : 2016-07-07 23:41
- Filename : cfield.go
- Description :
- ******************************************************
*********************************************************
- Author : Michael
- Email : dolotech@163.com
- Last modified : 2016-07-07 23:41
- Filename : csv.go
- Description :
- ******************************************************
Package csv provides `Marshal` and `UnMarshal` encoding functions for CSV(Comma Seperated Value) data. This package is built on the the standard library's encoding/csv.
*********************************************************
- Author : Michael
- Email : dolotech@163.com
- Last modified : 2016-07-07 23:41
- Filename : decode.go
- Description :
- ******************************************************
*********************************************************
- Author : Michael
- Email : dolotech@163.com
- Last modified : 2016-07-07 23:41
- Filename : encode.go
- Description :
- ******************************************************
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal returns the CSV encoding of i, which must be a slice of struct types.
Marshal traverses the slice and encodes the primative values.
The first row of the CSV output is a header row. The column names are based on the field name. If a different name is required a struct tag can be used to define a new name.
Field string `csv:"Column Name"`
To skip encoding a field use the "-" as the tag value.
Field string `csv:"-"`
Boolean fields can use string values to define true or false.
Bool bool `true:"Yes" false:"No"`
Example ¶
type Person struct { Name string `csv:"FullName"` Gender string Age int Wallet float32 `csv:"Bank Account"` Happy bool `true:"Yes!" false:"Sad"` private int `csv:"-"` } people := []Person{ Person{ Name: "Smith, Joe", Gender: "M", Age: 23, Wallet: 19.07, Happy: false, }, } out, _ := Marshal(people) fmt.Printf("%s", out)
Output: FullName,Gender,Age,Bank Account,Happy "Smith, Joe",M,23,19.07,Sad
func Unmarshal ¶
Unmarshal parses the CSV document and stores the result in the value pointed to by v. Only a slice of a struct is allowed for v.
The first line of the CSV is document is used for column names. These are paired to matching exported fields in v's type. See Marshal on how to use tags to map to different names and additional options.
Supported Types ¶
string, int, float and bool are supported. Any type which implements Unmarshal is also supported.
Example ¶
/********************************************************** * Author : Michael * Email : dolotech@163.com * Last modified : 2016-07-07 23:42 * Filename : example_test.go * Description : * *******************************************************/ package main import ( "fmt" ) type Person struct { Name string `csv:"Full Name"` income string // unexported fields are not Unmarshalled Age int `csv:"-"` // skip this field Address Address `csv:"Street"` // skip this field } type Address struct { City string Street string } func (a *Address) UnmarshalCSV(val string, row *Row) error { c, _ := row.Named("City") s, _ := row.Named("Street") a.Street = s a.City = c return nil } func main() { people := []Person{} sample := []byte( `Full Name,income,Age,City,Street John Doe,"32,000",45,Brooklyn,"7th Street" `) err := Unmarshal(sample, &people) if err != nil { fmt.Println("Error: ", err) } fmt.Printf("%+v", people) }
Output: [{Name:John Doe income: Age:0 Address:{City:Brooklyn Street:7th Street}}]
Types ¶
type Row ¶
type Row struct { Columns *[]string // The name of the columns, in order Data []string // the data for the row }
Row is one row of CSV data, indexed by column name or position.
type Unmarshaler ¶
type Unmarshaler interface { // UnmarshalCSV receives a string with the column value matching this field // and a reference to the the current row. // This allows composing a value from mutliple columns. UnmarshalCSV(string, *Row) error }
Unmarshaler is the interface implemented by objects which can unmarshall the CSV row itself.