Golang encoding/csv: Difference between revisions
From wikinotes
Tag: Reverted |
Tag: Manual revert |
||
Line 29: | Line 29: | ||
records, _ := reader.ReadAll() | records, _ := reader.ReadAll() | ||
fmt.Println(records[0][1]) // 2 | fmt.Println(records[0][1]) // 2 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Deserializing --> | </blockquote><!-- Deserializing --> |
Latest revision as of 02:50, 26 June 2022
Go's builtin module for encoding/decoding csv files.
NOTE:
go's builtin csv module does not conform to the normal encoding interface.
Serializing
data := [][]string{ {"1", "2", "3"}, {"4", "5", "6"}, } writer := new(strings.Builder) csvWriter := csv.NewWriter(writer) csvWriter.WriteAll(data) fmt.Println(writer.String()) // 1,2,3 // 4,5,6
Deserializing
data := `1,2,3 4,5,6` reader := csv.NewReader(strings.NewReader(data)) records, _ := reader.ReadAll() fmt.Println(records[0][1]) // 2