Golang Goji Sample

https://github.com/haruyama/golang-goji-sample 3 https://github.com/elcct/defaultproject 3 demo http://defaultproject.com/ 3

package main

 import (
         "flag"
         "fmt"
         "github.com/zenazn/goji"
         "github.com/zenazn/goji/web"
         "net/http"
 )

 func Home(w http.ResponseWriter, r *http.Request) {
         w.Write([]byte("Hello, World!"))
 }

 func SayHello(c web.C, w http.ResponseWriter, r *http.Request) {
         // get the name from web.C
         // a request-local context object
         // see https://github.com/zenazn/goji/blob/master/web/web.go
         name := c.URLParams["name"]
         w.Write([]byte(fmt.Sprintf("Hello %s !", name)))
 }

 func main() {
         goji.Get("/", Home)
         goji.Get("/:name", SayHello)

         // goji default port number is 8000
         // change to port number 8080
         // with flag.Set
         flag.Set("bind", ":8080")

         goji.Serve()

 }

https://elithrar.github.io/article/custom-handlers-avoiding-globals/

Relative Articles