基本示例

  1. package main
  2.  
  3. import (
  4. "github.com/dxvgef/httpdispatcher"
  5. "net/http"
  6. "log"
  7. )
  8.  
  9. func main() {
  10. //获得调度器实例
  11. var dispatcher = httpdispatcher.New()
  12.  
  13. //注册路由
  14. dispatcher.Router.GET("/", func(ctx *httpdispatcher.Context) error {
  15. ctx.ResponseWriter.WriteHeader(200)
  16. ctx.ResponseWriter.Write([]byte("Hello world!"))
  17. //路由函数的出参为nil,表示路由控制器正常执行完毕
  18. return nil
  19. })
  20.  
  21. if err := http.ListenAndServe(":8080", dispatcher); err != nil {
  22. log.Fatal(err.Error())
  23. }
  24. }

访问 http://127.0.0.0:8080/ 即可看到输出的字符串"Hello world!"