Golang

 

This application helps to store and manage contacts effectively using the Go programming language. It allows users to add, list, and search contacts in a simple and easy-to-use interface.

Objective

The objective of this program is to provide a basic structure for managing contacts. By using Go, we can efficiently add, store, and search contacts in a text-based interface. This simple contact book will allow you to enter new contacts, display existing ones, and search for a contact based on their name.

Program Code


        package main

        import (
            "fmt"
            "strings"
        )

        // Contact structure to store information about a contact
        type Contact struct {
            Name  string
            Phone string
            Email string
        }

        // ContactBook is a slice to store multiple contacts
        type ContactBook struct {
            contacts []Contact
        }

        // AddContact adds a new contact to the ContactBook
        func (cb *ContactBook) AddContact(name, phone, email string) {
            contact := Contact{
                Name:  name,
                Phone: phone,
                Email: email,
            }
            cb.contacts = append(cb.contacts, contact)
        }

        // ListContacts displays all the contacts in the ContactBook
        func (cb *ContactBook) ListContacts() {
            if len(cb.contacts) == 0 {
                fmt.Println("No contacts available.")
                return
            }
            for i, contact := range cb.contacts {
                fmt.Printf("Contact %d: %s, Phone: %s, Email: %s\n", i+1, contact.Name, contact.Phone, contact.Email)
            }
        }

        // SearchContact searches for a contact by name and displays it
        func (cb *ContactBook) SearchContact(name string) {
            found := false
            for _, contact := range cb.contacts {
                if strings.ToLower(contact.Name) == strings.ToLower(name) {
                    fmt.Printf("Found contact: Name: %s, Phone: %s, Email: %s\n", contact.Name, contact.Phone, contact.Email)
                    found = true
                    break
                }
            }
            if !found {
                fmt.Println("Contact not found.")
            }
        }

        func main() {
            var contactBook ContactBook

            // Adding sample contacts
            contactBook.AddContact("Alice Smith", "123-456-7890", "alice.smith@example.com")
            contactBook.AddContact("Bob Johnson", "987-654-3210", "bob.johnson@example.com")

            // Listing all contacts
            fmt.Println("List of all contacts:")
            contactBook.ListContacts()

            // Searching for a contact by name
            fmt.Println("\nSearch for contact 'Alice Smith':")
            contactBook.SearchContact("Alice Smith")
        }
        

Program Structure and Explanation

This Go program implements a simple Contact Book that allows you to:

  • Add contacts with a name, phone number, and email.
  • List all the contacts currently stored in the contact book.
  • Search for a contact by name.

Program Breakdown:

  • Contact struct: Defines the structure for storing each contact’s name, phone, and email.
  • ContactBook struct: Holds a slice of contacts and has methods to manipulate them.
  • AddContact: Adds a new contact to the contact book.
  • ListContacts: Displays all contacts stored in the contact book.
  • SearchContact: Searches for a contact by their name.
  • main function: Acts as the entry point of the program where contacts are added, listed, and searched.

How to Run the Program:

To run this program, follow these steps:

  1. Ensure you have Go installed on your machine. If not, download it from https://golang.org/dl/.
  2. Create a new directory and save the program code in a file with a .go extension (e.g., contactbook.go).
  3. Open your terminal and navigate to the directory where the file is saved.
  4. Run the program using the following command: go run contactbook.go.
  5. The program will display a list of all contacts and search for the contact ‘Alice Smith’.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)