基本示例
- package main
- import (
- "github.com/dxvgef/httpdispatcher"
- "net/http"
- "log"
- )
- func main() {
- //获得调度器实例
- var dispatcher = httpdispatcher.New()
- //注册路由
- dispatcher.Router.GET("/", func(ctx *httpdispatcher.Context) error {
- ctx.ResponseWriter.WriteHeader(200)
- ctx.ResponseWriter.Write([]byte("Hello world!"))
- //路由函数的出参为nil,表示路由控制器正常执行完毕
- return nil
- })
- if err := http.ListenAndServe(":8080", dispatcher); err != nil {
- log.Fatal(err.Error())
- }
- }
访问 http://127.0.0.0:8080/
即可看到输出的字符串"Hello world!"