Routing with the gorilla/mux Package

A powerful HTTP router and URL matcher for building Go web servers

https://github.com/gorilla/mux

Install package

go get -u github.com/gorilla/mux

Build sample 1:

package main

import (
"fmt"
"github.com/gorilla/mux"
"net/http"
) func main() {
r := mux.NewRouter()
r.HandleFunc("/foo", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "hi foo")
}).Methods("GET")
http.ListenAndServe(":8000", r)
}

Run the test sample 1.

Build sample 2: It's helpful to match and pass in parameters within the request patch (for example, when implementing a RESTful API)

package main

import (
"fmt"
"github.com/gorilla/mux"
"net/http"
) func main() {
r := mux.NewRouter()
r.HandleFunc("/users/{user}", func(w http.ResponseWriter, req *http.Request) {
user := mux.Vars(req)["user"]
fmt.Fprintf(w, "hi %s\n", user)
}).Methods("GET")
http.ListenAndServe(":8000", r)
}

Run and test sample 2.

Build sample 3: Use regular expression to qualify the patterns passed.

package main

import (
"fmt"
"github.com/gorilla/mux"
"net/http"
) func main() {
r := mux.NewRouter()
r.HandleFunc("/users/{user:[a-z]+}", func(w http.ResponseWriter, req *http.Request) {
user := mux.Vars(req)["user"]
fmt.Fprintf(w, "hi %s\n", user)
}).Methods("GET")
http.ListenAndServe(":8000", r)
}

Run and test sample 3.

Go Pentester - HTTP Servers(2)的更多相关文章

  1. Go Pentester - HTTP Servers(1)

    HTTP Server Basics Use net/http package and useful third-party packages by building simple servers. ...

  2. Go Pentester - HTTP Servers(3)

    Building Middleware with Negroni Reasons use middleware, including logging requests, authenticating ...

  3. Coping with the TCP TIME-WAIT state on busy Linux servers

    Coping with the TCP TIME-WAIT state on busy Linux servers 文章源自于:https://vincent.bernat.im/en/blog/20 ...

  4. How To Restart timer service on all servers in farm

    [array]$servers= Get-SPServer | ? {$_.Role -eq "Application"} $farm = Get-SPFarm foreach ( ...

  5. eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Sertomcat

    eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Serto ...

  6. coderforces #387 Servers(模拟)

    Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  7. Servers

    Servers¶ Server interface. class novaclient.v1_1.servers.Server(manager, info, loaded=False) Bases: ...

  8. 使用servers 启动项目时 ,一直处于启动中, 最后出现无法的问题。

    使用eclipse 中的servers 配置了一个server 来启动项目, 发现无法启动 排除法: 去掉项目配置,单独启动该server ,发现可以启动, 说明是项目出现问题 但是项目并没有报错, ...

  9. servers中添加server时,看不到运行环境的选择。

    servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.

随机推荐

  1. Cookie 和 Session 关系详解

     什么是 Cookie 和 Session ? 什么是 Cookie HTTP Cookie(也叫 Web Cookie或浏览器 Cookie)是服务器发送到用户浏览器并保存在本地的一小块数据,它会在 ...

  2. cb43a_c++_STL_算法_删除_(1)remove_remove_if

    cb43a_c++_STL_算法_删除_(1)remove_remove_ifremove()remove_if() 注意:1.并不是真正的删除,而是把后面的元素向前移动,覆盖被删除元素,元素个数并没 ...

  3. android屏幕适配的全攻略3-动态获取手机屏幕宽高及动态设置控件宽高

    1.获取手机屏幕宽高: DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetr ...

  4. Python 简明教程 --- 5,Python 表达式与运算符

    微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 靠代码行数来衡量开发进度,就像是凭重量来衡量飞机制造的进度. -- Bill Gates 目录 1, ...

  5. 入门大数据---Spark车辆监控项目

    一.项目简介 这是一个车辆监控项目.主要实现了三个功能: 1.计算每一个区域车流量最多的前3条道路. 2.计算道路转换率 3.实时统计道路拥堵情况(当前时间,卡口编号,车辆总数,速度总数,平均速度) ...

  6. Mariadb之日志相关配置

    前面我们聊到了mariadb的事务,以及事务隔离级别,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13198186.html:今天我们来聊一聊mariadb的 ...

  7. C# 接口(interface) 抽象类(abstract)

    类代码: interface Employee { void ShowEmp(); } abstract class EmployeeInPostion: Employee { public abst ...

  8. 《UNIX环境高级编程》(APUE) 笔记第九章 - 进程关系

    9 - 进程关系 GitHub 地址 1. 进程组 每个进程除了有一个 进程 ID 外,还属于一个 进程组 .进程组是一个或多个进程的 集合 ,通常,它们是在同一作业中结合起来的,同一进程组中的各进程 ...

  9. JedisUtils工具类模板

    redis.properties配置文件 redis.maxIdle=30 redis.minIdle=10 redis.maxTotal=100 redis.url=192.168.204.128 ...

  10. BAT 非右键方式以管理员身份运行批处理

    @echo off & PUSHD %~dp0 & TITLE Run The BAT File As An Administrator mode con lines=4 cols=6 ...