export const meta = {
  title: `Go API`,
  description: `How to deploy a Go API using Amplify Functions`,
};

In this guide, you will learn how to deploy a Go API.

## 1. Initialize a new Amplify project

```sh
amplify init

# Follow the steps to give the project a name, environment name, and set the default text editor.
# Accept defaults for everything else and choose your AWS Profile.
```

## 2. Add the API and function

```sh
amplify add api

? Please select from one of the below mentioned services: REST
? Provide a friendly name for your resource to be used as a label for this category in the project: goapi
? Provide a path (e.g., /book/{isbn}): /hello
? Choose a Lambda source: Create a new Lambda function
? Provide a friendly name for your resource to be used as a label for this category in the project: greetingfunction
? Provide the AWS Lambda function name: greetingfunction
? Choose the function runtime that you want to use: Go
? Do you want to access other resources created in this project from your Lambda function? N
? Do you want to invoke this function on a recurring schedule? N
? Do you want to edit the local lambda function now? N
? Restrict API access: N
? Do you want to add another path? N
```

The CLI should have created a new function located at **amplify/backend/function/greetingfunction**.

## 3. Updating the function code

Next, open  **amplify/backend/function/greetingfunction/src/main.go** and update the code to the following:

```go
package main

import (
	"bytes"
	"context"
	"encoding/json"
	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

type Response events.APIGatewayProxyResponse

func Handler(ctx context.Context) (Response, error) {
	var buf bytes.Buffer

	body, err := json.Marshal(map[string]interface{}{
		"message": "Congrats! Your function executed successfully!",
	})
	if err != nil {
		return Response{StatusCode: 404}, err
	}
	json.HTMLEscape(&buf, body)

	resp := Response{
		StatusCode:      200,
		IsBase64Encoded: false,
		Body:            buf.String(),
		Headers: map[string]string{
			"Content-Type": "application/json",
			"X-MyCompany-Func-Reply": "hello-handler",
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE",
			"Access-Control-Allow-Headers": "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization",
		},
	}

	return resp, nil
}

func main() {
	lambda.Start(Handler)
}
```

## 4. Deploy the API

To deploy the API, run the `push` command:

```sh
amplify push
```

## 5. Using the API

Here is how you can send a GET request to the API.

import js0 from "/src/fragments/guides/api-rest/js/go-api-call.mdx";

<Fragments fragments={{js: js0}} />

import ios1 from "/src/fragments/guides/api-rest/ios/rest-api-call.mdx";

<Fragments fragments={{ios: ios1}} />

import android2 from "/src/fragments/guides/api-rest/android/rest-api-call.mdx";

<Fragments fragments={{android: android2}} />

To learn more about interacting with REST APIs using Amplify, check out the complete documentation [here](/lib/restapi/getting-started).

The API endpoint is located in the `aws-exports.js` folder.

You can also interact directly with the API using this URL and the specified path:

```sh
curl https://<api-id>.execute-api.<api-region>.amazonaws.com/<your-env-name>/hello
```
