출처 : https://spectrumdig.blogspot.com/2012/02/routing-module-handler.html?m=0
package main
import (
"fmt"
)
type Handler func(method string)
type Route struct {
Method,Pattern string
}
type Routes struct {
Routes []Route
}
func (r Routes) routeCount() int {
return len(r.Routes)
}
func (r *Routes) addRoute(method, pattern string, handler Handler) {
r.Routes=append(r.Routes, Route{Method:method, Pattern:pattern})
fmt.Printf("route Count : %d\n", len(r.Routes))
handler(method)
}
func (r *Routes) GET(pattern string, handler Handler) {
r.addRoute("GET", pattern, handler)
}
func (r *Routes) POST(pattern string, handler Handler) {
r.addRoute("POST", pattern, handler)
}
func (r *Routes) PUT(pattern string, handler Handler) {
r.addRoute("PUT", pattern, handler)
}
func (r *Routes) DELETE(pattern string, handler Handler) {
r.addRoute("DELETE", pattern, handler)
}
func handler(method string) {
fmt.Printf("%s handler executed \n", method)
}
func main() {
r:=new(Routes)
r.GET("/user/:user", handler)
r.PUT("/add/:user", handler)
r.POST("/update/:user", handler)
r.DELETE("/delete/:user", handler)
for _,v:=range r.Routes {
fmt.Printf("%s:%s\n",v.Method, v.Pattern)
}
}
|
'go' 카테고리의 다른 글
cryptopunks raritysniper json 파싱 (0) | 2022.04.15 |
---|---|
JSON Transfer (0) | 2022.04.14 |
Web Handler (0) | 2022.04.14 |
go언어 설치 (0) | 2022.04.14 |
golang 개발환경 구축 (0) | 2021.06.21 |