Basic Syntax
Go Template uses{{}} as delimiters, and various functions and variables can be used within the delimiters. In MCP Gateway, we mainly use the following variables:
Configuration Variables
.Config: Service-level configuration.Args: Request parameters
Runtime Variables
.Request: Original request information.Response: Upstream service response information
Common Use Cases
1. Getting Configuration from Environment Variables
Using the
env function allows you to safely retrieve sensitive information from environment variables, avoiding hardcoding in configuration files.2. Extracting Values from Request Headers
3. Building Request Body
4. Processing Response Data
5. Handling Nested Response Data
For complex nested data structures, you can access them layer by layer:6. Handling Array Data
When you need to process array data in responses, you can use Go Template’s range functionality:Array Processing Tips Explanation
Array Processing Tips Explanation
This example shows how to:
- Use the
fromJSONfunction to convert JSON strings into traversable objects - Use
rangeto iterate through arrays - Use the
lenfunction to get array length - Use the
addfunction for mathematical operations - Use conditional statements
ifto control comma separation between array elements
7. Using Parameters in URLs
Dynamically building URL paths:8. Handling Complex Object Data
When you need to convert complex structures like objects and arrays from requests or responses to JSON, you can use thetoJSON function:
Here,
settings is a complex object. Using the toJSON function will automatically convert settings to a JSON string.9. Safely Access Nested Fields (safeGet/safeGetOr)
When a middle level of a nested object isnull (or missing), direct access will cause a template rendering error. For example:
safeGet and safeGetOr.
safeGet <path> <root>: Safely reads a nested field; if any middle level isnil/missing, returns an empty value (no error).safeGetOr <path> <root> <default>: LikesafeGet, but returns<default>when access fails.
preferences is null:
Built-in Functions
Currently supports the following built-in functions:env - Environment Variables
env - Environment Variables
Get environment variable values
add - Mathematical Operations
add - Mathematical Operations
Perform integer addition operations
fromJSON - JSON Parsing
fromJSON - JSON Parsing
Convert JSON strings to traversable objects
toJSON - JSON Serialization
toJSON - JSON Serialization
Convert objects to JSON strings
safeGet - Safe Field Access
safeGet - Safe Field Access
Safely read nested fields; if any middle level is
nil or missing, return an empty value without error.safeGetOr - Safe Access with Default
safeGetOr - Safe Access with Default
Like
safeGet, but returns a default value when access fails.Template Debugging Tips
1
Use Log Output
During development, you can view actual values of template variables through logs
2
Step-by-step Construction
For complex templates, it’s recommended to first build a simple version, then gradually add complex logic
3
Test Edge Cases
Ensure templates work correctly under various data conditions, including empty values, null values, etc.
Best Practices
Security
Security
- Avoid directly exposing sensitive information in templates
- Use environment variables to store keys and passwords
- Properly validate and escape user input
Performance
Performance
- Avoid complex calculations in templates
- Use caching mechanisms appropriately
- For large data processing, consider preprocessing in upstream services
Maintainability
Maintainability
- Keep templates concise and clear
- Add appropriate comments and explanations
- Use meaningful variable names
Extension Features
If you need to add new template functions, you can:- Describe specific use cases, create an issue explaining the requirements
- Welcome PR after implementation, but currently only accept general-purpose functions
More Resources
Go Template Official Documentation
View complete Go Template documentation and advanced usage