1---
2name: go-api-development
3description: Go API development guidelines using the standard library (1.22+) with best practices for RESTful API design, error handling, and security
4metadata:
5 source: https://skillmd.com/skills/aibot88/go-api-development
6---
7
8# Go API Development with Standard Library
9
10## Core Principles
11
12- 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.22
13- Follow the user's requirements carefully and to the letter
14- First think step-by-step - describe your plan for the API structure, endpoints, and data flow in pseudocode, written out in great detail
15- Write correct, up-to-date, bug-free, fully functional, secure, and efficient Go code for APIs
16- Leave NO todos, placeholders, or missing pieces in the API implementation
17- Always prioritize security, scalability, and maintainability in your API designs
18
19## API Development Guidelines
20
21### Routing and HTTP Handling
22
23- Use the new `http.ServeMux` introduced in Go 1.22 for routing
24- Implement proper HTTP method handling (GET, POST, PUT, DELETE, PATCH)
25- Use appropriate HTTP status codes for responses
26- Implement proper content-type handling for requests and responses
27
28### Error Handling
29
30- Implement proper error handling, including custom error types when beneficial
31- Return appropriate HTTP status codes with error responses
32- Use structured error responses in JSON format
33- Log errors appropriately for debugging and monitoring
34
35### Input Validation
36
37- Implement input validation for API endpoints
38- Validate request bodies, query parameters, and path parameters
39- Return clear validation error messages to clients
40- Sanitize inputs to prevent injection attacks
41
42### JSON Handling
43
44- Use `encoding/json` for JSON serialization/deserialization
45- Implement proper struct tags for JSON field mapping
46- Handle JSON parsing errors gracefully
47- Use appropriate JSON formatting for responses
48
49### Concurrency
50
51- Leverage Go's built-in concurrency features when appropriate for API performance
52- Use goroutines for concurrent operations where beneficial
53- Implement proper synchronization for shared state
54- Use context for request cancellation and timeouts
55
56### Middleware
57
58- Implement middleware for cross-cutting concerns (logging, authentication, rate limiting)
59- Use middleware chaining for composable request processing
60- Implement CORS handling where needed
61- Add request/response logging middleware
62
63### Security
64
65- Implement authentication and authorization where appropriate
66- Use HTTPS in production
67- Implement rate limiting to prevent abuse
68- Validate and sanitize all user inputs
69- Use secure defaults for cookies and sessions
70
71### Logging
72
73- Use standard library logging with structured output
74- Log appropriate information for debugging and monitoring
75- Avoid logging sensitive information
76- Use log levels appropriately
77
78### Testing
79
80- Write unit tests for handlers and business logic
81- Implement integration tests for API endpoints
82- Use table-driven tests where appropriate
83- Mock external dependencies in tests