format

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2022 License: MIT Imports: 7 Imported by: 2

README

go-format

String formatting package using Rust-style formatting directives.

Output is generally more visually-friendly than "fmt", while performance is neck-and-neck.

README is WIP.

todos

  • improved verbose + options for printing of number types

  • more test cases

  • improved verbose printing of string ptr types

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. See Formatter.Append() for more documentation.

func Appendf

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

Appendf will append the formatted string with supplied values into 'buf' using default formatter. See Formatter.Appendf() for more documentation.

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 Formatter.Appendf() for more documentation.

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 Formatter.Appendf() for more documentation.

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 Formatter.Appendf() for more documentation.

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: - '{}' => format supplied arg, in place - '{0}' => format arg at index 0 of supplied, in place - '{:?}' => format supplied arg verbosely, in place - '{:k}' => format supplied arg as key, in place - '{:v}' => format supplied arg as value, in place - '{:T}' => format supplied arg's type, in place

To escape either of '{}' simply append an additional brace e.g. - '{{' => '{' - '}}' => '}' - '{{}}' => '{}' - '{{:?}}' => '{:?}'

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