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 "github.com/sanity-io/litter"

type Person struct {
	Name   string
	Age    int
	Parent *Person
}

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

will output

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

It supports slices, pointers, and circular types.

Good Hacking

sources:

blog

© All rights reserved. Powered by Astro with ♥.