-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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
proposal: Go 2: remove byte
alias and always use uint8
#22180
Comments
I disagree with this in particular
|
The purpose of the alias is to make it clear when one is using bytes as character string elements as opposed to small integers. It adds clarity to the code and should stay. |
@fcntl please remember this issue tracker is governed by the code of conduct. Please refrain from ad hominem attacks in the future. |
There's nothing crazy here. I think the idea had good intentions to simplify the language. I see from your repositories that you have quite a lot of experience with Go. Is there a particular section of code in stdlib or otherwise that made you think of this proposal? I more so would like to understand the reason you feel this change would make things more clear. |
Thank you, I didn't know that intension. Should we follow the intentions strictly? As There are some usages that aren't related to characters. e.g. https://golang.org/src/image/ycbcr.go#L167
@as I don't suggest to replace file names or comments, so the code would be: cat buffer.go
// Read reads the next len(p) bytes from the buffer or until the buffer
// is drained. The return value n is the number of bytes read. If the
// buffer has no data to return, err is io.EOF (unless len(p) is zero);
// otherwise it is nil.
func (b *Buffer) Read(p []uint8) (n int, err error) {
b.lastRead = opInvalid
if b.off >= len(b.buf) {
// Buffer is empty, reset to recover space.
b.Reset()
if len(p) == 0 {
return
}
return 0, io.EOF
}
n = copy(p, b.buf[b.off:])
b.off += n
if n > 0 {
b.lastRead = opRead
}
return
}
Thank you. No, there isn't. I forgot in what situation I started to feel like Other discussion why I prefer |
I'm 👎 on this. The solution is to have both types be transparently interchangeable, so that you can use an |
Thank you for the opinion. |
Ok, that's a good news for you? It means that you can use whatever type you want instead of the one used in the stdlib? The fact that What would it change to switch the type in the stdlib given that they are interchangeable? Most peoples won't even notice it. |
My intention is to make Go spec a little simpler. As everyone knows I didn't know when to use |
Whether they know it or not, it has long been documented and is easy to understand. |
Thanks, I found https://golang.org/pkg/builtin/#byte
I couldn't understand what is the difference between byte and uint8 here... |
In practice there is no difference. Conceptually a value of type |
I still feel like they are same even in terms of concepts, but I need to learn more. I appreciate your elaborating. |
IMO, in a strict meaning, uint8 has direction forward to uint16, uint32. And it have direction signed/unsigned like uint8/int8 too. If you want to declare the direction for the name of function or variable, you will provide APIs like below, you will use uint8 instead of byte. func ReadUint8() uint8 { ... }
func ReadUint16() uint16 { ... }
func ReadUint32() uint32 { ... } If you just read buffer, you use byte instead of uint8. In many cases, ReadByte is enough to read stream. func ReadByte() byte {} We can choose their name by whether it has directionality or not. |
@robpike has already said most of this very succinctly. I'm elaborating here to drive the point home once more: @hajimehoshi First and foremost, The only way to refer to this type is by giving it a name (there's no way to construct it from more basic things). In Go we decided from the start to give this type two names, We do this everywhere in programming: For instance we may call a It just so happens that I'm not saying people are doing this consistently, or even should follow this as a hard and fast rule. Guidelines can only go so far - good naming requires experience and is more art than science. So to answer your question explicitly: No, there should be no rule to be followed strictly. You yourself mention that you don't see a problem with Finally, and especially now that the language supports type aliases as a first-class construct, there's really no complexity to speak of here, neither in the implementation nor the spec. In closing, there doesn't seem to be anything to gain here by removing I'm against this proposal. There's bigger fish to fry. |
Well, that's the point. As there is no strict rule here compared to
Right. Probably I'd be fine even if |
@hajimehoshi Before removing As I said above, it's all about being able to chose a fitting name depending on context. |
Perhaps drop all of Perhaps there could even be an optimized conversion between |
There is little support for this proposal. Declined. |
IMO, using
byte
alias doesn't hide the fact that the number is unsigned 8bit integer, and this doesn't change code readability. Rather,uint8
is more explicit and fits more with Go way.On the other hand, I don't have a strong opinion on
rune
, that is an alias forint32
. I thinkrune
makes code readable to some extent.The text was updated successfully, but these errors were encountered: