csv

package module
v0.0.0-...-e3d8a69 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

CI codecov Go Reference Go Report Card License

grpc-gateway-csv

A CSV marshaler for grpc-gateway. Implements the runtime.Marshaler interface to render gRPC responses as CSV.

Installation

go get github.com/mgoeppe/grpc-gateway-csv

Usage

Register the marshaler with the grpc-gateway ServeMux:

import csv "github.com/mgoeppe/grpc-gateway-csv"

mux := runtime.NewServeMux(
    runtime.WithMarshalerOption("text/csv", &csv.Marshaler{}),
)
Options
  • RowDelim — row delimiter (default: \n)
  • FieldDelim — field delimiter (default: ;)
  • InnerDelim — delimiter for merged values within a field (default: |)
  • NoHeader — suppress header row
  • Printf — custom format function (e.g., for locale-aware number formatting)

Documentation on grpc-gateway custom marshalers: Customizing Your Gateway.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Marshaler

type Marshaler struct {
	runtime.Marshaler

	// RowDelim specifies the delimiter between rows
	RowDelim string
	// FieldDelim specifies the delimiter between fields
	FieldDelim string
	// InnerDelim specifies the delimiter of merged values (slices / maps) within one field.
	InnerDelim string

	// used to print types (e.g. int, float, ...)
	Printf func(format string, a ...any) string

	// NoHeader suppresses to render the header
	NoHeader bool
}

func (*Marshaler) ContentType

func (m *Marshaler) ContentType(v interface{}) string

ContentType returns 'text/csv'.

func (*Marshaler) Marshal

func (m *Marshaler) Marshal(i interface{}) ([]byte, error)

Marshal renders the structure in i as CSV.

If i is a slice or a struct that contains slices each slice is rendered to a CSV block. These top-level slices need to contain structs otherwise an error is returned. Each top-level slices is rendered to one block. The blocks are delimited by '---\n'. Empty slices or nil pointers are ignored.

Each csv block consists of a header (if NoHeader option is false) and multiple rows delimited by m.RowDelimi. Each row is a 'flat' representation of the corresponding slice elements:

  • struct fields are visible on top-level with own header delimited by m.FieldDelim
  • nested slices / maps are flatened delimited by m.InnerDelim

Directories

Path Synopsis
Package main is a reverse proxy.
Package main is a reverse proxy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL