Golang encoding/csv: Difference between revisions
From wikinotes
Tag: Manual revert |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
{{ NOTE | | {{ NOTE | | ||
go's builtin csv module does not conform to | go's builtin csv module does not conform to the normal encoding interface. }} | ||
= Serializing = | = Serializing = | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="go"> | |||
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 | |||
</syntaxhighlight> | |||
</blockquote><!-- Serializing --> | </blockquote><!-- Serializing --> | ||
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