Skip to main content

Prerequisites

  • A Zerops account
  • Basic knowledge of Go
  • Go installed locally (for development)

Deploy from Recipe

The fastest way to get started is to deploy our ready-made Go example:
1

Import the Project

Log in to Zerops GUI and click Import a project.
2

Paste the Configuration

Use this YAML configuration:
project:
  name: my-first-go-app
services:
  - hostname: app
    type: go@1.22
    minContainers: 1
    maxContainers: 3
    buildFromGit: https://github.com/zeropsio/recipe-go-hello-world@main
    enableSubdomainAccess: true
3

Wait for Deployment

Click Import project and wait for the build and deployment to complete.
4

Access Your App

Once deployed, find your subdomain URL in the IP Addresses & Public Routing Overview.Visit the URL to see “Hello, World!”

Deploy Your Own Go Application

Step 1: Create Your Application

Create a simple HTTP server:
main.go
package main

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

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello from Go on Zerops!")
    })
    
    http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        fmt.Fprintf(w, "OK")
    })
    
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }
    
    log.Printf("Server starting on port %s", port)
    if err := http.ListenAndServe(":"+port, nil); err != nil {
        log.Fatal(err)
    }
}
Initialize Go modules:
go mod init myapp
go mod tidy

Step 2: Add zerops.yaml

Create a zerops.yaml file in your repository root:
zerops:
  - setup: app
    build:
      base: go@1.22
      buildCommands:
        - go mod download
        - go build -o app .
      deployFiles:
        - app
      cache: ~/go/pkg/mod
    
    run:
      start: ./app
      ports:
        - port: 8080
          httpSupport: true
      envVariables:
        PORT: "8080"

Step 3: Configure Your Service

Create a description.yaml:
project:
  name: my-go-app
services:
  - hostname: app
    type: go@1.22
    minContainers: 1
    maxContainers: 6
Import the project:
zcli project project-import description.yaml

Step 4: Deploy Your Code

Deploy using zCLI:
zcli service deploy

Framework Examples

Gin Framework

Install Gin:
go get -u github.com/gin-gonic/gin
Create your app:
main.go
package main

import (
    "github.com/gin-gonic/gin"
    "net/http"
    "os"
)

func main() {
    r := gin.Default()
    
    r.GET("/", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
            "message": "Hello from Gin on Zerops!",
        })
    })
    
    r.GET("/health", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{"status": "healthy"})
    })
    
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }
    r.Run(":" + port)
}
zerops.yaml
zerops:
  - setup: api
    build:
      base: go@1.22
      buildCommands:
        - go mod download
        - CGO_ENABLED=0 go build -ldflags="-s -w" -o app .
      deployFiles:
        - app
    
    run:
      start: ./app
      ports:
        - port: 8080
      envVariables:
        GIN_MODE: release

Echo Framework

go get github.com/labstack/echo/v4
main.go
package main

import (
    "github.com/labstack/echo/v4"
    "github.com/labstack/echo/v4/middleware"
    "net/http"
)

func main() {
    e := echo.New()
    
    e.Use(middleware.Logger())
    e.Use(middleware.Recover())
    
    e.GET("/", func(c echo.Context) error {
        return c.JSON(http.StatusOK, map[string]string{
            "message": "Hello from Echo on Zerops!",
        })
    })
    
    e.GET("/health", func(c echo.Context) error {
        return c.JSON(http.StatusOK, map[string]string{
            "status": "healthy",
        })
    })
    
    e.Start(":8080")
}

Fiber Framework

go get github.com/gofiber/fiber/v2
main.go
package main

import (
    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/logger"
)

func main() {
    app := fiber.New()
    
    app.Use(logger.New())
    
    app.Get("/", func(c *fiber.Ctx) error {
        return c.JSON(fiber.Map{
            "message": "Hello from Fiber on Zerops!",
        })
    })
    
    app.Get("/health", func(c *fiber.Ctx) error {
        return c.JSON(fiber.Map{"status": "healthy"})
    })
    
    app.Listen(":8080")
}

Adding a Database

Extend your project with PostgreSQL:
description.yaml
project:
  name: my-go-app
services:
  - hostname: app
    type: go@1.22
    minContainers: 1
    maxContainers: 6
  
  - hostname: db
    type: postgresql@16
    mode: NON_HA
Connect using GORM:
go get -u gorm.io/gorm
go get -u gorm.io/driver/postgres
package main

import (
    "fmt"
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
    "os"
)

func main() {
    dsn := fmt.Sprintf(
        "host=%s user=%s password=%s dbname=%s port=5432 sslmode=disable",
        "db",
        os.Getenv("DB_USER"),
        os.Getenv("DB_PASSWORD"),
        os.Getenv("DB_NAME"),
    )
    
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
    if err != nil {
        panic("failed to connect database")
    }
    
    // Your database operations here
}

Environment Variables

Set environment variables in zerops.yaml:
run:
  envVariables:
    APP_ENV: production
    LOG_LEVEL: info
    PORT: "8080"
Access in your code:
import "os"

func main() {
    env := os.Getenv("APP_ENV")
    logLevel := os.Getenv("LOG_LEVEL")
}

Build Optimizations

Create Smaller Binaries

Use build flags to reduce binary size:
build:
  buildCommands:
    - go mod download
    - CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o app .

Cache Go Modules

Speed up builds by caching dependencies:
build:
  cache: ~/go/pkg/mod

Multi-Stage Builds

For complex applications with multiple binaries:
build:
  buildCommands:
    - go mod download
    - go build -o api cmd/api/main.go
    - go build -o worker cmd/worker/main.go
  deployFiles:
    - api
    - worker
    - config

Troubleshooting

Ensure go.mod and go.sum are committed:
go mod tidy
git add go.mod go.sum
git commit -m "Update dependencies"
Check that your start command and port configuration match:
run:
  start: ./app
  ports:
    - port: 8080
Make sure you’re deploying the compiled binary:
build:
  buildCommands:
    - go build -o app .  # Creates 'app' binary
  deployFiles:
    - app  # Deploys the 'app' binary
For CGO dependencies, install build tools:
build:
  os: ubuntu
  prepareCommands:
    - sudo apt-get update
    - sudo apt-get install -y gcc g++ make
  buildCommands:
    - CGO_ENABLED=1 go build -o app .

Next Steps

Runtime Overview

Learn about Go runtime features

Environment Variables

Manage secrets and configuration

Scaling

Configure auto-scaling

Monitoring

Monitor your application

Build docs developers (and LLMs) love