Home > Backend Development > Golang > Are formatting directives required in variadic functions?

Are formatting directives required in variadic functions?

WBOY
Release: 2024-02-09 14:40:09
forward
1175 people have browsed it

Are formatting directives required in variadic functions?

In PHP, the variable parameter function is a very convenient function that allows us to use an indefinite number of parameters in the function definition. However, whether you need to use formatting directives in variadic functions depends on your specific needs. Format instructions are usually used to output or process parameters in a specific format. For example, when using the printf() function, you need to specify a format string. If you need to format parameters in a variable parameter function, then of course you need to use formatting instructions. But if you are simply passing parameters to other functions or performing some basic operations, then there is no need to use formatting instructions. In short, whether you need to use formatting instructions in variadic functions depends on specific business requirements.

Question content

I have a variadic function respond that accepts several positional arguments and propagates an any slice at the end. The function uses the last parameter differently depending on the context (note how message and data differ depending on the value of status.):

func respond(w http.responsewriter, status int, format string, a ...any) {
    var (
        statusok bool
        message  string
        data     interface{}
    )
    if status >= 300 {
        statusok = false
        message = fmt.sprintf(format, a...)
        data = nil
        log.error().msg(message)
    } else {
        statusok = true
        message = format
        data = a[0]
        log.info().msg(fmt.sprintf("%d", status))
    }
    // issue response
    responder := render.new()
    responder.json(w, status, map[string]interface{}{
        "statusok": statusok,
        "status":   status,
        "message":  message,
        "data":     data,
    })
}
Copy after login

I don't get any warnings for the following call:

respond(w, http.statusnotfound, "person %v does not exist", personid)
Copy after login

However, for the next one, the warning respond is called with arguments, but no formatting directive is raised (but the code runs as expected, person is a struct) :

respond(w, http.StatusAccepted, "updated", person)
Copy after login

To my amateur eye, it looks like the variadic function expects a format string at the end and the argument in that format. But why is this so? Are there any limitations on what variadic functions can do?

Or should I better split the responder into two parts, one for each case ("ok" and "not ok")?

See also https://github.com/golang/go/issues/26486 and https://go.dev/src/cmd/vet/testdata/print/print.go (line 316) for discussion This message in go code (I'm getting a warning from the linter, as @mkopriva and @jimb mentioned in their comments)

WORKAROUND

The warning comes from the linter , because the format string you used when calling the respond() function does not match the arguments supplied afterwards.

This particular message comes from the printf parser< /a> which logs:

printf: Check the consistency of printf format string and parameters

This check applies to calls to formatting functions such as fmt.printf and fmt.sprintf, and to any detected wrappers for these functions.

Since your response() function calls fmt.sprintf() as-is with the format and a parameters, this is Think of it as a wrapper around fmt.sprintf().

If you want to avoid/get rid of this warning message, the easiest "fix" is to not use these parameters as is, e.g. copy one of the parameters and then pass it:

// Make and use a copy to avoid being marked as a wrapper of fmt.Sprintf
format := format
message = fmt.Sprintf(format, a...)
Copy after login

The above is the detailed content of Are formatting directives required in variadic functions?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template