must

package
v0.247.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2024 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

This section is empty.

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 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
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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