Skip to Content

Deploy Go

1. Prepare Your Project

Ensure your app reads the PORT environment variable:

main.go
package main import ( "fmt" "net/http" "os" ) func main() { port := os.Getenv("PORT") if port == "" { port = "8080" } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello from Ghayma!") }) fmt.Printf("Server running on port %s\n", port) http.ListenAndServe(":"+port, nil) }

2. Initialize

cd my-go-app ghayma init
.ghayma.json
{ "name": "my-go-app", "framework": "go", "build_command": "go build -o app .", "start_command": "./app", "port": 8080 }

3. Deploy

ghayma deploy

With Gin Framework

package main import ( "os" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.GET("/", func(c *gin.Context) { c.JSON(200, gin.H{"message": "Hello from Ghayma!"}) }) port := os.Getenv("PORT") if port == "" { port = "8080" } r.Run(":" + port) }

Add a Database

# Connected to the 'main' site, so DATABASE_URL is derived for it — the # running app picks it up without a redeploy once the database accepts logins ghayma db create my-db --site main

Leave out --site and, on a terminal, the command asks instead.

import "os" dbURL := os.Getenv("DATABASE_URL") // Use with pgx, GORM, sqlx, etc.

Internal Networking: If your Go app connects to Ghayma services (Auth, databases, storage API), use internal URLs for deployed apps. See the Networking Guide.