gpt4 book ai didi

go - 使用Go显示代码而不是请求GCP响应的代码

转载 作者:行者123 更新时间:2023-12-01 22:12:52 29 4
gpt4 key购买 nike

我很难在Google Cloud Platform中显示指示请求的结果。这是我尝试过的:

package main

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

const directionAPIKey = "MyAppKey"
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 context.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)
}
我已经成功部署了我的应用程序,但是,以上代码的结果是:
Get https://maps.googleapis.com/maps/api/directions/json?origin=&destination=&mode=&key=APIKey not an App Engine context
而不是请求的结果。这是来自浏览器的 image 。如何获得理想的结果,而不仅仅是该URL?提前致谢。

最佳答案

found,当您的AppEngine应用程序的运行时设置为go111并且该应用程序导入了任何“google.golang.org/appengine”包但该应用程序未调用“appengine.Main”

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

关于go - 使用Go显示代码而不是请求GCP响应的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62510522/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com