Using Redirects in GAE

huangapple go评论141阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2015年10月12日 11:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/33072737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定