FAQ
Hi, and Happy New Year.

I'm trying to make the next simple thing works but can't find any way to do it.
I hope for your help guys. My goal is pretty simple: join two different struct
slices into one slice.

He is my code:

// - - - (http://play.golang.org/p/kkWSsQfjjD)
package main


type A struct {}
type B struct {}

func foo() []interface{} {
sa := make([]A, 1)
sa[0] = A{}

sb := make([]B, 1)
sb[0] = B{}

s := make([]interface{}, 0, 10)
s = append(s, ([]interface{}(sa))...)
// s = append(s, sb...)

return s
}

func main() {
foo()
}
// - - -

I have an error at append function call.
prog.go:15: cannot convert sa (type []A) to type []interface {}
I can't append sa to s... I there way to do it without for loop?


P. S.
Or maybe I'm trying to do it not in the "goic" (in the same sense as pythonic is used)?
I just need to retrieve slice with As and Bs inside and loop over its elements.

--
Best regards, Viacheslav Chimishuk
email/jabber: voice@root.ua
Ukraine, Khmelnitsky

--

Search Discussions

  • David Symonds at Jan 1, 2013 at 5:09 am

    On Tue, Jan 1, 2013 at 9:54 AM, Viacheslav Chumushuk wrote:

    I can't append sa to s... I there way to do it without for loop?
    No, you need to loop.
    http://golang.org/doc/go_faq.html#convert_slice_of_interface

    --
  • Viacheslav Chumushuk at Jan 1, 2013 at 12:12 pm
    Thanks a lot for helping. Now I get it.
    On Tue, Jan 01, 2013 at 04:09:12PM +1100, David Symonds wrote:
    On Tue, Jan 1, 2013 at 9:54 AM, Viacheslav Chumushuk wrote:

    I can't append sa to s... I there way to do it without for loop?
    No, you need to loop.
    http://golang.org/doc/go_faq.html#convert_slice_of_interface

    --
    --
    Best regards, Viacheslav Chimishuk
    email/jabber: voice@root.ua
    Ukraine, Khmelnitsky

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 31, '12 at 11:27p
activeJan 1, '13 at 12:12p
posts3
users2
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase