Go Value Receiver: Implementing a Flexible Data Processor
This challenge focuses on building a robust and flexible value receiver in Go. You will create a function that can accept various data types and process them based on their underlying structure, demonstrating effective use of Go's type system and interfaces. This skill is crucial for building adaptable APIs and handling diverse data inputs.
Problem Description
Your task is to implement a function ProcessValue that accepts a single argument of type interface{}. This function should be able to handle and process specific data types: int, string, and a custom struct type named Message.
The ProcessValue function should exhibit the following behavior:
- Handle
int: If the input is anint, print its value along with a descriptive label. - Handle
string: If the input is astring, print its value along with a descriptive label. - Handle
Messagestruct: If the input is aMessagestruct, print itsContentfield along with a descriptive label. - Handle unknown types: If the input is of any other type, print a message indicating that the type is not supported.
You will also need to define the Message struct.
Key Requirements:
- The
ProcessValuefunction must acceptinterface{}. - Use Go's type assertion or type switch to determine the concrete type of the received value.
- Print output to standard output (e.g., using
fmt.Println).
Expected Behavior:
The function should gracefully handle all specified types and report unsupported types. The output format should be clear and indicate the type of data being processed.
Edge Cases:
- Consider what happens when
nilis passed as input. (While not explicitly required forint,string, orMessage, a robust receiver might handlenil.) For this challenge, assumenilfalls under "unknown types."
Examples
Example 1:
Input: 42 (type int)
Output: Received an integer: 42
Example 2:
Input: "Hello, Go!" (type string)
Output: Received a string: Hello, Go!
Example 3:
Input: Message{Content: "Important data"} (type Message struct)
Output: Received a Message struct with content: Important data
Example 4:
Input: 3.14 (type float64)
Output: Unsupported type: float64
Constraints
- The
ProcessValuefunction must be implemented as a standalone function. - The
Messagestruct must have a field namedContentof typestring. - Standard Go libraries (e.g.,
fmt) are permitted. - Performance is not a primary concern for this specific challenge, but the solution should be efficient for its purpose.
Notes
This challenge is an excellent opportunity to practice Go's dynamic typing features within a statically typed language. Think about the most idiomatic way to handle multiple distinct types within a single function. A type switch is often a very clear and readable solution for this scenario.