Menu
Coddy logo textTech

Recap - Contact Book

Part of the Object Oriented Programming section of Coddy's GO journey — lesson 18 of 107.

challenge icon

Challenge

Easy

Let's build a contact book system that brings together custom types, nested structs, struct tags, and zero values—all the concepts you've mastered in this chapter.

You'll organize your code across two files:

  • contact.go: Define your contact book data model with meaningful custom types and proper JSON formatting:
    • Create custom types Phone and Email based on string to make your code self-documenting
    • Define an Address struct with Street, City, and Country fields, each with appropriate JSON tags
    • Define a Contact struct containing Name (string), Phone (your Phone type), Email (your Email type), and Address (nested struct). Use JSON tags where phone and email should be omitted if empty
  • main.go: Read contact information from input, create a Contact instance, and output it as formatted JSON. Some contacts may have incomplete information—let zero values and omitempty handle those gracefully.

The following inputs will be provided:

  • Line 1: Contact name
  • Line 2: Phone number (may be empty)
  • Line 3: Email address (may be empty)
  • Line 4: Street address
  • Line 5: City
  • Line 6: Country

Use json.MarshalIndent with two spaces for indentation. Print the resulting JSON string.

For example, if given a contact with name Alice Smith, no phone, email alice@example.com, street 123 Main St, city Boston, and country USA, your output should look like:

{
  "name": "Alice Smith",
  "email": "alice@example.com",
  "address": {
    "street": "123 Main St",
    "city": "Boston",
    "country": "USA"
  }
}

Notice how the phone field doesn't appear at all since it was empty—that's your omitempty tag at work!

Try it yourself

package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	// Read name
	scanner.Scan()
	name := scanner.Text()

	// Read phone (may be empty)
	scanner.Scan()
	phone := scanner.Text()

	// Read email (may be empty)
	scanner.Scan()
	email := scanner.Text()

	// Read street
	scanner.Scan()
	street := scanner.Text()

	// Read city
	scanner.Scan()
	city := scanner.Text()

	// Read country
	scanner.Scan()
	country := scanner.Text()

	// TODO: Create a Contact instance using the input values
	// Use your custom types Phone and Email for the respective fields
	// Create the nested Address struct with street, city, and country

	// TODO: Use json.MarshalIndent to convert the contact to JSON
	// Use "" as prefix and "  " (two spaces) as indent

	// TODO: Print the JSON string
	_ = name
	_ = phone
	_ = email
	_ = street
	_ = city
	_ = country
}

All lessons in Object Oriented Programming