Skip to content

Instantly share code, notes, and snippets.

@sriharshaj
Created June 28, 2020 03:22
Show Gist options
  • Save sriharshaj/024d835d623c615d20996502f24625cb to your computer and use it in GitHub Desktop.
Save sriharshaj/024d835d623c615d20996502f24625cb to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"io"
)
const form = `<html><body><form action="#" method="post" name="bar">
<input type="text" name="in"/>
<input type="submit" value="Submit"/>
</form></html></body>`
/* handle a simple get request */
func SimpleServer(w http.ResponseWriter, request *http.Request) {
io.WriteString(w, "<h1>hello, world</h1>")
}
/* handle a form, both the GET which displays the form
and the POST which processes it.*/
func FormServer(w http.ResponseWriter, request *http.Request) {
w.Header().Set("Content-Type", "text/html")
switch request.Method {
case "GET":
/* display the form to the user */
io.WriteString(w, form );
case "POST":
/* handle the form data, note that ParseForm must
be called before we can extract form data with Form */
// request.ParseForm();
//io.WriteString(w, request.Form["in"][0])
// easier method:
io.WriteString(w, request.FormValue("in"))
}
}
func main() {
http.HandleFunc("/test1", SimpleServer)
http.HandleFunc("/test2", FormServer)
if err := http.ListenAndServe("0.0.0.0:3000", nil); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment