resetgen

command module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2025 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

resetgen

Generate allocation-free Reset() methods for your structs. Perfect for sync.Pool usage.

Tools

This project provides two tools:

Tool Description
resetgen Code generator — creates Reset() methods from struct tags
resetgen-analyzer Static analyzer — detects missing Reset() calls before sync.Pool.Put()

resetgen (Code Generator)

Installation
go install github.com/flaticols/resetgen@latest

Or add as a tool dependency (Go 1.24+):

go get -tool github.com/flaticols/resetgen
Usage

Add reset tags to your struct fields and run the generator:

//go:generate resetgen

package main

type Request struct {
    ID      string            `reset:""`
    Method  string            `reset:"GET"`
    Headers map[string]string `reset:""`
    Body    []byte            `reset:""`
}

Run:

go generate ./...

Generated request.gen.go:

func (s *Request) Reset() {
    s.ID = ""
    s.Method = "GET"
    clear(s.Headers)      // preserves capacity
    s.Body = s.Body[:0]   // preserves capacity
}

Tag Syntax

Tag Behavior
reset:"" Zero value
reset:"value" Default value
reset:"-" Skip field

Features

  • Allocation-free — slices truncate (s[:0]), maps clear (clear(m))
  • Embedded structs — calls Reset() recursively
  • Selective — only structs with reset tags are processed
  • Fast — single-pass AST, minimal allocations

[!TIP] Structs without any reset tags are automatically ignored. You can have pooled and regular structs in the same file.

Resetter Interface

Define a common interface for pooled objects:

type Resetter interface {
    Reset()
}

All generated Reset() methods satisfy this interface, enabling generic pool helpers.

sync.Pool Examples

Example 1: HTTP Request Pool
//go:generate resetgen

package server

type Request struct {
    Path    string            `reset:""`
    Method  string            `reset:"GET"`
    Headers map[string]string `reset:""`
    Body    []byte            `reset:""`
    UserID  int               `reset:""`
}

var requestPool = sync.Pool{
    New: func() any { return new(Request) },
}

func HandleRequest(path, method string, body []byte) {
    req := requestPool.Get().(*Request)

    req.Path = path
    req.Method = method
    req.Body = append(req.Body, body...)

    process(req)

    req.Reset()
    requestPool.Put(req)
}
Example 2: Generic Pool with Resetter
//go:generate resetgen

package pool

type Resetter interface {
    Reset()
}

// Pool is a generic, allocation-free pool for any Resetter
type Pool[T Resetter] struct {
    p sync.Pool
}

func NewPool[T Resetter](newFn func() T) *Pool[T] {
    return &Pool[T]{
        p: sync.Pool{New: func() any { return newFn() }},
    }
}

func (p *Pool[T]) Get() T      { return p.p.Get().(T) }
func (p *Pool[T]) Put(v T)     { v.Reset(); p.p.Put(v) }

Usage with a buffer:

//go:generate resetgen

package encoding

type Buffer struct {
    data []byte `reset:""`
}

var bufPool = pool.NewPool(func() *Buffer { return new(Buffer) })

// MarshalTo writes encoded data directly to dst — zero allocations
func MarshalTo(dst io.Writer, v any) error {
    buf := bufPool.Get()

    buf.data = encodeJSON(buf.data, v)
    _, err := dst.Write(buf.data)

    bufPool.Put(buf)
    return err
}

[!NOTE] Both examples avoid defer closures and return values that reference pooled memory.


resetgen-analyzer (Static Analyzer)

Detects when sync.Pool.Put() is called without a preceding Reset() call.

Installation
go install github.com/flaticols/resetgen/cmd/resetgen-analyzer@latest

Or add as a tool dependency (Go 1.24+):

go get -tool github.com/flaticols/resetgen/cmd/resetgen-analyzer
Usage

Run standalone:

resetgen-analyzer ./...

Run with go vet:

go vet -vettool=$(which resetgen-analyzer) ./...

Add to your CI pipeline or Makefile:

.PHONY: lint
lint:
	go vet -vettool=$(which resetgen-analyzer) ./...
What it detects
func BadUsage() {
    buf := bufferPool.Get().(*Buffer)
    buf.data = append(buf.data, "hello"...)
    bufferPool.Put(buf) // ERROR: sync.Pool.Put() called without Reset() on buf
}

func GoodUsage() {
    buf := bufferPool.Get().(*Buffer)
    buf.data = append(buf.data, "hello"...)
    buf.Reset()
    bufferPool.Put(buf) // OK
}
Detected patterns
Pattern Example
Global pool bufferPool.Put(buf) without buf.Reset()
Struct field pool s.pool.Put(buf) without buf.Reset()
Wrapped variable pool.Put(w.buf) without w.buf.Reset()
Pool wrapper p.p.Put(v) without v.Reset() inside wrapper method

Benchmarks

BenchmarkReset-8    1000000000    0.32 ns/op    0 B/op    0 allocs/op

License

MIT


Made with ❤️ by Denis

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
cmd
resetgen-analyzer command
resetgen-analyzer checks that Reset() is called before sync.Pool.Put()
resetgen-analyzer checks that Reset() is called before sync.Pool.Put()
resetgen-analyzer/analyzer
Package analyzer provides a static analyzer that detects sync.Pool usage without calling Reset() before Put().
Package analyzer provides a static analyzer that detects sync.Pool usage without calling Reset() before Put().
internal

Jump to

Keyboard shortcuts

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