Skip to content

Module 3: Functions & Packages (The Recipe Book)

📚 Module 3: Functions & Packages

Course ID: GO-103
Subject: The Recipe Book

In Go, we don’t like complex “Classes” or “Inheritance.” We like simple Functions and Packages.


🏗️ Step 1: Functions (The “Kitchen Task”)

A Function is just a specific job.

🧩 The Analogy: The Coffee Maker

  1. Input: You give it Coffee Beans and Water.
  2. Job: It grinds, brews, and pours.
  3. Output: It gives you a hot cup of Coffee.

In Go:

func makeCoffee(beans string, water int) string {
    return "Hot Cup of Coffee"
}

🏗️ Step 2: Multiple Returns (The “Check and Return”)

In most languages, a function can only give back one thing. In Go, we usually return Two Things: the Result and an Error.

🧩 The Analogy: The Pizza Delivery

  • “Here is your Pizza (Result).”
  • “But wait, I dropped one on the way (Error).”

Why do we do this? Because Go wants you to handle mistakes immediately instead of waiting for the app to crash.


🏗️ Step 3: Packages (The “Toolbox”)

A Package is just a folder of related recipes.

  • fmt: Tools for printing.
  • net/http: Tools for the internet.
  • math: Tools for calculations.

🧩 The Analogy: The Filing Cabinet

Instead of dumping all your papers on the floor, you put them in Folders (Packages) based on what they do.


🥅 Module 3 Review

  1. Function: A reusable block of code for a specific task.
  2. Multiple Returns: Getting both a result and a status report.
  3. Package: A way to group related functions into a single folder.

:::tip Senior Tip In Go, if a function name starts with a Capital Letter (e.g., MakeCoffee), it is Public (others can use it). If it’s lowercase (makeCoffee), it is Private. Simple! :::