注意:Go 1.11 已於 2024 年 1 月 30 日
停止支援。現有的 Go 1.11 應用程式將繼續執行並接收流量。不過,App Engine 可能會阻擋
支援期限過後使用執行階段的應用程式重新部署。建議您
改用最新的 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-06-16 (世界標準時間)。
[[["容易理解","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-06-16 (世界標準時間)。"],[[["This application counts and displays the number of times a string is entered via an HTML form."],["When 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."],["The task queue forwards the task to a worker handler, which then writes the string to a datastore asynchronously."],["An HTTP GET request retrieves and displays a list of the entered strings and their corresponding counts."],["The application can be deployed to App Engine using specific configuration files and the `gcloud` command-line tool."]]],[]]