Skip to content

Instantly share code, notes, and snippets.

@jimbo8098
Last active August 30, 2020 17:59
Show Gist options
  • Save jimbo8098/dd006db6f8dcf5b4460318e578a7737a to your computer and use it in GitHub Desktop.
Save jimbo8098/dd006db6f8dcf5b4460318e578a7737a to your computer and use it in GitHub Desktop.
Go HTTP Server
// Using this Gist, you can kick off a web server and start receiving and sending data when you visit localhost:5000/foo or /bar
package main
import (
"net/http"
)
type fooHandler struct {
Message string
}
func main() {
http.Handle("/foo",&fooHandler{Message: "foo called"})
http.HandleFunc("/bar",barHandler)
http.ListenAndServe(":5000",nil)
}
func (f *fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(f.Message));
}
func barHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("bar called"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment