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.
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...)
}
A Tour of Go
Exercise: Stringers
Make the
IPAddr
type implementfmt.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With