Golang

 

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

    1. Install Go programming language from here.
    2. Create a new file, for example, scale_recipe.go.
    3. Copy and paste the above code into your file.
    4. 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
  1. 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.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)