Skip to content

Instantly share code, notes, and snippets.

@TeemuKoivisto
Last active May 29, 2017 10:35
Show Gist options
  • Save TeemuKoivisto/dc4ea96bd3e56844ba980fcd07d018fa to your computer and use it in GitHub Desktop.
Save TeemuKoivisto/dc4ea96bd3e56844ba980fcd07d018fa to your computer and use it in GitHub Desktop.
Guide to starting up a Go project

How to start a Go project on Mac OS and deploy it to Heroku

What you need:

  1. Travis account (just log in with your GitHub credentials to create one)
  2. Heroku account
  3. Heroku CLI (brew install heroku)
  4. Travis CLI (gem install travis)

Let's start:

  1. You should install Go, here's their website.
  2. Set your GOPATH and PATH to your .bash_profile etc. with:
export GOPATH=/Users/YOUR_USER_NAME/go
export PATH=$GOPATH/bin:$PATH

Where you should replace YOUR_USER_NAME with your name.

  1. Create a repository on GitHub.
  2. Go to this path and create it if it doesn't exist: cd $GOPATH/src/github.com/GITHUB_NAME where GITHUB_NAME is your GitHub account name and then clone the repository. This is because Go is kinda retarded how it wants you to store your projects. You should probably favourite the folder on finder by dragging the folder to the favourites.
  3. Open the folder in your editor and create file main.go
  4. Add this code to it:
package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

func determineListenAddress() (string, error) {
	port := os.Getenv("PORT")
	if port == "" {
		return "", fmt.Errorf("$PORT not set")
	}
	return ":" + port, nil
}

func serveRest(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello world!")
}

func main() {
	addr, err := determineListenAddress()
	if err != nil {
		log.Fatal(err)
	}
	http.HandleFunc("/", serveRest)

	log.Printf("Listening on %s...\n", addr)
	if err := http.ListenAndServe(addr, nil); err != nil {
		panic(err)
	}
}
  1. Install godep with go get github.com/tools/godep.
  2. Init the folder with godep save.
  3. Login to Heroku with heroku login and then initialize the Heroku app with heroku create YOUR_APP_NAME.
  4. Initialize Travis with travis init && travis setup heroku. You can use this as a guide:
language: go
go:
- '1.8'
deploy:
  provider: heroku
  api_key:
    secure: mEtwIW2YPJCXIByA6BTZX3DR6N4wBBhPSxSRVXvZiSaD/VWBN3o2tyEL3nT9xtkBcybqYIM14F4GY6l0N8Vw6L8jE0T3nOtbo9uwWe4gTiQGY/XBiXe/xzEZdUNH/ri2/yUKbsSmQeiJU50rNL1nNLaeKNFA2rLH9TrL2mUeJvVp+KFYSVZdACEVXEmNzw3KaTD1L9PQ7X01UMiK+2Ci7dlIuXBb/pLS3ZaaWw+UW65M5c5zJa9dA/0jg/7B9wHZHNFcseJYV2OoVgQOq+E30YUKaCgINs7mKQkfKa/oRUNZTkXmpMnDDL4p1rCZE9A2mBgGCaAF8pjHIjkcukgacQ5G5NF4zw3J/kTMkiYf/Pf28zIzGg0ckv55vnpVjK4Dt1+77cHxyApMEg6R0F03CoXM14E4gaIKSWUQ69d/6S87BQOD9+xsfsatFY/YMU5tpFxzCKF7ED53nvkL+2cjshrDRlbjEGG43JEO/WfvRce3AdV3aKSoFqS2f6VYo+0tD8j9DuD65k7pEj2rblCKA8YvAysPaKXCbgEmY36BHSZurY4lZZMcpuq0S5wBRjdYww1Y4LMJH+JSb+wvYGRjpnpDDamHJlBSdBthfkXEBu9YLVUrAWTmUbKtghJVegyWyaIStiIsQC7c15yMEZc7s5KYyH9jcEyRLHW24Bw4AlI=
  app: teemun-go-app
  on:
    repo: TeemuKoivisto/go-test-project
notifications:
  email:
    on_success: never
    on_failure: never
  1. Create Procfile and add this to it: web: YOUR_REPOSITORY_NAME so Heroku knows what to run to start the server. Go is kinda cool because it compiles the project with go build into binary which you can run directy with ./YOUR_REPOSITORY_NAME.
  2. Make sure you have set Travis to build your project https://travis-ci.org/profile/.
  3. git add and git commit your code and push it to GitHub.
  4. Watch as Travis builds the project and pushes it to Heroku.
  5. Profit! Go to your Heroku url to see your app in its glory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment