How to Print Go Structs


Sometimes for debugging or even testing purposes, you may need to log variables as Go structs directly.

After some errands, I fell on go-spew and pretty that I used quite extensively.

But today I can say I found the Holy Graal, litter:

package main

import ""

type Person struct {
	Name   string
	Age    int
	Parent *Person

func main() {
	Name:   "Bob",
	Age:    20,
	Parent: &Person{
			Name: "Jane",
			Age:  50,

will output

	Name: "Bob",
	Age: 20,
	Parent: &Person{
		Name: "Jane",
		Age: 50,

It supports slices, pointers, and circular types.

Good Hacking



© All rights reserved. Powered by Astro with ♥.