go - 使用变量注入字符串模板
问题描述
我在看这篇文章:
https://medium.com/@IndianGuru/understanding-go-s-template-package-c5307758fab0
我想知道如何使用模板将变量注入字符串,例如:
func getTemplate(v string) string {
return `CREATE TABLE share_${v} PARTITION OF share FOR VALUES IN (${v});`
}
文章中的示例将输出写入标准输出,但我需要将模板的结果存储为变量,有人知道怎么做吗?
就像是:
result := getTemplate("0")
解决方案
Golang 模板是他妈的疯子。这应该有效:
func getTableCreationCommands(v string) string {
return `
CREATE TABLE share_` + v + ` PARTITION OF share FOR VALUES IN (` + v + `);
CREATE TABLE nearby_` + v + ` PARTITION OF nearby FOR VALUES IN (` + v + `);
`
}
推荐阅读
- airflow - DAG 级别的气流资源池使用情况?
- php - MySQL 表值与 php var 相等(然后显示它)
- android - 将来自 Api 的消息显示为通知
- vue.js - Vuejs - returning data from an action
- regex - 跳过前导和尾随空格
- mysql - Convert Selection query to update query
- tensorboardx - tensorboardX add_graph()ImportError: cannot import name 'OperatorExportTypes'
- javascript - 使用 mailto 时在 textarea 中保留换行符
- outlook - 如何在 macOS 中禁用 Outlook 的暗模式?
- css - zoom inside a width fixed div