How to print struct in golang?
April 27, 2022Hi 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…
- define a struct type
- use struct type and assign some sample data
- Print struct
- 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.
All the best 👍