Sling Academy
Home/Golang/Deploying Go Apps to Heroku in 2 Minutes

Deploying Go Apps to Heroku in 2 Minutes

Last updated: November 27, 2024

Deploying your Go applications to Heroku can be both easy and speedy, enabling you to test and run your app in no time. This guide will take you through the quick steps to get your Go app deployed to Heroku in just two minutes.

Prerequisites

Step 1: Create a Heroku App

Open your terminal, navigate to your Go project directory, and log in to your Heroku account with the CLI:

heroku login

After authentication, create a new Heroku app:

heroku create

Step 2: Prepare Your Go Application

Add a Procfile to your project root. This file is essential for telling Heroku how to run your app. Here's how a Procfile for a Go app might look like:

web: go run main.go

Ensure your main.go file contains your running code. Check your file structure follows best practices:

.
|-- main.go
|-- Procfile
|-- go.mod
|-- go.sum

Step 3: Create the Go Modules

If your application doesn’t already utilize Go modules, initialize it by running:

go mod init <your-module-name>

Step 4: Deploy the Application to Heroku

With everything in place, use git to push your application to Heroku:

git add .
git commit -m "Initial Deployment"
git push heroku master

If your repository default branch is main instead of master, push using:

git push heroku main

Step 5: Open Your Application

Once the deploy is complete, open your app with:

heroku open

This command will launch the default browser with your app running live on Heroku.

Troubleshooting and Optimization

If you encounter deployment issues, check the Heroku logs for more detail:

heroku logs --tail

Heroku also provides several add-ons which can optimize and scale your application's performance effectively to meet production demands.

Conclusion

By following these steps, you have now deployed your Go application on Heroku in under two minutes! Heroku abstracts complex infrastructure details, allowing you to focus on building your application rather than worrying about deployment logistics.

Next Article: Deploying Go Applications to AWS Lambda

Previous Article: Environment Variables in Go: Managing Configurations

Series: Development and Debugging in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant