首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏人人都是架构师

    Go每日一库之84:httprouter

    httprouter 一句话描述 httprouter是一个轻量的、高效的http请求路由器,对http请求进行路由转发 入门示例 package main import ( "fmt" "net/http" "log" "github.com/julienschmidt/httprouter" ) func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprint(w, "Welcome! \n") } func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { fmt.Fprintf(w, GoDoc:https://pkg.go.dev/github.com/julienschmidt/httprouter

    62450编辑于 2023-09-30
  • 来自专栏Golang语言社区

    Golang:使用 httprouter 构建 API 服务器

    回到多路复用器,我发现了 3 个是非常有用的好东西,即 Gorilla mux、httprouter 和 bone(按性能从低到高排列)。 因此,我最终使用了 httprouter。在本教程中,我将使用 httprouter 构建一个简单的 REST API 服务器。 the values to httprouter.Handle func NewRouter(routes Routes) *httprouter.Router { router := httprouter.New 注 https://github.com/gsingharoy/httprouter-tutorial/tree/master/part1 https://github.com/gsingharoy/httprouter-tutorial /httprouter https://github.com/go-zoo/bone ---- 关于作者 作者: Oopsguy 来源: 个人博客

    2.5K140发布于 2018-03-19
  • 来自专栏sunsky

    httprouter与 fasthttp 的性能对比

    10.13 内存:8 cpu核心数:4 为了模拟网络请求处理时间,所以在处理的Handle function 中加入了 time.Sleep(200 *time.Millisecond) 测试对象: httprouter :本打算使用gin 框架进行测试,但是fasthttp是一个http的包 使用 gin使用的httprouter 进行测试,优势在于实现了restful 风格的地址,使用前缀树实现了路由查找,使用了go 80% 223 90% 225 95% 227 98% 231 99% 235 100% 246 (longest request) 2、httprouter ID COMMAND %CPU TIME #TH #WQ #POR MEM PURG CMPR PGRP PPID STATE BOOSTS 20746 httprouter PID COMMAND %CPU TIME #TH #WQ #POR MEM PURG CMPR PGRP PPID STATE BOOSTS 20746 httprouter

    1.6K30发布于 2020-08-20
  • 来自专栏程序员的酒和故事

    通过httprouter和redis框架搭建restful api服务

    ##**httprouter** HttpRouter is a lightweight high performance HTTP request router (also called multiplexer 应用: package main import ( "fmt" "log" "net/http" "github.com/julienschmidt/httprouter " ) func Index(w http.ResponseWriter, r \*http.Request, \_ httprouter.Params) { fmt.Fprint(w \n") } func Hello(w http.ResponseWriter, r \*http.Request, ps httprouter.Params) { fmt.Fprintf \n", ps.ByName("name")) } func main() { router := httprouter.New() router.GET("/", Index

    1.9K100发布于 2018-02-23
  • 来自专栏歪歪梯Club

    Httprouter—用go实现的高性能路由器

    简介 官方——https://github.com/julienschmidt/httprouter HttpRouter is a lightweight high performance HTTP 简单描述,httprouter是一个golang实现的路由组件。httprouter使用一个前缀树来维护映射的父子关系,通过前缀树快速路由。 同时其里面的HttpRouter结构体实现了golang的net.http.server的Handler接口,可以作为httpHandle发布。 golang以性能出名的gin使用的也就是httprouter来做路由处理。 , _ httprouter.Params) { w.Write([]byte("hello world")) } func main() { router := httprouter.New

    1.8K30发布于 2020-08-17
  • 来自专栏飞雪无情的博客

    Go语言经典库使用分析(七)| 高性能可扩展 HTTP 路由 httprouter

    httprouter httprouter 是一个高性能、可扩展的HTTP路由,上面我们列举的net/http默认路由的不足,都被httprouter 实现,我们先用一个例子,认识下 httprouter httprouter 命名参数 现代的API,基本上都是Restful API,httprouter提供的命名参数的支持,可以很方便的帮助我们开发Restful API。 httprouter 静态文件服务 httprouter提供了很方便的静态文件服务,可以把一个目录托管在服务器上,以供访问。 httprouter 异常捕获 很少有路由支持这个功能的,httprouter允许使用者,设置PanicHandler用于处理HTTP请求中发生的panic。 httprouter因为实现了http.Handler,所以可扩展性非常好,可以和其他库、中间件结合使用,gin这个web框架就是使用的自定义的httprouter

    1.3K40发布于 2020-02-10
  • 来自专栏火丁笔记

    在Golang的HTTP请求中共享数据

    实际上,我之所以关注这个问题是因为 httprouter,众所周知,httprouter 是目前 Golang 社区最流行的 HTTP 路由库,不过它有一个问题,其 handler 参数定义如下: func , *http.Request) 也就是说, httprouter 为了传递路由参数,搞了一个 httprouter.Params 参数,可惜它破坏了兼容性,关于此问题,官方给出了说明: The router Therefore httprouter.Handle has a third function parameter. 大概意思是说 httprouter 提供了兼容模式,不过兼容模式不能使用路由参数。 (httprouter.Params) w.Write([]byte(p.ByName("name"))) } func main() { router := httprouter.New()

    94520编辑于 2021-12-14
  • 来自专栏程序员的酒和故事

    Golang中使用echo框架、MongoDB、JWT搭建REST API

    之前介绍过golang中restful api的博客,是使用redis作为持久化,httprouter作为框架: Go实战–通过httprouter和redis框架搭建restful api服务(github.com /julienschmidt/httprouter) 今天,继续echo框架,这次加入mongodb作为持久化存储,使用jwt进行验证,来搭建一套rest api,类似Twitter。

    2.4K50发布于 2018-03-12
  • 来自专栏云原生运维社区

    老板与秘书的故事理解CORS(跨域),真的超级简单

    := chi.NewRouter() httpRouter.Route( "/api/ v1" , func (r chi.Router) { r.Get( "/books" , getAllBooks := chi.NewRouter() httpRouter.Get("/", serveIndex) server := &http.Server{Addr: "localhost:3333" , Handler: httpRouter} return server.ListenAndServe() } func serveIndex(w http.ResponseWriter, req httpRouter.Route( "/api/v1" , func (r chi.Router) { r.Options( "/books" , corsOptions) r.Get( " := chi.NewRouter() httpRouter.Route("/api/v1", func(r chi.Router) { r.Options("/books", corsOptions

    70610编辑于 2024-02-03
  • 来自专栏飞雪无情的博客

    Golang Gin 实战(三)| 路由参数

    Gin的路由采用的是httprouter,所以它的路由参数的定义和httprouter也是一样的。 Gin内部使用的路由是httprouter,我这里前段时间正好有一篇关于httprouter的详细分析,可以看下。 Go语言经典库使用分析(七)| 高性能可扩展 HTTP 路由 httprouter 星号路由参数 上面我们介绍的是:号的路由参数,这种路由参数最常用。

    6.2K10发布于 2020-02-10
  • 来自专栏太白技术

    那些年我做的开源项目之web篇

    So I did some searching on Github and found the wonderful julienschmidt/httprouter: it is very fast,unfortunately 所以我在Github上做了一些搜索,发现了很棒的julienschmidt/httprouter[5]:它非常快,不幸的是它不支持regexp。 感谢 2、一些Issure: 特地来说一声感谢[10] 这位网友专门来感谢我,很开心 Benchmark is not (only) measuring the routing cost[11] httprouter //eclipse-ee4j.github.io/jersey/ [4]Gorouter: https://github.com/xujiajun/gorouter [5]julienschmidt/httprouter : https://github.com/julienschmidt/httprouter [6]mux: https://github.com/gorilla/mux [7]xujiajun/gorouter

    66710编辑于 2022-09-01
  • 来自专栏CSDN专栏

    (Go Gin)Gin学习笔记(二):路由配置、基本路由、表单参数、上传单个文件、上传多个文件、浅扒路由原理

    路由 gin 框架中采用的路优酷是基于httprouter做的 HttpRouter 是一个高性能的 HTTP 请求路由器,适用于 Go 语言。 主要特点 显式匹配:与其他路由器不同,HttpRouter 只支持显式匹配,即一个请求只能匹配一个或不匹配任何路由。这避免了意外匹配,提高了 SEO 和用户体验。 自动处理尾部斜杠:HttpRouter 会自动重定向缺少或多余尾部斜杠的请求,前提是新路径有处理程序。如果不需要这种行为,可以关闭2。 路径自动校正:除了处理尾部斜杠,HttpRouter 还可以修正错误的大小写和多余的路径元素(如 …/ 或 //)。 典型的压缩字典树结构如下: 1.9 参考文章:gin框架httprouter路由原理

    38010编辑于 2025-10-13
  • 来自专栏薯条的编程修养

    [Golang]从0到1写一个web服务(上)

    不过前些日子官方推出了Go Module这个工具,虽然目前这个工具还有一些问题,但是这是官方出品并强推的,肯定不会死,让我们通过go module把httprouter引用进来。 Go Modules可以使用之前的go get 命令来拉去包,比如这样:go get -u github.com/julienschmidt/httprouter。 首先开一个logic目录,用来放项目的主要逻辑;接着开一个model目录,用来存放note模型,接着在main同级下写一个route目录,引入httprouter。 { router := httprouter.New() router.GET("/", logic.Hello) router.GET("/note", logic.GetAll) .. " ) func Hello(wr http.ResponseWriter, r *http.Request, _ httprouter.Params) { _, err := wr.Write([

    89720编辑于 2022-08-10
  • 来自专栏薯条的编程修养

    从0到0.5用golang写一个web项目

    不过前些日子官方推出了Go Module这个工具,虽然目前这个工具还有一些问题,但是这是官方出品并强推的,肯定不会死,让我们通过go module把httprouter引用进来。 Go Modules可以使用之前的go get 命令来拉去包,比如这样:go get -u github.com/julienschmidt/httprouter。 首先开一个logic目录,用来放项目的主要逻辑;接着开一个model目录,用来存放note模型,接着在main同级下写一个route目录,引入httprouter。 { router := httprouter.New() router.GET("/", logic.Hello) router.GET("/note", logic.GetAll) .. " ) func Hello(wr http.ResponseWriter, r *http.Request, _ httprouter.Params) { _, err := wr.Write([

    90930编辑于 2022-08-10
  • 来自专栏Golang语言社区

    go http 服务器编程(2)

    httprouter 和 mux 一样,也是扩展了自带 ServeMux 功能的路由库。 package main import ( "fmt" "github.com/julienschmidt/httprouter" "net/http" "log" ) func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprint(w, "Welcome \n") } func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { fmt.Fprintf(w, \n", ps.ByName("name")) } func main() { router := httprouter.New() router.GET("/", Index)

    1.6K40发布于 2018-03-23
  • 来自专栏GoLang全栈

    这些 API 设计里的坑,你踩了几个?

    Gin 的路由库 Gin 使用的是 httprouter 作为路由库,Github 地址如下: https://github.com/julienschmidt/httprouter httprouter

    33940编辑于 2022-05-10
  • 来自专栏CSDN专栏

    (Go Gin)上手Go Gin 基于Go语言开发的Web框架,本文介绍了各种路由的配置信息;包含各场景下请求参数的基本传入接收

    路由 gin 框架中采用的路优酷是基于httprouter做的 HttpRouter 是一个高性能的 HTTP 请求路由器,适用于 Go 语言。 主要特点 显式匹配:与其他路由器不同,HttpRouter 只支持显式匹配,即一个请求只能匹配一个或不匹配任何路由。这避免了意外匹配,提高了 SEO 和用户体验。 自动处理尾部斜杠:HttpRouter 会自动重定向缺少或多余尾部斜杠的请求,前提是新路径有处理程序。如果不需要这种行为,可以关闭2。 路径自动校正:除了处理尾部斜杠,HttpRouter 还可以修正错误的大小写和多余的路径元素(如 …/ 或 //)。 不用关心/,例如当请求/foo/时,此path不存在,如果有/foo会自动302转发 path自动修正,例如//foo转化成/foo httprouter使用的数据结构就是压缩字典树。

    27510编辑于 2025-10-13
  • 来自专栏编程之路的专栏

    Go语言入门——实践篇(五)

    创建自定义的多路复用器来代替net/http包中的ServeMux是可行的,并且目前市面上已经出现了很多第三方的多路复用器可供使用,而HttpRouter就是一个功能强大的轻量级第三方多路复用器。 执行以下命令安装 HttpRouter go get github.corn/julienschrnidt/httprouter 这表示从GitHub上下载HttpRouter包源码,并将其保存到GOPATH /src目录中 使用示例 package main import ( "fmt" "github.com/julienschmidt/httprouter" "net/http " ) func hello(w http.ResponseWriter, r *http.Request, p httprouter.Params) { fmt.Fprintf(w, "hello \n", p.ByName("name")) } func main() { //创建多路复用器 mux := httprouter.New() //将处理器函数与给定的HTTP

    91210发布于 2019-09-09
  • 来自专栏HelloGitHub

    《HelloGitHub》第 87 期

    package main import ( "fmt" "net/http" "log" "github.com/julienschmidt/httprouter" ) func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprint(w, "Welcome \n") } func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { fmt.Fprintf(w, \n", ps.ByName("name")) } func main() { router := httprouter.New() router.GET("/", Index) Hello) log.Fatal(http.ListenAndServe(":8080", router)) } 地址:https://github.com/julienschmidt/httprouter

    58230编辑于 2023-08-19
  • 来自专栏希里安

    Gin路由之routes group

    gin框架中采用的路由库是基于httprouter做的,地址 https://github.com/julienschmidt/httprouter来个hello world先 package main

    48210编辑于 2023-10-30
领券