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 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us


Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

I'm working on some more Cheat Sheets book on Jquery, TypeScript, React js and for other languages. I will send it once it's completed.

Stay tuned working on React Js Cheat Sheets Book

Related Posts