0

I was looking at this article:

https://medium.com/@IndianGuru/understanding-go-s-template-package-c5307758fab0

I am wondering how to inject a variable into a string using templating, for example:

func getTemplate(v string) string {
    return `CREATE TABLE share_${v} PARTITION OF share FOR VALUES IN (${v});`
}

the example in the article writes the output to stdout, but I need to store the result of the template as a variable, anyone know how?

Something like:

result := getTemplate("0")
  • Possible duplicate of [Capture or assign golang template output to variable](https://stackoverflow.com/questions/40164896/capture-or-assign-golang-template-output-to-variable) – zerkms Oct 25 '18 at 23:56
  • yeah doing this using golang is pretty r*tarded but I am sure I will figure it out –  Oct 25 '18 at 23:58
  • 1
    This isn't templating, and even if it was, you aren't supposed to do it this way anyway. You need to use prepared statements to construct SQL queries. – Michael Hampton Oct 26 '18 at 03:03
  • yeah I guess so thanks –  Oct 26 '18 at 04:41
  • @MichaelHampton can you parameterise identifiers? – zerkms Oct 26 '18 at 09:53

1 Answers1

0

Golang templating is motherf*cking bonkers. This should work:

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 + `);
    `
}