Deploying a go application on Heroku

View the source code for this video on Github

Here are the shell commands from the video:

go run server.go
  PORT=5000 go run server.go
  git add .
  git commit -m "initial app"
  heroku create -b https://github.com/kr/heroku-buildpack-go.git
  git push heroku master
  

Here is the content of the server.go file:

package main
  
  import (
    "fmt"
    "net/http"
    "os"
  )
  
  func sayHello(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello World!")
  }
  
  func getPort() string {
    p := os.Getenv("PORT")
    if p != "" {
      return ":" + p
    }
    return ":1234"
  }
  
  func main() {
    port := getPort()
  
    http.HandleFunc("/", sayHello)
  
    fmt.Println("Listening on port", port)
    err := http.ListenAndServe(port, nil)
    if err != nil {
      panic(err)
    }
  }
  

Here is the content of your .godir file:

myapp
  

And for your Procfile:

web: myapp
  

Reference

← Read all articles