FAQ
Having "type A int" and "type B A", I try to convert []A to []B, but it
fails.

It seems that Go doesn't allow such conversion, notwithstanding the memory
layout is the same for []A and []B ?

So, I must convert each element to the target type into a new slice, as in
the two following snippets ?

Conversion []A to []B:
http://play.golang.org/p/BXHg66NKUs

Conversion []*A to []*B:
http://play.golang.org/p/jUoOirBybN


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Brad Fitzpatrick at Jun 26, 2014 at 4:10 pm
    http://golang.org/doc/faq#convert_slice_of_interface

    On Thu, Jun 26, 2014 at 8:59 AM, wrote:

    Having "type A int" and "type B A", I try to convert []A to []B, but it
    fails.

    It seems that Go doesn't allow such conversion, notwithstanding the memory
    layout is the same for []A and []B ?

    So, I must convert each element to the target type into a new slice, as in
    the two following snippets ?

    Conversion []A to []B:
    http://play.golang.org/p/BXHg66NKUs

    Conversion []*A to []*B:
    http://play.golang.org/p/jUoOirBybN


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Erwin at Jun 26, 2014 at 6:42 pm
    it can be done using unsafe.Pointer, see

    http://play.golang.org/p/PbhbnlTN_v

    On 26 June 2014 17:59, wrote:

    Having "type A int" and "type B A", I try to convert []A to []B, but it
    fails.

    It seems that Go doesn't allow such conversion, notwithstanding the memory
    layout is the same for []A and []B ?

    So, I must convert each element to the target type into a new slice, as in
    the two following snippets ?

    Conversion []A to []B:
    http://play.golang.org/p/BXHg66NKUs

    Conversion []*A to []*B:
    http://play.golang.org/p/jUoOirBybN


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Chris dollin at Jun 26, 2014 at 6:54 pm

    On 26 June 2014 19:42, Erwin wrote:

    it can be done using unsafe.Pointer, see

    http://play.golang.org/p/PbhbnlTN_v

    You can also keep warm by setting fire to the carpet.
    Using unsafe to do a type conversions is an open opportunity
    for error.

    One suspects that there as an earlier design decision leading
    the code up the garden path.

    Chris

    --
    Chris "allusive" Dollin

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Erwin at Jun 26, 2014 at 6:56 pm
    Surely caution is needed, but here it is actually safe to do so.

    On 26 June 2014 20:53, chris dollin wrote:
    On 26 June 2014 19:42, Erwin wrote:

    it can be done using unsafe.Pointer, see

    http://play.golang.org/p/PbhbnlTN_v

    You can also keep warm by setting fire to the carpet.
    Using unsafe to do a type conversions is an open opportunity
    for error.

    One suspects that there as an earlier design decision leading
    the code up the garden path.

    Chris

    --
    Chris "allusive" Dollin
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jesse McNelis at Jun 26, 2014 at 11:09 pm

    On Fri, Jun 27, 2014 at 4:56 AM, Erwin wrote:
    Surely caution is needed, but here it is actually safe to do so.
    As long as everyone that works on this code knows that A and B need to
    always be the same size and that you need to be careful with what you
    assign to the variable 'b' and that the size of slices doesn't change
    in a type specific way sometime in the future or on some different
    implementation.

    I'm sure the newbie asking this question knows all of this.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Chai2010 at Jun 27, 2014 at 1:01 am
    This is a example:
    http://godoc.org/github.com/chai2010/gopkg/builtin#example-Slice

    convert []A to []B:
    b := Slice([]A{1,2}, reflect.TypeOf([]B(nil))).([]B)

    And sizeof(A) and sizeof(B) don't need the same size.




    2014-06-27 7:09 GMT+08:00 Jesse McNelis <jessta@jessta.id.au>:
    On Fri, Jun 27, 2014 at 4:56 AM, Erwin wrote:
    Surely caution is needed, but here it is actually safe to do so.
    As long as everyone that works on this code knows that A and B need to
    always be the same size and that you need to be careful with what you
    assign to the variable 'b' and that the size of slices doesn't change
    in a type specific way sometime in the future or on some different
    implementation.

    I'm sure the newbie asking this question knows all of this.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    chaishushan
    http://my.oschina.net/chai2010
    https://github.com/chai2010

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 26, '14 at 3:59p
activeJun 27, '14 at 1:01a
posts7
users6
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase