# Go Best Practices - FikoYT

## 🎯 API Pattern

```go
// Handler function
func aggregateHandler(c *fiber.Ctx) error {
    // Context al
    ctx := c.Locals("FikoYTCtx").(*FikoYTCtx)

    // İstek parse
    var req AggregateRequest
    if err := c.BodyParser(&req); err != nil {
        return err
    }

    // Process
    result, err := processAggregate(ctx, req)
    if err != nil {
        return err
    }

    // Response
    return c.JSON(result)
}
```

## 🔄 Error Handling

```go
// Structured error
type FikoError struct {
    Code    int    `json:"code"`
    Message string `json:"message"`
    Details string `json:"details,omitempty"`
}

// HTTP error
func HTTPError(c *fiber.Ctx, err FikoError) error {
    c.Status(err.Code)
    return c.JSON(err)
}
```

## 📦 MongoDB Pattern

```go
// Collection helper
func Koleksiyon(db *mongo.Database, name string) *mongo.Collection {
    return db.Collection(name)
}

// Upsert helper
func EkleYadaGuncelle(col *mongo.Collection, data interface{}) (*mongo.UpdateResult, error) {
    filter := bson.M{"_id": data.(bson.M)["_id"]}
    update := bson.M{"$set": data}
    return col.UpdateOne(context.Background(), filter, update, options.Update().SetUpsert(true))
}
```

## ⚡ Performance

- **Prefork:** Multiple processes
- **Connection pool:** MongoDB
- **Context:** Request timeout
- **Compression:** gzip middleware

---
2025-11-06