You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`bytesizer` is a Go package that provides a simple and intuitive way to work with byte counts. It allows you to easily convert between different units of byte sizes, such as bytes, kilobytes, megabytes, gigabytes, terabytes, and petabytes. Similar to how `time.Duration` works in Go, `bytesizer` helps in representing byte sizes with appropriate types and methods.
4
+
5
+
## Features
6
+
7
+
- Conversion between byte units and bytes
8
+
- Formatting of byte sizes into human-readable strings
9
+
- Parsing of strings into byte sizes with support for different units
10
+
- Fetching byte sizes as integer or floating-point numbers for precision work
11
+
12
+
## Installation
13
+
14
+
To install `bytesizer`, you can use the following Go command:
15
+
16
+
```bash
17
+
go get github.com/iamlongalong/bytesizer
18
+
```
19
+
20
+
Replace `iamlongalong` with your actual GitHub username where the package is hosted.
21
+
22
+
## Usage
23
+
24
+
Below you'll find the methods provided by the `bytesizer` package and some examples of how to use them.
25
+
26
+
### Constants
27
+
28
+
`bytesizer` defines the following byte size units:
29
+
30
+
```go
31
+
const (
32
+
ByteByteSize = 1 << (10 * iota)
33
+
KB
34
+
MB
35
+
GB
36
+
TB
37
+
PB
38
+
)
39
+
```
40
+
41
+
### Methods
42
+
43
+
#### Calc
44
+
Calculate the length of a byte slice and return it as a `ByteSize`:
45
+
46
+
```go
47
+
size:= bytesizer.Calc([]byte("Hello World!"))
48
+
```
49
+
50
+
#### Format
51
+
Format a `ByteSize` value to a string according to a specified unit:
52
+
53
+
```go
54
+
formattedSize:= size.Format(bytesizer.KB) // returns string like "1KB"
55
+
```
56
+
57
+
#### String
58
+
Convert a `ByteSize` to a string with an automatic unit:
59
+
60
+
```go
61
+
sizeString:= size.String() // returns string like "11B"
62
+
```
63
+
64
+
#### Byte, KB, MB, GB, TB, PB
65
+
Get the byte size as different units (returns `float64`):
66
+
67
+
```go
68
+
bytes:= size.Byte()
69
+
kilobytes:= size.KB()
70
+
// ... and so on for MB, GB, TB, PB
71
+
```
72
+
73
+
#### ByteInt, KBInt, MBInt, GBInt, TBInt, PBInt
74
+
Get the byte size as different units (returns `int`):
75
+
76
+
```go
77
+
bytesInt:= size.ByteInt()
78
+
kilobytesInt:= size.KBInt()
79
+
// ... and so on for MBInt, GBInt, TBInt, PBInt
80
+
```
81
+
82
+
#### Parse
83
+
Parse a string representation of a byte size into a `ByteSize` object:
84
+
85
+
```go
86
+
size, err:= bytesizer.Parse("10KB")
87
+
if err != nil {
88
+
log.Fatal(err)
89
+
}
90
+
fmt.Println(size) // Output: 10240 (Bytes equivalent of 10KB)
91
+
```
92
+
93
+
## Contributing
94
+
95
+
Contributions to `bytesizer` are welcome! Feel free to report issues or submit pull requests on our GitHub repository.
96
+
97
+
## License
98
+
99
+
This package is licensed under the MIT License - see the LICENSE.md file for details.
0 commit comments