Yes, to use the functions above you will need to copy the slices of 
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/eFTUqpImyPc
package 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")
}

Is this what you meant? I see how something like 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.


On Thursday, April 19, 2018 at 12:59:12 AM UTC-4, Sakthi Natesan wrote:
>
> Usescases that I came across doesn't involve []interface{}. 
>
> My usecases will be similar to this sample code 
> <https://play.golang.org/p/LezKzpiMndl>
>
> 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) {
> //....
> }
>
>
>
> If I want to pass the variable 'a' to both the functions, then I have to 
> write two functions to clone and typeAssert to respective interfaces.
>
>
> On Wednesday, 18 April 2018 19:53:33 UTC+5:30, Jan Mercl wrote:
>>
>> 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
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to