How to deploy a golang server in heroku?

Table of contents


  1. Introduction
  2. Installation
  3. Deploy


Introduction



First of all, before getting into manner let's know a bit more what is golang and why use it.

According golang official webpage:
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 flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

By this point, are we going to work with pure golang or combine it with a framework?

In my case, I've choosen echo as my Go to framework, and godep as my package manager so those two technologies are going to be my go to explanations in this entry.



Installation


First of all, we're going to choose our go project, in my case I have it on gitlab so I'll proceed with it using the explained steps in the readme.

https://gitlab.com/sergislm4/golang-echo-plusone

First of all, we'll clone this repo to our gopath:

git clone https://gitlab.com/sergislm4/golang-echo-plusone.git

Once our project is in place, we'll move ourselves to the folder:

cd $GOPATH/src/gitlab.com/sergislm4/golang-echo-plusone

Now that we're on the folder we are where the gopkg.toml is located, let's add the needed line for heroku to work and install dependencies.

[metadata.heroku]
  root-package = "gitlab.com/sergislm4/golang-echo-plusone"
  go-version = "1.11.5"

  install = [ "./..." ]

Here we are setting up where the project is located regarding the gopath and which go version is it running on.

Then, two final things regarding our local project:


  • Ensure all dependencies:
dep ensure -v
  • Make sure server only has a specific PORT if .env is present:

 

Deploy


Now that all needed technologies are installed, it's time to start.

First, we need heroku locally instaled so that our app can be deployed.


sudo snap install heroku --classic

After that we'll execute a login that will pop-up a window on our primary browser where we'll login in heroku's official page.

heroku login

Once logged, make sure that all files will be uploaded using git tool:


git add -A .

git commit -m "Local files to heroku master"

And finally, push those files:

git push heroku master

Your app will be visible in your heroku control panel:



And if you open it up you'll see is already running.



This tutorial ends up here, but, if your app needs an appropiate database, make sure to use heroku add-ons to provide one and then set it up in your app db connection file.



What do you think? Will you try it?

Thanks for reading the entry!

Comentarios