mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-12-01 10:38:59 +00:00
48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
|
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||
|
|
||
|
// See page 195.
|
||
|
|
||
|
// Http4 is an e-commerce server that registers the /list and /price
|
||
|
// endpoint by calling http.HandleFunc.
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
//!+main
|
||
|
|
||
|
func main() {
|
||
|
db := database{"shoes": 50, "socks": 5}
|
||
|
http.HandleFunc("/list", db.list)
|
||
|
http.HandleFunc("/price", db.price)
|
||
|
log.Fatal(http.ListenAndServe("localhost:8000", nil))
|
||
|
}
|
||
|
|
||
|
//!-main
|
||
|
|
||
|
type dollars float32
|
||
|
|
||
|
func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) }
|
||
|
|
||
|
type database map[string]dollars
|
||
|
|
||
|
func (db database) list(w http.ResponseWriter, req *http.Request) {
|
||
|
for item, price := range db {
|
||
|
fmt.Fprintf(w, "%s: %s\n", item, price)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (db database) price(w http.ResponseWriter, req *http.Request) {
|
||
|
item := req.URL.Query().Get("item")
|
||
|
if price, ok := db[item]; ok {
|
||
|
fmt.Fprintf(w, "%s\n", price)
|
||
|
} else {
|
||
|
w.WriteHeader(http.StatusNotFound) // 404
|
||
|
fmt.Fprintf(w, "no such item: %q\n", item)
|
||
|
}
|
||
|
}
|