欢迎访问我的博客,目前从事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 @   有蚊子  阅读(145)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
历史上的今天:
2018-06-27 web机试
2018-06-27 web笔试
点击右上角即可分享
微信分享提示