如何将值附加到…interface {}?

Here's a sample function I want to emulate:

package main

import "fmt"

func main() {
    fmt.Println(fmt.Sprintf("Initially I want %d %s", 2, "dogs"))
    fmt.Println(Sprintf2("Now I want %d %s", 2, "dogs"))
}

func Sprintf2(format string, a ...interface{}) string {

return fmt.Sprintf(format + " and %d cats", append(a, 5))
}

here's a link in playground: https://play.golang.org/p/dHDwTlbRLDu

expected output:

Initially I want 2 dogs
Now I want 2 dogs and 5 cats

actual output:

Initially I want 2 dogs
Now I want [2 %!d(string=dogs) 5] %!s(MISSING) and %!d(MISSING) cats

You need to first append your new value to the a slice, and then unpack the slice when calling fmt.Sprintf:

func Sprintf2(format string, a ...interface{}) string {
    a = append(a, 5)
    return fmt.Sprintf(format+" and %d cats", a...)
}

https://play.golang.org/p/YRWzAT2Yxm_q