当前位置:网站首页>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")))
}
边栏推荐
- 电磁兼容简明教程(6)测试项目
- NIO编程
- 小白的0基础教程SQL: 安装MYSQL 03
- 日志导致线程Block的这些坑,你不得不防
- Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
- return;代表含义
- Why is the lightweight VsCode used more and more?Why eat my C drive 10G?How to Painlessly Clean VsCode Cache?Teach you how to lose weight for C drive
- matlab 风速模型 小波滤波
- weight distribution
- 湖仓一体电商项目(一):项目背景和架构介绍
猜你喜欢
随机推荐
Offer brush questions - 1
仿牛客网项目总结
Windows taskbar icon abnormal solution
05-SDRAM:仲裁
crypto-js使用
曲柄滑块机构运动分析和参数优化
Json对象和Json字符串的区别
爆肝3万字,最硬核丨Mysql 知识体系、命令全集 【建议收藏 】
仿牛客网讨论社区项目—项目总结及项目常见面试题
Vim简介
支付宝如何生成及配置公钥证书
Win任务栏图标异常解决
Golang:go获取url和表单属性值
从零开始—仿牛客网讨论社区项目(一)
戴尔PowerEdge服务器R450 RAID配置步骤
NIO programming
weight distribution
基于MATLAB的BP神经网络进行语音特征信号分类
监听父元素宽高,自适应插件大小
自制一款远程控制软件——VeryControl