Skip to content

Instantly share code, notes, and snippets.

@xxjapp
Last active May 17, 2019 02:28
Show Gist options
  • Save xxjapp/bb30289e05fea23407f7e6a2c3cc360e to your computer and use it in GitHub Desktop.
Save xxjapp/bb30289e05fea23407f7e6a2c3cc360e to your computer and use it in GitHub Desktop.
golang csv encoding & decoding
package main
import (
"bytes"
"encoding/csv"
"fmt"
)
func main() {
array := []string{"a s,dA", "Bdaf"}
b := &bytes.Buffer{} // creates IO Writer
w := csv.NewWriter(b) // creates a csv writer that uses the io buffer.
// convert array data to csv bytes
w.Write(array)
w.Flush()
fmt.Println(b.Bytes())
fmt.Println(string(b.Bytes()))
// convert csv bytes to array data
r := csv.NewReader(bytes.NewReader(b.Bytes()))
array2, _ := r.Read()
fmt.Println(array2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment