From 90a1c525082cf5592c366e38ead0cb52156f9f57 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Fri, 2 Oct 2026 15:52:52 +0200 Subject: [PATCH] Add MarshalBytesWithoutHeaders --- csv.go | 9 +++++++++ encode_test.go | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/csv.go b/csv.go index aeb6f85..52178af 100644 --- a/csv.go +++ b/csv.go @@ -149,6 +149,15 @@ func MarshalBytes(in interface{}) (out []byte, err error) { return bufferString.Bytes(), nil } +// MarshalBytesWithoutHeaders returns the CSV bytes from the interface. +func MarshalBytesWithoutHeaders(in interface{}) (out []byte, err error) { + bufferString := bytes.NewBuffer(out) + if err := MarshalWithoutHeaders(in, bufferString); err != nil { + return nil, err + } + return bufferString.Bytes(), nil +} + // Marshal returns the CSV in writer from the interface. func Marshal(in interface{}, out io.Writer) (err error) { writer := getCSVWriter(out) diff --git a/encode_test.go b/encode_test.go index f80af84..02d5eb9 100644 --- a/encode_test.go +++ b/encode_test.go @@ -102,6 +102,21 @@ func Test_writeTo_NoHeaders(t *testing.T) { assertLine(t, []string{"e", "3", "b", "0.46153846153846156", "", "", ""}, lines[1]) } +func TestMarshalBytesWithoutHeaders(t *testing.T) { + s := []Sample{ + {Foo: "f", Bar: 1, Baz: "baz", Frop: 0.1}, + {Foo: "e", Bar: 3, Baz: "b", Frop: 0.5}, + } + csvContent, err := MarshalBytesWithoutHeaders(&s) + if err != nil { + t.Fatal(err) + } + + if expected := "f,1,baz,0.1,,,\ne,3,b,0.5,,,\n"; string(csvContent) != expected { + t.Fatalf("expected: %q\ngot: %q", expected, csvContent) + } +} + func Test_writeTo_multipleTags(t *testing.T) { b := bytes.Buffer{} e := &encoder{out: &b}