Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alesr/bytesplitter
A Go package for splitting slices of bytes into smaller slices based on a given memory size
https://github.com/alesr/bytesplitter
Last synced: 3 days ago
JSON representation
A Go package for splitting slices of bytes into smaller slices based on a given memory size
- Host: GitHub
- URL: https://github.com/alesr/bytesplitter
- Owner: alesr
- License: mit
- Created: 2023-08-27T07:21:04.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-08-27T07:25:25.000Z (over 1 year ago)
- Last Synced: 2024-11-22T05:28:41.171Z (2 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ByteSplitter
## Introduction
The `bytesplitter` package is a Go library designed to split slices of bytes into smaller slices based on a given memory size. It supports various units of memory like Kilobytes (KB) and Megabytes (MB).
## Features
- Types for memory sizes: `KB`, `MB`.
- Function for splitting byte slices: `Split`.## Installation
```bash
go get -u github.com/alesr/bytesplitter
```## Usage
### Importing the Package
```go
import "github.com/alesr/bytesplitter"
```### Example 1: Splitting a Byte Slice into 1KB Chunks
```go
package mainimport (
"github.com/alesr/bytesplitter"
)func main() {
data := make([]byte, 5000) // 5KB of data// Split the data into 1KB chunks
chunks, err := bytesplitter.Split(data, bytesplitter.KB(1))
if err != nil {
fmt.Println("Error:", err)
return
}fmt.Println("Number of chunks:", len(chunks))
}
```### Example 2: Splitting a Byte Slice into 2MB Chunks
```go
package mainimport (
"github.com/alesr/bytesplitter"
)func main() {
data := make([]byte, 5000000) // ~5MB of data// Split the data into 2MB chunks
chunks, err := bytesplitter.Split(data, bytesplitter.MB(2))
if err != nil {
fmt.Println("Error:", err)
return
}fmt.Println("Number of chunks:", len(chunks))
}
```## API Documentation
### Types
- `KB`: Represents Kilobytes.
- `MB`: Represents Megabytes.### Functions
- `Split(data []byte, size T) ([][]byte, error)`: Splits a byte slice `data` into smaller slices based on the given `size`. Returns an error if the splitting fails.
## Contributing
Feel free to submit issues and pull requests.