pretty

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2015 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package pretty pretty-prints Go structures.

This package uses reflection to examine a Go value and can print out in a nice, aligned fashion. It supports three modes (normal, compact, and extended) for advanced use.

See the Reflect and Print examples for what the output looks like.

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultConfig = &Config{}

Functions

func Compare

func Compare(got, want interface{}) string

Compare returns a string containing a line-by-line unified diff of the values in got and want. Compare includes unexported fields.

Each line in the output is prefixed with '+', '-', or ' ' to indicate if it should be added to, removed from, or is correct for the "got" value with respect to the "want" value.

Example
package main

import (
	"fmt"
	"github.com/kylelemons/godebug/pretty"
)

func main() {
	type ShipManifest struct {
		Name     string
		Crew     map[string]string
		Androids int
		Stolen   bool
	}

	reported := &ShipManifest{
		Name: "Spaceship Heart of Gold",
		Crew: map[string]string{
			"Zaphod Beeblebrox": "Galactic President",
			"Trillian":          "Human",
			"Ford Prefect":      "A Hoopy Frood",
			"Arthur Dent":       "Along for the Ride",
		},
		Androids: 1,
		Stolen:   true,
	}

	expected := &ShipManifest{
		Name: "Spaceship Heart of Gold",
		Crew: map[string]string{
			"Rowan Artosok": "Captain",
		},
		Androids: 1,
		Stolen:   false,
	}

	fmt.Println(pretty.Compare(reported, expected))
}
Output:

 {
  Name:     "Spaceship Heart of Gold",
  Crew:     {
-            Arthur Dent:       "Along for the Ride",
-            Ford Prefect:      "A Hoopy Frood",
-            Trillian:          "Human",
-            Zaphod Beeblebrox: "Galactic President",
+            Rowan Artosok: "Captain",
            },
  Androids: 1,
- Stolen:   true,
+ Stolen:   false,
 }

func Fprint

func Fprint(w io.Writer, vals ...interface{}) (n int64, err error)

Fprint writes the representation of the given value to the writer according to the default config.

func Print

func Print(vals ...interface{})

Print writes the default representation of the given values to standard output.

Example
package main

import (
	"github.com/kylelemons/godebug/pretty"
)

func main() {
	type ShipManifest struct {
		Name     string
		Crew     map[string]string
		Androids int
		Stolen   bool
	}

	manifest := &ShipManifest{
		Name: "Spaceship Heart of Gold",
		Crew: map[string]string{
			"Zaphod Beeblebrox": "Galactic President",
			"Trillian":          "Human",
			"Ford Prefect":      "A Hoopy Frood",
			"Arthur Dent":       "Along for the Ride",
		},
		Androids: 1,
		Stolen:   true,
	}

	pretty.Print(manifest)

}
Output:

{Name:     "Spaceship Heart of Gold",
 Crew:     {Arthur Dent:       "Along for the Ride",
            Ford Prefect:      "A Hoopy Frood",
            Trillian:          "Human",
            Zaphod Beeblebrox: "Galactic President"},
 Androids: 1,
 Stolen:   true}

func Sprint

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

Sprint returns a string representation of the given value according to the default config.

Types

type Config

type Config struct {
	// Verbosity options
	Compact  bool // One-line output. Overrides Diffable.
	Diffable bool // Adds extra newlines for more easily diffable output.

	// Field and value options
	IncludeUnexported bool // Include unexported fields in output
	PrintStringers    bool // Call String on a fmt.Stringer

	// Output transforms
	ShortList int // Maximum character length for short lists if nonzero.
}

A Config represents optional configuration parameters for formatting.

Some options, notably ShortList, dramatically increase the overhead of pretty-printing a value.

func (*Config) Fprint

func (cfg *Config) Fprint(w io.Writer, vals ...interface{}) (n int64, err error)

Fprint writes the representation of the given value to the writer according to the cfg.

func (*Config) Print

func (cfg *Config) Print(vals ...interface{})

Print writes the configured presentation of the given values to standard output.

func (*Config) Sprint

func (cfg *Config) Sprint(vals ...interface{}) string

Sprint returns a string representation of the given value according to cfg.

Example
package main

import (
	"fmt"
	"github.com/kylelemons/godebug/pretty"
)

func main() {
	type Pair [2]int
	type Map struct {
		Name      string
		Players   map[string]Pair
		Obstacles map[Pair]string
	}

	m := Map{
		Name: "Rock Creek",
		Players: map[string]Pair{
			"player1": {1, 3},
			"player2": {0, -1},
		},
		Obstacles: map[Pair]string{
			Pair{0, 0}: "rock",
			Pair{2, 1}: "pond",
			Pair{1, 1}: "stream",
			Pair{0, 1}: "stream",
		},
	}

	// Specific output formats
	compact := &pretty.Config{
		Compact: true,
	}
	diffable := &pretty.Config{
		Diffable: true,
	}

	// Print out a summary
	fmt.Printf("Players: %s\n", compact.Sprint(m.Players))

	// Print diffable output
	fmt.Printf("Map State:\n%s", diffable.Sprint(m))

}
Output:

Players: {player1:[1,3],player2:[0,-1]}
Map State:
{
 Name:      "Rock Creek",
 Players:   {
             player1: [
                       1,
                       3,
                      ],
             player2: [
                       0,
                       -1,
                      ],
            },
 Obstacles: {
             [0,0]: "rock",
             [0,1]: "stream",
             [1,1]: "stream",
             [2,1]: "pond",
            },
}

Jump to

Keyboard shortcuts

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