Use a custom Go buildpack with IBM Bluemix


IBM® Bluemix™ provides out-of-the-box support for various fit-for-purpose web development frameworks, programming languages, and runtimes. When you deploy an application to Bluemix that you've written in a supported language, the platform detects the runtime and automatically provisions it as a service to run your application.

But what if Bluemix doesn't support your preferred language and web development framework? Bluemix, like some other PaaS environments, implements a pluggable model that adds support for additional runtimes. A buildpack is collection of scripts that implements the actions needed to examine the application you're deploying, and download and configure the needed dependencies.

So, the answer is: Develop a buildpack yourself for your preferred runtime, or find a third-party or community-developed buildpack on the web. Luckily, plenty of community buildpacks are available. I'll demonstrate the easy process of using a community buildpack for the Go programming language to run a simple web application on Bluemix.

What you need to build and run the app

Run the appGet the code

Note: To fork the code for this exercise, click the EDIT CODE button in the upper right-hand corner (enter your DevOps Services credentials if you're not already logged in) and click the FORK button on the menu to create a new project. Alternatively, you can export the code by selecting the root folder (mcrudele/Goweb), then selecting File > Export from the left navigation.

Step 1. Write the code

You'll stand up a web server in Go for the sample app, which performs a couple of simple math calculations.

  1. Import the packages:
    package main
    import (
  2. Write an HTTP handler:
    func primeFactorsHandler(w http.ResponseWriter, r *http.Request) {
      // accept only POST
      if r.Method != "POST" {
      // get the form value and convert to a number
      num_s := r.FormValue("number")
      num, _ := strconv.Atoi(num_s)
      // calculate prime factors of the number
      factors, err := PrimeFactors(uint64(num))
      // Send results to the client
      ss := prettyPrintPrimeFactors(uint64(num), factors)
      fmt.Printf(w, ss)
  3. Write the web server:
    func main() {
      // set the handler to calculate prime factors of a number
      http.HandleFunc("/primefactors", primeFactorsHandler)
      log.Printf("Starting GoWeb on port %+v\n", port)
      http.ListenAndServe(":" + port, nil)
  4. Stand up the server locally. From your Go console type:
    go run goweb.go

    Now your server is listening on port 4001, and you can test the app by pointing your browser to http://localhost:4001.

Step 2. Select a Cloud Foundry buildpack for Go

Though other choices might be available, I've selected the Heroku buildpack for Go, modified by Michal Jemala to work with Cloud Foundry-based clouds. It's an experimental buildpack, but it does its job well on Bluemix.

Step 3. Create the deployment artifacts

You need to create a couple of files on the root folder of your app before deploying it in the cloud:

  • .godir — This file should contain, in its entirety, the desired name of the final Go library. For example:
  • Procfile — This file contains the command to start the web app. For example:

Step 4. Deploy the app

Deploying the app to Bluemix is pretty much the same as deploying any other app, with the exception of a command-line flag to set the custom buildpack the platform should use to provision the runtime.

Log in to your Bluemix account and run this command from the root folder of your application, where appname represents a unique name for your Bluemix-hosted app:

cf push appname -b

You'll see the output similar to mine:

Creating app goweb in org / space dev as

Using route
Binding to goweb...

Uploading goweb...
Uploading from: C:\projects\goweb\src
21.7K, 10 files

Starting app goweb in org / space dev as
-----> Downloaded app package (16K)
-----> Downloaded app buildpack cache (38M)
Initialized empty Git repository in /tmp/buildpacks/cloudfoundry-buildpack-go/.git/
-----> Using Go 1.2
-----> Installing Virtualenv... done
-----> Installing Mercurial... done
-----> Installing Bazaar... done
-----> Running 'go get -tags cf ./...'... done in 1s
-----> Uploading droplet (2.1M)

0 of 1 instances running, 1 down
0 of 1 instances running, 1 starting
1 of 1 instances running

App started

Showing health and status for app goweb in org / space dev as

requested state: started
instances: 1/1
usage: 256M x 1 instances

     state     since                    cpu    memory         disk
#0   running   2014-02-04 06:18:38 PM   0.0%   1.5M of 256M   8.7M of 1G

Step 5. Connect to your app

Point your browser to to bring up the UI and start testing the app:

Screen capture of the Go Sample App UI
Screen capture of the Go Sample App UI


In developing the sample web app, I used the standard packages that come with the Go programming language binary distributions. Now that you're sure that Bluemix will support your efforts, you might want to challenge yourself by modifying the app to use a REST-style web services framework for Go; you'll find some by surfing the web. And you could try Godev, an IDE hosted in a web interface, which is freely available on DevOps Services.


A big thanks to Michal Jemala, who made this exercise possible by building the Cloud Foundry buildpack for Go. You might want to take a look at his work.

Downloadable resources

Related topics


Sign in or register to add and subscribe to comments.

Zone=Cloud computing
ArticleTitle=Use a custom Go buildpack with IBM Bluemix