Why can't I do fmt.Sprintf("%d.%d.%d.%d", a...)?

A Tour of Go

Exercise: Stringers

Make the IPAddr type implement fmt.Stringer to print the address as a dotted quad.

For instance, IPAddr{1, 2, 3, 4} should print as "1.2.3.4".

package main

import "fmt"

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.

func main() {
  addrs := map[string]IPAddr{
      "loopback":  {127, 0, 0, 1},
      "googleDNS": {8, 8, 8, 8},
  }
  for n, a := range addrs {
      fmt.Printf("%v: %v\n", n, a)
  }
}

There is no implicit conversion of []string to []interface {}. See Conversions in The Go Programming Language Specification. You need to provide an explicit conversion. For example,

package main

import "fmt"

type IPAddr [4]byte

// A "String() string" method for IPAddr.
func (a IPAddr) String() string {
    return fmt.Sprintf("%d.%d.%d.%d", a[0], a[1], a[2], a[3])
}

func main() {
    addrs := map[string]IPAddr{
        "loopback":  {127, 0, 0, 1},
        "googleDNS": {8, 8, 8, 8},
    }
    for n, a := range addrs {
        fmt.Printf("%v: %v\n", n, a)
    }
}

Output:

loopback: 127.0.0.1
googleDNS: 8.8.8.8

As stated in the Go FAQ section Can I convert a []T to an []interface{}, there is no implicit conversion from a typed array to an []interface{}:

It is disallowed by the language specification because the two types do not have the same representation in memory. It is necessary to copy the elements individually to the destination slice

The following solution works but require the creation of an intermediate slice:

func (ip IPAddr) String() string {
    tmp := make([]interface{}, len(ip))
    for i, val := range ip {
        tmp[i] = val
    }
    return fmt.Sprintf("%d.%d.%d.%d", tmp...)
}

From the go language specification:

If f is variadic with a final parameter p of type ...T, then within f the type of p is equivalent to type []T

But in Go slices and arrays are type invariant. So an []T is different from []U if T and U are different types. They are not related at all, even if T is an structural subtype of U. So []string is not an []interface.

Tags:

Go