prettyslice
prettyslice copied to clipboard
Pretty prints slices to any io.Writer: I created this package to teach slices in my Go course.
Pretty Slice Printer
It pretty prints any type of slices to any io.Writer with adjustable coloring features.
Example
package main
import s "github.com/inancgumus/prettyslice"
func main() {
nums := []int{1, 3, 5, 2, 4, 8}
odds := nums[:3]
evens := nums[3:]
nums[1], nums[3] = 9, 6
s.Show("nums", nums)
s.Show("odds : nums[:3]", odds)
s.Show("evens: nums[3:]", evens)
}
Output:

Example #2 — Render Colorless
package main
import s "github.com/inancgumus/prettyslice"
func main() {
// Render colorless output to a file
f, _ := os.Create("out.txt")
defer f.Close()
nums := []int{1, 3, 5, 2, 4, 8}
s.Writer = f
s.Colors(false)
s.Show("nums", nums)
}
Printing Options
- Writer: Control where to draw the output. Default: colors.Output (It's like os.Stdout but with colors).
- PrintBacking: Whether to print the backing array. Default: false.
- PrettyByteRune: Prints the bytes and runes as characters instead of numbers. Default: true.
- MaxPerLine: Maximum number of slice items on a line. Default: 5.
- MaxElements: Limits the number of elements printed. 0 means printing all elements. Default: 0.
- Width: Number of space characters (padding) between the header message and the slice details like len, cap and ptr. Default: 45.
- NormalizePointers: Prints the addresses of the slice elements as if they're contiguous. It basically normalizes by the element type size. See the source code for more information. Default: false.
- PrintHex: Prints the pointers as hexadecimals. Default: false.
- PrintBytesHex: Prints byte elements as hex digits. Overrides the PrettyByteRune option for byte values. Default: false.
- PrintElementAddr: Prints the element addresses. Default: false.
Coloring Options
- ColorHeader: Sets the color for the header. Default: color.New(color.BgHiBlack, color.FgMagenta, color.Bold).
- ColorSlice: Sets the color for the slice elements. Default: color.New(color.FgCyan).
- ColorBacker: Sets the color for the backing array elements. Default: color.New(color.FgHiBlack).
- ColorIndex: Sets the color for the index numbers. Default: ColorBacker.
- ColorAddr: Sets the color for the element addresses. Default: ColorBacker.
Have fun!