本地请求
2016-09-13 本文已影响0人
后来我经过梵高的葵花田
运行下面代码.
在浏览器中访问:http://localhost:4000/ 可以看到hello输出
//定义类型
type Hello struct{}
//复写 http请求和相应接口
func (h Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello!")
}
func main() {
//声明变量
var h Hello
//创建请求
err := http.ListenAndServe("localhost:4000", h)
if err != nil {
log.Fatal(err)
}
}