⇠ back to TILs

Go's fmt package Explicit Argument Indexes

Not sure how I hadn’t seen this before: in Go, you can specify the index of the format argument you want to use for each formatting verb.

Simply enough, from the package docs:

fmt.Sprintf("%[2]d %[1]d\n", 11, 22) // => "22 11"

In the past, I’ve actually done something along the lines of:

s := "test"
fmt.Sprintf("%s %s", s, s) // => "test test"

Instead, you can do:

s := "test"
fmt.Sprintf("%[1]s %[1]s", s) // => "test test"

This came up today at $WORK while providing a table name to a constructed SQL query - the table name needed to be supplied multiple times in an upsert, so we could simply do:

query := fmt.Sprintf(`
INSERT INTO %[1]s (...) VALUES ($1, $2)
ON CONFLICT (...) DO UPDATE SET ... = %[1]s.(...) WHERE %[1]s.(...) = $2;
`, tableName)

Instead of needing to supply tableName three times, it could be supplied once using %[1]s.

Thoughts? Corrections? Feel free to reply via email!