Skip to content

Instantly share code, notes, and snippets.

@unders
Last active November 17, 2018 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unders/c8ec1892b6d6a23f5544acc0014b66cc to your computer and use it in GitHub Desktop.
Save unders/c8ec1892b6d6a23f5544acc0014b66cc to your computer and use it in GitHub Desktop.
Simple Go handler
func productHandler(w http.ResponseWriter, r *http.Request) {
        key := r.URL.Path[len("/products/"):]
        switch r.Method {
        case "GET":
            // do GET stuff
        case "POST":
            // Fo POST stuff
        default:
            http.Error(w, "Method Not Allowed", 405)
        }
}
type server struct {
    container   *someContainer
    router      *someRouter
}


func (s *server) product() http.HandlerFunc {
    type getRequest struct {
        Name string
    }
    
    type getResponse struct {
        Greeting string `json:"greeting"`
    }
    
    get := func(r *http.Request) () {
    
    }
    GET /products
    POST /products
    GET  /products/10
    PUT  /products/10
    return func(w http.ResponseWriter, r *http.Request) {
        key := r.URL.Path[len("/products/"):]
        switch r.Method {
        case "GET":
            data, err := parseGet(r)
            s.container.Get
        case "POST":
            // Fo POST stuff
        default:
            http.Error(w, s.template.NotAllowed(), 405)
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment