

High-order functions allow you to abstract recurring operations, making your code scalable as you incorporate additional properties or shapes.
Basic Circle Property Functions
Begin by defining basic functions to calculate the area, perimeter, and diameter of a circle. Each function receives the circle’s radius as a float64 and returns a float64 value.Handling User Input with Conditional Logic
A common approach is to use conditional statements to decide which calculation to perform based on user input. The following code demonstrates this method:Simplifying with High-Order Functions
The following sections demonstrate how to refactor the code using high-order functions. We introduce two helper functions: one to display the result and another to map the query number to the appropriate calculation function.Defining the printResult Function
The printResult function accepts the radius and a calculation function as parameters. It executes the calculation, stores the result, and displays it.Defining the getFunction Function
The getFunction function maps a user’s query (an integer) to the corresponding calculation function using a predefined map.Refactored Main Function Using High-Order Functions
By integrating these high-order functions, the main function becomes succinct. It reads user input, selects the appropriate calculation function via getFunction, and passes it to printResult.High-order functions reduce code redundancy while clarifying the logical separation between input handling and processing logic—making it easier to extend and maintain your application.