Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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
Google app engine appengine的跨平台go代码_Google App Engine_Go - Fatal编程技术网

Google app engine appengine的跨平台go代码

Google app engine appengine的跨平台go代码,google-app-engine,go,Google App Engine,Go,创建FetchUrl/GetURL函数的合适方法是什么?该函数可以从命令行运行,也可以从googleappengine使用其自定义方式获取url 我有获取和处理URL上的一些数据的基本代码。我希望能够从我在桌面上使用的代码和部署到AppEngine的代码中调用它 希望这是清楚的,如果没有请让我知道,我会澄清。你可以从项目本身中得到一些线索 例如,its提供了远程连接到用户生产应用程序的客户端。如果您有一些代码可以在本地计算机和AppEngine环境中运行,那么您就无需做任何事情 如果您需要在Ap

创建FetchUrl/GetURL函数的合适方法是什么?该函数可以从命令行运行,也可以从googleappengine使用其自定义方式获取url

我有获取和处理URL上的一些数据的基本代码。我希望能够从我在桌面上使用的代码和部署到AppEngine的代码中调用它


希望这是清楚的,如果没有请让我知道,我会澄清。

你可以从项目本身中得到一些线索


例如,its提供了远程连接到用户生产应用程序的客户端。

如果您有一些代码可以在本地计算机和AppEngine环境中运行,那么您就无需做任何事情

如果您需要在AppEngine上执行一些应该或必须以不同方式执行的操作,那么您需要检测环境并为不同的环境编写不同的代码

这种检测和代码选择最容易使用。您可以在
.go
文件的开头放置一个特殊的注释行,根据环境的不同,它可能会被编译并运行,也可能不会被编译和运行

引述自:

appenginesdk引入了一个新的构建约束术语:
“appengine”
。指定

// +build appengine
// +build !appengine
将由App Engine SDK生成,并被go工具忽略。相反,指定

// +build appengine
// +build !appengine
appenginesdk忽略了它们,而go工具将愉快地构建它们

例如,您可以有两个独立的
.go
文件,一个用于AppEngine,一个用于本地(非AppEngine)环境。在两者中定义相同的函数(使用相同的参数列表),因此无论代码构建在哪个环境中,函数都将有一个声明。我们将使用此签名:

func GetURL(url string, r *http.Request) ([]byte, error)
请注意,第二个参数(
*http.Request
)仅对AppEngine是必需的(以便能够创建
上下文
),因此在本地环境的实现中不使用它(甚至可以是
nil

优雅的解决方案可以利用在标准环境和AppEngine中都可用的类型,并可用于执行HTTP GET请求。可以在AppEngine上以不同的方式获取
http.Client
值,但是一旦我们获得了
http.Client
值,我们就可以以相同的方式进行操作。因此,我们将有一个公共代码来接收
http.Client
,并可以完成其余的工作

示例实现可以如下所示:

url\u local.go

// +build !appengine

package mypackage

import (
    "net/http"
)

func GetURL(url string, r *http.Request) ([]byte, error) {
    // Local GetURL implementation
    return GetClient(url, &http.Client{})
}
// +build appengine

package mypackage

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

func GetURL(url string, r *http.Request) ([]byte, error) {
    // Appengine GetURL implementation
    ctx := appengine.NewContext(r)
    c := urlfetch.Client(ctx)
    return GetClient(url, c)
}
// No build constraint: this is common code

package mypackage

import (
    "net/http"
)

func GetClient(url string, c *http.Client) ([]byte, error) {
    // Implementation for both local and AppEngine
    resp, err := c.Get(url)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }
    return body, nil
}
url\u gae.go

// +build !appengine

package mypackage

import (
    "net/http"
)

func GetURL(url string, r *http.Request) ([]byte, error) {
    // Local GetURL implementation
    return GetClient(url, &http.Client{})
}
// +build appengine

package mypackage

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

func GetURL(url string, r *http.Request) ([]byte, error) {
    // Appengine GetURL implementation
    ctx := appengine.NewContext(r)
    c := urlfetch.Client(ctx)
    return GetClient(url, c)
}
// No build constraint: this is common code

package mypackage

import (
    "net/http"
)

func GetClient(url string, c *http.Client) ([]byte, error) {
    // Implementation for both local and AppEngine
    resp, err := c.Get(url)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }
    return body, nil
}
url\u通用。转到

// +build !appengine

package mypackage

import (
    "net/http"
)

func GetURL(url string, r *http.Request) ([]byte, error) {
    // Local GetURL implementation
    return GetClient(url, &http.Client{})
}
// +build appengine

package mypackage

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

func GetURL(url string, r *http.Request) ([]byte, error) {
    // Appengine GetURL implementation
    ctx := appengine.NewContext(r)
    c := urlfetch.Client(ctx)
    return GetClient(url, c)
}
// No build constraint: this is common code

package mypackage

import (
    "net/http"
)

func GetClient(url string, c *http.Client) ([]byte, error) {
    // Implementation for both local and AppEngine
    resp, err := c.Get(url)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }
    return body, nil
}

你通过列举例子,超越了这个答案。我不知道使用
//+build
的能力。非常感谢。