当前位置:网站首页>Golang:go获取url和表单属性值
Golang:go获取url和表单属性值
2022-08-01 06:50:00 【ZzzWClock】
go获取url和表单属性值
一.前言
本章节会使用到net/http包开启web服务,获取request请求, 以下的路由器方法会集成全局的handle处理器,为了让文章简化,所以在图片的代码块中,只更新handle处理器里面的执行逻辑
package main
import (
"fmt"
"net/http"
)
// 处理器
func handle(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "访问的path路径是: ", r.URL.Path)
}
func main() {
// 路由器
http.HandleFunc("/", handle)
// 开启server服务
http.ListenAndServe(":8081", nil)
}
二.示例
- 获取访问url的path路径
// 处理器
func handle(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "访问的path路径是: ", r.URL.Path)
}
- 获取访问url的param参数值
// 处理器
func handle(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "访问的path路径是: ", r.URL.Path)
fmt.Fprintln(w, "访问的param参数是: ", r.URL.RawQuery)
}
- 获取访问url单个的param参数值
// 处理器
func handle(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "访问的path路径是: ", r.URL.Path)
fmt.Fprintln(w, "访问的param参数是: ", r.URL.RawQuery)
fmt.Fprintln(w, "访问的method是: ", r.Method)
// 注意FormValue 可以获取get参数还有post参数
fmt.Fprintln(w, fmt.Sprintf("admin值: %s, age值: %s", r.FormValue("admin"), r.FormValue("age")))
}
- 获取post请求参数值
// 处理器
func handle(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "访问的path路径是: ", r.URL.Path)
fmt.Fprintln(w, "访问的param参数是: ", r.URL.RawQuery)
fmt.Fprintln(w, "访问的method是: ", r.Method)
// 注意FormValue 可以获取get参数还有post参数
fmt.Fprintln(w, fmt.Sprintf("admin值: %s, age值: %s", r.FormValue("admin"), r.FormValue("age")))
// PostFormValue 只会获取post传递过来的参数
fmt.Fprintln(w, fmt.Sprintf("username值: %s, age值: %s", r.PostFormValue("username"), r.PostFormValue("age")))
}
边栏推荐
猜你喜欢
LeetCode 0149. 直线上最多的点数
Dbeaver connect the MySQL database and error Connection refusedconnect processing
I have three degrees, and I have five faces. I was "confessed" by the interviewer, and I got an offer of 33*15.
点餐系统数据库设计--SQL Server
旋度(7)连接失败localhost8080;连接拒绝了
爬虫基本原理介绍、实现以及问题解决
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
crypto-js uses
leetcode125 Verify palindrome string
Detailed explanation of the crawler framework Scrapy
随机推荐
NUMPY
表的创建、修改与删除
2022.7.27好题选讲
从零开始—仿牛客网讨论社区项目(一)
Srping bean in the life cycle
Introduction to the basic principles, implementation and problem solving of crawler
More than 2022 cattle guest school game 4 yue
Solve the problem of page flicker caused by browser scroll bars
NIO programming
小程序通过云函数操作数据库【使用get取数据库】
第5章——以程序方式处理MySQL数据表的数据
测试工具(四)Jenkins环境搭建与使用
R语言使用tidyquant包的tq_transmute函数计算持有某只股票的天、月、周收益率、ggplot2使用条形图可视化股票月收益率数据、使用百分比显示Y轴坐标数据、使用不同的色彩表征正负收益率
Practical training Navicat Chinese and English mode switching
数据机构----线性表之单向链表
JS的运行原理
2022.7.26 Mock Competition
图像基本操作的其他内容
Data organization -- singly linked list of the linear table
轻量级的VsCode为何越用越大?为什么吃了我C盘10G?如何无痛清理VsCode缓存?手把手教你为C盘瘦身