当前位置:网站首页>Golang: go get url and form attribute value
Golang: go get url and form attribute value
2022-08-01 07:04:00 【ZzzWClock】
go获取urlAnd the form attribute values
一.前言
This section will be used tonet/http包开启web服务,获取request请求, The following router method can integrate globalhandle处理器,In order to make the article simplify,So in the image block of code,只更新handleInside the processor execution logic
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")))
}
边栏推荐
- NIO programming
- LeetCode 0149. 直线上最多的点数
- Qt Widget project loading example of qml
- rhcsa 第三次
- Golang: go to connect and use mysql
- Datagrip error "The specified database userpassword combination is rejected..."Solutions
- 轻量级的VsCode为何越用越大?为什么吃了我C盘10G?如何无痛清理VsCode缓存?手把手教你为C盘瘦身
- 滚动条样式修改
- 【南瓜书ML】(task4)神经网络中的数学推导(更新ing)
- Win任务栏图标异常解决
猜你喜欢

Offer刷题——1

LeetCode 0149. Maximum number of points on a line

阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外

Vim简介

crypto-js使用

Offer brush questions - 1

Windows taskbar icon abnormal solution

Srping bean in the life cycle

datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法

Data organization -- singly linked list of the linear table
随机推荐
Explosive 30,000 words, the hardest core丨Mysql knowledge system, complete collection of commands [recommended collection]
NIO编程
2022年牛客多校第四场补题
我说过无数遍了:从来没有一种技术是为灵活组合这个目标而设计的
JVM: Runtime Data Area - PC Register (Program Counter)
05-SDRAM:仲裁
Golang:go连接和使用mysql
MATLAB程序设计与应用 2.5 MATLAB运算
Offer刷题——1
datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
GO错误处理方式
More than 2022 cattle guest school game 4 yue
JS的运行原理
旋度(7)连接失败localhost8080;连接拒绝了
crypto-js uses
【FiddlerScript】利用FiddlerScript抓包保利威下载
Self-made a remote control software - VeryControl
支付宝如何生成及配置公钥证书
LeetCode240+312+394
CSP-S2019兴奋不已