must

package
v0.295.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2025 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Overview

Package must is a syntax sugar package to make the use of `Must` functions.

The `must` package provides an easy way to make functions panic on error. This is typically used at the global variable scope where returning an error is inconvenient and meaningful error recovery isn't possible due to it being a programming error. For example, the two variant functions behave the same:

must.Must(regexp.Compile(`regexp`))
regexp.Must(regexp.Compile(`regexp`)

Dot import can be used since the package is intentionally kept small and focused on this specific topic:

Must(regexp.Compile(`regexp`))

Index

Examples

Constants

View Source
const ErrNotOK = "ErrNotOK"

Variables

This section is empty.

Functions

func Must

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

Must is a syntax sugar to express things like must.Must(regexp.Compile(`regexp`))

Example
package main

import (
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := func() (int, error) { return 42, nil }
	val := must.Must(fn())
	_ = val
}
Example (HtmlTemplate)
package main

import (
	htmlTemplate "html/template"

	"go.llib.dev/frameless/pkg/must"
)

func main() {
	tmpl := must.Must(htmlTemplate.New("").Parse(`<div>{{.Label}}</div>`))
	_ = tmpl
}
Example (IoReadAll)
package main

import (
	"bytes"
	"io"

	"go.llib.dev/frameless/pkg/must"
)

func main() {
	in := bytes.NewReader([]byte("Hello, world!"))
	data := must.Must(io.ReadAll(in))
	_ = data
}
Example (Regexp)
package main

import (
	"regexp"

	"go.llib.dev/frameless/pkg/must"
)

func main() {
	must.Must(regexp.Compile(`^\w+$`))
}
Example (TextTemplate)
package main

import (
	txtTemplate "text/template"

	"go.llib.dev/frameless/pkg/must"
)

func main() {
	tmpl := must.Must(txtTemplate.New("").Parse(`{{.Label}}`))
	_ = tmpl
}

func Must0 added in v0.265.0

func Must0(err error)
Example
package main

import (
	"go.llib.dev/frameless/pkg/errorkit"
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := errorkit.NullErrFunc
	must.Must0(fn())
}

func Must2

func Must2[A, B any](a A, b B, err error) (A, B)
Example
package main

import (
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := func() (string, int, error) { return "the answer is", 42, nil }
	a, b := must.Must2(fn())
	_, _ = a, b
}

func Must3

func Must3[A, B, C any](a A, b B, c C, err error) (A, B, C)
Example
package main

import (
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := func() (string, int, bool, error) { return "the answer is", 42, true, nil }
	a, b, c := must.Must3(fn())
	_, _, _ = a, b, c
}

func Must4

func Must4[A, B, C, D any](a A, b B, c C, d D, err error) (A, B, C, D)
Example
package main

import (
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := func() (string, int, bool, float32, error) { return "the answer is", 42, true, 42.42, nil }
	a, b, c, d := must.Must4(fn())
	_, _, _, _ = a, b, c, d
}

func Must5

func Must5[A, B, C, D, E any](a A, b B, c C, d D, e E, err error) (A, B, C, D, E)
Example
package main

import (
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := func() (string, int, bool, float32, float64, error) {
		return "the answer is", 42, true, 42.42, 24.24, nil
	}
	a, b, c, d, e := must.Must5(fn())
	_, _, _, _, _ = a, b, c, d, e
}

func OK added in v0.271.0

func OK[T any](v T, ok bool) T

func OK0 added in v0.271.0

func OK0(ok bool)
Example
package main

import (
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := func() bool { return true }
	must.OK0(fn())
}

func OK2 added in v0.271.0

func OK2[A, B any](a A, b B, ok bool) (A, B)
Example
package main

import (
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := func() (string, int, bool) { return "the answer is", 42, true }
	a, b := must.OK2(fn())
	_, _ = a, b
}

func OK3 added in v0.271.0

func OK3[A, B, C any](a A, b B, c C, ok bool) (A, B, C)
Example
package main

import (
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := func() (string, int, bool, bool) { return "the answer is", 42, true, true }
	a, b, c := must.OK3(fn())
	_, _, _ = a, b, c
}

func OK4 added in v0.271.0

func OK4[A, B, C, D any](a A, b B, c C, d D, ok bool) (A, B, C, D)
Example
package main

import (
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := func() (string, int, bool, float32, bool) { return "the answer is", 42, true, 42.42, true }
	a, b, c, d := must.OK4(fn())
	_, _, _, _ = a, b, c, d
}

func OK5 added in v0.271.0

func OK5[A, B, C, D, E any](a A, b B, c C, d D, e E, ok bool) (A, B, C, D, E)
Example
package main

import (
	"go.llib.dev/frameless/pkg/must"
)

func main() {
	fn := func() (string, int, bool, float32, float64, bool) {
		return "the answer is", 42, true, 42.42, 24.24, true
	}
	a, b, c, d, e := must.OK5(fn())
	_, _, _, _, _ = a, b, c, d, e
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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