DEV Community

Huseyn
Huseyn

Posted on

Understanding Go Modules, Packages and main Package

Let's break this down in a very simple, real-world way. We'll use a **bakery **as a metaphor to explain Go modules, packages, imports, and the main package.

๐Ÿž Real-World Analogy: A Bakery Shop

๐Ÿ”น Imagine:

  • You're running a bakery shop.
  • Your shop has multiple departments: Bread, Cakes, Billing, etc.
  • Each department has its own set of tools and recipes (code).
  • You also sell your baked goods to customers (this is your main service).

๐Ÿ“ฆ 1. What is a Go package?

A package is like a department in your bakery:

  • bread โ†’ All code to make bread.
  • cake โ†’ All code to make cakes.
  • billing โ†’ All code to handle payments.

Each department (package) focuses on a specific job and has its own folder.

// cake/cake.go
package cake

func MakeCake() string {
    return "Chocolate Cake"
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ 2. What is a Go module?

A module is your entire bakery business.

Itโ€™s a** collection of all departments (packages)** under one roof.

  • 1. A Go module is created with go mod init and has a go.mod file.
  • 2. It tells Go: โ€œThis is where my business (code) starts and who I depend on.โ€
go mod init github.com/mybakery/shop

Enter fullscreen mode Exit fullscreen mode

This means your bakery's official name is: github.com/mybakery/shop.

๐Ÿ”— 3. What is an import?

An import is when one department wants to use tools from another.

// main.go
package main

import (
    "fmt"
    "github.com/mybakery/shop/cake"
)

func main() {
    fmt.Println("We made:", cake.MakeCake())
}

Enter fullscreen mode Exit fullscreen mode
  • Here, the main shop is using the cake department's recipe to make a cake.
  • You use import to pull in the code you need from other packages (your own or third-party).

๐Ÿง  4. What is the main package, and why is it mandatory?

The main package is the front counter of your bakery.

  • This is where customers walk in and interact with your shop.
  • In Go, the program starts execution from the main package, specifically the main() function.
// main.go
package main

func main() {
    // Entry point: This is where Go starts running your program
}

Enter fullscreen mode Exit fullscreen mode

If there's no main package, it's like a bakery with no front counter. It has great recipes, but nobody knows how to start using them!

โœ… Summary

Summary image

๐Ÿ’กReal Example
Youโ€™re building a tool that:

  • Has maths functions (addition, subtraction).
  • Shows results from the main program.

File: go.mod

module github.com/mytools/calc

Enter fullscreen mode Exit fullscreen mode

File: maths/add.go

package maths

func Add(a, b int) int {
    return a + b
}

Enter fullscreen mode Exit fullscreen mode

File: main.go

package main

import (
    "fmt"
    "github.com/mytools/calc/maths"
)

func main() {
    sum := maths.Add(3, 4)
    fmt.Println("Sum is:", sum)
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)