Example of a Go program which reads URL parameters and prints them out

This is an example Go program which demonstrates getting parameters from a form and printing them out.

package main

import (
        "log"
        "net/http"
        "fmt"
)

func reply(rw http.ResponseWriter, req *http.Request) {
        err := req.ParseForm()
        if err != nil {
                rw.Write([]byte(fmt.Sprintf ("Error %s processing request", err)))
                return;
        }
        rw.Write([]byte(`
<html>
<body>
<form>
<input name='t' type='text'>
<input type='submit'>
</form>`))
        if len(req.Form) > 0 {
                rw.Write([]byte(fmt.Sprintf ("<pre>%s</pre>", req.Form)))
        }
        rw.Write([]byte(`
</body>
</html>
`))
}

func main() {
        http.HandleFunc("/", reply)
        log.Fatal(http.ListenAndServe(":8082", nil))
}

(download)


Copyright © Ben Bullock 2009-2018. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer