欢迎访问我的博客,目前从事Machine Learning,欢迎交流

【Go/golang】原生监听处理http请求的写法

原文地址:https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/03.4.md

由于自己经常忘了怎么写,而Github又经常抽风,所以这里做个备份

package main

import (
	"fmt"
	"net/http"
)

type MyMux struct {
}

func (p *MyMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path == "/" {
		sayhelloName(w, r)
		return
	}
	http.NotFound(w, r)
	return
}

func sayhelloName(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello myroute!")
}

func main() {
	mux := &MyMux{}
	http.ListenAndServe(":9090", mux)
}
posted @ 2023-06-27 17:19  有蚊子  阅读(135)  评论(0编辑  收藏  举报