If you're coming from Laravel, one thing feels missing in Go:
👉 Structure
By default, Go gives you freedom—but no clear architecture.
So in this post, we’ll structure a Go API like Laravel:
- Controller → Handle request
- Service → Business logic
- Repository → Database
🧠 Why Structure Matters
Without structure:
- Code becomes messy quickly
- Hard to scale
- Difficult to debug
With structure:
- Clean separation of concerns
- Easier testing
- Production-ready codebase
📁 Folder Structure
project/
├── main.go
├── controller/
├── service/
├── repository/
├── model/
🧱 Model (model/user.go)
```go id="6pn1u5"
package model
type User struct {
ID int json:"id"
Name string json:"name"
}
---
## 🗄️ Repository Layer
Handles database queries
```go id="aq1u8u"
package repository
import (
"database/sql"
"yourapp/model"
)
type UserRepository struct {
DB *sql.DB
}
func (r *UserRepository) GetAll() ([]model.User, error) {
rows, err := r.DB.Query("SELECT id, name FROM users")
if err != nil {
return nil, err
}
defer rows.Close()
var users []model.User
for rows.Next() {
var user model.User
rows.Scan(&user.ID, &user.Name)
users = append(users, user)
}
return users, nil
}
⚙️ Service Layer
Handles business logic
```go id="qf1kmz"
package service
import (
"yourapp/model"
"yourapp/repository"
)
type UserService struct {
Repo *repository.UserRepository
}
func (s *UserService) GetUsers() ([]model.User, error) {
return s.Repo.GetAll()
}
---
## 🌐 Controller Layer
Handles HTTP requests
```go id="n8t1rn"
package controller
import (
"encoding/json"
"net/http"
"yourapp/service"
)
type UserController struct {
Service *service.UserService
}
func (c *UserController) GetUsers(w http.ResponseWriter, r *http.Request) {
users, err := c.Service.GetUsers()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
json.NewEncoder(w).Encode(users)
}
🚀 main.go (Wire Everything)
```go id="b4r8f2"
package main
import (
"database/sql"
"log"
"net/http"
_ "github.com/lib/pq"
"yourapp/controller"
"yourapp/repository"
"yourapp/service"
)
func main() {
db, _ := sql.Open("postgres", "your_connection_string")
repo := &repository.UserRepository{DB: db}
service := &service.UserService{Repo: repo}
controller := &controller.UserController{Service: service}
http.HandleFunc("/users", controller.GetUsers)
log.Println("Server running on :8080")
http.ListenAndServe(":8080", nil)
}
---
## 🔥 What You Achieved
* Clean architecture like Laravel
* Separation of concerns
* Scalable Go backend structure
---
## 🧭 When to Use This
Use this structure when:
* Building real APIs
* Working in teams
* Scaling projects
---
## 💬 Final Thought
Go doesn’t force structure…
👉 But professionals create one.
If you combine Go performance with Laravel-style architecture, you get the best of both worlds.
---
## 🚀 Coming Next
👉 Add middleware (logging, auth)
👉 Add validation layer
👉 Dockerize Go + PostgreSQL
---
#golang #backend #architecture #programming #webdev























