分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

9.5 处理http 请求

发布时间:2023-09-06 01:47责任编辑:顾先生关键词:http
package mainimport ( ???"fmt" ???"net/http")func main() { ???mux := http.NewServeMux() ???mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) { ???????if r.Method == http.MethodGet { ???????????fmt.Fprintln(w, "User GET") ???????} ???????if r.Method == http.MethodPost { ???????????fmt.Fprintln(w, "User POST") ???????} ???}) ???// separate handler ???itemMux := http.NewServeMux() ???itemMux.HandleFunc("/items/clothes", func(w http.ResponseWriter, r *http.Request) { ???????fmt.Fprintln(w, "Clothes") ???}) ???mux.Handle("/items/", itemMux) ???// Admin handlers ???adminMux := http.NewServeMux() ???adminMux.HandleFunc("/ports", func(w http.ResponseWriter, r *http.Request) { ???????fmt.Fprintln(w, "Ports") ???}) ???mux.Handle("/admin/", ???????http.StripPrefix("/admin", adminMux)) ???// Default server ???http.ListenAndServe(":8080", mux)}/*(sx3.5.3) ? ?~ curl -XI http://127.0.0.1:8080/user(sx3.5.3) ? ?~ curl -XI http://127.0.0.1:8080/userA404 page not found(sx3.5.3) ? ?~ curl -X POST http://127.0.0.1:8080/userUser POST(sx3.5.3) ? ?~ curl ?http://127.0.0.1:8080/admin<a href="/admin/">Moved Permanently</a>.*/

9.5 处理http 请求

原文地址:https://www.cnblogs.com/zrdpy/p/8654883.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved