Appending items to a variadic function wrapper without reallocating a new slice

I would just do two prints:

func Debug(a ...interface{}) {
    if debug {
        fmt.Fprint(out, prefix, sep)
        fmt.Fprintln(out, a...)
    }
}

If you believed you needed to make a single call to Fprint, you could do,

func Debug(a ...interface{}) {
    if debug {
        fmt.Fprint(out, prefix, sep, fmt.Sprintln(a...))
    }
}

Either way seems simpler that building a new slice.


You can also use append for a one-liner:

func Debug (a ...interface{}) {
    if debug {
        fmt.Fprintln(out, append([]interface{}{prefix, sep}, a...)...)
    }
}