Basic Contact List
Part of the Logic & Flow section of Coddy's GO journey — lesson 66 of 68.
Challenge
EasyCreate 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:
- Define a
Contactstruct with three string fields:Name,Phone, andEmail - Create a function called
addContactthat takes a slice ofContactstructs and a newContact, then returns a new slice with the contact added - Create a function called
displayContactsthat takes a slice ofContactstructs and prints each contact's information - Parse the first input by splitting on commas to get individual contact entries
- For each contact entry, split on colons to extract name, phone, and email
- Create
Contactstructs from the parsed data and store them in a slice - Parse the second input by splitting on colons to create a new contact
- Display the application header:
"=== CONTACT MANAGEMENT SYSTEM ===" - Display the initial contacts using your
displayContactsfunction with the header:"Initial contact list:" - Display the new contact being added:
"Adding new contact: [name] ([phone]) - [email]" - Use your
addContactfunction to add the new contact to the slice - Display the updated contacts using your
displayContactsfunction with the header:"Updated contact list:" - Display contact statistics:
"=== CONTACT STATISTICS ===""Total contacts: [number_of_contacts]""Contacts added this session: 1"
- 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
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and Behaviors