Webserver in Go Lang


SUBMITTED BY: Guest

DATE: July 22, 2013, 9:20 p.m.

FORMAT: Text only

SIZE: 957 Bytes

HITS: 722

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gorilla/pat"
  5. "net/http"
  6. "runtime"
  7. )
  8. var r *pat.Router
  9. type handler func(http.ResponseWriter, *http.Request) error
  10. func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  11. err := h(w, req)
  12. if err != nil {
  13. http.Error(w, err.Error(), http.StatusInternalServerError)
  14. return
  15. }
  16. }
  17. func index(w http.ResponseWriter, req *http.Request) (err error) {
  18. fmt.Fprint(w, "Bonjour")
  19. return
  20. }
  21. func hello(w http.ResponseWriter, req *http.Request) (err error) {
  22. fmt.Fprint(w, "hello")
  23. return
  24. }
  25. func main() {
  26. runtime.GOMAXPROCS(runtime.NumCPU())
  27. r = pat.New()
  28. r.Add("GET", "/hello", handler(hello)).Name("hello")
  29. r.Add("GET", "/", handler(index)).Name("index")
  30. if err := http.ListenAndServe(":8000", r); err != nil {
  31. panic(err)
  32. }
  33. }

loading...

comments powered by Disqus