package main
import (
"fmt"
)
func main() {
var a A
var b B
for _, v := range []WhatAreYou{a, b} {
fmt.Println(v.Question())
}
}
type WhatAreYou interface {
Question() string
}
type A struct {
string
}
type B struct {
int
}
func (a A) Question() string {
return "I'm an A"
}
func (b B) Question() string {
return "I'm a B"
}
The code above works as I expect and calls the function on each interface as expected. Go Play
In my actual code I intend to have many different types implementing an interface. How can I get rid of the var a A var b B etc and simply declare them all in the slice? i.e. I tried and failed with the following and other variations
for _, v := range []WhatAreYou{a A, b B} {