How to Deploy a Golang WebApp to Heroku?
Last Updated :
07 Apr, 2022
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.
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.
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: [email protected]
Password (typing will be hidden):
Logged in as [email protected]
Heroku and Git require authentication in order to function.
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.
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 [email protected] below:
$ git config –global user.name “muthuannamalai12”
$ git config –global user.email [email protected]
Change the user name and email address to your own.
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”
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.
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
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.
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!
Similar Reads
How to deploy React app to Heroku?
React is a very popular and widely used library for building User Interfaces. So if you are thinking about deploying your React app to the cloud platform, there are various choices for doing that such as AWS EC2 or Heroku. But for testing your React app, Heroku will be the best option as it is free
3 min read
How to Deploy Django application on Heroku ?
Django is an MVT web framework used to build web applications. It is robust, simple, and helps web developers to write clean, efficient, and powerful code. In this article, we will learn how to deploy a Django project on Heroku in simple steps. For this, a Django project should be ready, visit the f
4 min read
How to Deploy MongoDB App to Heroku?
Pre-requisite:- MongoDB MongoDB is an open-source document-oriented database designed to store a large scale of data and allows you to work with that data very efficiently. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval of data in the MongoDB are not in t
7 min read
How To Deploy a Django Application to Heroku with Git CLI?
Deploying a Django application to Heroku using the Git command-line interface (CLI) is a simple process. Heroku provides a platform-as-a-service (PaaS) that enables you to deploy, manage, and scale your applications easily. This guide will walk you through the steps to deploy your Django application
3 min read
How to Deploy a Basic Static HTML Website to Heroku?
Heroku is a simple and one-stop solution to host any website or server. This article revolves around how you can host your own Static HTML webpage on Heroku. To demonstrate this we are going to build a simple webpage and host it. PrerequisitesGitHeroku AccountHeroku CLI Let's create a directory name
3 min read
How to deploy Node.js app on Heroku from GitHub ?
In this article, we will be looking at how to deploy your Demo Node.js app to Heroku. At the end of this article, we will have a basic Hello World app running on a public domain that can be accessed by anyone. The Node must be installed on your machine. Refer to this article How to install Node on y
3 min read
How to Install Heroku and Deploy a Static Website on Ubuntu?
Heroku is a cloud platform that provides a platform as a service(PAAS) to deploy either static or dynamic websites. Heroku is managed by Salesforce. Heroku allows CI/CD, code rollbacks, automatic deploys, Github integration, app metrics, and much more. Heroku allows building, running, and deploying
3 min read
Deploying a Django App to Heroku Using Github Repository
Heroku is a free hosting cloud service provider. We can use our free dynos to deploy our applications on the cloud. The only disadvantage is that it loses all the data once the app sleeps and it cannot handle multiple requests at a time when hosted on free dynos. First of all, to proceed further you
2 min read
How to Install Golang on CentOS 7?
The Go programming language is an open-source project to make programmers more productive. Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexibl
2 min read
How to Deploy a Go Web Application with Docker?
Pre-requisite: Docker A popular containerization tool called Docker enables developers to quickly package programs and their environments, enabling shorter iteration times and greater resource efficiency while delivering the same intended environment each time. A container orchestration tool called
3 min read