mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-28 17:19:06 +00:00
47 lines
816 B
Go
47 lines
816 B
Go
|
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||
|
|
||
|
// See page 191.
|
||
|
|
||
|
// Http1 is a rudimentary e-commerce server.
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
//!+main
|
||
|
|
||
|
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
|
||
|
|
||
|
func (db database) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||
|
for item, price := range db {
|
||
|
fmt.Fprintf(w, "%s: %s\n", item, price)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//!-main
|
||
|
|
||
|
/*
|
||
|
//!+handler
|
||
|
package http
|
||
|
|
||
|
type Handler interface {
|
||
|
ServeHTTP(w ResponseWriter, r *Request)
|
||
|
}
|
||
|
|
||
|
func ListenAndServe(address string, h Handler) error
|
||
|
//!-handler
|
||
|
*/
|