Standard library / encoding/csv

encoding/csv.NewWriter()

Construct a buffered CSV writer using comma and LF by default. Flush and check Error after individual writes. Expected stdout: 'a,b\n'.

Formal Syntax & Structure

go syntaxgo
func NewWriter(w io.Writer) *Writer

Parameters & Attributes

NameTypeDescription
wio.WriterDestination writer.

Return Value

*Writer

New CSV writer.

Working Example

Example Snippetgo
package main
import (  "bytes"  "encoding/csv"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  w := csv.NewWriter(&b)  must(w.Write([]string{"a", "b"}))  w.Flush()  must(w.Error())  fmt.Print(b.String())}
Go Reference