Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 如何在使用围棋获得方向时使用上下文?_Go_Google Cloud Platform_Google App Engine Golang - Fatal编程技术网

Go 如何在使用围棋获得方向时使用上下文?

Go 如何在使用围棋获得方向时使用上下文?,go,google-cloud-platform,google-app-engine-golang,Go,Google Cloud Platform,Google App Engine Golang,我使用以下代码从谷歌云获取方向: import ( "google.golang.org/appengine" "google.golang.org/appengine/urlfetch" "fmt" "io/ioutil" "net/http" ) const directionAPIKey = "APIKey" const directionURL = "https://maps.googleapis.com/maps/api/directions/

我使用以下代码从谷歌云获取方向:

import (
    "google.golang.org/appengine"
    "google.golang.org/appengine/urlfetch"
    "fmt"
    "io/ioutil"
    "net/http"
)

const directionAPIKey = "APIKey"
const directionURL = "https://maps.googleapis.com/maps/api/directions/json?origin=%s&destination=%s&mode=%s&key=%s"

func main() {
    http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)
    direction, err := fetchDirection(ctx, r.FormValue("origin"), r.FormValue("destination"), r.FormValue("mode"))
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    w.Header().Add("Content-Type", "application/json; charset=utf-8")
    w.Write(direction)
}

func fetchDirection(ctx appengine.Context, origin string, destination string, mode string) ([]byte, error) {
    client := urlfetch.Client(ctx)
    resp, err := client.Get(fmt.Sprintf(directionURL, origin, destination, mode, directionAPIKey))
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    return ioutil.ReadAll(resp.Body)
}
但我有一个错误:

未定义:appengine.Context

尝试部署应用程序时。我尝试的是改变:

ctx := appengine.NewContext(r)
进入

进入

但我得到:

undefined: Context
我完全迷路了。我是新来Go和GCP的,所以请耐心等待我。如果您查看,您将看到它的链接,谢谢。这反过来告诉您“从Go 1.7开始,此软件包在标准库中的名称上下文下可用…”

因此,添加一个导入:

import "context"
并称之为:

func fetchDirection(ctx context.Context, origin string...)

你好,aviraldg,谢谢你花时间回答我的问题。我照你说的做了,现在应用程序确实部署得很好,但是,结果是,我没有得到JSON文件的实际方向,我只得到了显示。您对此有什么建议吗?您可能需要修复main()函数,如下所示:
undefined: Context
import "context"
func fetchDirection(ctx context.Context, origin string...)