Standard library / encoding/json

encoding/json.MarshalerError.Unwrap()

Return the underlying error from a custom marshaling failure, enabling errors.Is and errors.As traversal. Expected stdout: 'true true\n'.

Formal Syntax & Structure

go syntaxgo
func (e *MarshalerError) Unwrap() error

Return Value

error

Underlying error.

Working Example

Example Snippetgo
package main
import (  "encoding/json"  "errors"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
var broken = errors.New("broken")
type Bad struct{}
func (Bad) MarshalJSON() ([]byte, error) { return nil, broken }func main() {  _, e := json.Marshal(Bad{})  var x *json.MarshalerError  fmt.Println(errors.As(e, &x), x.Unwrap() == broken)}
Go Reference