• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

go http.Handler

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
chaosblade-exec-os项目的burnio.go文件解读发布时间:2022-07-10
下一篇:
Sublime Text 2搭建Go开发环境(Windows)发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap