- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program Prints a Person's Name and Address by Taking Person Struct as a Parameter
Throughout this article, we will dive into the details of implementing the PrintPerson function, understanding the structure of a Person, and executing the program to obtain the desired output. So, let's get started and learn how to utilize Go's features to print person details effectively. In this article, we will explore how to create a Go program that features a function called PrintPerson. Here we are going to use two different methods: using printin function and using printf function along with examples to elaborate the concept.
Syntax
printperson(person)
This represents a function called printperson, and the person is an argument passed to that function.
p.name
It is used to access the name field of the variable p.
p − represent a variable of a specific type.
name − it represents the field name.
p.address
It is used to access the address of the variable p.
p − represent a variable of a specific type.
address − it represent the field address
Algorithm
Define a Person struct with fields for name and address.
Create the PrintPerson function that takes a Person struct as a parameter.
Inside the function, access the name and address fields of the Person struct parameter.
Print out the name and address values using fmt.Println or a similar printing function.
Test the program by creating instances of the Person struct and passing them to the PrintPerson function.
Verify that the function correctly prints the name and address values for each Person instance.
Example 1
In this example, we first define the Person struct with Name and Address fields.
Next, we create the PrintPerson function that takes a Person struct as a parameter. Inside the function, we use the Println function from the fmt package to print the name and address of the Person. In the main function, we create an instance of the Person struct with a name and address. Then, we call the PrintPerson function, passing the person instance as an argument. This will output the name and address of the person using the Println function.
package main import "fmt" type Person struct { Name string Address string } func PrintPerson(p Person) { fmt.Println("Name:", p.Name) fmt.Println("Address:", p.Address) } func main() { person := Person{ Name: "John Doe", Address: "123 Main Street", } PrintPerson(person) }
Output
Name: John Doe Address: 123 Main Street
Example 2
In this code example, we define the Person struct with Name and Address fields.
Next, we create the PrintPerson function that takes a Person struct as a parameter. Inside the function, we use the Printf function from the fmt package to format and print the name and address of the Person. The %s placeholder is used to indicate where the corresponding string value should be inserted.
package main import "fmt" type Person struct { Name string Address string } func PrintPerson(p Person) { fmt.Printf("Name: %s\n", p.Name) fmt.Printf("Address: %s\n", p.Address) } func main() { person := Person{ Name: "John Doe", Address: "123 Main Street", } PrintPerson(person) }
Output
Name: John Doe Address: 123 Main Street
Conclusion
The Golang program to form the PrintPerson work represents the control of capacities in Go programming. Bypassing an Individual struct as a parameter, the work proficiently prints out the title and address of the individual. This advances code seclusion, lucidness, and reusability, permitting for a clean and organized program plan. The capacity to typify particular errands in capacities upgrades the general productivity and practicality of the code. By utilizing this approach, engineers can effortlessly print the points of interest of an individual put away in an Individual struct all through their Go programs.