Documentation ¶
Overview ¶
Package pretty offers print functions that format values of any Go type in a compact single line string suitable for logging and debugging. Strings are escaped to be single line with fmt.Sprintf("%#q", s). %#q is used instead of %q to minimize the number of double quotes that would have to be escaped in JSON logs.
MaxStringLength, MaxErrorLength, MaxSliceLength can be set to values greater zero to prevent excessive log sizes. An ellipsis rune is used as last element to represent the truncated elements.
Index ¶
- Constants
- Variables
- func Fprint(w io.Writer, value any, indent ...string)
- func Fprintln(w io.Writer, value any, indent ...string)
- func Indent(source []byte, indent string, linePrefix ...string) []byte
- func Print(value any, indent ...string)
- func PrintAsJSON(input any, indent ...string)
- func Println(value any, indent ...string)
- func Sprint(value any, indent ...string) string
- type Nullable
- type Printable
- type Printer
- func (p *Printer) Fprint(w io.Writer, value any, indent ...string)
- func (p *Printer) Fprintln(w io.Writer, value any, indent ...string)
- func (p *Printer) Print(value any, indent ...string)
- func (p *Printer) Println(value any, indent ...string)
- func (p *Printer) Sprint(value any, indent ...string) string
Examples ¶
Constants ¶
const CircularRef = "CIRCULAR_REF"
CircularRef is a replacement token CIRCULAR_REF that will be printed instad of a circular data reference.
Variables ¶
var DefaultPrinter = Printer{
MaxStringLength: 200,
MaxErrorLength: 2000,
MaxSliceLength: 20,
}
DefaultPrinter is used by the package level print functions
Functions ¶
func Indent ¶
Indent pretty printed source using the passed indent string and an optional linePrefix used for every line in case of a multiple line result.
func PrintAsJSON ¶
PrintAsJSON marshalles input as indented JSON and calles fmt.Println with the result. If indent arguments are given, they are joined into a string and used as JSON line indent. If no indet argument is given, two spaces will be used to indent JSON lines. A byte slice as input will be marshalled as json.RawMessage.
func Println ¶
Println pretty prints a value to os.Stdout followed by a newline
Example ¶
type Parent struct { Map map[int]string } type Struct struct { Parent Int int unexported bool Str string Sub struct { Map map[string]string } } value := &Struct{ Sub: struct{ Map map[string]string }{ Map: map[string]string{ "key": "value", // Note that the resulting `Multi\nLine` is not a valid Go string. // Double quotes are avoided for better readability of // pretty printed strings in JSON. "Multi\nLine": "true", }, }, } Println(value) Println(value, " ") Println(value, " ", " ")
Output: Struct{Parent{Map:nil};Int:0;Str:``;Sub:{Map:{`Multi\nLine`:`true`;`key`:`value`}}} Struct{ Parent{ Map: nil } Int: 0 Str: `` Sub: { Map: { `Multi\nLine`: `true` `key`: `value` } } } Struct{ Parent{ Map: nil } Int: 0 Str: `` Sub: { Map: { `Multi\nLine`: `true` `key`: `value` } } }
Types ¶
type Nullable ¶
type Nullable interface { // IsNull returns true if the implementing value is considered null. IsNull() bool }
Nullable can be implemented to print "null" instead of the representation of the underlying type's value.
type Printer ¶
type Printer struct { // MaxStringLength is the maximum length for escaped strings. // Longer strings will be truncated with an ellipsis rune at the end. // A value <= 0 will disable truncating. MaxStringLength int // MaxErrorLength is the maximum length for escaped errors. // Longer errors will be truncated with an ellipsis rune at the end. // A value <= 0 will disable truncating. MaxErrorLength int // MaxSliceLength is the maximum length for slices. // Longer slices will be truncated with an ellipsis rune as last element. // A value <= 0 will disable truncating. MaxSliceLength int }
Printer holds a pretty-print configuration