1---2name: go-api-development3description: Go API development guidelines using the standard library (1.22+) with best practices for RESTful API design, error handling, and security4metadata:5 maintainer: Mindrally6 source: https://skillmd.com/skills/mindrally/go-api-development7 upstream: https://github.com/Mindrally/skills8---910# Go API Development with Standard Library1112## Core Principles1314- Always use the latest stable version of Go (1.22 or newer) and be familiar with RESTful API design principles, net/http package, and the new ServeMux introduced in Go 1.2215- Follow the user's requirements carefully and to the letter16- First think step-by-step - describe your plan for the API structure, endpoints, and data flow in pseudocode, written out in great detail17- Write correct, up-to-date, bug-free, fully functional, secure, and efficient Go code for APIs18- Leave NO todos, placeholders, or missing pieces in the API implementation19- Always prioritize security, scalability, and maintainability in your API designs2021## API Development Guidelines2223### Routing and HTTP Handling2425- Use the new `http.ServeMux` introduced in Go 1.22 for routing26- Implement proper HTTP method handling (GET, POST, PUT, DELETE, PATCH)27- Use appropriate HTTP status codes for responses28- Implement proper content-type handling for requests and responses2930### Error Handling3132- Implement proper error handling, including custom error types when beneficial33- Return appropriate HTTP status codes with error responses34- Use structured error responses in JSON format35- Log errors appropriately for debugging and monitoring3637### Input Validation3839- Implement input validation for API endpoints40- Validate request bodies, query parameters, and path parameters41- Return clear validation error messages to clients42- Sanitize inputs to prevent injection attacks4344### JSON Handling4546- Use `encoding/json` for JSON serialization/deserialization47- Implement proper struct tags for JSON field mapping48- Handle JSON parsing errors gracefully49- Use appropriate JSON formatting for responses5051### Concurrency5253- Leverage Go's built-in concurrency features when appropriate for API performance54- Use goroutines for concurrent operations where beneficial55- Implement proper synchronization for shared state56- Use context for request cancellation and timeouts5758### Middleware5960- Implement middleware for cross-cutting concerns (logging, authentication, rate limiting)61- Use middleware chaining for composable request processing62- Implement CORS handling where needed63- Add request/response logging middleware6465### Security6667- Implement authentication and authorization where appropriate68- Use HTTPS in production69- Implement rate limiting to prevent abuse70- Validate and sanitize all user inputs71- Use secure defaults for cookies and sessions7273### Logging7475- Use standard library logging with structured output76- Log appropriate information for debugging and monitoring77- Avoid logging sensitive information78- Use log levels appropriately7980### Testing8182- Write unit tests for handlers and business logic83- Implement integration tests for API endpoints84- Use table-driven tests where appropriate85- Mock external dependencies in tests