golang重定向方法怎么调用

655Z技术栈 Go编程 2025年07月10日 14

在Golang中,可以使用http.Redirect函数来实现重定向。

package main import ( "net/http" ) func redirectHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "http://www.example.com", http.StatusFound) } func main() { http.HandleFunc("/redirect", redirectHandler) http.ListenAndServe(":8080", nil) }

在上面的示例中,redirectHandler函数会被注册为处理/redirect路径的请求。当收到该请求时,http.Redirect函数会将响应重定向到http://www.example.com网站,并返回状态码http.StatusFound

你可以根据自己的需求修改重定向的目标URL和状态码。

提供PHP及ThinkPHP框架的定制开发、代码优化,PHP修改、ThinkPHP修改。

邮箱:yvsm@163.com 微信:yvsm316 QQ:316430983
关闭

用微信“扫一扫”