以下是使用 GoFrame 返回重定向的简单示例:
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func main() {
s := g.Server()
// 定义一个路由,用于触发重定向
s.BindHandler("/redirect", func(r *ghttp.Request) {
// 执行重定向
r.Response.Redirect("https://www.example.com")
})
s.Run()
}
在这个示例中,当访问 /redirect 路由时,服务器会返回一个重定向到 https://www.example.com。这样,客户端将会跳转到指定的 URL。
你也可以使用 http.Status* 常量来设置重定向的 HTTP 状态码,例如:
r.Response.Redirect("https://www.example.com", http.StatusMovedPermanently)
这将返回 HTTP 状态码 301 Moved Permanently。
转载请注明出处:http://www.zyzy.cn/article/detail/7828/GoFrame