This is a simple example of a PostgreSQL-backed Go API application running on Heroku
heroku-go-db-example -- Directory / repository name
├── .env.dev -- Environment variables for local development
├── Makefile -- Targets for local development (optional)
├── Procfile -- Defines a single `web` process
├── README.md -- This file
├── bin
│ └── heroku-go-db-example -- The locally-built binary (gitignored)
├── main.go -- The main Go application
└── vendor
├── github.com
│ └── lib
│ └── pq -- Vendored dependency [1]
└── vendor.json -- Buildpack identification / Heroku configuration [2]
When the application is deployed, the Heroku Go buildpack will run go install . to build and install your application to /app/bin. We want to mimic this in local development, so the run target in the Makefile will:
- Copy
.env.devto.envto make environment variables available to the application - Create the local database
- Compile
main.gotobin/heroku-go-db-example-- this is important because Go will use the pacakge name (heroku-go-db-example) when creating the binary. - Prepend the
./bindirectory to the$PATHand runheroku local. This allows thewebprocess in theProcfileto find theheroku-go-db-examplebinary.
This is all done for you by running make since run is the default target.
$ heroku apps:create heroku-go-db-example
$ heroku addons:create heroku-postgresql
$ git push heroku master
GET: curl -s https://heroku-go-db-example.herokuapp.com | jq .
POST: curl -s -d '{"username":"reagent"}' https://heroku-go-db-example.herokuapp.com | jq .
404: curl -i -X OPTIONS https://heroku-go-db-example.herokuapp.com
404: curl -i https://heroku-go-db-example.herokuapp.com/unknown