Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
How to Deploy a Golang WebApp to Heroku?
Next article icon

How to Deploy a Golang WebApp to Heroku?

Last Updated : 07 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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: [email protected]

Password (typing will be hidden):

Logged in as [email protected]

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 [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.

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!


Next Article
How to Deploy a Golang WebApp to Heroku?

M

muthuannamalai2002
Improve
Article Tags :
  • Go Language
  • How To
  • Installation Guide
  • Heroku Cloud

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 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences