Created
October 10, 2014 17:48
-
-
Save joedborg/b075b49e81ef5cd285a3 to your computer and use it in GitHub Desktop.
Example of a POST handle in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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