-j
On Wed, Apr 18, 2018 at 4:13 PM <nsak...@gmail.com> wrote:
When possible, avoid using '[]interface{}' and use just 'interface{}' instead: https://play.golang.org/p/oPtPoGChkMZ.
-j
type landSpace struct {
//consider that landSpace implements all functions from both Shape and property interfaces
...
}
type Shape interface {
...
}
type property interface {
...
}
var a []*landSpace
func processShapes(shapes []Shape) { //....}
func evaluateProperties(properties []Property) { //....}
landSpaceto slices of the respective interface types. But I you do not need to do any type assertions. Like this:
https://play.golang.org/p/eFTUqpImyPcpackage main
import (
"fmt"
)
type landSpace struct{}
func (*landSpace) Foo() {
fmt.Println("Foo")
}
func (*landSpace) Bar() {
fmt.Println("Bar")
}
type Shape interface {
Foo()
}
type Property interface {
Bar()
}
var a []*landSpace
func processShapes(shapes []Shape) {
for _, s := range shapes {
s.Foo()
}
}
func evaluateProperties(properties []Property) {
for _, p := range properties {
p.Bar()
}
}
func main() {
a = append(a, &landSpace{}, &landSpace{})
var shapes []Shape
for _, l := range a {
shapes = append(shapes, l)
}
processShapes(shapes)
var properties []Property
for _, l := range a {
properties = append(properties, l)
}
evaluateProperties(properties)
fmt.Println("Hello, playground")
}
shapes = a.(
[]Shape) would be convenient to avoid having to do an explicit conversion. Hoever, until Go has some form of generics, it seems unlikely. Keep in mind that a Shape and *
landSpace
are two different types, with different in memory representations. So to avoid major language changes, the type assertion would have to create a new slice anyway.