当前位置:网站首页>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")))
}
边栏推荐
猜你喜欢

sum of special numbers

太厉害了,终于有人能把文件上传漏洞讲的明明白白了

Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers

Win任务栏图标异常解决

mysql的行锁和间隙锁

Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)

特别数的和

爬虫基本原理介绍、实现以及问题解决

点餐系统数据库设计--SQL Server

安装SQL Server详细教程
随机推荐
【FiddlerScript】利用FiddlerScript抓包保利威下载
我三本学历,五面阿里,被面试官“供”着出来了,拿了33*15的Offer
第6章——数据库的安全性
选择排序—直接选择排序和堆排序
LeetCode 0150. 逆波兰表达式求值
Xiaobai's 0 Basic Tutorial SQL: An Overview of Relational Databases 02
Offer brush questions - 1
JVM:运行时数据区-PC寄存器(程序计数器)
从购买服务器到网站搭建成功保姆级教程~超详细
阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
torch
Jupyter shortcuts
第5章——以程序方式处理MySQL数据表的数据
响应式织梦模板园林景观类网站
After the image is updated, Glide loading is still the original image problem
return; represents meaning
return;代表含义
Dbeaver connect the MySQL database and error Connection refusedconnect processing
奇葩问题 npm install 报错 gyp ERR
特别数的和