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

cmd/gofmt: canonicalize octal literals to the '0o' form #33363

Open
cespare opened this issue Jul 30, 2019 · 5 comments
Open

cmd/gofmt: canonicalize octal literals to the '0o' form #33363

cespare opened this issue Jul 30, 2019 · 5 comments
Assignees
Labels

Comments

@cespare
Copy link
Contributor

@cespare cespare commented Jul 30, 2019

Go 1.13 adds the 0o form for octal numbers: instead of writing 0644 we can write 0o644. Gofmt is also changing to canonicalize 0O to 0o.

We should go one step further and have gofmt canonicalize the old style of octal literals to the 0o style: 0644 should be changed to 0o644.

Octal numbers are a surprising feature for programmers coming from a language that does not have them, or where they are rarely used. It's unintuitive that adding a leading zero to an integer changes its meaning. Some modern languages, such as Rust and Swift, have opted to omit the 0-prefix syntax altogether and only support the 0o form. Removing the 0-prefix syntax from Go would be too disruptive now, but we should strongly encourage use of the 0o form via this gofmt rule.

As a separate justification, with the new octal syntax there will be two different octal styles the programmer can choose. It's better for gofmt to remove this choice and enforce a single standard.

This will cause some churn in code bases, mostly around os.OpenFile and similar calls, but I believe the change is well worth it.

@agnivade
Copy link
Contributor

@agnivade agnivade commented Jul 30, 2019

@mvdan
Copy link
Member

@mvdan mvdan commented Jul 30, 2019

One way to ease the transition would be for -s to apply the change for a couple of Go releases, so that codebases can transition at their pace, and later enforce it as part of regular gofmt.

@agnivade
Copy link
Contributor

@agnivade agnivade commented Jul 30, 2019

This seems like a -s only feature to me. I thought regular gofmt only did whitespace and newline adjustments ?

@griesemer griesemer self-assigned this Jul 30, 2019
@cespare
Copy link
Contributor Author

@cespare cespare commented Jul 30, 2019

@agnivade

I thought regular gofmt only did whitespace and newline adjustments ?

With Go 1.13, gofmt will already be canonicalizing integers to some extent:

For instance, 0B1010, 0XabcDEF, 0O660, 1.2E3, and 01i become 0b1010, 0xabcDEF, 0o660, 1.2e3, and 1i after applying gofmt.

@slrz
Copy link

@slrz slrz commented Jul 31, 2019

Please no. This will result in lots of gratuitous churn, especially considering that many people will want to support older versions of Go for some time to come.

Maybe bring it up again once the new literal variants are understood by all gc versions still on support and the appropriate releases of gccgo (and other implementations of Go) have had a chance to propagate to their users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.