英文:
How to pass variadic argumens to other functions
问题
函数Warn可以通过以下方式来使其工作吗?
func Warn(s string, args ...interface{}) {
log.Printf("warn: "+s, args)
}
func main() {
Warn("%d个苹果,%s ", 10, "好") //它应该输出与下面相同的结果
log.Printf("%d个苹果,%s ", 10, "好")
}
输出结果:
2009/11/10 23:00:00 warn: [10 %!d(string=good)]个苹果,%s(MISSING)
2009/11/10 23:00:00 10个苹果,好
我正在尝试使其工作:http://play.golang.org/p/W62f2NGDUe
英文:
Is there any way the function Warn could be made to work?
func Warn(s string, args ...interface{}) {
log.Printf("warn: "+s, args)
}
func main() {
Warn("%d apples, %s ", 10, "good") //it should output the same as below
log.Printf("%d apples, %s ", 10, "good")
}
Output:
2009/11/10 23:00:00 warn: [10 %!d(string=good)] apples, %s(MISSING)
2009/11/10 23:00:00 10 apples, good
I'm trying to make this work: http://play.golang.org/p/W62f2NGDUe
答案1
得分: 8
func Warn(s string, args ...interface{}) {
log.Printf("警告: "+s, args...)
}
现在它可以工作了。
英文:
Got it:
func Warn(s string, args ...interface{}) {
log.Printf("warn: "+s, args...)
}
Now it works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论