Jankiel
Jankiel

Reputation: 255

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

I'm learning Go and I'm stuck with Go tour (exercise-stringer.go: https://tour.golang.org/methods/7).

Here's some code:

type IPAddr [4]byte  
// TODO: Add a "String() string" method to IPAddr.
func (a IPAddr) String() string {
    return fmt.Sprintf("%d.%d.%d.%d", a...)
}

So I figured the inner representation of IPAddr is [4]byte, so spread operator works. But I'm getting:

cannot use []string literal (type []string) as type []interface {} in argument to fmt.Sprintf

What the heck? String slice doesn't work either, what's going on here?

EDIT: Sorry, there's an error in my question - error was about type IPAddr, not []string. I was playing with the code and I've pasted wrong output. Anyway, thanks to peterSO and 0x434D53 about invariance of slices in Go.

Well, this raises another question. Why is it implemented in this way? I imagine you'd just have some Iterable interface, so any struct implementing it would "just work".

Sidenote: when I first heard about Go there was this bold statement "compiled, but expressive". And explicit interface implementation is great example of this, but things like explicit conversion, lack of operator overloading and so on give me "90s Java feel". Which is sad, because Go seems like a great language.

Upvotes: 17

Views: 14193

Answers (7)

ynsta
ynsta

Reputation: 111

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...)
}

Upvotes: 11

joHN
joHN

Reputation: 1805

func (ip IPAddr) String() string {
    var s []string;
    for _, v := range ip {
        s = append(s, fmt.Sprintf("%v", v))
    }
    return strings.Join(s, ".")
}

Upvotes: 0

mathsyouth
mathsyouth

Reputation: 4198

The range should be used to iterate over the array, so the answer would be like as follows:

func (ip IPAddr) String() string {
    out := fmt.Sprintf("%v", ip[0])
    for _, value := range ip[1:] {
        out += fmt.Sprintf(".%v", value)
    }
    return out
}

Upvotes: 0

Jeremy
Jeremy

Reputation: 19

You need to implement this method for the Stringer interface.

func (ip IPAddr) String() string {
    return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3])
}

Upvotes: 0

gbulmer
gbulmer

Reputation: 4290

Firstly, when I 'run': package main

import "fmt"

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.
func (a IPAddr) String() string {
    return fmt.Sprintf("%d.%d.%d.%d", a...)
}

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)
    }
}

The error is:

prog.go:9: cannot use a (type IPAddr) as type []interface {} in argument to fmt.Sprintf

and not

cannot use []string literal (type []string) as type []interface {} in argument to fmt.Sprintf

So, I think something got out of synch while copying and pasting.

type IPAddr [4]byte does not define a string, so the error message in the question is misleading.

It's a [4]byte, a completely different type (from a Go language type perspective) from string. It isn't a []byte either.

Nor does type IPAddr [4]byte satisfy an interface, for instance implement String(), that fmt.Sprintf could use, because IPAddr's String() method doesn't get compiled.

You might try to convert the [4]byte to a string, but that conversion, string(a) isn't legal. Worse, the four byte values would be treated as character codes, and not converted to a character representation of the 4 small integer values. It is quite likely that some IPAddr byte values might be invalid UTF-8 which would be even more weird if a program tried to print it.

As explained in other answers,
return fmt.Sprintf("%d.%d.%d.%d", a[0], a[1], a[2], a[3])
returns a string value of IPAddr in the format you are aiming for.

Once func (a IPAddr) String() string is valid, it works; IPAddr implements the fmt.Stringer interface.

Then %v in
fmt.Printf("%v: %v\n", n, a)
can be replaced by %s in
fmt.Printf("%s: %s\n", n, a)
because the fmt output methods have an implementation of String().

I prefer %s to %v because it signals that the program is not relying on a 'default Go value representation' (i.e. for that array [127 0 0 1]), and that the type implements String().

Upvotes: -1

peterSO
peterSO

Reputation: 166915

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

Upvotes: 13

0x434D53
0x434D53

Reputation: 1463

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.

Upvotes: 4

Related Questions