英文:
How do you wrap var args in Go?
问题
根据几个群组的帖子,以下代码应该可以工作:
package main
import "fmt"
func demo(format string, args ...interface{}) {
var count = len(args)
for i := 0; i < count; i++ {
fmt.Printf("! %s\n", args[i])
}
fmt.Printf("%+v\n", format)
fmt.Printf("%+v\n", args)
fmt.Printf(format, args)
fmt.Printf("\n")
}
func main() {
demo("%s %d", "Hello World", 10)
fmt.Printf("\n\n")
demo("%d %s", 10, "Hello")
}
但是它并没有产生"Hello World 10"和"10 Hello",而是产生了:
! Hello World
! %!s(int=10)
%s %d
[Hello World 10]
[Hello World %!s(int=10)] %d(MISSING)
! %!s(int=10)
! Hello
%d %s
[10 Hello]
[10 %!d(string=Hello)] %s(MISSING)
也就是说,将[]interface{}传递给以...interface{}作为参数的函数时,不会展开参数,而只是将其作为值传递。第一个%s将[]interface{}展开为字符串,而后续的参数没有被处理。
我确定在日志记录中可能有很多需要这样做的情况,但我找不到任何可以工作的示例。
这基本上就是C语言中的'vprintf'函数族。
英文:
According to several groups posts, the following code should work:
package main
import "fmt"
func demo(format string, args ...interface{}) {
var count = len(args)
for i := 0; i < count; i++ {
fmt.Printf("! %s\n", args[i])
}
fmt.Printf("%+v\n", format)
fmt.Printf("%+v\n", args)
fmt.Printf(format, args)
fmt.Printf("\n")
}
func main() {
demo("%s %d", "Hello World", 10)
fmt.Printf("\n\n")
demo("%d %s", 10, "Hello")
}
And yield "Hello World 10" and "10 Hello", but it does not. Instead it yields:
! Hello World
! %!s(int=10)
%s %d
[Hello World 10]
[Hello World %!s(int=10)] %d(MISSING)
! %!s(int=10)
! Hello
%d %s
[10 Hello]
[10 %!d(string=Hello)] %s(MISSING)
Which is to say that passing []interface{} to a function that takes ...interface{} as an argument does not expand the argument and instead simply passes it as the value. The first %s expands the []interface{} into a string, and no further arguments are processed.
I'm sure there must be lots of situations where this is required in logging; but I can't find any working examples of how to do it.
This is basically the 'vprintf' family of functions in C.
答案1
得分: 10
我不认为 OP 程序应该“工作”。也许这是打算的替代方式(?):
package main
import "fmt"
func demo(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
func main() {
demo("%s %d\n\n", "Hello World", 10)
demo("%d %s", 10, "Hello")
}
(还有这里
输出:
Hello World 10
10 Hello
英文:
I don't think the OP program should "work". Maybe this was intended instead(?):
package main
import "fmt"
func demo(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
func main() {
demo("%s %d\n\n", "Hello World", 10)
demo("%d %s", 10, "Hello")
}
(Also here
Output:
Hello World 10
10 Hello
答案2
得分: 4
fmt.Printf(format, args...)
应该可以实现你想要的功能。
英文:
fmt.Printf(format, args...)
should do what you want I think.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论