Go vet sprinf checking number of arguments

Hello,

I’ve found an interesting behavior of go vet with sprintf which I would not expect (it’s inside the lint check).

For this check, I get a violation:
fmt.Sprintf("%[1]s%[2]s", "foo")

When I pass more arguments into Sprintf than expected, everything is fine from go vet perspective:

fmt.Sprintf("%[1]s", "foo", "bar")

Could you explain me why go vet is not throwing a violation in this case? I’d expect it should also cover this case as developer might forget to update the formatting string. Or, at least to have a possibility to turn this kind of check on by configuration.

Thanks for explanation.

Hi and welcome to the forum.

I’m only guessing but I think the difference is because the first code can potentially panic in production while the second don’t.
It’s more “expected parameter” vs “unused parameter”

thanks for the answer.

I probably forget to mention that when there are no numbered arguments, go vet violates properly an error:

fmt.Sprintf("%s", "foo", "bar")

Why there’s also not an error for numbered arguments?

There’s already an explanation for my question at StackOverflow, so I’m closing the topic here

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.