How do you wrap var args in Go? -



How do you wrap var args in Go? -

according several groups posts, next code should work:

package main import "fmt" func demo(format string, args ...interface{}) { var count = len(args) := 0; < 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" , "10 hello", not. instead 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 passing []interface{} function takes ...interface{} argument does not expand argument , instead passes value. first %s expands []interface{} string, , no farther arguments processed.

i'm sure there must lots of situations required in logging; can't find working examples of how it.

this 'vprintf' family of functions in c.

i don't think op programme should "work". maybe 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

go

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -