当前位置 博文首页 > Go语言实现简单Web服务器的方法

    Go语言实现简单Web服务器的方法

    作者:不是JS 时间:2021-02-13 12:07

    本文实例讲述了Go语言实现简单Web服务器的方法。分享给大家供大家参考。具体分析如下:

    包 http 通过任何实现了 http.Handler 的值来响应 HTTP 请求:
    package http
    type Handler interface {
    ServeHTTP(w ResponseWriter,
    r *Request)
    }

    在这个例子中,类型 Hello 实现了 http.Handler。
    注意: 这个例子无法在基于 web 的指南用户界面运行。为了尝试编写 web 服务器,可能需要安装 Go。

    复制代码 代码如下:
    package main
    import (
        "fmt"
        "net/http"
    )
    type Hello struct{}
    func (h Hello) ServeHTTP(
            w http.ResponseWriter,
            r *http.Request) {
        fmt.Fprint(w, "Hello!")
    }
    func main() {
        var h Hello
        http.ListenAndServe("localhost:4000",h)
    }

    希望本文所述对大家的Go语言程序设计有所帮助。

    js
    下一篇:没有了