Last active
May 17, 2019 02:28
-
-
Save xxjapp/bb30289e05fea23407f7e6a2c3cc360e to your computer and use it in GitHub Desktop.
golang csv encoding & decoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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