Getting Started

Concepts

Writer

gfmt.Writer is the interface that wraps the generic Write method, which writes arbitrary types to the underlying output stream. Typically, this is an io.Writer such as http.ResponseWriter. Write returns the number of bytes written and any error encountered that caused the write to stop early.

type Writer interface {
	Write(i interface{}) (int, error)
}

Formatter

A gfmt.Formatter converts values of a certain type to their string representation.

type Formatter interface {
	Format(i interface{}) (string, error)
}

Typically, Formatter implementations are not used directly. Instead, they are used by Writer to customize the output of complex data types. Unlike Writer, a Formatter should be stateless and could be used concurrently.

Resolver

A Resolver returns field metadata for the given struct type. In particular, a field holds information like

type Resolver func(typ reflect.Type) []field

By default, gutenfmt uses a Resolver, based on encoding/json. It uses reflection to lists fields that JSON should recognize for the given type. The algorithm is breadth-first search over the set of structs to include - the top struct and then any reachable anonymous structs.

The default Resolver assumes JSON semantics.

However, this is can be customized as follows: Use different metadata for a struct

Examples

More comprehensive examples, including, but not limited to

can be found on the Examples page.