英文:
Using Redirects in GAE
问题
我对Go语言还比较新,正在尝试为GAE创建一些应用程序。我在网上找不到和我遇到的问题相同的人,所以想在这里问一下!
基本上,我正在创建一个程序,它会从一个数组中随机选择一个URL并将用户重定向到该URL。
问题是,无论我在哪台电脑上尝试,它都会发送不同的视频,但对于同一个人来说,它总是发送相同的视频。
如果有人对此有任何见解,我将非常感激!
package randvid
import (
"net/http"
"math/rand"
"time"
)
func RandLink() string{
VideoList := []string{
...
}
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
vid := r.Intn(len(VideoList)) + 1;
return VideoList[vid]
}
func redirectHandler(path string) func(http.ResponseWriter, *http.Request) {
return func (w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path, http.StatusMovedPermanently)
}
}
func init() {
http.HandleFunc("/", redirectHandler(RandLink()))
}
以上是你提供的代码。
英文:
I am fairly new to Go, and I am trying to make a few apps for GAE. I couldn't find anyone with the same problem as me so I thought I would ask here!
Basically, I am creating a program that will take a random url from an array and redirect the user.
The problem that arises is that on every computer I try it on, it will send a different video, but it will always send the same video to that person.
If anyone has any insight into this it would be greatly appreciated!
package randvid
import (
"net/http"
"math/rand"
"time"
)
func RandLink() string{
VideoList := []string{
...
}
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
vid := r.Intn(len(VideoList)) + 1;
return VideoList[vid]
}
func redirectHandler(path string) func(http.ResponseWriter, *http.Request) {
return func (w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path, http.StatusMovedPermanently)
}
}
func init() {
http.HandleFunc("/", redirectHandler(RandLink()))
}
答案1
得分: 3
你的客户端可能会缓存301响应,这是根据RFC 2616允许的。将响应状态码更改为302,即http.StatusFound。
英文:
Your clients are probably caching the 301 response as allowed by RFC 2616.
Change the response status code to 302, http.StatusFound.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论