How to Deploy a Golang WebApp to Heroku?
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: muthuannamalai2002@gmail.com
Password (typing will be hidden):
Logged in as muthuannamalai2002@gmail.com
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 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.
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!
Contact Us