douzhi3776 2017-12-26 09:19
浏览 81
已采纳

从邮递员检查时出现404页面未找到错误

I'm running the below code using goapp serve. Somehow getting 404 page not found error while checking from postman. Could you please help me to fix this

    package hello

        import (
        "fmt"
        "net/http"

        "github.com/julienschmidt/httprouter"
    )

    func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
        fmt.Fprint(w, "Welcome!
")
    }

    func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
        fmt.Fprintf(w, "hello, %s!
", ps.ByName("name"))
    }

    func init() {
        router := httprouter.New()
        router.GET("/", Index)
        router.GET("/hello/:name", Hello)
//log.Fatal(http.ListenAndServe(":8080", router))

    }

In postman passing endpoint http://localhost:8080/hello/hyderabad

  • 写回答

1条回答 默认 最新

  • dongtanlin0765 2017-12-26 11:02
    关注

    To expand on my comment above: A handler function (or the router from julienschmidt/httprouter) does not register itself. Instead, it needs to be registered with the http server.

    The simplest way to do that is usually do register with the default ServeMux using: http.Handle("/", router)

    Thus, changing the init function to the following will work:

       func init() {
            router := httprouter.New()
            router.GET("/", Index)
            router.GET("/hello/:name", Hello)
            http.Handle("/", router)
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行