DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for How to Deploy A Golang App to Heroku.
Kehinde Olawuwo.
Kehinde Olawuwo.

Posted on

How to Deploy A Golang App to Heroku.

This is a step by step tutorial on how to deploy a Golang app on Heroku.

Requirements.

  1. Heroku Account
  2. Heroku CLI
  3. Golang v1.14 and above

Step 1.

Create a Heroku account using your email and password or log in if you already have an account.

Download the Heroku CLI by running the command below on Mac OS

$ brew tap heroku/brew && brew install heroku
Enter fullscreen mode Exit fullscreen mode

Or go to https://devcenter.heroku.com/articles/heroku-cli#download-and-install to install on Windows.
Login to Heroku from your terminal using the command

$ heroku login
Enter fullscreen mode Exit fullscreen mode

This will open your web browser to complete the login flow.

Step 2.

Create Procfile by running

$ touch Procfile
Enter fullscreen mode Exit fullscreen mode

or just create it from the directory. Run echo "web: [directory]" > Procfile replace [directory] e.g

$ echo "web: currency" > Procfile
Enter fullscreen mode Exit fullscreen mode

Step 3.

Generate a go.mod file using go mod init github.com/[user_name]/[project_name] e.g.

$ go mod init github.com/heavykenny/currency
Enter fullscreen mode Exit fullscreen mode

Step 4.

Set variable names inΒ .env file e.g. PORT=8080 using

$ heroku config:set PORT=8080
Enter fullscreen mode Exit fullscreen mode

You can also set other variables using this command.

Step 5.

Commit your code using the following commands

$ git init
$ git add . && git commit -m "Deploying to Heroku"
Enter fullscreen mode Exit fullscreen mode

Step 6.

Create a Heroku application using heroku create [project_name] e.g.

$ heroku create go-currency
$ git push heroku master
Enter fullscreen mode Exit fullscreen mode

You can then proceed to test your app using https://[project_name].herokuapp.com i.e.
https://go-currency.herokuapp.com.

If you encounter any error during any of these processes, do not hesitate to comment or reach out. Thank you.

Top comments (4)

Collapse
 
dev117uday profile image
Uday Yadav

thank you, this is the best and easiest tutorial i could find

Collapse
 
musale profile image
Musale Martin

I always find it way easier deploying go applications in Heroku. It's intuitive.

Collapse
 
heavykenny profile image
Kehinde Olawuwo. Author

Yeah, it is easy and straightforward.

Collapse
 
animesh9893 profile image
Animesh Shrivatri

Thank You for easy steps.

🌚 Life is too short to browse without dark mode