resetgen

command module
v0.0.2 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.

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.

Example

//go:generate resetgen

package pool

type Buffer struct {
    Data    []byte            `reset:""`
    Headers map[string]string `reset:""`
    Status  int               `reset:"200"`
    err     error             // no tag = unchanged
}

// Config has no reset tags — ignored
type Config struct {
    Timeout int
    Debug   bool
}

[!NOTE] Only Buffer gets a Reset() method. Config is ignored.

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
internal

Jump to

Keyboard shortcuts

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