mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-12-27 15:26:20 +00:00
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 348.
|
|
|
|
// Search is a demo of the params.Unpack function.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
//!+
|
|
|
|
import "gopl.io/ch12/params"
|
|
|
|
// search implements the /search URL endpoint.
|
|
func search(resp http.ResponseWriter, req *http.Request) {
|
|
var data struct {
|
|
Labels []string `http:"l"`
|
|
MaxResults int `http:"max"`
|
|
Exact bool `http:"x"`
|
|
}
|
|
data.MaxResults = 10 // set default
|
|
if err := params.Unpack(req, &data); err != nil {
|
|
http.Error(resp, err.Error(), http.StatusBadRequest) // 400
|
|
return
|
|
}
|
|
|
|
// ...rest of handler...
|
|
fmt.Fprintf(resp, "Search: %+v\n", data)
|
|
}
|
|
|
|
//!-
|
|
|
|
func main() {
|
|
http.HandleFunc("/search", search)
|
|
log.Fatal(http.ListenAndServe(":12345", nil))
|
|
}
|
|
|
|
/*
|
|
//!+output
|
|
$ go build gopl.io/ch12/search
|
|
$ ./search &
|
|
$ ./fetch 'http://localhost:12345/search'
|
|
Search: {Labels:[] MaxResults:10 Exact:false}
|
|
$ ./fetch 'http://localhost:12345/search?l=golang&l=programming'
|
|
Search: {Labels:[golang programming] MaxResults:10 Exact:false}
|
|
$ ./fetch 'http://localhost:12345/search?l=golang&l=programming&max=100'
|
|
Search: {Labels:[golang programming] MaxResults:100 Exact:false}
|
|
$ ./fetch 'http://localhost:12345/search?x=true&l=golang&l=programming'
|
|
Search: {Labels:[golang programming] MaxResults:10 Exact:true}
|
|
$ ./fetch 'http://localhost:12345/search?q=hello&x=123'
|
|
x: strconv.ParseBool: parsing "123": invalid syntax
|
|
$ ./fetch 'http://localhost:12345/search?q=hello&max=lots'
|
|
max: strconv.ParseInt: parsing "lots": invalid syntax
|
|
//!-output
|
|
*/
|