From 5796af6c2b0d9940c07fb402c3c15bf903e04686 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Wed, 4 Oct 2023 10:20:49 +0200 Subject: [PATCH] Add MustMarshal (#9) --- jsn.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/jsn.go b/jsn.go index e15eac7..cf2fdfa 100644 --- a/jsn.go +++ b/jsn.go @@ -44,4 +44,13 @@ func UnmarshalFrom(r io.Reader, v any) error { return nil } +// MustMarshal an object to JSON, and panics if any error occurs. +func MustMarshal(obj any) []byte { + b, err := json.Marshal(obj) + if err != nil { + panic(err) + } + return b +} + var errMore = errors.New("body must contain only one JSON entity")