Skip to content

fmt: clarify precision for strings and slices #73296

@jiceathome

Description

@jiceathome

What is the URL of the page with the issue?

https://pkg.go.dev/fmt

What is your user agent?

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36

Screenshot

Image

What did you do?

fmt.Printf("%8s", "ab")
fmt.Printf("%8s", "abcdefghijklmn")

What did you expect to see?

ab
abcdefgh

What did you see instead?

      ab
abcdefghijklmn

Problem is this phrase: "For strings, byte slices and byte arrays, however, precision limits the length of the input to be formatted (not the size of the output), truncating if necessary. ". The reality is the exact opposite. "%8s" will cause the output to padded with space; and not the input to be truncated. Either behavior is useful, but the doc states the wrong one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions