package main
import (
"log"
"net/http"
"fmt"
"strconv"
)
func main() {
db := database{"shoes": 50, "socks": 5}
http.HandleFunc("/list", db.list)
http.HandleFunc("/price", db.price)
http.HandleFunc("/add",db.add)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
type dollars float32
func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) }
type database map[string]dollars
func (db database) add(w http.ResponseWriter, req *http.Request) {
k := req.URL.Query().Get("key");
v := req.URL.Query().Get("value");
if _,ok := db[k]; ok {
fmt.Printf("the %s is existing\n",k)
return
}
vv,_ := strconv.Atoi(v);
db[k] = dollars(vv);
}
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.
go实现本地数据库
最新推荐文章于 2025-06-25 07:43:21 发布