Search⌘ K
AI Features

Adding Business Logic to the App

Explore how to implement business logic in a Go app by organizing code into packages and modules. Learn to add functions that manage and search data, run the app using the main function, and understand how to transition from running simple scripts to developing a service-oriented application.

So far, we have some code in our project in a package called coffee. But how can we execute it? Like most languages, the execution point of entry in Go is the main function (except when we use init as the execution of entry).

Adding the main function

Let's add a main.go function.

Go (1.18.2)
package main
import (
"fmt"
"coffeeshop/coffee"
)
func main() {
fmt.Println("Printing the list of coffees available in the CoffeeShop ")
coffees, err := coffee.GetCoffees()
for _, element := range coffees.List {
result := fmt.Sprintf("%s for $%v", element.Name, element.Price)
fmt.Println(result)
}
}

Quite a few things are going on in this file. Let's go over it line by line.

  • In line 1, we declare a package main for the file.

  • In line 3, we start an import block. The first one is fmt, ...