Today I Learned

hashrocket A Hashrocket project

Use Go Printf argument twice with adverbs

Usually, when you use one of the format functions from the fmt library you pass in one argument or operand for each verb you use.

fmt.Printf("Hello %s, nice to meet you %s", "Lauren", "Harry")
// Prints --> Hello Lauren, nice to meet you Harry

But what if you want to use one of those operands twice? Well, you can use what go terms an adverb. Place square brackets after the percent symbol with an index that refers to the argument you'd like to use.

fmt.Printf("Hello %s, nice to meet you %[1]s", "Lauren")
// Prints --> Hello Lauren, nice to meet you Lauren
See More #go TILs