1
// Package pretty provides pretty-printing for Go values. This is
2
// useful during debugging, to avoid wrapping long output lines in
5
// It provides a function, Formatter, that can be used with any
6
// function that accepts a format string. It also provides
7
// convenience wrappers for functions in packages fmt and log.
17
// Errorf is a convenience wrapper for fmt.Errorf.
19
// Calling Errorf(f, x, y) is equivalent to
20
// fmt.Errorf(f, Formatter(x), Formatter(y)).
21
func Errorf(format string, a ...interface{}) error {
22
return fmt.Errorf(format, wrap(a, false)...)
25
// Fprintf is a convenience wrapper for fmt.Fprintf.
27
// Calling Fprintf(w, f, x, y) is equivalent to
28
// fmt.Fprintf(w, f, Formatter(x), Formatter(y)).
29
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, error error) {
30
return fmt.Fprintf(w, format, wrap(a, false)...)
33
// Log is a convenience wrapper for log.Printf.
35
// Calling Log(x, y) is equivalent to
36
// log.Print(Formatter(x), Formatter(y)), but each operand is
37
// formatted with "%# v".
38
func Log(a ...interface{}) {
39
log.Print(wrap(a, true)...)
42
// Logf is a convenience wrapper for log.Printf.
44
// Calling Logf(f, x, y) is equivalent to
45
// log.Printf(f, Formatter(x), Formatter(y)).
46
func Logf(format string, a ...interface{}) {
47
log.Printf(format, wrap(a, false)...)
50
// Logln is a convenience wrapper for log.Printf.
52
// Calling Logln(x, y) is equivalent to
53
// log.Println(Formatter(x), Formatter(y)), but each operand is
54
// formatted with "%# v".
55
func Logln(a ...interface{}) {
56
log.Println(wrap(a, true)...)
59
// Print pretty-prints its operands and writes to standard output.
61
// Calling Print(x, y) is equivalent to
62
// fmt.Print(Formatter(x), Formatter(y)), but each operand is
63
// formatted with "%# v".
64
func Print(a ...interface{}) (n int, errno error) {
65
return fmt.Print(wrap(a, true)...)
68
// Printf is a convenience wrapper for fmt.Printf.
70
// Calling Printf(f, x, y) is equivalent to
71
// fmt.Printf(f, Formatter(x), Formatter(y)).
72
func Printf(format string, a ...interface{}) (n int, errno error) {
73
return fmt.Printf(format, wrap(a, false)...)
76
// Println pretty-prints its operands and writes to standard output.
78
// Calling Println(x, y) is equivalent to
79
// fmt.Println(Formatter(x), Formatter(y)), but each operand is
80
// formatted with "%# v".
81
func Println(a ...interface{}) (n int, errno error) {
82
return fmt.Println(wrap(a, true)...)
85
// Sprint is a convenience wrapper for fmt.Sprintf.
87
// Calling Sprint(x, y) is equivalent to
88
// fmt.Sprint(Formatter(x), Formatter(y)), but each operand is
89
// formatted with "%# v".
90
func Sprint(a ...interface{}) string {
91
return fmt.Sprint(wrap(a, true)...)
94
// Sprintf is a convenience wrapper for fmt.Sprintf.
96
// Calling Sprintf(f, x, y) is equivalent to
97
// fmt.Sprintf(f, Formatter(x), Formatter(y)).
98
func Sprintf(format string, a ...interface{}) string {
99
return fmt.Sprintf(format, wrap(a, false)...)
102
func wrap(a []interface{}, force bool) []interface{} {
103
w := make([]interface{}, len(a))
104
for i, x := range a {
105
w[i] = formatter{v: reflect.ValueOf(x), force: force}