package main import ( "bytes" "encoding/json" "fmt" "golang.org/x/xerrors" ) type idError struct { ID int64 Err error Frame xerrors.Frame } func newIDError(err error, id int64) error { return idError{ ID: id, Err: err, Frame: xerrors.Caller(1), } } func (err idError) Unwrap() error { return err.Err } func (err idError) FormatError(p xerrors.Printer) error { err.Frame.Format(p) return err.Unwrap() } func (err idError) Error() string { return fmt.Sprintf("error while processing id %d: %s", err.ID, err.Err) } type printer struct { *bytes.Buffer } func (p *printer) Detail() bool { return true } func (p *printer) Print(args ...interface{}) { fmt.Fprint(p.Buffer, args...) } func (p *printer) Printf(format string, args ...interface{}) { fmt.Fprintf(p.Buffer, format, args...) } type errorjson struct { Error error `json:"error"` Message string `json:"message"` Type string `json:"type"` Location string `json:"location,omitempty"` Original json.RawMessage `json:"original,omitempty"` } func encodeError(err error) ([]byte, error) { if err == nil { return nil, nil } var original error if w, ok := err.(xerrors.Wrapper); ok { original = w.Unwrap() } var originalBytes []byte if original != nil { var eerr error originalBytes, eerr = encodeError(original) if eerr != nil { return nil, xerrors.Errorf("encoding original error: %w", err) } } var p = &printer{ Buffer: &bytes.Buffer{}, } if f, ok := err.(xerrors.Formatter); ok { f.FormatError(p) } var ejson = errorjson{ Error: err, Message: err.Error(), Type: fmt.Sprintf("%T", err), Location: p.String(), Original: json.RawMessage(originalBytes), } return json.MarshalIndent(ejson, "", " ") } func main() { var original = xerrors.New("original error") var wrapped = xerrors.Errorf("error while doing stuff: %w", original) var iderr = newIDError(wrapped, 42) var b, err = encodeError(iderr) fmt.Printf("%s

%v

", b, err) }

About the Playground

The Go Playground is a web service that runs on golang.org's servers. The service receives a Go program, vets, compiles, links, and runs the program inside a sandbox, then returns the output.

If the program contains tests or examples and no main function, the service runs the tests. Benchmarks will likely not be supported since the program runs in a sandboxed environment with limited resources.

There are limitations to the programs that can be run in the playground:

The playground can use most of the standard library, with some exceptions. The only communication a playground program has to the outside world is by writing to standard output and standard error.

In the playground the time begins at 2009-11-10 23:00:00 UTC (determining the significance of this date is an exercise for the reader). This makes it easier to cache programs by giving them deterministic output.

There are also limits on execution time and on CPU and memory usage.

The article "Inside the Go Playground" describes how the playground is implemented. The source code is available at https://go.googlesource.com/playground.

The playground uses the latest stable release of Go.

The current version is go1.14.8.

The playground service is used by more than just the official Go project (Go by Example is one other instance) and we are happy for you to use it on your own site. All we ask is that you contact us first (note this is a public mailing list), use a unique user agent in your requests (so we can identify you), and that your service is of benefit to the Go community.

Any requests for content removal should be directed to security@golang.org. Please include the URL and the reason for the request.