我在编写demo时,刚遇见个小问题一直不明白,请求各位解答一下。代码如下:
```
package main
import (
"fmt"
)
func MySprintf(tag string, format string, v ...interface{}) {
msg := fmt.Sprintf(format, v)
fmt.Println("msg: " + msg)
fmt.Println(tag + ": " + msg)
}
func main() {
v := fmt.Sprintf("%s is bigger than %s", "beijing", "shanghai")
fmt.Println("value: " + v)
MySprintf("main/MySprintf", "%s is bigger than %s", "beijing", "shanghai")
}
```
执行结果:
```
value: beijing is bigger than shanghai
msg: [beijing shanghai] is bigger than %!s(MISSING)
main/MySprintf: [beijing shanghai] is bigger than %!s(MISSING)
```
“beijing”和“shanghai”明明是两个变参,怎么会替换到一个值去了?
有疑问加站长微信联系(非本文作者)