πΉ @format Operator in TuskLang - Go Guide
@format Operator in TuskLang - Go Guide
π¨ Format Power: @format Operator Unleashed
TuskLang's @format
operator is your presentation rebellion. We don't bow to any kingβespecially not to ugly, unformatted data. Here's how to use @format
in Go projects to create beautiful, consistent, and professional data presentation.
π Table of Contents
- What is @format? - Basic Usage - Format Types - Template Formatting - Go Integration - Best Practicesπ¨ What is @format?
The @format
operator formats data according to specified patterns and templates. No more raw dataβjust pure, beautiful formatting.
π οΈ Basic Usage
[formatting]
currency: @format.currency(1234.56, "USD")
date: @format.date(@date.now(), "2006-01-02")
phone: @format.phone("1234567890")
email_template: @format.template("Hello {{name}}, your order {{order_id}} is ready!")
π§ Format Types
Numeric Formatting
[numbers]
currency_usd: @format.currency(1234.56, "USD")
currency_eur: @format.currency(1234.56, "EUR")
percentage: @format.percent(0.1234)
decimal: @format.decimal(3.14159, 2)
scientific: @format.scientific(1234567, 2)
Date/Time Formatting
[dates]
iso_date: @format.date(@date.now(), "2006-01-02")
iso_datetime: @format.datetime(@date.now(), "2006-01-02T15:04:05Z07:00")
human_date: @format.date(@date.now(), "Monday, January 2, 2006")
short_time: @format.time(@date.now(), "15:04")
String Formatting
[strings]
phone: @format.phone("1234567890")
ssn: @format.ssn("123456789")
credit_card: @format.credit_card("1234567890123456")
postal_code: @format.postal_code("12345")
Data Structure Formatting
[structures]
json: @format.json(@query("SELECT * FROM users LIMIT 5"))
xml: @format.xml(@query("SELECT * FROM products LIMIT 3"))
csv: @format.csv(@query("SELECT name, email FROM users"))
yaml: @format.yaml(@query("SELECT * FROM settings"))
π Template Formatting
Email Templates
[email_templates]
welcome: @format.template("Welcome {{name}}! Your account is ready.")
order_confirmation: @format.template("Order {{order_id}} for {{amount}} has been confirmed.")
password_reset: @format.template("Click here to reset your password: {{reset_url}}")
Notification Templates
[notifications]
alert: @format.template("ALERT: {{service}} is {{status}}")
update: @format.template("Update available: {{version}}")
maintenance: @format.template("Maintenance scheduled for {{date}} at {{time}}")
Report Templates
[reports]
summary: @format.template("Total users: {{user_count}}, Active: {{active_count}}")
performance: @format.template("Response time: {{response_time}}ms, Throughput: {{throughput}} req/s")
error_report: @format.template("Error rate: {{error_rate}}%, Last error: {{last_error}}")
π Go Integration
// Access formatted data
currency := config.GetString("currency")
date := config.GetString("date")
phone := config.GetString("phone")
emailTemplate := config.GetString("email_template")// Use formatted values
fmt.Printf("Price: %s\n", currency)
fmt.Printf("Date: %s\n", date)
fmt.Printf("Phone: %s\n", phone)
// Process templates
template := config.GetString("welcome")
// Replace template variables
formatted := strings.ReplaceAll(template, "{{name}}", "John")
Manual Formatting
type DataFormatter struct{}func (f *DataFormatter) FormatCurrency(amount float64, currency string) string {
switch currency {
case "USD":
return fmt.Sprintf("$%.2f", amount)
case "EUR":
return fmt.Sprintf("β¬%.2f", amount)
default:
return fmt.Sprintf("%.2f", amount)
}
}
func (f *DataFormatter) FormatDate(date time.Time, format string) string {
return date.Format(format)
}
func (f *DataFormatter) FormatTemplate(template string, data map[string]interface{}) string {
result := template
for key, value := range data {
placeholder := fmt.Sprintf("{{%s}}", key)
result = strings.ReplaceAll(result, placeholder, fmt.Sprint(value))
}
return result
}
π₯ Best Practices
- Use consistent formatting across your application - Cache expensive formatting operations - Validate formatted output - Use templates for dynamic content - Consider localization for international users---
TuskLang: Beautiful formatting with @format.