教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 关于go语言中http做服务器使用正则的实例

关于go语言中http做服务器使用正则的实例

发布时间:2021-04-29   编辑:jiaochengji.com
教程集为您提供关于go语言中http做服务器使用正则的实例等资源,欢迎您收藏本站,我们将为您提供最新的关于go语言中http做服务器使用正则的实例资源
<pre name="code" class="plain">package main

import (
	"net/http"
	"regexp"
)

func main() {
	http.HandleFunc("/", route)
	http.ListenAndServe(":8080", nil)
}

var num = regexp.MustCompile(`\d`)
var str = regexp.MustCompile(`\w`)

func route(w http.ResponseWriter, r *http.Request) {
	switch {
	case num.MatchString(r.URL.Path):
		digits(w, r)
	case str.MatchString(r.URL.Path):
		str(w, r)
	default:
		w.Write([]byte("位置匹配项"))
	}
}

func digits(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("receive digits"))
}

func str(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("receive string"))
}


                                                                                

                                
                                

                                到此这篇关于“关于go语言中http做服务器使用正则的实例”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!
                                

                                                                
                                                            

您可能感兴趣的文章:
关于go语言中http做服务器使用正则的实例
dubbo-go 的开发、设计与功能介绍
支持多语言的微服务框架Tars-Go
想系统学习GO语言(Golang
Go 语言到底适合干什么?
Go语言的主要特性和发展影响
Go 开发关键技术指南 | 为什么你要选择 Go?(内含超全知识大图)
Golang库集合
兄弟连golang神技(1)-关于 Go 语言的介绍
今日头条 Go 建千亿级微服务的实践

[关闭]
~ ~