Hello everyone,
I’ve been working with Golang for backend development and I’m interested in its efficiency for larger monolithic or modular monolithic systems. One of my biggest challenges has been the excessive boilerplate code required for complex functionalities.
For instance, I recently developed a product search feature that needed to accommodate various filters. The amount of code required to execute my database queries and validate inputs was quite extensive; here’s a simplified illustration:
func SearchProducts(criteria ProductCriteria) ([]Product, error) {
var results []Product
query := "SELECT * FROM products WHERE 1=1"
params := []interface{}{}
paramCount := 0
if criteria.CategoryId != 0 {
paramCount++
query += fmt.Sprintf(" AND category_id = $%d", paramCount)
params = append(params, criteria.CategoryId)
}
if criteria.MinPrice > 0 {
paramCount++
query += fmt.Sprintf(" AND price >= $%d", paramCount)
params = append(params, criteria.MinPrice)
}
if criteria.MaxPrice > 0 {
paramCount++
query += fmt.Sprintf(" AND price <= $%d", paramCount)
params = append(params, criteria.MaxPrice)
}
resultRows, err := db.Query(query, params...)
if err != nil {
return nil, err
}
defer resultRows.Close()
// Additional scanning logic...
return results, nil
}
This part alone took over 250 lines for database interactions and validation. Adding file uploads for multiple product images only complicates things further.
Previously, I utilized frameworks like Spring Boot and found that I could implement similar features with less code.
So, is Golang truly a viable option for large monolithic backends? Are there certain design patterns or libraries that could help streamline the amount of code I need to write?
I’d appreciate any feedback from your experiences!