Hone logo
Hone
Problems

Mastering Go's Switch Statement: Implementing the Default Case

In Go programming, the switch statement is a powerful control flow mechanism for selecting one of many code blocks to execute. Often, you'll want to handle situations where none of the explicit cases match. This is where the default case becomes indispensable, providing a fallback mechanism to ensure your program behaves predictably. This challenge will help you solidify your understanding and practical application of the default case in Go's switch statements.

Problem Description

Your task is to implement a function that processes different types of shapes based on a string identifier. The function should use a switch statement to determine the shape type and perform a specific action for each recognized shape. Crucially, you must also implement a default case to handle any shape identifiers that are not explicitly defined, providing a clear indication that an unknown shape was encountered.

Requirements:

  • Create a Go function named processShape that accepts a single string argument, shapeType.
  • Inside processShape, use a switch statement on shapeType.
  • Implement cases for the following shape types:
    • "circle"
    • "square"
    • "rectangle"
  • For each recognized shape, the function should print a descriptive message (e.g., "Drawing a circle.", "Calculating the area of a square.").
  • Implement a default case that prints a message indicating an unknown shape type (e.g., "Unknown shape type: [shapeType]").
  • The function should not return any value.

Expected Behavior:

  • If shapeType is "circle", "square", or "rectangle", a specific message corresponding to that shape should be printed.
  • If shapeType is anything else, the default case should be executed, printing the "Unknown shape type" message along with the provided shapeType.

Examples

Example 1:

Input: shapeType = "circle"
Output: Drawing a circle.

Explanation: The switch statement matches "circle" and executes the corresponding case.

Example 2:

Input: shapeType = "triangle"
Output: Unknown shape type: triangle

Explanation: The switch statement does not find a match for "triangle" and falls through to the default case.

Example 3:

Input: shapeType = "SQUARE" (Note: case sensitive)
Output: Unknown shape type: SQUARE

Explanation: The input string "SQUARE" is not an exact match for the "square" case, so the default case is triggered.

Constraints

  • The shapeType input will always be a string.
  • The input string can be of any length.
  • There are no performance constraints beyond typical function execution.

Notes

  • Remember that Go's switch statements do not fall through by default; you do not need to add break statements explicitly.
  • Consider how case sensitivity might affect the matching of your switch cases.
  • This exercise is designed to practice fundamental Go control flow. Pay close attention to the exact string literals used in your cases.
Loading editor...
go