Go 1.11 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Go 1.11 應用程式,即使貴機構先前使用機構政策重新啟用舊版執行階段的部署作業也一樣。現有的 Go 1.11 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
遷移至最新支援的 Go 版本。
Go 工作佇列範例
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本範例會建立一個能顯示 HTML 表單的應用程式。在對話方塊中輸入字串,然後按一下 Add
。應用程式會計算您以這種方式輸入任何字串的次數。
應用程式會執行下列程序:
- 當您按一下
Add
時,表單會使用 HTTP POST
要求,將字串傳送至在 App Engine 上執行的應用程式。此時,應用程式會將字串與工作綁定成套件,再傳送至預設佇列
。
- 佇列會將工作轉送至對應至網址
/worker
的內含工作處理常式,該常式會將字串非同步寫入資料儲存庫。
- 傳送 HTTP
GET
要求會顯示您輸入的字串清單,以及您Add
每個字串的次數 (透過輸入或點選下拉式方塊中的字串)。
將此應用程式部署至 App Engine:
將下列內容複製到名為 queue.yaml
的檔案。這會將工作處理率從每秒 5 件的預設值變更為每秒 3 件。
queue:
- name: default
rate: 3/s
在同一個目錄中,將下列內容複製到您喜歡的檔案名稱 (結尾為 .go
)。這是應用程式程式碼,包括工作處理常式。
在同一個目錄中,將下列內容複製到名為 app.yaml
的檔案。隨即完成在 App Engine 中使用該應用程式的設定:
請確認您的 Google Cloud Platform 專案確實已備妥 App Engine 應用程式,您也已經初始化並設定該專案的 gcloud
指令。
使用 gcloud app deploy
指令將應用程式部署至 App Engine。
使用 gcloud app browse
指令查看應用程式執行情形。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eThis application counts and displays the number of times a string is entered via an HTML form.\u003c/p\u003e\n"],["\u003cp\u003eWhen a string is added, it's sent to App Engine via an HTTP POST request, bundled into a task, and added to the default queue.\u003c/p\u003e\n"],["\u003cp\u003eThe task queue forwards the task to a worker handler, which then writes the string to a datastore asynchronously.\u003c/p\u003e\n"],["\u003cp\u003eAn HTTP GET request retrieves and displays a list of the entered strings and their corresponding counts.\u003c/p\u003e\n"],["\u003cp\u003eThe application can be deployed to App Engine using specific configuration files and the \u003ccode\u003egcloud\u003c/code\u003e command-line tool.\u003c/p\u003e\n"]]],[],null,["# A Go Task Queue Example\n\nThis example creates an app that displays an HTML form. You enter\na string into the dialog box and click `Add`. The app counts the number of times\nthat you enter any string in this way.\n\nThe app does these things:\n\n- When you click `Add`, the form uses an HTTP `POST`request to send the string to the app which is running on App Engine. There the app bundles the string into a task and sends it to the default queue.\n- The queue forwards the task to an included task handler, mapped to the URL `/worker`, which asynchronously writes the string to a datastore.\n- Sending an HTTP `GET` request displays a list of the strings you have entered and the number of times you have`Add`ed each string, either by typing it or by clicking on it in the dropdown box.\n\nTo deploy this app to App Engine:\n\n1. Copy the following into a file named `queue.yaml`. This changes the rate at\n which tasks will be processed from the default 5 per second to 3 per second.\n\n queue:\n - name: default\n rate: 3/s\n\n2. In the same directory, copy the following into a file named as you like (ending in `.go`). This is the application code, including the task handler.\n\n\n package counter\n\n import (\n \t\"html/template\"\n \t\"net/http\"\n\n \t\"google.golang.org/appengine\"\n \t\"google.golang.org/appengine/datastore\"\n \t\"google.golang.org/appengine/log\"\n \t\"google.golang.org/appengine/taskqueue\"\n )\n\n func init() {\n \thttp.HandleFunc(\"/\", handler)\n \thttp.HandleFunc(\"/worker\", worker)\n }\n\n type Counter struct {\n \tName string\n \tCount int\n }\n\n func handler(w http.ResponseWriter, r *http.Request) {\n \tctx := appengine.NewContext(r)\n \tif name := r.FormValue(\"name\"); name != \"\" {\n \t\tt := taskqueue.NewPOSTTask(\"/worker\", map[string][]string{\"name\": {name}})\n \t\tif _, err := taskqueue.Add(ctx, t, \"\"); err != nil {\n \t\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n \t\t\treturn\n \t\t}\n \t}\n \tq := datastore.NewQuery(\"Counter\")\n \tvar counters []Counter\n \tif _, err := q.GetAll(ctx, &counters); err != nil {\n \t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n \t\treturn\n \t}\n \tif err := handlerTemplate.Execute(w, counters); err != nil {\n \t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n \t\treturn\n \t}\n \t// OK\n }\n\n func worker(w http.ResponseWriter, r *http.Request) {\n \tctx := appengine.NewContext(r)\n \tname := r.FormValue(\"name\")\n \tkey := datastore.NewKey(ctx, \"Counter\", name, 0, nil)\n \tvar counter Counter\n \tif err := datastore.https://cloud.google.com/appengine/docs/legacy/standard/go111/reference/latest/datastore.html#google_golang_org_appengine_datastore_Get(ctx, key, &counter); err == datastore.https://cloud.google.com/appengine/docs/legacy/standard/go111/reference/latest/datastore.html#google_golang_org_appengine_datastore_ErrInvalidEntityType_ErrInvalidKey_ErrNoSuchEntity {\n \t\tcounter.Name = name\n \t} else if err != nil {\n \t\tlog.https://cloud.google.com/appengine/docs/legacy/standard/go111/reference/latest/log.html#google_golang_org_appengine_log_Errorf(ctx, \"%v\", err)\n \t\treturn\n \t}\n \tcounter.Count++\n \tif _, err := datastore.Put(ctx, key, &counter); err != nil {\n \t\tlog.https://cloud.google.com/appengine/docs/legacy/standard/go111/reference/latest/log.html#google_golang_org_appengine_log_Errorf(ctx, \"%v\", err)\n \t}\n }\n\n var handlerTemplate = template.Must(template.New(\"handler\").Parse(handlerHTML))\n\n const handlerHTML = `\n {{range .}}\n \u003cp\u003e{{.Name}}: {{.Count}}\u003c/p\u003e\n {{end}}\n \u003cp\u003eStart a new counter:\u003c/p\u003e\n \u003cform action=\"/\" method=\"POST\"\u003e\n \u003cinput type=\"text\" name=\"name\"\u003e\n \u003cinput type=\"submit\" value=\"Add\"\u003e\n \u003c/form\u003e\n `\n\n3. In the same directory, copy the following into a file named `app.yaml`. This configures your\n application for App Engine:\n\n runtime: go\n api_version: go1\n\n handlers:\n - url: /worker/.*\n script: _go_app\n login: admin\n - url: /.*\n script: _go_app\n\n4. Make sure you have a [Google Cloud Platform project with an App Engine app](/appengine/docs/legacy/standard/go111/console)\n prepared and that you have [initialized](/sdk/docs/initializing) and\n configured the `gcloud` command for that project.\n\n5. Use the `gcloud app deploy` command to deploy the app to App Engine.\n\n6. See the app in action by using the `gcloud app browse` command."]]