How to deploy a Go app on anynines and Cloud Foundry

Thanks to Cloud Foundry’s support of buildpacks it’s very easy to deploy mostly any app of any language to anynines.

This of course includes the Go language.

Derived from Heroku’s Go buildpack Michael Jemala was so nice to create a slightly modified Cloud Foundry buildpack for Go. As all Cloud Foundry buildpacks can also be used on anynines it’s just a matter of minutes to get a Go app running.

In case the application starts a web server the server shouldn’t bind to a particular host and must read the port from the environment variable provided by the executing application server (called DEA in Cloud Foundry terms).

This may look like this (full example):
func main() {
  port := os.Getenv("PORT")
  fmt.Println("PORT:", port)

  var h Hello
  http.ListenAndServe(":" + port, h)
}

In addition to this your project should contain two files.

.godir – This file contains the desired name of your executable, “server” in our example.

Procfile – This file contains the name of the executable to be started. “web: server” in our case.

The push command then looks like this:
cf push --name goexample --buildpack https://github.com/michaljemala/cloudfoundry-buildpack-go.git
Have fun!

Find a working example of the anynines go example app on github.

3 thoughts on “How to deploy a Go app on anynines and Cloud Foundry

  1. Might be good to do a follow up article that shows using non-std libraries, such as Martini for the web framework – what dependency/internal $GOPATH options are supported?

Leave a Reply

Your email address will not be published. Required fields are marked *