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.