package utils

Contents
π Overview
The utils module provides a set of lightweight, focused utility packages designed to extend Go's standard library with commonly needed functionality. These packages follow Go's philosophy of simplicity and efficiency, offering well-tested solutions for everyday development challenges.
For full documentation, see https://pkg.go.dev/github.com/lif0/pkg/utils.
βοΈ Requirements
π¦ Installation
To add this package to your project, use go get:
go get github.com/lif0/pkg/utils@latest
Import the reflect extension in your code:
import "github.com/lif0/pkg/utils/reflect"
π Package reflect
Function: EstimatePayloadOf
func EstimatePayloadOf(v any) int
Returns an approximate payload size (in bytes) of the given value. It is designed for efficiency and is allocation-free for supported types
β οΈ Return Value and Errors
- Returns the estimated size in bytes for supported types.
- Returns
reflect.ErrFailEstimatePayload = -1 if the type is not supported or cannot be estimated.
This function performs zero allocations and runs with 0 B/op for supported types. See benchmark
- No memory allocations β
0 B/op for all primitive types.
- Reflection is used only for arrays and structs:
- Supports any primitive types in arrays.
- Supports only
time.Time{} for structs.
- For arrays
[N]T, prefer passing *[N]T to avoid value copying.
Check:
β
Supported Types
Scalar types (and pointers to them):
int, int8, int16, int32 (rune), int64
uint, uint8 (byte), uint16, uint32, uint64, uintptr
float32, float64
complex64, complex128
bool
string
time.Time, time.Duration
Containers:
[]T, []*T, *[]T, *[]*T β slices
[N]T, *[N]T, [N]*T, *[N]*T β arrays (via reflection)
For pointers and slices, nil is treated as zero-size.
For string and []string, the actual content size is summed.
If the type is not supported, the function returns reflect.ErrFailEstimatePayload(-1).
π‘ Use Case
Calculate request/response sizes for logging or monitoring, such as writing to a span in a database provider library.
π§ͺ Examples
Estimate an int value:
var v int = 42
n := EstimatePayloadOf(v)
// n == 8 on 64-bit systems
Estimate a string:
s := EstimatePayloadOf("hello")
// s == 5 (len("hello"))
Estimate a slice of strings:
names := []string{"John", "Doe"}
size := EstimatePayloadOf(names)
// size == len("John") + len("Doe") == 4 + 3 == 7
Estimate a slice with nil strings:
names := []string{"John", nil}
size := EstimatePayloadOf(names)
// size == len("John") + len("") == 4 + 0 == 4
Estimate a struct:
type User struct {
ID int64
Name string
}
u := User{ID: 123, Name: "Alice"}
size := EstimatePayloadOf(u)
// size == reflect.ErrFailEstimatePayload(-1), because it is a custom struct
Estimate an array (pass by pointer for performance):
var arr [1000]int32
size := EstimatePayloadOf(&arr)
// size == 1000 * 4 == 4000
πΊοΈ Roadmap
The future direction of this package is community-driven! Ideas and contributions are highly welcome.
βΉοΈ No idea
Contributions and ideas are welcome! π€
Contributions:
Feel free to open an Issue to discuss a new idea or a Pull Request to implement it! π€
π License
MIT