go
Golang tools, examples, and packages

Development
Don't forget to install pre-commit and setup the commit hook.
Package brdocs
Validation for CPF and CNPJ
// IsCPF verifies if the given string is a valid CPF document.
// Punctuation will be automatically removed
func IsCPF(doc string) bool
// IsCNPJ verifies if the given string is a valid CNPJ document.
// Punctuation will be automatically removed. Rules for new alfanumeric format.
func IsCNPJ(doc string) bool
// RemoveNonDigitAndLetters updates the value, keeping only 0-9, A-Z characters
func RemoveNonDigitAndLetters(value *string)
Package flow
Simplify logic flows
// Default returns the second argument (valueIfZero) when the value has the default (zero)
func Default[T comparable](value T, valueIfZero T) T
// If is a generic ternary operator
func If[T any](condition bool, valueIfTrue T, valueIfFalse T) T
Package fraction
This package is originally a work of Miguel Dorta go-fraction.
I needed to encapsulate in this repository due to release name restrictions.
// Fraction represents a fraction. It is an immutable type.
//
// It is always a valid fraction (never x/0) and it is always simplified.
type Fraction struct
Package mid
Machine Identification using data from operational system trying to detect unique machine
- Linux: hostnamectl, /var/lib/dbus/machine-id, or /etc/machine-id
- Windows: MachineID from registry SQMClient
- MacOS: "{model number}|{serial number}|{hardware uuid}" from system_profiler (under validation)
// Machine ID
func MachineID() string
// DirExists simply returns true if the pathName is a existing directory
func DirExists(pathName string) bool
// CreatePath Create full path, with permissions updated from parent folder.
func CreatePath(path string) error
// FileExists symply returns true if the fileName is a existing file
func FileExists(fileName string) bool
// FindFileInPath searches for a file in the paths from the PATH environment variable
// returns the first occurrence or error
// Handles OS-specific path separators
func FindFileInPath(filename string) (string, error)
Utilities to parse and reconstruct simple shell-like argument lists.
- Type:
QuotedShellArgs — a []string wrapper that holds parsed arguments.
- Constructor:
NewQuotedShellArgs(s string) QuotedShellArgs — parse input string into QuotedShellArgs.
- Method:
QuotedShellArgs.String() string — join arguments back into a shell-safe string (quotes added as needed).
Behavior:
- Supports single
' and double " quotes. Quotes are removed from parsed arguments.
- Splits on whitespace.
- Reconstructs a shell-like string adding quotes only when needed.
Example:
package main
import (
"fmt"
"github.com/guionardo/go/pkg/shell_tools"
)
func main() {
input := `one "two three" 'four five' six\ seven`
args := shell_tools.NewQuotedShellArgs(input)
// args is a QuotedShellArgs (slice of strings)
fmt.Printf("%q\n", []string(args)) // ["one" "two three" "four five" "six seven"]
fmt.Println(args.String()) // one "two three" "four five" "six seven"
}
Package set
Generic set struct
// Set values methods
type Set[T comparable] map[T]struct{}
Set[T] can be [un]marshaled and respects the Scanner and Valuer interfaces
type Scanner = database/sql.Scanner
type Valuer = database/sql/driver.Valuer
Package httptest_mock
Utilities for mocking HTTP servers in tests.
- Easily create mock HTTP servers with custom handlers.
- Record requests and responses for assertions.
- Supports setting up expected responses and verifying received requests.
- More documentation
Utilities for working with Go's reflection, including zero value checks.
// IsZeroValue checks if the provided value is considered a zero value.
// Handles numeric types, strings, booleans, time.Time, time.Duration, slices, arrays, maps, and pointers.
// Returns true if the value is zero, nil or empty, false otherwise.
func IsZeroValue(value any) bool
🤝 Contributing
Bugs or contributions on new features can be made in the issues page.