go http.Handler

http1

复制代码
package main

import (
    "log"
    "net/http"
    "fmt"
)

func main() {
    db:=database{"shoes":50,"socks":5}
    log.Fatal(http.ListenAndServe("localhost:5000",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)
    }
}
View Code
复制代码

打开浏览器:http://localhost:5000/

 

 http2

复制代码
package main

import (
    "fmt"
    "net/http"
    "log"
)

func main() {
    db:=database{"shoes":50,"socks":5}
    mux:=http.NewServeMux()
    mux.Handle("/list",http.HandlerFunc(db.list))
    mux.Handle("/price",http.HandlerFunc(db.price))
    log.Fatal(http.ListenAndServe("localhost:5000",mux))
}
type database map[string]dollars
type dollars float32

func (d dollars) String() string {
    return fmt.Sprintf("$%.2f",d)
}
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")
    price,ok:=db[item]
    if !ok{
        w.WriteHeader(http.StatusNotFound)
        fmt.Fprintf(w,"no such item: %q\n",item)
        return
    }
    fmt.Fprintf(w,"%s\n",price)
}
View Code
复制代码

打开浏览器:http://localhost:5000/price?item=socks

 clock1

复制代码
package main

import (
    "net"
    "io"
    "time"
    "log"
)

func main() {
    listener,err:=net.Listen("tcp","localhost:8000")
    if err!=nil{
        log.Fatal(err)
    }
    for{
        conn,err:=listener.Accept()
        if err!=nil{
            log.Print(err)
            continue
        }
        handleConn(conn)
    }
}

func handleConn(c net.Conn) {
    defer c.Close()
    for{
        _,err:=io.WriteString(c,time.Now().Format("15:04:05\r\n"))
        if err!=nil{
            return
        }
        time.Sleep(1*time.Second)
    }
}
View Code
复制代码

运行clock1,打开cmd,使用telnet localhost 8000 进行连接

 或者使用下面的程序

netcat1

复制代码
package main

import (
    "io"
    "log"
    "net"
    "os"
)

func main() {
    conn,err:=net.Dial("tcp","localhost:8000")
    if err!=nil{
        log.Fatal(err)
    }
    defer conn.Close()
    mustCopy(os.Stdout,conn)
}

func mustCopy(dst io.Writer, src io.Reader) {
    if _,err:=io.Copy(dst,src);err!=nil{
        log.Fatal(err)
    }
}
View Code
复制代码

为了让服务器支持并发,只需要在handleConn上添加一个go

clock2

复制代码
    for{
        conn,err:=listener.Accept()
        if err!=nil{
            log.Print(err)
            continue
        }
        go handleConn2(conn)
    }
View Code
复制代码

 

posted @   uptothesky  阅读(247)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
历史上的今天:
2016-03-16 设计模式:单例模式
2016-03-16 设计模式:迭代器模式
点击右上角即可分享
微信分享提示