Golang กับ Clean Architecture: แยกชั้นโปรเจคแบบมืออาชีพ

Sharing is caring!

การพัฒนาแอปพลิเคชันด้วยภาษา Go (Golang) ให้มีโครงสร้างที่สามารถขยาย ดูแล และทดสอบได้ง่ายนั้นไม่ใช่เรื่องง่าย โดยเฉพาะเมื่อโปรเจคเติบโตขึ้น การใช้แนวทาง Clean Architecture คือหนึ่งในกลยุทธ์ยอดนิยมที่ช่วยให้เราสามารถแยก concerns ต่าง ๆ ออกจากกันได้อย่างชัดเจน

Clean Architecture คืออะไร?

แนวคิดของ Clean Architecture ถูกเสนอโดย Robert C. Martin (Uncle Bob) ซึ่งมีเป้าหมายเพื่อแยก logic ทางธุรกิจออกจากเรื่องภายนอก เช่น database, framework หรือการแสดงผล

เป้าหมายของ Clean Architecture:

  • ทดสอบง่าย (Testable)
  • แยก concerns (Separation of concerns)
  • เปลี่ยน technology ภายนอกได้โดยไม่กระทบ core
  • อ่านโค้ดง่าย เข้าใจโฟลว์

ชั้นต่าง ๆ ใน Clean Architecture

  • Entities: กฎทางธุรกิจ (Business rules)
  • Use Cases: Logic ของระบบ
  • Interface Adapters: แปลงข้อมูลให้ตรงตามที่ต้องการ
  • Frameworks & Drivers: HTTP, DB, UI ฯลฯ

โครงสร้างไฟล์ในโปรเจค Go

.
├── cmd/                  # จุด entrypoint
├── internal/
│   ├── domain/           # entity, model
│   ├── usecase/          # business logic
│   ├── interface/
│   │   ├── controller/   # http handler
│   │   └── repository/   # interface ของ repo
│   └── infrastructure/   # DB, external
└── pkg/                  # public package (option)

ตัวอย่างใช้งานแบบง่าย

Entity: User

// internal/domain/user.go
type User struct {
    ID   int
    Name string
}
  

Usecase: GetUser

// internal/usecase/user_usecase.go
type UserUsecase interface {
    GetUser(id int) (*User, error)
}

type userUsecase struct {
    repo UserRepository
}

func (u *userUsecase) GetUser(id int) (*User, error) {
    return u.repo.FindByID(id)
}
  

Repository Interface

// internal/interface/repository/user_repository.go
type UserRepository interface {
    FindByID(id int) (*User, error)
}
  

Infrastructure (เช่น DB)

// internal/infrastructure/user_repo_mysql.go
type userRepoMysql struct {
    db *sql.DB
}

func (r *userRepoMysql) FindByID(id int) (*User, error) {
    // query database...
}
  

Controller

// internal/interface/controller/user_controller.go
func GetUserHandler(u UserUsecase) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        id := 1 // parse จาก URL
        user, _ := u.GetUser(id)
        json.NewEncoder(w).Encode(user)
    }
}
  

ทำไมควรใช้ Clean Architecture กับ Go?

  • แยก logic ธุรกิจออกจาก HTTP, DB, หรือ gRPC
  • เปลี่ยน storage backend ได้ง่าย (เช่น จาก MySQL → Redis)
  • เขียน unit test ได้ดี
  • สามารถทำ mock ได้โดยไม่ขึ้นกับ implementation

ข้อควรระวัง

  • อาจดูซับซ้อนเกินไปสำหรับโปรเจคเล็ก
  • ต้องวางแผนเรื่อง package, dependency ให้ดี
  • อาจต้องมี tooling เช่น Wire เพื่อช่วยจัดการ dependency

ภาพประกอบ Clean Architecture ใน Go

สรุป

Clean Architecture คือแนวคิดที่ช่วยให้โค้ดมีความเป็นระบบ แยก concerns ชัดเจน และรองรับการเติบโตของโปรเจคในระยะยาวได้ดี Golang แม้จะเป็นภาษาที่ simple แต่ก็สามารถปรับใช้แนวทางนี้ได้อย่างมีประสิทธิภาพ หากวางโครงสร้างตั้งแต่เริ่มต้น


เผยแพร่โดย: poolsawat.com | เขียนโดย: King Pool

Leave a Reply

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *