Skip to main content

Heroku Deployment Example

Github StackBlitz

This project demonstrates how to deploy a Go application using the Fiber framework on Heroku.

Note: Heroku removed its free tier in November 2022. A paid plan is required to deploy applications.

Prerequisites

Ensure you have the following installed:

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/heroku
  2. Install dependencies:

    go get
  3. Log in to Heroku:

    heroku login
  4. Create a new Heroku application:

    heroku create
  5. Build the binary and add a Procfile to the project directory:

    go build -o bin/main .

    Procfile:

    web: bin/main
  6. Deploy the application to Heroku:

    git add .
    git commit -m "Deploy to Heroku"
    git push heroku master

Running the Application

  1. Open the application in your browser:
    heroku open

Example

Here is an example main.go file for the Fiber application:

package main

import (
"log"
"os"

"github.com/gofiber/fiber/v3"
)

func main() {
app := fiber.New()

app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, Heroku!")
})

log.Fatal(app.Listen(":" + getPort()))
}

func getPort() string {
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
return port
}

References