goutil/dump - print beautiful and easy-to-read go data
Tags: go, tooling, gookit, commandline
Published: 2022-02-24 05:03:29

`goutil/dump` - is a golang data printing toolkit that can print beautiful and easy-to-read go slice, map, and struct data.

**main functions**:

- Simple to use, just call `dump.P(vars…)` directly
- Supports all basic data types.
- Supports slice, map, and struct data structures.
- Supports passing in and printing multiple variables
- Default output call location, easy to use
- Supports custom features, such as indentation and color themes.

**Preview**：

![output-example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fq9k1y8yulrokx2cd77u.png)
 
> Git Repo:

* Github: [https://github.com/gookit/goutil/](https://github.com/gookit/goutil)
* GoDoc: [https://pkg.go.dev/github.com/gookit/goutil/dump](https://pkg.go.dev/github.com/gookit/goutil/dump)

## Print basic type

```go
package main
import "github.com/gookit/goutil/dump"

// rum demo:
//     go run ./dump/_examples/basic_types.go
func main() {
    dump.P(
        nil, true,
        12, int8(12), int16(12), int32(12), int64(12),
        uint(22), uint8(22), uint16(22), uint32(22), uint64(22),
        float32(23.78), float64(56.45),
        'c', byte('d'),
        "string",
    )
}
```

**Output**：

![print-basic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wva7m0bigau7cc5wkcxy.png)
 

## Print slice

Printing array, slice will output one element per line, and will output the length at the end.

```go
package main

import "github.com/gookit/goutil/dump"

// rum demo:
//     go run ./dump/_examples/slice.go
func main() {
    dump.P(
        []byte("abc"),
        []int{1, 2, 3},
        []string{"ab", "cd"},
        []interface{}{
            "ab",
            234,
            []int{1, 3},
            []string{"ab", "cd"},
        },
    )
}
```

**Output**：

![print-slice](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qk3jkepvfo16pvesi4u9.png)
 

## Print map

When printing the map data structure, one element per line is output, and the length of the map is output at the end.

```go
package main

import "github.com/gookit/goutil/dump"

// rum demo:
//     go run ./map.go
//     go run ./dump/_examples/map.go
func main() {
    dump.P(
        map[string]interface{}{
            "key0": 123,
            "key1": "value1",
            "key2": []int{1, 2, 3},
            "key3": map[string]string{
                "k0": "v0",
                "k1": "v1",
            },
        },
    )
}
```

**Output**：

![print-map](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkzb4e6mjfjc1h3r4d92.png)
 
## Print struct

Print struct data, the pointer type will automatically print the underlying real data.

```go
package main

import (
    "fmt"
    "github.com/gookit/color"
    "github.com/gookit/goutil/dump"
)

// rum demo:
//     go run ./struct.go
//     go run ./dump/_examples/struct.go
func main() {
    s1 := &struct {
        cannotExport map[string]interface{}
    }{
        cannotExport: map[string]interface{}{
            "key1": 12,
            "key2": "abcd123",
        },
    }    s2 := struct {
        ab string
        Cd int
    }{
        "ab", 23,
    }
    color.Infoln("- Use fmt.Println:")
    fmt.Println(s1, s2)
    color.Infoln("\n- Use dump.Println:")
    dump.P(
        s1,
        s2,
    )
}
```

**Output**：

![print-struct](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9e1obvxn0h7bp34b3tsf.png)
 

## Custom dumper

you can customize dumper options. Such as indentation and color theme

```go
// Options for dump vars
type Options struct {
    // Output the output writer
    Output io.Writer
    // NoType dont show data type TODO
    NoType bool
    // NoColor don't with color
    NoColor bool
    // IndentLen width. default is 2
    IndentLen int
    // IndentChar default is one space
    IndentChar byte
    // MaxDepth for nested print
    MaxDepth int
    // ShowFlag for display caller position
    ShowFlag int
    // MoreLenNL array/slice elements length > MoreLenNL, will wrap new line
    // MoreLenNL int
    // CallerSkip skip for call runtime.Caller()
    CallerSkip int
    // ColorTheme for print result.
    ColorTheme Theme
}
```

## Git & docs

* Github: [https://github.com/gookit/goutil/](https://github.com/gookit/goutil)
* GoDoc: [https://pkg.go.dev/github.com/gookit/goutil/dump](https://pkg.go.dev/github.com/gookit/goutil/dump)