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.
- Heroku Account
- Heroku CLI
- 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
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
This will open your web browser to complete the login flow.
Step 2.
Create Procfile by running
$ touch Procfile
or just create it from directory.
Run echo “web: [directory]” > Procfile
replace [directory] e.g
$ echo “web: currency” > Procfile
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
Step 4.
Set variable names in .env
file e.g. PORT=8080
using
$ heroku config:set PORT=8080
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"
Step 6.
Create a Heroku application using heroku create [project_name]
e.g.
$ heroku create go-currency
$ git push heroku master
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 Kehinde A. Olawuwo. Thank you.