V8013. Incorrect format. A different number of format items is expected.
The analyzer has detected an error when the format I/O function was used. The number of passed arguments does not match what is expected according to the format string.
Here are some examples where the analyzer issues warnings.
Example N1:
fmt.Printf("Name: %s; Age: %d", "Donald")
The format string expects a name (%s) and an age (%d), but the argument for the age is missing. As a result, stdout outputs a malformed string.
To fix this, pass the age as the next argument:
fmt.Printf("Name: %s; Age: %d", "Donald", 13)
Example N2:
fmt.Printf("Name: %s", "Donald", 13)
The function here receives more arguments than intended. The 13 argument has no placeholder in the format string. In this case, stdout also prints a malformed string.
To fix this, remove the redundant argument:
fmt.Printf("Name: %s", "Donald")
You can use the passed arguments to set the width and precision of the format item.
Take a look at a width example:
fmt.Printf("Name: %*s", "Donald")
The format string defines the width using the * symbol, but the corresponding argument is missing.
To fix this, add the missing argument that specifies the width:
fmt.Printf("Name: %*s", 10, "Donald")
The analyzer also issues warnings about precision-related issues that can be specified using the .* sequence.
You can explicitly specify the argument index to be substituted into a given format element (similar to width and precision). Note that the next argument index comes from the index of the previous substitution argument, except for the first one. As a result, if you explicitly specify that the n argument should be substituted, the n + 1 argument will follow it.
Look at the example:
fmt.Printf("2: %[2]s, 1: %[1]s, 3: %s", "Donald", "James", "Michael")
In this case, the following will appear in the stdout:
2: James, 1: Donald, 3: James
The "Michael" argument is ignored. This happens because the first argument %[1]s is substituted before it. So, next argument used for substitution will be the second one, corresponding to James.
To fix this, explicitly specify the index of the argument that should match the last format item:
fmt.Printf("2: %[2]s, 1: %[1]s, 3: %[3]s",
"Donald", "James", "Michael")
Now, stdout will output the correct string:
2: James, 1: Donald, 3: Michael