英文:
How to create a task for app engine task queue in Go?
问题
Google的文档省略了最重要的部分:如何创建一个任务。有人能完成这个示例代码吗:
import (
    "appengine/datastore"
    "appengine/taskqueue"
)
func f(c appengine.Context) {
    err := datastore.RunInTransaction(c, func(c appengine.Context) error {
        t := ... // 为什么Google没有解释这部分???
        // 在调用taskqueue.Add时使用事务的上下文。
        _, err := taskqueue.Add(c, t, "")
        // ...
    })
    // ...
}
英文:
Google's docs omit the most important aspect: How a Task is created. Can anybody complete the sample code:
import (
    "appengine/datastore"
    "appengine/taskqueue"
)
func f(c appengine.Context) {
    err := datastore.RunInTransaction(c, func(c appengine.Context) error {
        t := ... // WHY DOES GOOGLE NOT EXPLAIN THIS PART???
        // Use the transaction's context when invoking taskqueue.Add.
        _, err := taskqueue.Add(c, t, "")
        // ...
    })
    // ...
}
答案1
得分: 2
我认为你需要的内容在datastore transactions的文档中有描述。
创建任务的缺失代码如下:
t := &taskqueue.Task{Path: "/path/to/workertask"}
英文:
I think what you need is described in the docs for datastore transactions.
So the missing code to create a task is:
t := &taskqueue.Task{Path: "/path/to/workertask"}
答案2
得分: 1
Task类型的参考文档显示,Task是一个具有大约10个字段的结构体,所以你可能不想自己构造一个Task。然而,文档还提供了NewPOSTTask函数(就在上面提到的那个函数下面):
NewPOSTTask创建一个将使用给定表单数据进行POST请求的Task。
不过,我同意文档可能需要改进。
英文:
The reference for the Task type shows that Task is a struct with 10 or so fields, so you probably don't want to construct a Task yourself. However, it also provides the NewPOSTTask function (just below that):
> NewPOSTTask creates a Task that will POST to a path with the given form data
I agree the documentation could be much better though.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论