当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > Go语言实现简单Web服务器的方法

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

2017年12月08日  | 移动技术网IT编程  | 我要评论
本文实例讲述了go语言实现简单web服务器的方法。分享给大家供大家参考。具体分析如下: 包 http 通过任何实现了 http.handler 的值来响应 http 请求

本文实例讲述了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语言程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网