How to deploy Go with Traffic on Heroku

It’s really easy to deploy a Go application based on the Traffic web framework on Heroku. Let’s start with a simple example.

First of all, we need a simple Traffic app:

mkdir hello
cd hello

Create the main.go file with the following content:

package main

import (

func hello(w traffic.ResponseWriter, r *traffic.Request) {
  w.WriteText("Hello World")

func main() {
  r := traffic.New()
  r.Get("/", hello)

Now you should be able to run the example with:

go run main.go

Check http://localhost:7000.

Now we need to create a new Heroku app based on the Go Heroku Buildpack. You can red more about that on this tutorial, but it’s very easy, we just need a couple of files and commands.

Create the .godir file with the app name inside:

echo 'hello' > .godir

Now we need a file called Procfile with some configuration for Traffic:


We set 3 environment variables before calling our executable.


By default Traffic runs on port 7000. You can change the port in your code using traffic.SetPort(3000) or just using the TRAFFIC_PORT variable.


By default Traffic runs in development mode. This means that it adds some useful middleware for development like the ShowErrorsMiddleware, but you don’t want to add them in production. To run Traffic in production mode, just set TRAFFIC_ENV=production. If you need to serve static files like css or images, you need to use the StaticMiddleware, which is automatically loaded in development mode but not in production.

To enable it in production, add the following code:

if traffic.Env() == "production" {


By default Traffic starts on, and it’s visible only from your local machine. To be able to see your application in production you need to bind it to

Now we just need to initialize our git repository, create the heroku app, and deploy it:

git init
git add .
git commit -am "initial commit"

heroku create -b

git push heroku master

Check if it works:

heroku open