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

fmt: printing of types inconsistent for byte and uint8 with %#v #14830

Closed
martisch opened this issue Mar 15, 2016 · 2 comments
Closed

fmt: printing of types inconsistent for byte and uint8 with %#v #14830

martisch opened this issue Mar 15, 2016 · 2 comments

Comments

@martisch
Copy link
Contributor

@martisch martisch commented Mar 15, 2016

go1.4, go1.6, tip
( note that with the recent changes i made to fmt i was careful to not change the behavior but leave the issue open)

https://play.golang.org/p/TrmwckEwqu

          %T | reflect Type | %#V
       uint8 |        uint8 | 0x0
       uint8 |        uint8 | 0x0
     []uint8 |      []uint8 | []byte{}
     []uint8 |      []uint8 | []byte{}
      main.S |       main.S | main.S{in:[]uint8{}}
      main.S |       main.S | main.S{in:[]uint8{}}
    [3]uint8 |     [3]uint8 | [3]uint8{0x0, 0x0, 0x0}
    [3]uint8 |     [3]uint8 | [3]uint8{0x0, 0x0, 0x0}

i would expect uint8 to be consistent everywhere in agreement with %T and especially for []uint8.

i would argue it should and could be changed to always print uint8 because:

  • there are no issues that reflect package treats byte as uint8 in type strings (its only an alias anyway)
  • there is no way i know of to differentiate byte from uint8 with the reflect package or at all during runtime so a fix to differentiate the cases seems impossible
  • most cases already print uint8 types even if its specified as []byte inside e.g. a struct
  • %T and %#v are inconsistent for the type specification currently
  • there will be no extra code needed for %v,%d,%b bytes formatting vs normal array,slice formatting so it also automatically agrees e.g. what to print for nil slices.
  • if the behavior is changed to print []byte as []uint8 no test in std lib besides fmt_test breaks (those were mostly only added after go1.6)
  • there is precedent for fixes after go1 that made %T and %v consistent and not print byte type #4685 but uint8 is still shown as byte
  • the language spec defines "byte alias for uint8" not uint8 as alias for byte
@gopherbot
Copy link

@gopherbot gopherbot commented Mar 15, 2016

CL https://golang.org/cl/20686 mentions this issue.

@martisch
Copy link
Contributor Author

@martisch martisch commented Mar 20, 2016

Closing since we do not want to break existing code changing this.

@martisch martisch closed this Mar 20, 2016
@golang golang locked and limited conversation to collaborators Mar 20, 2017
@martisch martisch added the Go2 label Mar 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

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