utils

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2025 License: MIT Imports: 0 Imported by: 0

README ΒΆ

utils

Utils for Go

πŸ“¦ go get github.com/lif0/pkg/utils@latest
πŸ§ͺ Requires Go 1.19+

build Go Reference last version utils coverage utils report card


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.


βš™οΈ Requirements

  • Go 1.19 or higher

πŸ“¦ 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.
Performance Notes

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

Documentation ΒΆ

Overview ΒΆ

Package utils ...

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL