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 ¶
- Constants
- func Must[T any](v T, err error) T
- func Must0(err error)
- func Must2[A, B any](a A, b B, err error) (A, B)
- func Must3[A, B, C any](a A, b B, c C, err error) (A, B, C)
- func Must4[A, B, C, D any](a A, b B, c C, d D, err error) (A, B, C, D)
- 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)
- func OK[T any](v T, ok bool) T
- func OK0(ok bool)
- func OK2[A, B any](a A, b B, ok bool) (A, B)
- func OK3[A, B, C any](a A, b B, c C, ok bool) (A, B, C)
- func OK4[A, B, C, D any](a A, b B, c C, d D, ok bool) (A, B, C, D)
- 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)
Examples ¶
Constants ¶
View Source
const ErrNotOK = "ErrNotOK"
Variables ¶
This section is empty.
Functions ¶
func Must ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 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
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
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
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 }
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.