debug

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2022 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

debug - functions that can be useful for debugging and testing

Go Reference

Package debug provides functions that can be useful for debugging and testing.

Currently, it provides functions:


Feedback

Feel free to open the issue if you have any suggestions, comments or bug reports.

Documentation

Overview

Package debug provides functions that can be useful for debugging and testing.

Example (PrintSliceAllFlags)
slice := []int{1, 1, 2, 3, 5, 8, 13}

PrintSlice(slice, PrintType, PrintCommaSep, PrintNoSharp, PrintGoSyntax, PrintLenCap, PrintValType, PrintValPerLine)
Output:
[]int(7:7){
  0(int):1,
  1(int):1,
  2(int):2,
  3(int):3,
  4(int):5,
  5(int):8,
  6(int):13
}
Example (PrintSliceCommaSepNoSharp)
slice := []int{1, 1, 2, 3, 5, 8}

PrintSlice(slice, PrintCommaSep|PrintNoSharp)
Output:
[0:1, 1:1, 2:2, 3:3, 4:5, 5:8]
Example (PrintSliceDefault)
slice := []string{"one", "two", "three"}

PrintSlice(slice)
Output:
[#0:one #1:two #2:three]
Example (PrintSliceGoSyntaxValType)
slice := []map[string]int{{"one": 1}, {"two": 2}, {"three": 3}}

PrintSlice(slice, PrintGoSyntax, PrintValType)
Output:
[#0(map[string]int):map[string]int{"one":1} #1(map[string]int):map[string]int{"two":2} #2(map[string]int):map[string]int{"three":3}]
Example (PrintSliceNil)
var nilSlice []any

PrintSlice(nilSlice)
Output:
[]
Example (PrintSliceStructs)
type point struct{ x, y int }
type eventInfo struct {
	cond   bool
	amount int
	avg    float32
	descr  string
	pos    point
}
slice := []eventInfo{
	{
		cond:   true,
		amount: 5,
		avg:    3.434,
		descr:  "positive condition",
		pos:    point{x: 15, y: 83},
	},
}

PrintSlice(slice, PrintGoSyntax)
Output:
[#0:debug.eventInfo{cond:true, amount:5, avg:3.434, descr:"positive condition", pos:debug.point{x:15, y:83}}]
Example (PrintSliceTypeLenCap)
slice := []string{"one", "two", "three"}

PrintSlice(slice, PrintType|PrintLenCap)
Output:
[]string(3:3){#0:one #1:two #2:three}

Index

Examples

Constants

View Source
const (
	PrintNoFlags    = PrintFlags(1) << iota
	PrintType       // print type of the argument before the actual argument's content
	PrintCommaSep   // print commas as a content element separator
	PrintNoSharp    // disables print # chbefore the ordinal number of the items
	PrintGoSyntax   // enables Go-syntax style output of argument elements
	PrintLenCap     // print of the length and capacity of the argument before the actual content
	PrintValType    // print the type of each element before print the element's content
	PrintValPerLine // print one element per line
)

PrintFlags configure the output format of Print* functions.

Variables

This section is empty.

Functions

func PrintSlice

func PrintSlice[T any](slice []T, flagsVariadic ...PrintFlags)

PrintSlice outputs a slice of type T (see Go generics). The flagsVariadic parameter determines the output format and can be a bitmask:

PrintSlice(slice, debug.PrintType|debug.PrintCommaSep)

or a separately defined argument list:

PrintSlice(slice, debug.PrintType, debug.PrintCommaSep)

By default, PrintSlice output is similar to fmt.Println output, but each item is preceded by its ordinal number, denoted by #, and separated from the item value by a colon. The output is terminated with a newline character.

For example,

ints := []int{1, 2, 3, 4}
debug.PrintSlice(ints)

strs := []string{"one", "two", "three", "four"}
debug.PrintSlice(strs)

will produce:

[#0:1 #1:2 #2:3 #3:4]
[#0:one #1:two #2:three #3:four]

See more examples in the Examples section.

Types

type PrintFlags

type PrintFlags uint32

PrintFlags is a set of flags that configure the Print* functions behavior.

func (PrintFlags) Is

func (pf PrintFlags) Is(flagsSet PrintFlags) bool

Is method returns true if pf contains any flag of the flagSet.

func (PrintFlags) Not

func (pf PrintFlags) Not(flagsSet PrintFlags) bool

Not method returns true if pf has no flags from the flagSet.

Jump to

Keyboard shortcuts

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