* yves baumes <ybau...@gmail.com> [200629 03:22]:
> Hello,
> 
> considering this code
> 
> ```
> package main
> 
> import (
> "fmt"
> )
> 
> func main() {
> host := []string{"127", "0", "0", "1"}
> 
> fmt.Printf("%v.%v.%v.%v\n", host[0], host[1], host[2], host[3])
> fmt.Printf("%v.%v.%v.%v\n", host[0:4]...)
> }
> ```
> 
> The first Printf works and prints the host value.
> While the second one does not compile : "./args.go:11:34: cannot use 
> host[0:4] (type []string) as type []interface {} in argument to fmt.Printf"
> 
> If I use append() instead of Printf(), this expanding of the host variables 
> just works out. Is this a compiler bug in the case of the fmt.Printf() ?

>From the language spec at
https://golang.org/ref/spec#Passing_arguments_to_..._parameters:

    If the final argument is assignable to a slice type []T, it is
    passed unchanged as the value for a ...T parameter if the argument
    is followed by .... In this case no new slice is created.

The final argument to Printf is []interface{}, while you are trying to
pass []string.  In order for this to work, []string would have to be
assignable to []interface{}, which it isn't.

This is related to a FAQ at
https://golang.org/doc/faq#convert_slice_of_interface.

If, after reading the above links and the definition of assignability at
https://golang.org/ref/spec#Assignability, you are still unclear why it
is not compiling, ask on the list for more help.

...Marvin

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/20200629181003.attbsi36qwc6rrtv%40basil.wdw.

Reply via email to