Skip to content

Print Struct as String in Golang

When debugging a code it is helpful to have a way to simply print the values into the console.

If you want the indention and pretty print I wrote about it here: JSON Pretty Print.

If you prefer a simpler way then Printf function from the fmt package can help you:

fmt.Printf("%+v\n", yourStruct)

Simple example:

package main

import (
	"fmt"
)

type User struct {
	ID   int64
	Name string
}

func main() {
	user := User{
		ID:   1,
		Name: "Morteza",
	}

	fmt.Printf("%+v\n", user)
}

Leave a Reply

Your email address will not be published. Required fields are marked *