跪拜 Guibai
← All articles
Frontend · Backend · Go

From PHP to Go + AI: Building an Admin Model and Hot-Reload in a GORM-Powered Project

By 妙码生花 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

This series shows a real-world workflow where a non-Go developer uses AI tools to produce production-quality Go code. The security warning about AI installing counterfeit dependencies is a practical alert for any developer using AI assistants in their build pipeline.

Summary

In the latest installment of a series documenting a PHP full-stack engineer's transition to Go with AI assistance, the focus is on two concrete tasks: designing an admin database model using GORM, and configuring Air for hot-reload in development.

The admin model is built without embedding gorm.Model — all fields are defined explicitly to control ordering and add Chinese comments. The Register function is refactored to accept variadic parameters, allowing multiple models to be registered in a single call. The author emphasizes that AI-generated model designs should be manually reviewed and fine-tuned, and that varchar lengths are chosen for readability rather than strict technical reasons.

For hot-reload, Air is installed via go install (not as a project dependency) and configured with a .air.toml file. The author includes a notable security warning: always provide official repository links when asking AI to install dependencies, to prevent the AI from being tricked into installing counterfeit packages that could contain malware.

Takeaways
GORM's AutoMigrate creates and updates database tables automatically from Go struct definitions.
The Register function was refactored to accept variadic parameters, enabling batch model registration.
The Admin model avoids embedding gorm.Model to allow custom field ordering and Chinese comments.
Air is installed via go install to $GOPATH/bin, not as a project dependency, and configured via .air.toml.
Always include official repository URLs in AI prompts to prevent installation of counterfeit or malicious packages.
Conclusions

Using AI to generate model code is efficient, but manual review of field types, comments, and tags is still essential for production quality.

The decision to avoid enum types for the Status field — using a string instead — gives developers flexibility to add custom statuses without schema changes.

The security practice of pinning official dependency URLs in AI prompts is a low-effort, high-impact safeguard that many developers overlook.

Hot-reload tools like Air are not built into Go's standard library, making third-party setup a necessary step for efficient Go development workflows.

Concepts & terms
GORM AutoMigrate
A GORM feature that automatically creates, updates, or migrates database tables to match Go struct definitions, reducing manual schema management.
Air (hot-reload)
A Go tool that watches for file changes and automatically rebuilds and restarts the application, providing a live-reload experience during development.
Variadic parameters in Go
A function parameter syntax (e.g., `func Register(models ...any)`) that allows passing zero or more arguments of a specified type, enabling flexible APIs.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗