Go 1.11 ha raggiunto la fine del supporto
e verrà
ritirato
il 31 gennaio 2026. Dopo il ritiro, non potrai eseguire il deployment delle applicazioni Go 1.11, anche se la tua organizzazione ha utilizzato in precedenza un criterio dell'organizzazione per riattivare i deployment dei runtime legacy. Le tue applicazioni Go
1.11 continueranno a essere eseguite e a ricevere traffico dopo la
data di ritiro. Ti
consigliamo di
eseguire la migrazione all'ultima versione supportata di Go.
Esempio di una coda di attività Go
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questo esempio crea un'app che mostra un modulo HTML. Inserisci
una stringa nella finestra di dialogo e fai clic su Add
. L'app conta il numero di volte
in cui inserisci una stringa in questo modo.
L'app esegue le seguenti operazioni:
- Quando fai clic su
Add
, il modulo utilizza una richiesta HTTP POST
per inviare la stringa
all'app in esecuzione su App Engine. L'app raggruppa la stringa
in un'attività e la invia alla coda predefinita.
- La coda inoltra l'attività a un gestore di attività incluso, mappato all'URL
/worker
,
che scrive in modo asincrono la stringa in un datastore.
- L'invio di una richiesta HTTP
GET
mostra un elenco delle stringhe che hai inserito
e il numero di volte in cui haiAdd
ato ogni stringa, digitandola o
facendo clic su di essa nella casella a discesa.
Per eseguire il deployment di questa app in App Engine:
Copia quanto segue in un file denominato queue.yaml
. In questo modo, la velocità di elaborazione delle attività passerà da 5 al secondo (valore predefinito) a 3 al secondo.
queue:
- name: default
rate: 3/s
Nella stessa directory, copia il seguente codice in un file con il nome che preferisci (che termina con .go
). Questo è il codice dell'applicazione, incluso il gestore delle attività.
Nella stessa directory, copia il seguente codice in un file denominato app.yaml
. In questo modo configuri la tua
applicazione per App Engine:
Assicurati di avere un progetto Google Cloud con un'app App Engine
preparato e di aver inizializzato e
configurato il comando gcloud
per il progetto.
Utilizza il comando gcloud app deploy
per eseguire il deployment dell'app su App Engine.
Guarda l'app in azione utilizzando il comando gcloud app browse
.
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-09-04 UTC.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\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."]]