debug
v0.1.3
Documentation
pkg.go.dev ↗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}
Constants
PrintNoFlags (8 declarations)
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.
Functions
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
PrintFlags
type PrintFlags uint32
PrintFlags is a set of flags that configure the Print* functions behavior.
PrintFlags.Is
func (pf PrintFlags) Is(flagsSet PrintFlags) bool
Is method returns true if pf contains any flag of the flagSet.
PrintFlags.Not
func (pf PrintFlags) Not(flagsSet PrintFlags) bool
Not method returns true if pf has no flags from the flagSet.