|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create a Go web app' |
| 3 | +description: Deploy your first Go (GoLang) Hello World to Azure App Service in minutes. |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 10/13/2022 |
| 6 | +ms.devlang: go |
| 7 | +ms.author: msangapu |
| 8 | +author: msangapu-msft |
| 9 | +--- |
| 10 | + |
| 11 | +# Deploy a Go web app to Azure App Service |
| 12 | + |
| 13 | +> [!IMPORTANT] |
| 14 | +> Go on App Service on Linux is _experimental_. |
| 15 | +> |
| 16 | +
|
| 17 | +In this quickstart, you'll deploy a Go web app to Azure App Service. Azure App Service is a fully managed web hosting service that supports Go 1.18 and higher apps hosted in a Linux server environment. |
| 18 | + |
| 19 | +To complete this quickstart, you need: |
| 20 | + |
| 21 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs). |
| 22 | +- [Go 1.18](https://go.dev/dl/) or higher installed locally. |
| 23 | + |
| 24 | +## 1 - Sample Application |
| 25 | + |
| 26 | +First, create a folder for your project. |
| 27 | + |
| 28 | +Go to the terminal window, change into the folder you created and run `go mod init <ModuleName>`. The ModuleName could just be the folder name at this point. |
| 29 | + |
| 30 | +The `go mod init` command creates a go.mod file to track your code's dependencies. So far, the file includes only the name of your module and the Go version your code supports. But as you add dependencies, the go.mod file will list the versions your code depends on. |
| 31 | + |
| 32 | +Create a file called main.go. We'll be doing most of our coding here. |
| 33 | + |
| 34 | +```go |
| 35 | +package main |
| 36 | +import ( |
| 37 | + "fmt" |
| 38 | + "net/http" |
| 39 | +) |
| 40 | +func main() { |
| 41 | + http.HandleFunc("/", HelloServer) |
| 42 | + http.ListenAndServe(":8080", nil) |
| 43 | +} |
| 44 | +func HelloServer(w http.ResponseWriter, r *http.Request) { |
| 45 | + fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +This program uses the `net.http` package to handle all requests to the web root with the HelloServer function. The call to `http.ListenAndServe` tells the server to listen on the TCP network address `:8080`. |
| 50 | + |
| 51 | +Using a terminal, go to your project’s directory and run `go run main.go`. Now open a browser window and type the URL `http://localhost:8080/world`. You should see the message `Hello, world!`. |
| 52 | + |
| 53 | +## 2 - Create a web app in Azure |
| 54 | + |
| 55 | +To host your application in Azure, you need to create Azure App Service web app in Azure. You can create a web app using the Azure CLI. |
| 56 | + |
| 57 | +Azure CLI commands can be run on a computer with the [Azure CLI installed](/cli/azure/install-azure-cli). |
| 58 | + |
| 59 | +Azure CLI has a command `az webapp up` that will create the necessary resources and deploy your application in a single step. |
| 60 | + |
| 61 | +If necessary, log in to Azure using [az login](/cli/azure/authenticate-azure-cli). |
| 62 | + |
| 63 | +```azurecli |
| 64 | +az login |
| 65 | +``` |
| 66 | + |
| 67 | +Create the webapp and other resources, then deploy your code to Azure using [az webapp up](/cli/azure/webapp#az-webapp-up). |
| 68 | + |
| 69 | +```azurecli |
| 70 | +az webapp up --runtime GO:1.18 --sku B1 |
| 71 | +``` |
| 72 | + |
| 73 | +* The `--runtime` parameter specifies what version of Go your app is running. This example uses Go 1.18. To list all available runtimes, use the command `az webapp list-runtimes --os linux --output table`. |
| 74 | +* The `--sku` parameter defines the size (CPU, memory) and cost of the app service plan. This example uses the B1 (Basic) service plan, which will incur a small cost in your Azure subscription. For a full list of App Service plans, view the [App Service pricing](https://azure.microsoft.com/pricing/details/app-service/linux/) page. |
| 75 | +* You can optionally specify a name with the argument `--name <app-name>`. If you don't provide one, then a name will be automatically generated. |
| 76 | +* You can optionally include the argument `--location <location-name>` where `<location_name>` is an available Azure region. You can retrieve a list of allowable regions for your Azure account by running the [`az account list-locations`](/cli/azure/appservice#az-appservice-list-locations) command. |
| 77 | + |
| 78 | +The command may take a few minutes to complete. While the command is running, it provides messages about creating the resource group, the App Service plan, and the app resource, configuring logging, and doing ZIP deployment. It then gives the message, "You can launch the app at http://<app-name>.azurewebsites.net", which is the app's URL on Azure. |
| 79 | + |
| 80 | +<pre> |
| 81 | +The webapp '<app-name>' doesn't exist |
| 82 | +Creating Resource group '<group-name>' ... |
| 83 | +Resource group creation complete |
| 84 | +Creating AppServicePlan '<app-service-plan-name>' ... |
| 85 | +Creating webapp '<app-name>' ... |
| 86 | +Creating zip with contents of dir /home/tulika/myGoApp ... |
| 87 | +Getting scm site credentials for zip deployment |
| 88 | +Starting zip deployment. This operation can take a while to complete ... |
| 89 | +Deployment endpoint responded with status code 202 |
| 90 | +You can launch the app at http://<app-name>.azurewebsites.net |
| 91 | +{ |
| 92 | + "URL": "http://<app-name>.azurewebsites.net", |
| 93 | + "appserviceplan": "<app-service-plan-name>", |
| 94 | + "location": "centralus", |
| 95 | + "name": "<app-name>", |
| 96 | + "os": "<os-type>", |
| 97 | + "resourcegroup": "<group-name>", |
| 98 | + "runtime_version": "go|1.18", |
| 99 | + "runtime_version_detected": "0.0", |
| 100 | + "sku": "FREE", |
| 101 | + "src_path": "<your-folder-location>" |
| 102 | +} |
| 103 | +</pre> |
| 104 | + |
| 105 | +[!include [az webapp up command note](../../includes/app-service-web-az-webapp-up-note.md)] |
| 106 | + |
| 107 | +## 3 - Browse to the app |
| 108 | + |
| 109 | +Browse to the deployed application in your web browser at the URL `http://<app-name>.azurewebsites.net`. If you see a default app page, wait a minute and refresh the browser. |
| 110 | + |
| 111 | +The Go sample code is running a Linux container in App Service using a built-in image. |
| 112 | + |
| 113 | +**Congratulations!** You've deployed your Go app to App Service. |
| 114 | + |
| 115 | +## 4 - Clean up resources |
| 116 | + |
| 117 | +When no longer needed, you can use the [az group delete](/cli/azure/group#az-group-delete) command to remove the resource group, and all related resources: |
| 118 | + |
| 119 | +```azurecli-interactive |
| 120 | +az group delete --resource-group <resource-group-name> |
| 121 | +``` |
| 122 | +## Next steps |
| 123 | + |
| 124 | +> [!div class="nextstepaction"] |
| 125 | +> [Configure an App Service app](./configure-common.md) |
| 126 | +
|
| 127 | +> [!div class="nextstepaction"] |
| 128 | +> [Tutorial: Deploy from Azure Container Registry](./tutorial-custom-container.md) |
| 129 | +
|
| 130 | +> [!div class="nextstepaction"] |
| 131 | +> [Tutorial: Map a custom domain name](./app-service-web-tutorial-custom-domain.md) |
0 commit comments