Standard library / encoding/json

encoding/json.MarshalerError.Error()

Describe a failure returned by a custom JSON or text marshaler, identifying the associated Go type. Expected stdout: 'true true\n'.

Formal Syntax & Structure

go syntaxgo
func (e *MarshalerError) Error() string

Return Value

string

Text representation or diagnostic.

Working Example

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