🐹 🚀 TuskLang Go Advanced Features Guide
🚀 TuskLang Go Advanced Features Guide
"We don't bow to any king" - Go Edition
Unlock the full power of TuskLang with advanced features. This guide covers @ operators, FUJSEN executable functions, machine learning, real-time monitoring, and enterprise-grade capabilities.
⚡ @ Operator System
Environment Variables
// config.tsk
[secrets]
api_key: @env("API_KEY")
database_password: @env("DB_PASSWORD", "default_secret")
jwt_secret: @env("JWT_SECRET")[config]
debug_mode: @env("DEBUG", "false")
log_level: @env("LOG_LEVEL", "info")
environment: @env("APP_ENV", "development")
// main.go
package mainimport (
"fmt"
"os"
"github.com/tusklang/go"
)
func main() {
// Set environment variables
os.Setenv("API_KEY", "secret_key_123")
os.Setenv("DB_PASSWORD", "my_password")
os.Setenv("APP_ENV", "production")
parser := tusklanggo.NewEnhancedParser()
data, err := parser.ParseFile("config.tsk")
if err != nil {
panic(err)
}
secrets := data["secrets"].(map[string]interface{})
fmt.Printf("API Key: %s\n", secrets["api_key"])
fmt.Printf("Environment: %s\n", data["config"].(map[string]interface{})["environment"])
}
Date and Time Operations
// config.tsk
[timing]
current_time: @date.now()
formatted_date: @date("Y-m-d H:i:s")
yesterday: @date.subtract("1d")
last_week: @date.subtract("7d")
next_month: @date.add("1M")
timestamp: @date.timestamp()[queries]
recent_orders: @query("SELECT * FROM orders WHERE created_at > ?", @date.subtract("7d"))
future_events: @query("SELECT * FROM events WHERE start_date > ?", @date.now())
daily_stats: @query("SELECT * FROM stats WHERE date = ?", @date("Y-m-d"))
// main.go
type Config struct {
Timing struct {
CurrentTime time.Time tsk:"current_time"
FormattedDate string tsk:"formatted_date"
Yesterday time.Time tsk:"yesterday"
LastWeek time.Time tsk:"last_week"
NextMonth time.Time tsk:"next_month"
Timestamp int64 tsk:"timestamp"
} tsk:"timing"
Queries struct {
RecentOrders interface{} tsk:"recent_orders"
FutureEvents interface{} tsk:"future_events"
DailyStats interface{} tsk:"daily_stats"
} tsk:"queries"
} tsk:""
Caching Operations
// config.tsk
[performance]
expensive_calculation: @cache("5m", @query("SELECT COUNT(*) FROM large_table"))
user_analytics: @cache("1h", @query("SELECT * FROM user_statistics"))
daily_revenue: @cache("1d", @query("SELECT SUM(total) FROM orders WHERE DATE(created_at) = ?", @date("Y-m-d")))
api_response: @cache("10m", @http("GET", "https://api.example.com/data"))[smart_cache]
user_profile: @cache.conditional(@request.user_id, "30m", @query("SELECT * FROM users WHERE id = ?", @request.user_id))
search_results: @cache.keyed(@request.search_term, "15m", @query("SELECT * FROM products WHERE name LIKE ?", @request.search_term))
// main.go
func main() {
parser := tusklanggo.NewEnhancedParser()
// Set up cache
memoryCache := cache.NewMemoryCache()
parser.SetCache(memoryCache)
// Execute with request context
context := map[string]interface{}{
"request": map[string]interface{}{
"user_id": 123,
"search_term": "%laptop%",
},
}
data, err := parser.ParseStringWithContext(tskContent, context)
if err != nil {
panic(err)
}
fmt.Printf("Cached results: %+v\n", data["performance"])
}
HTTP Requests
// config.tsk
[external_data]
weather: @http("GET", "https://api.weatherapi.com/v1/current.json?key=${api_key}&q=London")
user_data: @http("POST", "https://api.example.com/users", {
"headers": {
"Authorization": "Bearer ${api_key}",
"Content-Type": "application/json"
},
"body": {
"user_id": @request.user_id
}
})
currency_rates: @http("GET", "https://api.exchangerate-api.com/v4/latest/USD")
// main.go
type Config struct {
ExternalData struct {
Weather interface{} tsk:"weather"
UserData interface{} tsk:"user_data"
CurrencyRates interface{} tsk:"currency_rates"
} tsk:"external_data"
} tsk:""
File Operations
// config.tsk
[files]
config_json: @file.read("config.json")
log_content: @file.read("/var/log/app.log")
data_csv: @file.read("data.csv")[file_operations]
write_log: @file.write("/var/log/app.log", "Application started at " + @date.now())
append_data: @file.append("data.csv", @request.new_record)
check_exists: @file.exists("/etc/config.json")
file_size: @file.size("/var/log/app.log")
// main.go
type Config struct {
Files struct {
ConfigJSON interface{} tsk:"config_json"
LogContent interface{} tsk:"log_content"
DataCSV interface{} tsk:"data_csv"
} tsk:"files"
FileOperations struct {
WriteLog interface{} tsk:"write_log"
AppendData interface{} tsk:"append_data"
CheckExists interface{} tsk:"check_exists"
FileSize interface{} tsk:"file_size"
} tsk:"file_operations"
} tsk:""
🔧 FUJSEN - Executable Functions
Basic FUJSEN Functions
// config.tsk
[calculations]
calculate_tax: """
function calculate(amount, rate) {
return amount * (rate / 100);
}
"""format_currency: """
function format(amount, currency = 'USD') {
return currency + ' ' + amount.toFixed(2);
}
"""
[order]
amount: 100.00
tax_rate: 8.5
tax_amount: @fujsen(calculate_tax, @order.amount, @order.tax_rate)
formatted_total: @fujsen(format_currency, @order.amount + @order.tax_amount)
// main.go
type Config struct {
Calculations struct {
CalculateTax string tsk:"calculate_tax"
FormatCurrency string tsk:"format_currency"
} tsk:"calculations"
Order struct {
Amount float64 tsk:"amount"
TaxRate float64 tsk:"tax_rate"
TaxAmount float64 tsk:"tax_amount"
FormattedTotal string tsk:"formatted_total"
} tsk:"order"
} tsk:""
Advanced FUJSEN Functions
// config.tsk
[processing]
validate_email: """
function validate(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
"""calculate_discount: """
function discount(amount, user_type) {
const rates = {
'vip': 0.20,
'premium': 0.15,
'regular': 0.05
};
return amount * (rates[user_type] || 0);
}
"""
format_address: """
function format(street, city, state, zip) {
return street + ', ' + city + ', ' + state + ' ' + zip;
}
"""
[user]
email: "john@example.com"
user_type: "vip"
purchase_amount: 500.00
discount_amount: @fujsen(calculate_discount, @user.purchase_amount, @user.user_type)
is_valid_email: @fujsen(validate_email, @user.email)
FUJSEN with Database Integration
// config.tsk
[analytics]
calculate_user_stats: """
function calculate(user_id) {
const orders = query("SELECT * FROM orders WHERE user_id = ?", user_id);
const total = orders.reduce((sum, order) => sum + order.total, 0);
const count = orders.length;
const average = count > 0 ? total / count : 0;
return {
total_spent: total,
order_count: count,
average_order: average
};
}
"""[user_analytics]
user_id: @request.user_id
stats: @fujsen(calculate_user_stats, @user_analytics.user_id)
FUJSEN Error Handling
// config.tsk
[robust_functions]
safe_division: """
function divide(a, b) {
try {
if (b === 0) {
throw new Error('Division by zero');
}
return a / b;
} catch (error) {
return 0;
}
}
"""validate_input: """
function validate(data) {
if (!data || typeof data !== 'object') {
return { valid: false, error: 'Invalid data format' };
}
if (!data.name || data.name.length < 2) {
return { valid: false, error: 'Name too short' };
}
return { valid: true, data: data };
}
"""
[calculations]
result: @fujsen(safe_division, 10, 2)
validation: @fujsen(validate_input, @request.user_data)
🤖 Machine Learning Integration
@learn Operator
// config.tsk
[ml_features]
optimal_cache_time: @learn("cache_time", "5m", {
"factors": ["user_count", "data_size", "access_frequency"],
"algorithm": "regression"
})best_query_limit: @learn("query_limit", 100, {
"factors": ["response_time", "memory_usage", "user_satisfaction"],
"algorithm": "classification"
})
recommended_timeout: @learn("timeout", "30s", {
"factors": ["server_load", "network_latency", "query_complexity"],
"algorithm": "neural_network"
})
// main.go
func main() {
parser := tusklanggo.NewEnhancedParser()
// Set up ML context
mlContext := map[string]interface{}{
"user_count": 1000,
"data_size": "large",
"access_frequency": "high",
"response_time": 150,
"memory_usage": 80,
"user_satisfaction": 0.9,
"server_load": 0.7,
"network_latency": 50,
"query_complexity": "medium",
}
data, err := parser.ParseStringWithContext(tskContent, mlContext)
if err != nil {
panic(err)
}
mlFeatures := data["ml_features"].(map[string]interface{})
fmt.Printf("Optimal cache time: %v\n", mlFeatures["optimal_cache_time"])
fmt.Printf("Best query limit: %v\n", mlFeatures["best_query_limit"])
}
@optimize Operator
// config.tsk
[optimization]
database_pool_size: @optimize("db_pool", {
"current": 10,
"target": "minimize_response_time",
"constraints": {
"max_memory": "512MB",
"max_connections": 50
},
"metrics": ["response_time", "throughput", "error_rate"]
})cache_strategy: @optimize("cache_strategy", {
"current": "lru",
"target": "maximize_hit_rate",
"options": ["lru", "lfu", "fifo", "random"],
"metrics": ["hit_rate", "memory_usage", "eviction_rate"]
})
Predictive Analytics
// config.tsk
[predictions]
load_forecast: @predict("server_load", {
"historical_data": @query("SELECT timestamp, load FROM server_metrics WHERE timestamp > ?", @date.subtract("30d")),
"horizon": "24h",
"algorithm": "lstm"
})user_behavior: @predict("user_actions", {
"features": ["user_id", "session_duration", "page_views", "time_of_day"],
"target": "next_action",
"algorithm": "random_forest"
})
📊 Real-Time Monitoring
@metrics Operator
// config.tsk
[monitoring]
response_time: @metrics("api_response_time_ms", @request.response_time)
error_rate: @metrics("api_error_rate", @request.error_count / @request.total_requests)
memory_usage: @metrics("memory_usage_mb", @system.memory_used)
cpu_usage: @metrics("cpu_usage_percent", @system.cpu_usage)
database_connections: @metrics("db_connections", @query("SELECT COUNT(*) FROM information_schema.processlist"))
// main.go
func main() {
parser := tusklanggo.NewEnhancedParser()
// Set up metrics context
metricsContext := map[string]interface{}{
"request": map[string]interface{}{
"response_time": 150,
"error_count": 5,
"total_requests": 1000,
},
"system": map[string]interface{}{
"memory_used": 512,
"cpu_usage": 75.5,
},
}
data, err := parser.ParseStringWithContext(tskContent, metricsContext)
if err != nil {
panic(err)
}
monitoring := data["monitoring"].(map[string]interface{})
fmt.Printf("Response time: %v ms\n", monitoring["response_time"])
fmt.Printf("Error rate: %v%%\n", monitoring["error_rate"])
}
@alert Operator
// config.tsk
[alerts]
high_error_rate: @alert("error_rate > 0.05", {
"message": "High error rate detected: ${error_rate}%",
"severity": "critical",
"channels": ["email", "slack", "pagerduty"]
})low_memory: @alert("@system.memory_available < 100MB", {
"message": "Low memory warning: ${memory_available}MB available",
"severity": "warning",
"channels": ["email", "slack"]
})
database_down: @alert("@query('SELECT 1') == null", {
"message": "Database connection failed",
"severity": "critical",
"channels": ["pagerduty", "sms"]
})
@health Operator
// config.tsk
[health_checks]
database_health: @health("database", {
"check": @query("SELECT 1"),
"timeout": "5s",
"interval": "30s"
})api_health: @health("api", {
"check": @http("GET", "https://api.example.com/health"),
"timeout": "10s",
"interval": "1m"
})
cache_health: @health("cache", {
"check": @cache.get("health_check"),
"timeout": "2s",
"interval": "15s"
})
🔐 Advanced Security Features
@encrypt Operator
// config.tsk
[security]
encrypted_password: @encrypt(@request.password, "AES-256-GCM")
encrypted_api_key: @encrypt(@env("API_KEY"), "ChaCha20-Poly1305")
encrypted_data: @encrypt(@request.sensitive_data, "AES-256-CBC")[decryption]
decrypted_value: @decrypt(@security.encrypted_data, "AES-256-CBC")
@validate Operator
// config.tsk
[validation]
email: @validate.email(@request.email)
url: @validate.url(@request.website)
phone: @validate.phone(@request.phone_number)
credit_card: @validate.credit_card(@request.card_number)
password_strength: @validate.password(@request.password, {
"min_length": 8,
"require_uppercase": true,
"require_lowercase": true,
"require_numbers": true,
"require_special": true
})
@hash Operator
// config.tsk
[hashes]
password_hash: @hash(@request.password, "bcrypt", {
"cost": 12
})
file_hash: @hash(@file.read("important.txt"), "sha256")
data_hash: @hash(@request.data, "sha512")
🔄 Advanced Flow Control
Conditional Execution
// config.tsk
[conditional]
feature_flag: @if(@env("FEATURE_ENABLED") == "true", true, false)
cache_strategy: @if(@system.memory_available > 1000, "aggressive", "conservative")
timeout: @if(@environment == "production", "30s", "5s")[complex_conditions]
load_balancing: @if(@system.cpu_usage > 80 && @system.memory_usage > 90, "reduce_load", "normal")
backup_strategy: @if(@date.hour() > 22 || @date.hour() < 6, "full_backup", "incremental")
Loops and Iterations
// config.tsk
[iterations]
user_emails: @map(@query("SELECT email FROM users"), "email")
order_totals: @map(@query("SELECT total FROM orders"), "total")
processed_data: @map(@request.data_array, @fujsen(process_item))
Error Handling
// config.tsk
[error_handling]
safe_query: @try(@query("SELECT * FROM users"), [])
safe_http: @try(@http("GET", "https://api.example.com"), null)
safe_file: @try(@file.read("config.json"), "{}")[fallbacks]
user_count: @fallback(@query("SELECT COUNT(*) FROM users"), 0)
api_data: @fallback(@http("GET", "https://api.example.com"), @cache.get("api_data"))
🚀 Performance Optimization
Lazy Loading
// config.tsk
[performance]
expensive_data: @lazy(@query("SELECT * FROM very_large_table"))
optional_feature: @lazy(@file.read("large_config.json"))
heavy_calculation: @lazy(@fujsen(expensive_function, @request.data))
Parallel Execution
// config.tsk
[parallel]
user_data: @parallel([
@query("SELECT * FROM users WHERE id = ?", @request.user_id),
@query("SELECT * FROM user_orders WHERE user_id = ?", @request.user_id),
@query("SELECT * FROM user_preferences WHERE user_id = ?", @request.user_id)
])api_calls: @parallel([
@http("GET", "https://api1.example.com/data"),
@http("GET", "https://api2.example.com/data"),
@http("GET", "https://api3.example.com/data")
])
Batch Processing
// config.tsk
[batch]
user_updates: @batch(@request.user_ids, @fujsen(update_user), {
"batch_size": 100,
"delay": "1s"
})data_processing: @batch(@request.data_items, @fujsen(process_item), {
"batch_size": 50,
"concurrency": 5
})
🔧 Custom Operators
Creating Custom @ Operators
// main.go
package mainimport (
"github.com/tusklang/go"
)
func main() {
parser := tusklanggo.NewEnhancedParser()
// Register custom operator
parser.RegisterOperator("custom", func(args []interface{}) (interface{}, error) {
// Custom logic here
return "custom_result", nil
})
// Register operator with validation
parser.RegisterOperator("validate_age", func(args []interface{}) (interface{}, error) {
if len(args) != 1 {
return nil, fmt.Errorf("validate_age requires exactly 1 argument")
}
age, ok := args[0].(int)
if !ok {
return nil, fmt.Errorf("age must be an integer")
}
return age >= 18, nil
})
// Use custom operators in TSK
tskContent :=
[validation]
is_adult: @validate_age(@request.age)
custom_value: @custom("some_argument")
data, err := parser.ParseString(tskContent)
if err != nil {
panic(err)
}
fmt.Printf("Validation results: %+v\n", data["validation"])
}
📊 Advanced Analytics
Statistical Functions
// config.tsk
[statistics]
average_order: @stats.mean(@query("SELECT total FROM orders"))
order_median: @stats.median(@query("SELECT total FROM orders"))
order_std_dev: @stats.stddev(@query("SELECT total FROM orders"))
order_percentile: @stats.percentile(@query("SELECT total FROM orders"), 95)
Time Series Analysis
// config.tsk
[time_series]
daily_trend: @timeseries.trend(@query("SELECT DATE(created_at) as date, COUNT(*) as count FROM orders GROUP BY DATE(created_at)"))
seasonal_pattern: @timeseries.seasonal(@query("SELECT DATE(created_at) as date, SUM(total) as revenue FROM orders GROUP BY DATE(created_at)"))
forecast: @timeseries.forecast(@query("SELECT DATE(created_at) as date, COUNT(*) as orders FROM orders GROUP BY DATE(created_at)"), "7d")
🎯 Best Practices
1. Use Appropriate Caching
// Good - Cache expensive operations
expensive_query: @cache("5m", @query("SELECT COUNT(*) FROM large_table"))// Bad - Cache everything
simple_value: @cache("1h", "static_value")
2. Handle Errors Gracefully
// Good - Use fallbacks
user_count: @fallback(@query("SELECT COUNT(*) FROM users"), 0)// Bad - No error handling
user_count: @query("SELECT COUNT(*) FROM users")
3. Optimize FUJSEN Functions
// Good - Efficient function
calculate_total: """
function calculate(items) {
return items.reduce((sum, item) => sum + item.price, 0);
}
"""// Bad - Inefficient function
calculate_total: """
function calculate(items) {
let total = 0;
for (let i = 0; i < items.length; i++) {
total += items[i].price;
}
return total;
}
"""
4. Monitor Performance
// Track metrics for optimization
query_time: @metrics("database_query_time", @query("SELECT COUNT(*) FROM users"))
function_time: @metrics("fujsen_execution_time", @fujsen(expensive_function))
🔍 Debugging Advanced Features
Enable Debug Mode
// main.go
func main() {
parser := tusklanggo.NewEnhancedParser()
parser.SetDebug(true)
// Enable operator debugging
parser.SetOperatorDebugger(func(operator string, args []interface{}, result interface{}, duration time.Duration) {
log.Printf("Operator: %s | Args: %v | Result: %v | Duration: %.2fms",
operator, args, result, duration.Seconds()*1000)
})
data, err := parser.ParseFile("config.tsk")
if err != nil {
panic(err)
}
fmt.Printf("Advanced features executed with debugging\n")
}
Performance Profiling
Profile TSK execution
tusk profile config.tskShow operator usage
tusk operators config.tskValidate advanced syntax
tusk validate config.tsk --advanced
📚 Summary
You've learned:
1. @ Operator System - Environment variables, dates, caching, HTTP, files 2. FUJSEN Functions - Executable JavaScript in configuration 3. Machine Learning - @learn, @optimize, and predictive analytics 4. Real-Time Monitoring - @metrics, @alert, and health checks 5. Advanced Security - Encryption, validation, and hashing 6. Flow Control - Conditionals, loops, and error handling 7. Performance Optimization - Lazy loading, parallel execution, batching 8. Custom Operators - Extending TuskLang with custom functionality 9. Advanced Analytics - Statistics and time series analysis 10. Best Practices - Efficient and maintainable advanced features
🚀 Next Steps
Now that you understand advanced features:
1. Web Framework Integration - Use with Gin, Echo, and other frameworks 2. Deployment - Docker, Kubernetes, and cloud deployment 3. Enterprise Features - Multi-tenant, scaling, and monitoring 4. Custom Extensions - Build your own operators and adapters 5. Performance Tuning - Optimize for production workloads
---
"We don't bow to any king" - You now have the power to build enterprise-grade applications with TuskLang's advanced features and Go's type safety!