Skip to content
Related Articles

Related Articles

How to Deploy a Golang WebApp to Heroku?

Improve Article
Save Article
  • Last Updated : 07 Apr, 2022
Improve Article
Save Article

Go, also known as “Golang,” is gaining popularity among DevOps professionals in recent years. There are many tools written in Go, including Docker and Kubernetes, but they can also be used for web applications and APIs. While Golang can perform as fast as a compiled language, coding in it feels more like interpreting with standard tools such as a compiler, runner, etc., you will get the full experience right out of the box. 
When you combine that with the robust and easy-to-understand concurrency framework designed to take full advantage of today’s multi-core or multi-CPU execution systems, it’s clear why the Go community has grown so rapidly. A deceptively simple design makes Go easy to learn and easy to master, but it isn’t limited in capabilities because it was created with specific goals in mind. In this article, we learn about how to make a simple web application in go and deploy it to Heroku without any hassle.

Deploy a Golang App to Heroku

Step 1: Sign up for a Heroku account. If you don’t have an account on Heroku, create one now. Up to 5 apps can be hosted for free without registering a credit card.

Sign-up

 

Step 2: Download and install Heroku’s Command Line Interface (CLI). With Heroku’s Command Line Interface (CLI), you can access Heroku’s services. The Heroku and git commands are available from your command window as soon as it is installed.

Heroku-CLI

 

Step 3: Access Heroku by logging in. The first thing we need to do is create a directory for the project, and then initialize it as a Go module with the help of the go mod init command. Use the email address and password that you used when you created your Heroku account to log in:

$ heroku login

Enter your Heroku credentials.

Email: muthuannamalai2002@gmail.com

Password (typing will be hidden):

Logged in as muthuannamalai2002@gmail.com

Heroku and Git require authentication in order to function.

Heroku-Login

 

Step 4: Create A Project Directory. The first thing we need to do is create a directory for the project, and then initialize it as a Go module using the go mod init command.

mkdir app

cd app

go mod init muthuannamalai12/app

It is advised that you should use your GitHub account username so your project names are unique, and your project dependencies don’t conflict with each other. It is not a necessary condition you can also use whatever name you wish.

Create-a-directory-for-the--project

 

Now, you should see a file named go.mod in your directory. This is the file Go uses to keep track of its project dependencies. It should look something like this when you look at the contents of the file:

module muthuannamalai12/app

go 1.14

Step 5: Get acquainted with Git. Open the command shell by starting the Command Prompt in Windows. You must now identify yourself to Git in order to make sure it can label your commits properly in the future. I am using muthuannamalai12 and muthuannamalai2002@gmail.com below:

$ git config –global user.name “muthuannamalai12”

$ git config –global user.email muthuannamalai2002@gmail.com

Change the user name and email address to your own.

Get-acquainted-with-Git

 

Step 6: Develop a web app. In this step, we prepare a simple Go application that can be deployed. In the folder app write the program app.go as follows:

Go




package main
import (
   "github.com/gin-gonic/contrib/static"
   "github.com/gin-gonic/gin"
)
func main() {
   r := gin.Default()
   r.GET("/hello", func(c *gin.Context) {
       c.String(200, "Hello")
   })
   api := r.Group("/api")
   api.GET("/ping", func(c *gin.Context) {
       c.JSON(200, gin.H{
           "message": "ping",
       })
   })
   r.Use(static.Serve("/", static.LocalFile("./views", true)))
   r.Run()
}


 

Step 7: Our Heroku app is deployed using Git. To deploy your app to Heroku, you’ll need to store it in Git. In the same folder i.e. $GOPATH/src/github.com/muthuannamalai12/app type:  

$ git init

$ git add -A .

$ git commit -m “code”

Heroku-app-is-deployed-using-Git

 

Step 8: Set up a Procfile. Specify the command to start our application by creating a Procfile in the root directory of our app. The Procfile looks like this:

web: app

In this, a single process type, web, is declared, along with the command needed to initiate it. The name web is crucial. This process type will be able to process web traffic since it is attached to Heroku’s HTTP routing stack.

Set-up-a-Procfile

 

Step 9: Download and install Godep. Heroku recommends using godep for managing Go package dependencies, which makes it easier to build applications reproducibly by fixing dependencies. Now let’s install Godep:

$ go get github.com/tools/godep

Download-and-install- Godep

 

Step 10: Declare dependencies for applications. Go apps are recognized by Heroku by the existence of a Godeps.json file in the Godeps directory located in the root directory of your application. The Godeps/Godeps.json file is used to declare what dependencies your application includes along with the Go version to use for compilation. Whenever an app is deployed, Heroku reads this file, installs the correct Go version, and compiles the code with godep go install ./…

Using godep in our project. In the folder app created type the following command

$ godep save -r

The dependencies will be saved to the file Godeps/Godeps.json.

Step 11: Add the new files to git

$ git add -A .

$ git commit -m “dependencies”

This is now ready for Heroku deployment.

Step 12: Create the app and deploy it. This step involves creating and deploying the app to Heroku. The creation of a Heroku app prepares Heroku to accept your source code.

$ heroku create

Creating app… done, immense-reaches-34382

https://immense-reaches-34382.herokuapp.com/ | https://git.heroku.com/immense-reaches-34382.git

A remote git repository (called heroku) is also created when you create an app. In this example, Heroku is generating the name immense-reaches-34382 for your app, or you can specify your own name as a parameter.

Create-the-app-and-deploy-it

 

When you’re ready, deploy your code using git push heroku master. Your application now has been deployed. To access it, go to the URL generated by the app name. The following shortcut opens the site:

$ heroku open

That’s it! Your Go app is running on Heroku now!


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!