format

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2022 License: MIT Imports: 8 Imported by: 2

README

go-format

Simple string formatting package with improved visually-friendly formatting of structs. This library will ascertain the values contained by pointers (where possible) unlike the default "fmt" package, which can make for improved debug output. Formatting recursion depth is also customizable, to prevent issues you'd expect from these things (see documentation for Formatter.MaxDepth).

Available string formatting directives are fairly limited, this might change in the future.

Benchmarks put this library on par with the standard library (we might be a little faster).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(buf *Buffer, v ...interface{})

Append will append formatted form of supplied values into 'buf' using default formatter.

func Appendf

func Appendf(buf *Buffer, s string, a ...interface{})

Appendf will append the formatted string with supplied values into 'buf' using default formatter. Supported format directives: - '%v' => format supplied arg, in place - '%#v' => format supplied arg verbosely, in place

To include a non-directive '%' character escape it with an additional '%'.

More formatting directives might be included in the future.

func Fprint

func Fprint(w io.Writer, v ...interface{}) (int, error)

Fprint will format supplied values, writing this to an io.Writer.

func Fprintf

func Fprintf(w io.Writer, s string, a ...interface{}) (int, error)

Fprintf will format supplied format string and args, writing this to an io.Writer. See documentation for Formatter.Appendf(...) for format string usage.

func Fprintln

func Fprintln(w io.Writer, v ...interface{}) (int, error)

Println will format supplied values, append a trailing newline and writer this to an io.Writer.

func Print

func Print(v ...interface{})

Print will format supplied values, print this to os.Stdout.

func Printf

func Printf(s string, a ...interface{})

Printf will format supplied format string and args, printing this to os.Stdout. See documentation for Formatter.Appendf(...) for format string usage.

func Println

func Println(v ...interface{})

Println will format supplied values, append a trailing newline and print this to os.Stdout.

func Sprint

func Sprint(v ...interface{}) string

Sprint will format supplied values, returning this string.

func Sprintf

func Sprintf(s string, a ...interface{}) string

Sprintf will format supplied format string and args, returning this string. See documentation for Formatter.Appendf(...) for format string usage.

Types

type Buffer

type Buffer struct {
	B []byte
}

Buffer is a simple wrapper around a byte slice.

func (*Buffer) Append

func (buf *Buffer) Append(b []byte)

Append will append given byte slice to the buffer.

func (*Buffer) AppendByte

func (buf *Buffer) AppendByte(b byte)

AppendByte appends given byte to the buffer.

func (*Buffer) AppendRune

func (buf *Buffer) AppendRune(r rune)

AppendRune appends given rune to the buffer.

func (*Buffer) AppendString

func (buf *Buffer) AppendString(s string)

AppendString appends given string to the buffer.

func (*Buffer) Cap

func (buf *Buffer) Cap() int

Cap returns the capacity of the buffer's underlying byte slice.

func (*Buffer) Len

func (buf *Buffer) Len() int

Len returns the length of the buffer's underlying byte slice.

func (*Buffer) Reset

func (buf *Buffer) Reset()

Reset will reset the buffer length to 0 (retains capacity).

func (*Buffer) String

func (buf *Buffer) String() string

String returns the underlying byte slice as a string. Please note this value is tied directly to the underlying byte slice, if you write to the buffer then returned string values will also change.

func (*Buffer) Truncate

func (buf *Buffer) Truncate(n int)

Truncate will reduce the length of the buffer by 'n'.

func (*Buffer) Write added in v1.0.1

func (buf *Buffer) Write(b []byte) (int, error)

Write will append given byte slice to buffer, fulfilling io.Writer.

type Formattable

type Formattable interface {
	AppendFormat([]byte) []byte
}

Formattable defines a type capable of being formatted and appended to a byte buffer.

type Formatter

type Formatter struct {
	// MaxDepth specifies the max depth of fields the formatter will iterate.
	// Once max depth is reached, value will simply be formatted as "...".
	// e.g.
	//
	// MaxDepth=1
	// type A struct{
	//     Nested B
	// }
	// type B struct{
	//     Nested C
	// }
	// type C struct{
	//     Field string
	// }
	//
	// Append(&buf, A{}) => {Nested={Nested={Field=...}}}
	MaxDepth uint8
}

Formatter allows configuring value and string formatting.

func (Formatter) Append

func (f Formatter) Append(buf *Buffer, v ...interface{})

Append will append formatted form of supplied values into 'buf'.

func (Formatter) Appendf

func (f Formatter) Appendf(buf *Buffer, s string, a ...interface{})

Appendf will append the formatted string with supplied values into 'buf'. Supported format directives: - '%v' => format supplied arg, in place - '%#v' => format supplied arg verbosely, in place

To include a non-directive '%' character escape it with an additional '%'.

More formatting directives might be included in the future.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL