Go (Gin)
Coming soon. Go code generation is not yet available. TypeScript (NestJS) is the only stable language template today. The information below is a preview of the planned Go support.
The Go template uses Gin, a high-performance HTTP web framework written in Go.
Overview
| Component | Technology |
|---|---|
| Framework | Gin |
| ORM | GORM |
| Validation | go-playground/validator |
| Documentation | swaggo/swag |
| Testing | go test |
Quick Start
# Create a new Go service
apso server new my-api --template go
# Navigate to project
cd my-api
# Download dependencies
go mod download
# Start development server
go run main.go
# Or with hot reload
airProject Structure
my-api/
├── cmd/
│ └── server/
│ └── main.go # Entry point
├── internal/
│ ├── entities/ # GORM models
│ │ ├── project.go
│ │ └── task.go
│ ├── handlers/ # HTTP handlers
│ │ ├── projects.go
│ │ └── tasks.go
│ ├── services/ # Business logic
│ ├── repository/ # Database layer
│ ├── middleware/ # HTTP middleware
│ └── extensions/ # Your custom code
├── pkg/ # Shared packages
│ ├── config/
│ └── database/
├── migrations/ # Database migrations
├── .apsorc # APSO schema
└── go.modKey Technologies
Gin
Fast and minimal framework:
- High performance
- Middleware support
- Route grouping
- Error handling
GORM
Type-safe ORM:
package entities
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
type Project struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey"`
Name string `gorm:"not null"`
Description *string
OrganizationID uuid.UUID `gorm:"not null;index"`
Tasks []Task `gorm:"foreignKey:ProjectID"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (p *Project) BeforeCreate(tx *gorm.DB) error {
p.ID = uuid.New()
return nil
}Validation
Struct tag validation:
type CreateProjectRequest struct {
Name string `json:"name" binding:"required,max=200"`
Description *string `json:"description"`
}Handler Example
// internal/handlers/projects.go
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"myapi/internal/services"
)
type ProjectHandler struct {
service *services.ProjectService
}
func NewProjectHandler(service *services.ProjectService) *ProjectHandler {
return &ProjectHandler{service: service}
}
func (h *ProjectHandler) List(c *gin.Context) {
user := c.MustGet("user").(*entities.User)
projects, err := h.service.GetAll(c, user.OrganizationID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"data": projects})
}
func (h *ProjectHandler) Create(c *gin.Context) {
var req CreateProjectRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
user := c.MustGet("user").(*entities.User)
project, err := h.service.Create(c, req, user.OrganizationID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, project)
}Route Setup
// cmd/server/main.go
func setupRoutes(r *gin.Engine) {
api := r.Group("/api/v1")
api.Use(middleware.Auth())
projects := api.Group("/projects")
{
projects.GET("/", projectHandler.List)
projects.POST("/", projectHandler.Create)
projects.GET("/:id", projectHandler.Get)
projects.PATCH("/:id", projectHandler.Update)
projects.DELETE("/:id", projectHandler.Delete)
}
}Configuration
Environment Variables
# .env
DATABASE_URL=postgres://user:pass@localhost:5432/db
JWT_SECRET=your-secret-key
PORT=3001
GIN_MODE=debugConfig Loading
// pkg/config/config.go
package config
import "os"
type Config struct {
DatabaseURL string
JWTSecret string
Port string
GinMode string
}
func Load() *Config {
return &Config{
DatabaseURL: os.Getenv("DATABASE_URL"),
JWTSecret: os.Getenv("JWT_SECRET"),
Port: getEnvOrDefault("PORT", "3001"),
GinMode: getEnvOrDefault("GIN_MODE", "debug"),
}
}Commands
| Command | Description |
|---|---|
go run main.go | Start server |
air | Start with hot reload |
go test ./... | Run tests |
go build -o bin/server | Build binary |
swag init | Generate Swagger docs |
Features
Auto-Generated
- GORM models with relationships
- CRUD handlers
- Request/response structs
- Swagger annotations
- Auth middleware
Customizable
- Add custom handlers in extensions
- Override service methods
- Add middleware
- Integrate external packages
Related
Last updated on