- Keep templates concise and clear
- Add appropriate comments and explanations
- Use meaningful variable names
How to use Go Template to process request and response data in MCP Gateway
{{}}
as delimiters, and various functions and variables can be used within the delimiters. In MCP Gateway, we mainly use the following variables:
.Config
: Service-level configuration.Args
: Request parameters.Request
: Original request information.Response
: Upstream service response informationenv
function allows you to safely retrieve sensitive information from environment variables, avoiding hardcoding in configuration files.Array Processing Tips Explanation
fromJSON
function to convert JSON strings into traversable objectsrange
to iterate through arrayslen
function to get array lengthadd
function for mathematical operationsif
to control comma separation between array elementstoJSON
function:
settings
is a complex object. Using the toJSON
function will automatically convert settings
to a JSON string.env - Environment Variables
add - Mathematical Operations
fromJSON - JSON Parsing
toJSON - JSON Serialization
Use Log Output
Step-by-step Construction
Test Edge Cases
Security
Performance
Maintainability