How to print struct in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

Today, we are going to learn how to print struct in golang?, here we will create a struct variable with sample data and we will print the whole struct and specific struct key.

One more thing, some developers think this is an object, yes it is objected but in golang, we call it a struct.

we are going to do…

  1. define a struct type
  2. use struct type and assign some sample data
  3. Print struct
  4. print struct-specific key.
package main
import "fmt"

func main() {

  // define struct type
  type T struct {
    A int
    B string

  // use struct type and assign some sample data
  t := T{23, "skidoo"}

  // Print struct 
  fmt.Println("t", t)

  // print struct sepecifc key
  fmt.Println("A", t.A)
  fmt.Println("B", t.B)

When you run the above code, you will see the golang console printing the whole struct and also the sepecfic struct key value.

print, struct, golang

Try it yourself

All the best 👍

Follow me on Twitter