Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/csv: Writer preserves \r #58718

Closed
zacharysyoung opened this issue Feb 24, 2023 · 1 comment
Closed

encoding/csv: Writer preserves \r #58718

zacharysyoung opened this issue Feb 24, 2023 · 1 comment

Comments

@zacharysyoung
Copy link

zacharysyoung commented Feb 24, 2023

What version of Go are you using (go version)?

1.20

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

Playground

What did you do?

Used the csv.Writer to write-out a string with a CRLF.

What did you expect to see?

The same string, but with the CR dropped; just LF.

What did you see instead?

The exact same string, the CRLF.

The documentation makes the blanket assertion:

Carriage returns before newline characters are silently removed.

Yet the single-field record, []string{" foo1 \r\n foo2 "} will be written-out as " foo1 \r\n foo2 ", and not " foo1 \n foo2 " as I interpret the documentation (and #22746).

package main

import (
	"encoding/csv"
	"fmt"
	"strings"
)

func main() {
	var b strings.Builder
	w := csv.NewWriter(&b)
	w.Write([]string{" foo1 \r\n foo2 "})
	w.Flush()

	want := " foo1 \n foo2 "
	got := b.String()
	got = got[1 : len(got)-2] // trim field quotes

	fmt.Printf("%q != %q", got, want)
}

Prints:

" foo1 \r\n foo2 " != " foo1 \n foo2 "

I use the term "blanket assertion" because that documentation doesn't differentiate between reading and writing, so I expect—and especially after reading #22746—the csv package to effectively disallow a carriage return (CR, \r).

Does the documentation need clarification? Is this a bug with the Writer?

@zacharysyoung zacharysyoung changed the title affected/package: affected/package: csv Feb 24, 2023
@zacharysyoung zacharysyoung changed the title affected/package: csv csv: csv.Writer in fact preserves \r Feb 24, 2023
@zacharysyoung zacharysyoung changed the title csv: csv.Writer in fact preserves \r csv: Writer in fact preserves \r Feb 24, 2023
@zacharysyoung zacharysyoung changed the title csv: Writer in fact preserves \r csv: Writer preserves \r Feb 24, 2023
@zacharysyoung zacharysyoung changed the title csv: Writer preserves \r encoding/csv: Writer preserves \r Feb 24, 2023
@seankhliao
Copy link
Member

The entire section only applies to the input format (for Reader not Writer). There are clearly parts that can only be for reading (blank lines) and the examples also support that.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 25, 2023
@golang golang locked and limited conversation to collaborators Feb 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants