Golang program to print struct variables


In this Golang article, we will write programs to print struct variables. Here, there is no concept of classes, instead of that structs are used to represent a collection of related fields or properties.

Using Name of the Field and Shorthand Declaration in the Main Function

In this method, we will create a child struct and further create two fields name and age in it. The field values are set by creating the instance of the struct. In the second example, the field values of the Child struct are set in the instance using shorthand declaration and the field values are printed using %s for the name and %d for the age of the child.

Algorithm

  • Step 1 − Import the required packages in the program

  • Step 2 − Create a child struct with name and age variables

  • Step 3 − Create a main function and in that function set the field values of the struct

  • Step 4 − The print statement is executed using Println function from the fmt package where ln means new line

Example 1

Following example demonstrates the Golang program to print struct variables using names of the fields

package main

import "fmt"

type child struct {
   name string
   age  int     
}

func main() {
   c := child{name: "Varun", age: 10}  
   fmt.Println("The name of child is:", c.name)
   fmt.Println("The age of child is:", c.age) 
}

Output

The name of child is: Varun
The age of child is: 10

Example 2

Following example explains how to create Golang program to print struct variables using shorthand declaration in the main function

package main

import "fmt"

type child struct {
   name string
   age  int
}

func main() {
   c := child{"Veronica", 15} 
   fmt.Printf("Name: %s\nAge: %d\n", c.name, c.age)  
}

Output

Name: Veronica
Age: 15

Using the Person Struct

In this method, we will write a Golang program to print struct variables using the Person struct.

Algorithm

  • Step 1 − This program imports the fmt and main package in the program where fmt helps in the formatting of the input and Output and main helps in producing executable codes

  • Step 2 − Create a Person struct with two fields: name of type string and age of type int

  • Step 3 − Create a function main in which set the values in the instance of the struct

  • Step 4 − Call the fields using the dot notation with the variable and print their values on the console

  • Step 5 − The print statement is executed using Println function from the fmt package where ln refers to new line

Example

Following example demonstrates the Golang program to print struct variables using the Person struct

package main

import "fmt"

type Person struct {
   name string
   age  int
}

func main() {
   ps := Person{name: "Saloni", age: 26}
   fmt.Println("Name:", ps.name)
   fmt.Println("Age:", ps.age)
}

Output

Name: Saloni
Age: 26

Conclusion

We compiled and executed the program of printing the struct variables using three examples. In both examples we created a child struct and created its instance in the main, in the second example, we set the field values using shorthand declaration and used formatting verbs to print the fields. In the third example, we used Person struct to print struct variables.

Updated on: 04-Apr-2023

909 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements