# Create and deploy an HTTP Cloud Run function
by using Go (1st gen)

This guide takes you through the process of writing a Cloud Run function
using the Go runtime. There are two types of Cloud Run functions:

- An HTTP function, which you invoke from standard HTTP requests.
- An event-driven function, which you use to handle events from your Cloud infrastructure, such as messages on a Pub/Sub topic, or changes in a Cloud Storage bucket.

The sample shows how to create a simple HTTP function.

> [!IMPORTANT]
> **Learn more** : For more details, read about [HTTP functions](https://docs.cloud.google.com/functions/1stgendocs/writing/write-http-functions.md.txt) and [event-driven functions](https://docs.cloud.google.com/functions/1stgendocs/writing/write-event-driven-functions.md.txt).

## Before you begin

1. Prepare your development environment.
   [Go to the Go setup guide](https://docs.cloud.google.com/go/docs/setup.md.txt)

<br />

## Create a function

1. Create a directory on your local system for the function code:

   ### Linux or Mac OS X

       mkdir ~/helloworld
       cd ~/helloworld

   ### Windows

       mkdir %HOMEPATH%\helloworld
       cd %HOMEPATH%\helloworld

2. Create a file called `hello_http.go` in the `helloworld`
   directory with the following contents:

   ```go

   // Package helloworld provides a set of Cloud Functions samples.
   package helloworld

   import (
   	"encoding/json"
   	"fmt"
   	"html"
   	"net/http"

   	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
   )

   func init() {
   	functions.HTTP("HelloHTTP", HelloHTTP)
   }

   // HelloHTTP is an HTTP Cloud Function with a request parameter.
   func HelloHTTP(w http.ResponseWriter, r *http.Request) {
   	var d struct {
   		Name string `json:"name"`
   	}
   	if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
   		fmt.Fprint(w, "Hello, World!")
   		return
   	}
   	if d.Name == "" {
   		fmt.Fprint(w, "Hello, World!")
   		return
   	}
   	fmt.Fprintf(w, "Hello, %s!", html.EscapeString(d.Name))
   }
   ```

   This example function takes a name supplied in the HTTP request and returns
   a greeting, or "Hello, World!" when no name is supplied.

## Specify dependencies

This example function only uses Go standard library packages, so you don't need
to declare any dependencies beyond just importing the packages.

For functions that require dependencies outside of the standard library,
you must provide the dependencies via either a `go.mod` file or a `vendor`
directory. For more details, read
[Specifying dependencies in Go](https://docs.cloud.google.com/functions/1stgendocs/writing/specifying-dependencies-go.md.txt).

## Deploy the function

To deploy the function with an HTTP trigger, run the following
command in the `helloworld` directory, specifying either `go113` or
`go111` as the value for the `--runtime` flag, depending on which version you
are using:

```sh
gcloud functions deploy HelloHTTP --no-gen2 --runtime go121 --trigger-http --allow-unauthenticated
```

> [!NOTE]
> **Note:** Cloud Run functions looks for deployable functions in your terminal's current directory by default. Use the [`--source`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--source) flag when [deploying your function via `gcloud`](https://docs.cloud.google.com/functions/1stgendocs/create-deploy-http-go-1st-gen.md.txt#deploying_the_function) to specify a different function source directory.

The `--allow-unauthenticated` flag lets you reach the function
[without authentication](https://docs.cloud.google.com/functions/1stgendocs/securing/managing-access-iam.md.txt#allowing_unauthenticated_http_function_invocation).
To require
[authentication](https://docs.cloud.google.com/functions/1stgendocs/securing/authenticating.md.txt#developers), omit the
flag.

> [!IMPORTANT]
> **Learn more** : For more details, read about [deploying Cloud Run functions](https://docs.cloud.google.com/functions/1stgendocs/deploy.md.txt#basics).

## Test the function

1. When the function finishes deploying, take note of the `httpsTrigger.url`
   property or find it using the following command:

       gcloud functions describe HelloHTTP

   It should look like this:

   ```
   https://GCP_REGION-PROJECT_ID.cloudfunctions.net/HelloHTTP
   ```
2. Visit this URL in your browser, or use cURL by running the command:

   ```sh
   curl https://GCP_REGION-PROJECT_ID.cloudfunctions.net/HelloHTTP
   ```

   You should see a "Hello, World!" message. Try passing a name
   in the HTTP request by running the following command:

   ```sh
   curl -X POST https://GCP_REGION-PROJECT_ID.cloudfunctions.net/HelloHTTP -H "Content-Type:application/json"  -d '{"name":"NAME"}'
   ```

   You should see the message "Hello, `NAME`!"

## View logs

Logs for Cloud Run functions are viewable using the Google Cloud CLI, and in the
Cloud Logging UI.

### Use the command-line tool

To view logs for your function with the gcloud CLI, use the
[`logs read`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/logs/read) command, followed by
the name of the function:

```bash
gcloud functions logs read HelloHTTP
```

The output should resemble the following:

```bash
LEVEL  NAME        EXECUTION_ID  TIME_UTC                 LOG
D      HelloHTTP  buv9ej2k1a7r  2019-09-20 13:23:18.910  Function execution started
D      HelloHTTP  buv9ej2k1a7r  2019-09-20 13:23:18.913  Function execution took 4 ms, finished with status code: 200
```

> [!NOTE]
> **Note:** There is typically a slight delay between when log entries are created and when they show up in Cloud Logging.

### Use the Logging dashboard

You can also view logs for Cloud Run functions from the
[Google Cloud console](https://console.cloud.google.com/project/_/logs?service=cloudfunctions.googleapis.com).

> [!IMPORTANT]
> **Learn more** : For more details, read about [writing, viewing, and responding to logs](https://docs.cloud.google.com/functions/1stgendocs/monitoring/logging.md.txt).