gg

package module
v0.0.0-...-bb08a0d Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 1 Imported by: 15

README

gg

Useful golang utilities.

Documentation

Overview

Package gg is a set of useful golang utilities.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectError

func CollectError(f func() error, dest *error)

CollectError executes f and collects its error into *dest. It is intended for use with defer so cleanup errors are not lost. If f returns an error and *dest is nil, store it in *dest; otherwise join it with *dest using errors.Join. Argument dest can't be nil, and it usually points to the function's named return error.

Example
package main

import (
	"errors"
	"fmt"
	"io"

	"github.com/mkch/gg"
)

// errWrite simulates an error returned from Write.
var errWrite = errors.New("write failed")

// errClose simulates an error returned from Close.
var errClose = errors.New("close failed")

// mockFile simulates a WriteCloser that always returns errors on Write and Close.
type mockFile struct{}

func Open() (io.WriteCloser, error) {
	return &mockFile{}, nil
}

func (m mockFile) Write(p []byte) (int, error) {
	return 0, errWrite
}

func (m mockFile) Close() error {
	return errClose
}

// FileOp opens a file, writes to it, and ensures proper error chaining.
func FileOp() (err error) {
	f, err := Open()
	if err != nil {
		return err
	}
	// Ensure f is closed and any close error is combined with err.
	defer gg.CollectError(f.Close, &err)

	_, err = f.Write([]byte("x"))
	return err
}

func main() {
	err := FileOp()
	// err contains both write and close errors.
	fmt.Println(err)
	fmt.Println(errors.Is(err, errWrite), errors.Is(err, errClose))

}
Output:

write failed
close failed
true true

func If

func If[T any](cond bool, truePart T, falsePart T) T

If returns truePart if cond is true, or returns falsePart. Note that Go evaluates truePart and falsePart regardless of cond when calling If.

Example
package main

import (
	"fmt"

	"github.com/mkch/gg"
)

func main() {
	fmt.Println(gg.If(true, "yes", "no"))
	fmt.Println(gg.If(false, 1, 0))
}
Output:

yes
0

func IfFunc

func IfFunc[T any](cond bool, truePart func() T, falsePart func() T) T

IfFunc returns the result of truePart() if cond is true, or the result of falsePart().

Example
package main

import (
	"fmt"

	"github.com/mkch/gg"
)

func main() {
	fmt.Println(gg.IfFunc(true,
		func() string { return "true" },
		func() string { return "false" },
	))
	fmt.Println(gg.IfFunc(false,
		func() int { return 1 },
		func() int { return 2 },
	))
}
Output:

true
2

func Must

func Must[T any](v T, err error) T

Must returns v if err is nil, or it panic with err. Must is useful to wrap a function call returning value and error when there is no better way to handle the error other than panicking.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/mkch/gg"
)

func main() {
	fmt.Print(gg.Must(strconv.Atoi("1")))
}
Output:

1

func MustOK

func MustOK(err error)

MustOK panics if err is not nil.

Example
package main

import (
	"os"

	"github.com/mkch/gg"
)

func main() {
	gg.MustOK(os.Setenv("some_key_for_test", "some value"))
}

func Zero

func Zero[T any]() (zero T)

Zero returns the zero value of type T.

Types

type Set

type Set[T comparable] map[T]struct{}

Set is a generic set data structure where each element of type T is unique.

func (Set[T]) Add

func (s Set[T]) Add(value T)

Add adds the given value to the set. If the value is not already present in the set, it will be added. If the value already exists, this method does nothing.

func (Set[T]) Contains

func (s Set[T]) Contains(value T) bool

Contains checks if the given value is present in the set.

func (Set[T]) Delete

func (s Set[T]) Delete(value T)

Directories

Path Synopsis
package errortrace provides utilities to work with errors containing stack traces.
package errortrace provides utilities to work with errors containing stack traces.
Package filepath implements utility routines for manipulating filename paths.
Package filepath implements utility routines for manipulating filename paths.
sorted
Package sorted provides functions for manipulating sorted slices.
Package sorted provides functions for manipulating sorted slices.
tools
cmd/list_n_val_error command
This package is generated by Copilot based on my original instruction to "find function/methods in Go std library that return 2 values plus an error" and some more.
This package is generated by Copilot based on my original instruction to "find function/methods in Go std library that return 2 values plus an error" and some more.

Jump to

Keyboard shortcuts

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