You can also use raw string literals inside backticks, for example:
columns := "id, name" table := "users" query := fmt.Sprintf(` SELECT %s FROM %s `, columns, table) fmt.Println(query)
There are a few caveats for this approach:
- Raw strings do not parse escape sequences
- All spaces will be saved, so in the
FROM in this request there will be a new line and then several tabs.
These problems can be a problem for some, and spaces will lead to some ugly result lines. However, I prefer this approach, as it allows you to copy and paste long complex SQL queries outside of your code and into other contexts, for example, to sql tables for testing.
source share