Skip to main content
Help improve this documentation

This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.

Your feedback helps us improve!

Encoding/CSV operatorsโ€‹

This page lists all operators available in the encoding/csv sub-package of ro.

Installโ€‹

First, import the sub-package in your project:

go get -u github.com/samber/ro/plugins/encoding/csv
  • NewCSVReaderโ€‹

    Creates an observable that reads records from a CSV reader.

    import (
    "encoding/csv"
    "strings"

    "github.com/samber/ro"
    rocsv "github.com/samber/ro/plugins/encoding/csv"
    )

    csvData := `name,age,city
    Alice,30,New York
    Bob,25,Los Angeles
    Charlie,35,Chicago`

    reader := csv.NewReader(strings.NewReader(csvData))
    obs := rocsv.NewCSVReader(reader)

    sub := obs.Subscribe(ro.PrintObserver[[]string]())
    defer sub.Unsubscribe()

    // Next: [name age city]
    // Next: [Alice 30 New York]
    // Next: [Bob 25 Los Angeles]
    // Next: [Charlie 35 Chicago]
    // Completed
    Prototype:
    func NewCSVReader(reader *csv.Reader)
  • NewCSVWriterโ€‹

    Creates an operator that writes string arrays to a CSV writer and returns the count of written records.

    import (
    "bytes"
    "encoding/csv"

    "github.com/samber/ro"
    rocsv "github.com/samber/ro/plugins/encoding/csv"
    )

    var buf bytes.Buffer
    writer := csv.NewWriter(&buf)

    obs := ro.Pipe(
    ro.Just(
    []string{"name", "age", "city"},
    []string{"Alice", "30", "New York"},
    []string{"Bob", "25", "Los Angeles"},
    []string{"Charlie", "35", "Chicago"},
    ),
    rocsv.NewCSVWriter(writer),
    )

    sub := obs.Subscribe(ro.PrintObserver[int]())
    defer sub.Unsubscribe()

    // Next: 4
    // Completed
    Prototype:
    func NewCSVWriter(writer *csv.Writer)