如何在golang里跳转到其他网页(非本项目)

我想要实现跳转到其它网站的功能,比如别人访问我某个接口,然后我跳转到百度,但是用了下面几种方法,都只能跳转我本地的接口,请问有什么解决方案吗?

我尝试过的方法
func HandleWelcome() func(c echo.Context) error {
return func(c echo.Context) error {
    res:= c.Response()
    w:=res.Writer
    r:=c.Request()

    http.Redirect(w,r,"http://www.baidu.com", http.StatusFound) //跳转到百度
    return nil
}

}

结果

clipboard.png

老提示我路由不存在,也就是当作我本地路由在跳转了,请问难道golang不能实现跳转其他网址的功能吗?

阅读 6.2k
1 个回答
package main

import (
    "github.com/labstack/echo"
    "net/http"
)

func HandleWelcome() func(c echo.Context) error {
    return func(c echo.Context) error {
        res := c.Response()
        w := res.Writer
        r := c.Request()
        http.Redirect(w, r, "http://www.baidu.com", http.StatusFound) //跳转到百度
        return nil
    }
}

func main() {
    e := echo.New()
    e.GET("/", HandleWelcome())
    e.Logger.Fatal(e.Start(":1323"))
}

测试正常, 能跳转,你重启服务试试。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题