This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var ( | |
| ordersCreated = promauto.NewCounterVec(prometheus.CounterOpts{ | |
| Name: "orders_created_total", | |
| Help: "Total orders created", | |
| }, []string{"payment_method", "tier"}) | |
| orderValue = promauto.NewHistogramVec(prometheus.HistogramOpts{ | |
| Name: "order_value_dollars", | |
| Help: "Distribution of order values", | |
| Buckets: []float64{1, 5, 10, 25, 50, 100, 250, 500, 1000}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Bad: This function does 3 things | |
| func processUser(userID string) error { | |
| // 1. Validate | |
| if userID == "" { | |
| return errors.New("user ID required") | |
| } | |
| // 2. Fetch from DB | |
| user, err := db.GetUser(userID) | |
| if err != nil { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| feature/oauth-google-login | |
| fix/order-status-not-updating | |
| chore/upgrade-postgres-16 | |
| experiment/grpc-migration-poc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // BAD: Trust third-party data blindly | |
| resp := stripe.GetPayment(paymentID) | |
| db.Save(Payment{Amount: resp.Amount, Currency: resp.Currency}) | |
| // GOOD: Validate before using | |
| resp := stripe.GetPayment(paymentID) | |
| if resp.Amount <= 0 || resp.Amount > MaxPaymentAmount { | |
| return errors.New("invalid amount from payment provider") | |
| } | |
| if !validCurrencies[resp.Currency] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func ReplayOrder(events []Event) Order { | |
| var order Order | |
| for _, event := range events { | |
| switch e := event.(type) { | |
| case OrderPlaced: | |
| order.ID = e.OrderID | |
| order.Status = "pending" | |
| order.Items = e.Items | |
| order.Total = e.Total | |
| case PaymentTaken: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Find files | |
| find /var/log -name "*.log" -newer /tmp/marker | |
| find . -name "*.go" -size +1M | |
| find . -mtime -1 # Modified in last 24 hours | |
| # Search inside files | |
| grep -r "ERROR" /var/log/myapp/ | |
| grep -r "panic" . --include="*.go" | |
| grep -n "func.*Handler" src/api/*.go |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| upstream backend { | |
| server 192.168.1.10:8080 max_fails=3 fail_timeout=30s; | |
| server 192.168.1.11:8080 max_fails=3 fail_timeout=30s; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Partition orders by year/month | |
| CREATE TABLE orders ( | |
| id BIGSERIAL, | |
| user_id INT NOT NULL, | |
| amount DECIMAL NOT NULL, | |
| created_at TIMESTAMP NOT NULL | |
| ) PARTITION BY RANGE (created_at); | |
| CREATE TABLE orders_2025 PARTITION OF orders | |
| FOR VALUES FROM ('2025-01-01') TO ('2026-01-01'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Type guard: narrows type based on runtime check | |
| function isApiError(error: unknown): error is ApiError { | |
| return ( | |
| typeof error === 'object' && | |
| error !== null && | |
| 'statusCode' in error && | |
| 'message' in error | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (c *Client) writePump(conn *websocket.Conn) { | |
| ticker := time.NewTicker(30 * time.Second) | |
| defer func() { | |
| ticker.Stop() | |
| conn.Close() | |
| }() | |
| conn.SetPongHandler(func(string) error { | |
| conn.SetReadDeadline(time.Now().Add(65 * time.Second)) | |
| return nil |