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 ¶
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 ¶
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 ¶
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 ¶
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
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.
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. |