-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: bytes: add bytes.Size units #19375
Comments
Many of the projects I'm working on contain the same code as here. and a quick search on Github shows that it is a common case. In general, I'm in favor of adding something like this as |
So when you suggest that the Similarly, we're not going to change I think that leaves this proposal with the idea of defining So in the end I'm not sure this proposal carries its weight. |
@a8m it is interesting to see in the Github search link that you gave how many cases the definition or conversion is incorrect, inaccurate, or inefficient: base 10 counting, division instead of shifting, floating point type, and so on. @ianlancetaylor choosing Second, from your reference, I don't understand the following sentence:
Two questions regarding this argument:
My opinion in this matter, if you just need to choose a type for counting bytes, I don't see any concrete reason not to choose |
@posener I don't agree that the Go library uses two different types to represent the same thing. The Go library consistently uses The POSIX C library makes the same distinction. The size of an object in memory is
As discussed above, memory sizes are not the same as disk sizes or file sizes, and there is no reason to use the same type for both.
I'm sorry, I'm not sure precisely what you are asking. Go is choosing to use a signed integer to represent the size of an object in memory, and therefore objects that use up more than half of available memory can not be represented. This choice is made because signed values are easier to work with than unsigned values, for the reason I mentioned in the blog post: they don't have odd behavior around 0. |
In particular, with regard to the type used for values in memory and file sizes, I will note that these days it's easy to create files that are far too large to fit in memory, especially on 32-bit systems. |
Independent of the merits of this proposal, putting such a type in package bytes seems like the wrong place. The main affinity of the proposal with that package is the name - less so actual functionality. It seems that if we had such a type, it would have to be "below" package bytes. I do see the usefulness of such a type for flags and human-readable input (flags again) or output. Perhaps it would suffice to have strconv have functionality that understands sizes (convert from/to size units, with the result always being an int representing bytes). That same conversion code would be used by flags (which might have a new flag.Size function). |
Thanks @ianlancetaylor , updated the proposal from |
@posener I agree about And even if we did, putting it into the |
time.Duration exists because there are APIs in package time that need to take a time.Duration. |
go doc bytes says "Package bytes implements functions for the manipulation of byte slices." So that's clearly not the right package. time.Duration solved the problem that there were many reasonable base units for times: maybe float64 seconds, maybe nanoseconds, maybe microseconds, and so on. In contrast, there are not really many choices for byte counts: they're essentially always in the unit "single bytes". We also can't retrofit this into existing APIs like io.Reader because that would break compatibility. But even if we could, it's not clear that it pulls its weight. io.Reader in particular returns int because you want to allow 'return len(p)' without a type conversion. If it returned io.ByteSize then you'd need a conversion and I guess to avoid being unit-dependent it would have to be something strange like (io.ByteSize is the type, io.Byte is a predefined constant). return io.ByteSize(len(p)) * io.Byte (to parallel Or I guess you could assume that io.ByteSize's unit is bytes, like size_t in C, and drop the multiply. But then it's still extra conversions. It seems like a fun API to design but not necessarily an important one to have. -rsc for @golang/proposal-review |
Thanks @rsc for the very descriptive decline! I appreciate that.
|
Package flag already lets one define custom flag implementations by implementing flag.Value. We'd want to see that this is something lots of people need and are already doing before thinking about promoting a canonical implementation to the standard library. |
As was referenced in this thread before (a message that was deleted), there is the go-humanize package. A gopher could use this one for now, but how would you know if that is "something lots of people need"? :-) |
I think that it would be nice to have unit that could be easily translated to bytes.
bytes.Size
of typeint64
similar totime.Duration
type.time
package, exposingtime.Second
for example, it would be nice to havebytes.Megabyte
for example and other units conversion.time
package, translate from string10mb
to the right amount ofbytes.Size
, such as1m
will be translated to the right amount oftime.Duration
.Why?
CACHE_SIZE_MB
, expecting an int. If the proposed units would have exists, this config could beCACHE_SIZE
expecting a string, which will be converted easily to bytes.int
instead ofint64
).Writer
to return abytes.Size
type and not an int, which will be easily converted and used.I know that it is not like the
time
package in the sense that you want to be able to change the basic units, but I think that, even though, it will be very convenient to have such units.Updates
bytes.Size
type according to @ianlancetaylor suggestion not to useuint
because of it's behavior around the zero value.The text was updated successfully, but these errors were encountered: