Introduction
Recipes often need to be adjusted depending on the number of servings required. Instead of manually recalculating the ingredients for each serving, you can automate this process by scaling the quantities up or down based on a given number of servings. In this tutorial, we will explore how to write a simple Go program that scales a recipe’s ingredients according to the desired servings.
Objective
The objective of this program is to create a function that takes a recipe’s ingredients and scales them according to the number of servings specified by the user. The program will allow you to input the original number of servings and the desired number of servings, and it will then calculate the scaled quantities for each ingredient.
Go Program Code
package main import "fmt" // Recipe struct holds the ingredient name and quantity type Recipe struct { Ingredient string Quantity float64 } func scaleRecipe(originalServings, desiredServings float64, recipe []Recipe) []Recipe { scaleFactor := desiredServings / originalServings scaledRecipe := []Recipe{} for _, item := range recipe { scaledItem := Recipe{ Ingredient: item.Ingredient, Quantity: item.Quantity * scaleFactor, } scaledRecipe = append(scaledRecipe, scaledItem) } return scaledRecipe } func main() { // Define a sample recipe with ingredients and quantities recipe := []Recipe{ {"Flour", 2.0}, // 2 cups of flour {"Sugar", 1.5}, // 1.5 cups of sugar {"Eggs", 3.0}, // 3 eggs {"Butter", 0.5}, // 0.5 cups of butter } var originalServings, desiredServings float64 fmt.Print("Enter original number of servings: ") fmt.Scan(&originalServings) fmt.Print("Enter desired number of servings: ") fmt.Scan(&desiredServings) scaledRecipe := scaleRecipe(originalServings, desiredServings, recipe) fmt.Println("\nScaled Recipe:") for _, item := range scaledRecipe { fmt.Printf("%s: %.2f\n", item.Ingredient, item.Quantity) } }
Explanation of the Program
The program consists of a few key components:
- Recipe struct: Defines the ingredient and its quantity. This helps us to store each ingredient’s name and amount in a structured way.
- scaleRecipe function: This function calculates the scaled quantities by multiplying the original quantity of each ingredient by a scale factor, which is derived from the ratio of desired servings to original servings.
- main function: This is the entry point of the program where the user inputs the original and desired servings. The program then calls the scaleRecipe function and displays the scaled recipe ingredients and quantities.
How to Run the Program
-
- Install Go programming language from here.
- Create a new file, for example,
scale_recipe.go
. - Copy and paste the above code into your file.
- Open a terminal or command prompt, navigate to the folder where your file is located, and run the following command to execute the program:
go run scale_recipe.go
- The program will ask for the original and desired number of servings. Enter the values, and it will display the scaled recipe with the adjusted ingredient quantities.