mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-12 22:52:05 +00:00
good good study, day day up!
This commit is contained in:
48
vendor/gopl.io/ch7/http2/main.go
generated
vendored
Normal file
48
vendor/gopl.io/ch7/http2/main.go
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 192.
|
||||
|
||||
// Http2 is an e-commerce server with /list and /price endpoints.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := database{"shoes": 50, "socks": 5}
|
||||
log.Fatal(http.ListenAndServe("localhost:8000", db))
|
||||
}
|
||||
|
||||
type dollars float32
|
||||
|
||||
func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) }
|
||||
|
||||
type database map[string]dollars
|
||||
|
||||
//!+handler
|
||||
func (db database) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
switch req.URL.Path {
|
||||
case "/list":
|
||||
for item, price := range db {
|
||||
fmt.Fprintf(w, "%s: %s\n", item, price)
|
||||
}
|
||||
case "/price":
|
||||
item := req.URL.Query().Get("item")
|
||||
price, ok := db[item]
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound) // 404
|
||||
fmt.Fprintf(w, "no such item: %q\n", item)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "%s\n", price)
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound) // 404
|
||||
fmt.Fprintf(w, "no such page: %s\n", req.URL)
|
||||
}
|
||||
}
|
||||
|
||||
//!-handler
|
Reference in New Issue
Block a user