How to Deploy a Go Web App on Heroku

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setup
  4. Creating a Go Web App
  5. Deploying to Heroku
  6. Conclusion

Introduction

In this tutorial, we will learn how to deploy a Go web application on the Heroku platform. By the end of this tutorial, you will have a functioning Go web app deployed and accessible online. We will cover the necessary setup, creating a simple Go web app, and deploying it to Heroku.

Prerequisites

Before starting this tutorial, you should have the following:

  • Basic knowledge of Go programming language
  • Go installed on your machine
  • An account on Heroku (https://www.heroku.com/)
  • Heroku CLI (https://devcenter.heroku.com/categories/command-line)

Setup

  1. Install Go: Visit the official Go website (https://golang.org/) and download the appropriate installer for your operating system. Follow the installation instructions to install Go on your machine. Verify the installation by running go version in your terminal.
  2. Sign up on Heroku: Go to the Heroku website (https://www.heroku.com/) and create an account if you don’t have one already.
  3. Install Heroku CLI: Download and install the Heroku CLI from the official website (https://devcenter.heroku.com/categories/command-line). Follow the installation instructions for your operating system.

  4. Verify Heroku CLI installation: Open a new terminal window and run heroku version to verify that the Heroku CLI was installed successfully.

Creating a Go Web App

  1. Create a new directory for your Go web app: Open a terminal and navigate to the desired location for your project. Run mkdir go-web-app to create a new directory.
  2. Initialize Go module: Go to the project directory by running cd go-web-app. Initialize the Go module by running go mod init github.com/YOUR_GITHUB_USERNAME/go-web-app. Replace YOUR_GITHUB_USERNAME with your actual GitHub username or choose a different module name.
  3. Create a main.go file: Run touch main.go to create a new main.go file in the project directory. Open the file in a code editor.

  4. Write a simple web app: In the main.go file, write the following code to create a basic Go web app:

     package main
        
     import (
       "fmt"
       "net/http"
     )
        
     func helloHandler(w http.ResponseWriter, r *http.Request) {
       fmt.Fprintf(w, "Hello, Go Web App on Heroku!")
     }
        
     func main() {
       http.HandleFunc("/", helloHandler)
       http.ListenAndServe(":8080", nil)
     }
    

    The code defines a helloHandler function that writes “Hello, Go Web App on Heroku!” as the response. In the main function, we set up a HTTP route for the root path (“/”) and start the HTTP server on port 8080.

  5. Test locally: To test the web app locally, run go run main.go in the terminal. Open a web browser and visit http://localhost:8080 to see the “Hello, Go Web App on Heroku!” message.

Deploying to Heroku

  1. Login to Heroku CLI: In your terminal, run heroku login and follow the prompts to log in to your Heroku account.
  2. Create a Heroku app: Run heroku create to create a new Heroku app. This will automatically set up a remote repository for your app.
  3. Deploy the app: Run git push heroku master to deploy your app to Heroku. Heroku will automatically build the app and start running it.

  4. View the app: After successfully deploying, run heroku open to open the app in your default browser. Alternatively, you can visit the app’s URL by running heroku apps:info -s | grep web_url | cut -d= -f2 in the terminal.

    Congratulations! You have successfully deployed a Go web app on Heroku.

Conclusion

In this tutorial, you learned how to deploy a Go web application on Heroku. We covered the necessary setup, creating a simple Go web app, and deploying it to Heroku. Now you can continue building and expanding your Go web app with the knowledge of deploying it to a production environment.

Remember to check the Heroku documentation and Go web development best practices for further guidance on improving your app and scaling it as needed.

Happy coding!