Menu
Coddy logo textTech

Basic Contact List

Part of the Logic & Flow section of Coddy's GO journey — lesson 66 of 68.

challenge icon

Challenge

Easy

Create a contact management system that demonstrates how structs, slices, and functions work together to organize and display contact information. This challenge will test your ability to define custom data structures, manage collections of data, and implement functions to manipulate that data.

You will receive two inputs:

  • A string containing contact information in the format "name1:phone1:email1,name2:phone2:email2,name3:phone3:email3" (e.g., "Alice Johnson:555-0123:alice@email.com,Bob Smith:555-0456:bob@email.com,Carol Davis:555-0789:carol@email.com")
  • A string containing a new contact to add in the format "name:phone:email" (e.g., "David Wilson:555-0321:david@email.com")

Your task is to:

  1. Define a Contact struct with three string fields: Name, Phone, and Email
  2. Create a function called addContact that takes a slice of Contact structs and a new Contact, then returns a new slice with the contact added
  3. Create a function called displayContacts that takes a slice of Contact structs and prints each contact's information
  4. Parse the first input by splitting on commas to get individual contact entries
  5. For each contact entry, split on colons to extract name, phone, and email
  6. Create Contact structs from the parsed data and store them in a slice
  7. Parse the second input by splitting on colons to create a new contact
  8. Display the application header: "=== CONTACT MANAGEMENT SYSTEM ==="
  9. Display the initial contacts using your displayContacts function with the header: "Initial contact list:"
  10. Display the new contact being added: "Adding new contact: [name] ([phone]) - [email]"
  11. Use your addContact function to add the new contact to the slice
  12. Display the updated contacts using your displayContacts function with the header: "Updated contact list:"
  13. Display contact statistics:
    • "=== CONTACT STATISTICS ==="
    • "Total contacts: [number_of_contacts]"
    • "Contacts added this session: 1"
  14. Display the completion message: "Contact management operations completed successfully"

The displayContacts function should print each contact in the format: "[index]. [name] - Phone: [phone], Email: [email]" where index starts from 1.

Use the strings package to split input strings and the fmt package for formatted output. This challenge demonstrates how to combine structs for data organization, slices for collections, and functions for code organization - fundamental patterns used throughout Go programming.

Try it yourself

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

// TODO: Define your Contact struct here

// TODO: Implement addContact function here

// TODO: Implement displayContacts function here

func main() {
	// Read input using bufio.Scanner to handle spaces in names properly
	scanner := bufio.NewScanner(os.Stdin)
	
	scanner.Scan()
	contactsInput := scanner.Text()
	
	scanner.Scan()
	newContactInput := scanner.Text()
	
	// TODO: Write your code below
	// Parse the contacts input and create Contact structs
	// Remember to check if parts slice has enough elements before accessing
	// Parse the new contact input
	// Display the application header
	// Display initial contacts
	// Add the new contact
	// Display updated contacts
	// Display statistics
	// Display completion message
}

All lessons in Logic & Flow