> ## Documentation Index
> Fetch the complete documentation index at: https://docs.valyent.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Go

> Learn how to deploy a Go web application to Valyent

## Introduction

Go is a statically typed, compiled programming language designed at Google. It provides excellent support for building high-performance web applications with its robust standard library and built-in concurrency features.

## Prerequisites

* a UNIX-like system (see [WSL](https://learn.microsoft.com/en-us/windows/wsl/install) if you are using Windows), in order to use the [Valyent CLI](https://docs.valyent.cloud/cli)
* a Valyent account, with a registered payment method (you can sign up [here](https://console.valyent.cloud/auth/sign-up))
* [Go](https://go.dev/doc/install) installed on your machine (version 1.21 or later)

## Installing the CLI

```bash theme={"system"}
curl -L https://cli.valyent.cloud | sh
```

You can inspect the installation script [here](https://cli.valyent.cloud) and the [GitHub repository](https://github.com/valyentdev/cli).

## Authenticating

```bash theme={"system"}
valyent auth login
```

## Initializing the project

First, let's create a new directory for our project and initialize it as a Go module:

```bash theme={"system"}
mkdir go-webapp && cd go-webapp
go mod init go-webapp
```

Create the main application file:

```bash theme={"system"}
touch main.go
```

In the `main.go` file, add the following code:

```go theme={"system"}
package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
)

func main() {
    mux := http.NewServeMux()
    
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        if r.URL.Path != "/" {
            http.NotFound(w, r)
            return
        }
        
        w.Header().Set("Content-Type", "text/html; charset=utf-8")
        w.WriteHeader(http.StatusOK)
        fmt.Fprint(w, "Welcome to the Go Web App!")
    })

    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }
    addr := ":" + port

    log.Printf("Server starting on port %s", port)
    if err := http.ListenAndServe(addr, mux); err != nil {
        log.Fatalf("Server failed to start: %v", err)
    }
}
```

Now, initialize the `valyent.toml` configuration file:

```bash theme={"system"}
valyent init
```

This command will guide you through selecting or creating a project and an associated application.

## Adding the Dockerfile

Create a Dockerfile for the application:

```bash theme={"system"}
touch Dockerfile
```

Add the following content to the Dockerfile:

```dockerfile theme={"system"}
# Stage 1: Builder
FROM golang:1.21 AS builder

WORKDIR /app

# Copy module files
COPY go.mod ./

# Copy the source code
COPY . .

# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -o webapp .

# Stage 2: Runtime
FROM alpine:latest

WORKDIR /app

# Copy the binary from builder
COPY --from=builder /app/webapp .

# Expose the application port
EXPOSE 8080

# Run the binary
ENTRYPOINT ["/app/webapp"]
```

## Deploy the project

To expose the application’s port, we need to set up a gateway (in this case, the port 8080):

```bash theme={"system"}
valyent gateways create
```

Deploy your application to Valyent:

```bash theme={"system"}
valyent deploy
```

Then, you can type the following:

```bash theme={"system"}
valyent open
```
