How to print struct variables data in Golang?

Print struct variables data in Go refers to the process of displaying the values of a struct's fields on the console or standard output in the Go programming language. Structs in Go are user-defined composite data types that group together variables with different data types under a single name. Printing struct variables data is useful for debugging purposes, logging, or presenting the information in a human-readable format.

To print struct variables data in Golang, you typically use the fmt package, which provides a variety of formatted input/output functions, such as fmt.Print, fmt.Println, or fmt.Printf. These functions allow you to display the values of the struct fields by specifying the appropriate format verbs for each data type (e.g., %s for strings, %d for integers, %f for floats, etc.).


package main

import (

type Person struct {
	Name    string
	Age     int
	Address string

func main() {
	p := Person{
		Name:    "John Doe",
		Age:     30,
		Address: "123 Main St",

	fmt.Printf("Name: %s, Age: %d, Address: %s\n", p.Name, p.Age, p.Address)

	// If you want to print the struct with field names, you can use the '%+v' verb:
	fmt.Printf("Person struct: %+v\n", p)

In this example, we define a Person struct with Name, Age, and Address fields. In the main function, we create a Person instance named p and initialize it with some values. To print the data of the struct variables, we use the fmt.Printf function along with appropriate formatting verbs for each field type (e.g., %s for strings, %d for integers).

Finally, we use the %+v verb to print the entire struct with field names.

Most Helpful This Week