Skip to content

Instantly share code, notes, and snippets.

@m-x-k
Created September 5, 2017 20:33
Show Gist options
  • Save m-x-k/e8e521c26a388865ba60a83936efa8ab to your computer and use it in GitHub Desktop.
Save m-x-k/e8e521c26a388865ba60a83936efa8ab to your computer and use it in GitHub Desktop.
Golang http request method alternative response
package main
import "net/http"
func main() {
mainMux := http.NewServeMux()
mainMux.HandleFunc("/speak", func(res http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
res.Write([]byte("Hello, World!\n"))
} else if req.Method == "POST" {
res.Write([]byte("Thank You!\n"))
} else if req.Method == "DELETE" {
res.Write([]byte("Goodbye, World!\n"))
}
})
http.ListenAndServe(":3000", mainMux)
}
curl http://localhost:3000/speak
curl -X POST http://localhost:3000/speak
curl -X DELETE http://localhost:3000/speak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment