Skip to content

Instantly share code, notes, and snippets.

@joedborg
Created October 10, 2014 17:48
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 joedborg/b075b49e81ef5cd285a3 to your computer and use it in GitHub Desktop.
Save joedborg/b075b49e81ef5cd285a3 to your computer and use it in GitHub Desktop.
Example of a POST handle in Go
package main
import (
"fmt"
"net/http"
)
var form = []string{
"Make",
"Model",
"Engine Capacity",
}
func handleForm(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<html><body><form method='POST' action='/submit'><table>")
for key := range form {
fmt.Fprintf(w, "<tr><th>%s</th><td><input type='text' name='%s'></td></tr>", form[key], form[key])
}
fmt.Fprintf(w, "</table>")
fmt.Fprintf(w, "<input type='submit'>")
fmt.Fprintf(w, "</form></body></html>")
}
func handlePost(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
for key := range form {
print(r.PostFormValue(form[key]) + "\n")
}
}
func main() {
http.HandleFunc("/", handleForm)
http.HandleFunc("/submit", handlePost)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment