Golang: Print Struct

By Xah Lee. Date: . Last updated: .

Here's how to print Struct .

fmt.Printf("%v\n", struct)
Print struct value only.
fmt.Printf("%+v\n", struct)
Print both name and value.
fmt.Printf("%#v\n", struct)
Print in golang syntax. Sample output: main.PP{x:1, y:2}
package main

import "fmt"

// using print struct

func main() {

	type PP struct {
		x int
		y int
	}

	var stc = PP{1, 2}
	fmt.Printf("%T\n", stc)  // main.PP
	fmt.Printf("%v\n", stc)  // {1 2}
	fmt.Printf("%+v\n", stc) // {x:1 y:2}
	fmt.Printf("%#v\n", stc) // main.PP{x:1, y:2}
}

Golang Print